1 /*****************************************************************************
2 * ctrl_radialslider.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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,
41 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ), m_numImg( numImg ),
42 m_rVariable( rVariable ), m_minAngle( minAngle ), m_maxAngle( maxAngle ),
43 m_cmdUpDown( this ), m_cmdDownUp( this ),
46 // Build the images of the sequence
47 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
48 m_pImgSeq = pOsFactory->createOSGraphics( rBmpSeq.getWidth(),
49 rBmpSeq.getHeight() );
50 m_pImgSeq->drawBitmap( rBmpSeq, 0, 0 );
52 m_width = rBmpSeq.getWidth();
53 m_height = rBmpSeq.getHeight() / numImg;
56 m_fsm.addState( "up" );
57 m_fsm.addState( "down" );
60 m_fsm.addTransition( "up", "mouse:left:down", "down", &m_cmdUpDown );
61 m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
62 m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );
65 m_fsm.setState( "up" );
67 // Observe the variable
68 m_rVariable.addObserver( this );
72 CtrlRadialSlider::~CtrlRadialSlider()
74 m_rVariable.delObserver( this );
75 SKINS_DELETE( m_pImgSeq );
79 void CtrlRadialSlider::handleEvent( EvtGeneric &rEvent )
81 // Save the event to use it in callbacks
84 m_fsm.handleTransition( rEvent.getAsString() );
88 bool CtrlRadialSlider::mouseOver( int x, int y ) const
90 return m_pImgSeq->hit( x, y + m_position * m_height );
94 void CtrlRadialSlider::draw( OSGraphics &rImage, int xDest, int yDest )
96 rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_height, xDest, yDest,
101 void CtrlRadialSlider::onUpdate( Subject<VarPercent,void*> &rVariable,
104 m_position = (int)( m_rVariable.get() * m_numImg );
105 notifyLayout( m_width, m_height );
109 void CtrlRadialSlider::CmdUpDown::execute()
111 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
113 // Change the position of the cursor, in non-blocking mode
114 m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
116 m_pParent->captureMouse();
120 void CtrlRadialSlider::CmdDownUp::execute()
122 m_pParent->releaseMouse();
126 void CtrlRadialSlider::CmdMove::execute()
128 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
130 // Change the position of the cursor, in blocking mode
131 m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
135 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
137 // Get the position of the control
138 const Position *pPos = getPosition();
144 // Compute the position relative to the center
145 int x = posX - pPos->getLeft() - m_width / 2;
146 int y = posY - pPos->getTop() - m_width / 2;
148 // Compute the polar coordinates. angle is -(-j,OM)
149 float r = sqrt((float)(x*x + y*y));
154 float angle = acos(y/r);
157 angle = 2*M_PI - angle;
160 if( angle >= m_minAngle && angle <= m_maxAngle )
162 float newVal = (angle - m_minAngle) / (m_maxAngle - m_minAngle);
163 // Avoid too fast moves of the cursor if blocking mode
164 if( !blocking || fabs( m_rVariable.get() - newVal ) < 0.5 )
166 m_rVariable.set( newVal );