]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_radialslider.cpp
* modules/gui/skins/*:
[vlc] / modules / gui / skins2 / controls / ctrl_radialslider.cpp
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 $
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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     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 )
44 {
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 );
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     SKINS_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 {
102     m_position = (int)( m_rVariable.get() * m_numImg );
103     notifyLayout();
104 }
105
106
107 void CtrlRadialSlider::transUpDown( SkinObject *pCtrl )
108 {
109     CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
110
111     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
112
113     // Change the position of the cursor, in non-blocking mode
114     pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
115
116     pThis->captureMouse();
117 }
118
119
120 void CtrlRadialSlider::transDownUp( SkinObject *pCtrl )
121 {
122     CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
123
124     pThis->releaseMouse();
125 }
126
127
128 void CtrlRadialSlider::transMove( SkinObject *pCtrl )
129 {
130     CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;
131
132     EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
133
134     // Change the position of the cursor, in blocking mode
135     pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
136 }
137
138
139 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
140 {
141     // Get the position of the control
142     const Position *pPos = getPosition();
143     if( !pPos )
144     {
145         return;
146     }
147
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;
151
152     // Compute the polar coordinates. angle is -(-j,OM)
153     float r = sqrt(x*x + y*y);
154     if( r == 0 )
155     {
156         return;
157     }
158     float angle = acos(y/r);
159     if( x > 0 )
160     {
161         angle = 2*M_PI - angle;
162     }
163
164     if( angle >= m_minAngle && angle <= m_maxAngle )
165     {
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 )
169         {
170             m_rVariable.set( newVal );
171         }
172     }
173 }
174