]> git.sesse.net Git - vlc/blob - modules/gui/skins2/controls/ctrl_radialslider.cpp
skins2: improve refresh of layouts
[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, int w, int h )
94 {
95     const Position *pPos = getPosition();
96     rect region( pPos->getLeft(), pPos->getTop(), m_width, m_height );
97     rect clip( xDest, yDest, w ,h );
98     rect inter;
99     if( rect::intersect( region, clip, &inter ) )
100         rImage.drawGraphics( *m_pImgSeq,
101                               inter.x - region.x,
102                               inter.y - region.y + m_position * m_height,
103                               inter.x, inter.y,
104                               inter.width, inter.height );
105 }
106
107
108 void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable,
109                                  void *arg  )
110 {
111     if( &rVariable == &m_rVariable )
112     {
113         int position = (int)( m_rVariable.get() * ( m_numImg - 1 ) );
114         if( position == m_position )
115             return;
116
117         m_position = position;
118         notifyLayout( m_width, m_height );
119     }
120 }
121
122
123 void CtrlRadialSlider::CmdUpDown::execute()
124 {
125     EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
126
127     // Change the position of the cursor, in non-blocking mode
128     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );
129
130     m_pParent->captureMouse();
131 }
132
133
134 void CtrlRadialSlider::CmdDownUp::execute()
135 {
136     m_pParent->releaseMouse();
137 }
138
139
140 void CtrlRadialSlider::CmdMove::execute()
141 {
142     EvtMouse *pEvtMouse = static_cast<EvtMouse*>(m_pParent->m_pEvt);
143
144     // Change the position of the cursor, in blocking mode
145     m_pParent->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
146 }
147
148
149 void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
150 {
151     // Get the position of the control
152     const Position *pPos = getPosition();
153     if( !pPos )
154     {
155         return;
156     }
157
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;
161
162     // Compute the polar coordinates. angle is -(-j,OM)
163     float r = sqrt((float)(x*x + y*y));
164     if( r == 0 )
165     {
166         return;
167     }
168     float angle = acos(y/r);
169     if( x > 0 )
170     {
171         angle = 2*M_PI - angle;
172     }
173
174     if( angle >= m_minAngle && angle <= m_maxAngle )
175     {
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 )
179         {
180             m_rVariable.set( newVal );
181         }
182     }
183 }
184