1 /*****************************************************************************
2 * ctrl_radialslider.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: ctrl_radialslider.cpp,v 1.2 2004/01/11 17:12:17 asmax Exp $
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
26 #include "ctrl_radialslider.hpp"
27 #include "../events/evt_mouse.hpp"
28 #include "../src/generic_bitmap.hpp"
29 #include "../src/generic_window.hpp"
30 #include "../src/os_factory.hpp"
31 #include "../src/os_graphics.hpp"
32 #include "../utils/position.hpp"
33 #include "../utils/var_percent.hpp"
36 CtrlRadialSlider::CtrlRadialSlider( intf_thread_t *pIntf,
37 const GenericBitmap &rBmpSeq, int numImg,
38 VarPercent &rVariable, float minAngle,
39 float maxAngle, const UString &rHelp ):
40 CtrlGeneric( pIntf, rHelp ), m_fsm( pIntf ), m_numImg( numImg ),
41 m_rVariable( rVariable ), m_minAngle( minAngle ), m_maxAngle( maxAngle ),
42 m_cmdUpDown( this, &transUpDown ), m_cmdDownUp( this, &transDownUp ),
43 m_cmdMove( this, &transMove ), m_position( 0 ), m_lastPos( 0 )
45 // Build the images of the sequence
46 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
47 m_pImgSeq = pOsFactory->createOSGraphics( rBmpSeq.getWidth(),
48 rBmpSeq.getHeight() );
49 m_pImgSeq->drawBitmap( rBmpSeq, 0, 0 );
51 m_width = rBmpSeq.getWidth();
52 m_height = rBmpSeq.getHeight() / numImg;
55 m_fsm.addState( "up" );
56 m_fsm.addState( "down" );
59 m_fsm.addTransition( "up", "mouse:left:down", "down", &m_cmdUpDown );
60 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
61 m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );
64 m_fsm.setState( "up" );
66 // Observe the variable
67 m_rVariable.addObserver( this );
71 CtrlRadialSlider::~CtrlRadialSlider()
73 m_rVariable.delObserver( this );
74 SKINS_DELETE( m_pImgSeq );
78 void CtrlRadialSlider::handleEvent( EvtGeneric &rEvent )
80 // Save the event to use it in callbacks
83 m_fsm.handleTransition( rEvent.getAsString() );
87 bool CtrlRadialSlider::mouseOver( int x, int y ) const
89 return m_pImgSeq->hit( x, y + m_position * m_height );
93 void CtrlRadialSlider::draw( OSGraphics &rImage, int xDest, int yDest )
95 rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_height, xDest, yDest,
100 void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable )
102 m_position = (int)( m_rVariable.get() * m_numImg );
107 void CtrlRadialSlider::transUpDown( SkinObject *pCtrl )
109 CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
111 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
113 // Change the position of the cursor, in non-blocking mode
114 pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
116 pThis->captureMouse();
120 void CtrlRadialSlider::transDownUp( SkinObject *pCtrl )
122 CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
124 pThis->releaseMouse();
128 void CtrlRadialSlider::transMove( SkinObject *pCtrl )
130 CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
132 EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
134 // Change the position of the cursor, in blocking mode
135 pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
139 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
141 // Get the position of the control
142 const Position *pPos = getPosition();
148 // Compute the position relative to the center
149 int x = posX - pPos->getLeft() - m_width / 2;
150 int y = posY - pPos->getTop() - m_width / 2;
152 // Compute the polar coordinates. angle is -(-j,OM)
153 float r = sqrt(x*x + y*y);
158 float angle = acos(y/r);
161 angle = 2*M_PI - angle;
164 if( angle >= m_minAngle && angle <= m_maxAngle )
166 float newVal = (angle - m_minAngle) / (m_maxAngle - m_minAngle);
167 // Avoid too fast moves of the cursor if blocking mode
168 if( !blocking || fabs( m_rVariable.get() - newVal ) < 0.5 )
170 m_rVariable.set( newVal );