]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_radialslider.cpp
skins2: fix RadialSlider
[vlc] / modules / gui / skins2 / controls / ctrl_radialslider.cpp
1 /*****************************************************************************
2  * ctrl_radialslider.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include <math.h>
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"
34
35
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                                     VarBool *pVisible ):
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 ),
44     m_cmdMove( this ), m_position( 0 )
45 {
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 );
50
51     m_width = rBmpSeq.getWidth();
52     m_height = rBmpSeq.getHeight() / numImg;
53
54     // States
55     m_fsm.addState( "up" );
56     m_fsm.addState( "down" );
57
58     // Transitions
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 );
62
63     // Initial state
64     m_fsm.setState( "up" );
65
66     // Observe the variable
67     m_rVariable.addObserver( this );
68 }
69
70
71 CtrlRadialSlider::~CtrlRadialSlider()
72 {
73     m_rVariable.delObserver( this );
74     delete m_pImgSeq;
75 }
76
77
78 void CtrlRadialSlider::handleEvent( EvtGeneric &rEvent )
79 {
80     // Save the event to use it in callbacks
81     m_pEvt = &rEvent;
82
83     m_fsm.handleTransition( rEvent.getAsString() );
84 }
85
86
87 bool CtrlRadialSlider::mouseOver( int x, int y ) const
88 {
89     return m_pImgSeq->hit( x, y + m_position * m_height );
90 }
91
92
93 void CtrlRadialSlider::draw( OSGraphics &rImage, int xDest, int yDest )
94 {
95     rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_height, xDest, yDest,
96                          m_width, m_height );
97 }
98
99
100 void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable,
101                                  void *arg  )
102 {
103     m_position = (int)( m_rVariable.get() * m_numImg );
104     notifyLayout( m_width, m_height );
105 }
106
107
108 void CtrlRadialSlider::CmdUpDown::execute()
109 {
110     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
111
112     // Change the position of the cursor, in non-blocking mode
113     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
114
115     m_pParent->captureMouse();
116 }
117
118
119 void CtrlRadialSlider::CmdDownUp::execute()
120 {
121     m_pParent->releaseMouse();
122 }
123
124
125 void CtrlRadialSlider::CmdMove::execute()
126 {
127     EvtMouse *pEvtMouse = static_cast<EvtMouse*>(m_pParent->m_pEvt);
128
129     // Change the position of the cursor, in blocking mode
130     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
131 }
132
133
134 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
135 {
136     // Get the position of the control
137     const Position *pPos = getPosition();
138     if( !pPos )
139     {
140         return;
141     }
142
143     // Compute the position relative to the center
144     int x = posX - pPos->getLeft() - m_width / 2;
145     int y = posY - pPos->getTop() - m_width / 2;
146
147     // Compute the polar coordinates. angle is -(-j,OM)
148     float r = sqrt((float)(x*x + y*y));
149     if( r == 0 )
150     {
151         return;
152     }
153     float angle = acos(y/r);
154     if( x > 0 )
155     {
156         angle = 2*M_PI - angle;
157     }
158
159     if( angle >= m_minAngle && angle <= m_maxAngle )
160     {
161         float newVal = (angle - m_minAngle) / (m_maxAngle - m_minAngle);
162         // Avoid too fast moves of the cursor if blocking mode
163         if( !blocking || fabs( m_rVariable.get() - newVal ) < 0.5 )
164         {
165             m_rVariable.set( newVal );
166         }
167     }
168 }
169