]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_radialslider.cpp
use OK instead of Ok
[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 )
45 {
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 );
51
52     m_width = rBmpSeq.getWidth();
53     m_height = rBmpSeq.getHeight() / numImg;
54
55     // States
56     m_fsm.addState( "up" );
57     m_fsm.addState( "down" );
58
59     // Transitions
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 );
63
64     // Initial state
65     m_fsm.setState( "up" );
66
67     // Observe the variable
68     m_rVariable.addObserver( this );
69 }
70
71
72 CtrlRadialSlider::~CtrlRadialSlider()
73 {
74     m_rVariable.delObserver( this );
75     SKINS_DELETE( m_pImgSeq );
76 }
77
78
79 void CtrlRadialSlider::handleEvent( EvtGeneric &rEvent )
80 {
81     // Save the event to use it in callbacks
82     m_pEvt = &rEvent;
83
84     m_fsm.handleTransition( rEvent.getAsString() );
85 }
86
87
88 bool CtrlRadialSlider::mouseOver( int x, int y ) const
89 {
90     return m_pImgSeq->hit( x, y + m_position * m_height );
91 }
92
93
94 void CtrlRadialSlider::draw( OSGraphics &rImage, int xDest, int yDest )
95 {
96     rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_height, xDest, yDest,
97                          m_width, m_height );
98 }
99
100
101 void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable,
102                                  void *arg  )
103 {
104     m_position = (int)( m_rVariable.get() * m_numImg );
105     notifyLayout( m_width, m_height );
106 }
107
108
109 void CtrlRadialSlider::CmdUpDown::execute()
110 {
111     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
112
113     // Change the position of the cursor, in non-blocking mode
114     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
115
116     m_pParent->captureMouse();
117 }
118
119
120 void CtrlRadialSlider::CmdDownUp::execute()
121 {
122     m_pParent->releaseMouse();
123 }
124
125
126 void CtrlRadialSlider::CmdMove::execute()
127 {
128     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
129
130     // Change the position of the cursor, in blocking mode
131     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
132 }
133
134
135 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
136 {
137     // Get the position of the control
138     const Position *pPos = getPosition();
139     if( !pPos )
140     {
141         return;
142     }
143
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;
147
148     // Compute the polar coordinates. angle is -(-j,OM)
149     float r = sqrt((float)(x*x + y*y));
150     if( r == 0 )
151     {
152         return;
153     }
154     float angle = acos(y/r);
155     if( x > 0 )
156     {
157         angle = 2*M_PI - angle;
158     }
159
160     if( angle >= m_minAngle && angle <= m_maxAngle )
161     {
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 )
165         {
166             m_rVariable.set( newVal );
167         }
168     }
169 }
170