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_position( 0 ), m_cmdUpDown( this ), m_cmdDownUp( this ),
46 // Build the images of the sequence
47 m_pImgSeq = OSFactory::instance( getIntf() )->createOSGraphics(
48 rBmpSeq.getWidth(), 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 );
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, int w, int h )
95 const Position *pPos = getPosition();
96 rect region( pPos->getLeft(), pPos->getTop(), m_width, m_height );
97 rect clip( xDest, yDest, w ,h );
99 if( rect::intersect( region, clip, &inter ) )
100 rImage.drawGraphics( *m_pImgSeq,
102 inter.y - region.y + m_position * m_height,
104 inter.width, inter.height );
108 void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable, void *arg )
111 if( &rVariable == &m_rVariable )
113 int position = (int)( m_rVariable.get() * ( m_numImg - 1 ) );
114 if( position == m_position )
117 m_position = position;
118 notifyLayout( m_width, m_height );
123 void CtrlRadialSlider::CmdUpDown::execute()
125 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
127 // Change the position of the cursor, in non-blocking mode
128 m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
130 m_pParent->captureMouse();
134 void CtrlRadialSlider::CmdDownUp::execute()
136 m_pParent->releaseMouse();
140 void CtrlRadialSlider::CmdMove::execute()
142 EvtMouse *pEvtMouse = static_cast<EvtMouse*>(m_pParent->m_pEvt);
144 // Change the position of the cursor, in blocking mode
145 m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
149 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
151 // Get the position of the control
152 const Position *pPos = getPosition();
158 // Compute the position relative to the center
159 int x = posX - pPos->getLeft() - m_width / 2;
160 int y = posY - pPos->getTop() - m_height / 2;
162 // Compute the polar coordinates. angle is -(-j,OM)
163 float r = sqrt((float)(x*x + y*y));
168 float angle = acos(y/r);
171 angle = 2*M_PI - angle;
174 if( angle >= m_minAngle && angle <= m_maxAngle )
176 float newVal = (angle - m_minAngle) / (m_maxAngle - m_minAngle);
177 // Avoid too fast moves of the cursor if blocking mode
178 if( !blocking || fabs( m_rVariable.get() - newVal ) < 0.5 )
180 m_rVariable.set( newVal );