1 /*****************************************************************************
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "ctrl_slider.hpp"
26 #include "../events/evt_enter.hpp"
27 #include "../events/evt_mouse.hpp"
28 #include "../events/evt_scroll.hpp"
29 #include "../src/generic_bitmap.hpp"
30 #include "../src/top_window.hpp"
31 #include "../src/os_factory.hpp"
32 #include "../src/os_graphics.hpp"
33 #include "../utils/position.hpp"
34 #include "../utils/var_percent.hpp"
38 #define SCROLL_STEP 0.05f
41 CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
42 const GenericBitmap &rBmpUp,
43 const GenericBitmap &rBmpOver,
44 const GenericBitmap &rBmpDown,
46 VarPercent &rVariable,
48 const UString &rTooltip,
49 const UString &rHelp ):
50 CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
51 m_rVariable( rVariable ), m_tooltip( rTooltip ),
52 m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() ),
53 m_cmdOverDown( this ), m_cmdDownOver( this ),
54 m_cmdOverUp( this ), m_cmdUpOver( this ),
55 m_cmdMove( this ), m_cmdScroll( this ),
56 m_lastPercentage( 0 ), m_xOffset( 0 ), m_yOffset( 0 ),
57 m_pEvt( NULL ), m_rCurve( rCurve )
59 // Build the images of the cursor
60 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
61 m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
63 m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
64 m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
65 rBmpDown.getHeight() );
66 m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
67 m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
68 rBmpOver.getHeight() );
69 m_pImgOver->drawBitmap( rBmpOver, 0, 0 );
72 m_fsm.addState( "up" );
73 m_fsm.addState( "over" );
74 m_fsm.addState( "down" );
77 m_fsm.addTransition( "over", "mouse:left:down", "down",
79 m_fsm.addTransition( "down", "mouse:left:up", "over",
81 m_fsm.addTransition( "over", "leave", "up", &m_cmdOverUp );
82 m_fsm.addTransition( "up", "enter", "over", &m_cmdUpOver );
83 m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );
84 m_fsm.addTransition( "over", "scroll", "over", &m_cmdScroll );
87 m_fsm.setState( "up" );
90 // Observe the position variable
91 m_rVariable.addObserver( this );
93 // Initial position of the cursor
94 m_lastPercentage = m_rVariable.get();
98 CtrlSliderCursor::~CtrlSliderCursor()
100 m_rVariable.delObserver( this );
101 SKINS_DELETE( m_pImgUp );
102 SKINS_DELETE( m_pImgDown );
103 SKINS_DELETE( m_pImgOver );
107 void CtrlSliderCursor::handleEvent( EvtGeneric &rEvent )
109 // Save the event to use it in callbacks
112 m_fsm.handleTransition( rEvent.getAsString() );
116 bool CtrlSliderCursor::mouseOver( int x, int y ) const
120 // Compute the position of the cursor
122 m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
124 // Compute the resize factors
125 float factorX, factorY;
126 getResizeFactors( factorX, factorY );
127 xPos = (int)(xPos * factorX);
128 yPos = (int)(yPos * factorY);
130 return m_pImg->hit( x - xPos + m_pImg->getWidth() / 2,
131 y - yPos + m_pImg->getHeight() / 2 );
140 void CtrlSliderCursor::draw( OSGraphics &rImage, int xDest, int yDest )
144 // Compute the position of the cursor
146 m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
148 // Compute the resize factors
149 float factorX, factorY;
150 getResizeFactors( factorX, factorY );
151 xPos = (int)(xPos * factorX);
152 yPos = (int)(yPos * factorY);
154 // Draw the current image
155 rImage.drawGraphics( *m_pImg, 0, 0,
156 xDest + xPos - m_pImg->getWidth() / 2,
157 yDest + yPos - m_pImg->getHeight() / 2 );
162 void CtrlSliderCursor::onUpdate( Subject<VarPercent> &rVariable )
164 // The position has changed
167 notifyLayout( m_rCurve.getWidth() + m_pImg->getWidth(),
168 m_rCurve.getHeight() + m_pImg->getHeight(),
169 - m_pImg->getWidth() / 2,
170 - m_pImg->getHeight() / 2 );
177 void CtrlSliderCursor::CmdOverDown::execute()
179 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
181 // Compute the resize factors
182 float factorX, factorY;
183 m_pParent->getResizeFactors( factorX, factorY );
185 // Get the position of the control
186 const Position *pPos = m_pParent->getPosition();
188 // Compute the offset
190 m_pParent->m_rCurve.getPoint( m_pParent->m_rVariable.get(), tempX, tempY );
191 m_pParent->m_xOffset = pEvtMouse->getXPos() - pPos->getLeft()
192 - (int)(tempX * factorX);
193 m_pParent->m_yOffset = pEvtMouse->getYPos() - pPos->getTop()
194 - (int)(tempY * factorY);
196 m_pParent->captureMouse();
197 m_pParent->m_pImg = m_pParent->m_pImgDown;
198 if( m_pParent->m_pImg )
200 m_pParent->notifyLayout(
201 m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
202 m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
203 - m_pParent->m_pImg->getWidth() / 2,
204 - m_pParent->m_pImg->getHeight() / 2 );
207 m_pParent->notifyLayout();
211 void CtrlSliderCursor::CmdDownOver::execute()
214 m_pParent->m_lastPercentage = m_pParent->m_rVariable.get();
216 m_pParent->releaseMouse();
217 m_pParent->m_pImg = m_pParent->m_pImgUp;
218 if( m_pParent->m_pImg )
220 m_pParent->notifyLayout(
221 m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
222 m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
223 - m_pParent->m_pImg->getWidth() / 2,
224 - m_pParent->m_pImg->getHeight() / 2 );
227 m_pParent->notifyLayout();
231 void CtrlSliderCursor::CmdUpOver::execute()
233 m_pParent->m_pImg = m_pParent->m_pImgOver;
234 if( m_pParent->m_pImg )
236 m_pParent->notifyLayout(
237 m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
238 m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
239 - m_pParent->m_pImg->getWidth() / 2,
240 - m_pParent->m_pImg->getHeight() / 2 );
243 m_pParent->notifyLayout();
247 void CtrlSliderCursor::CmdOverUp::execute()
249 m_pParent->m_pImg = m_pParent->m_pImgUp;
250 if( m_pParent->m_pImg )
252 m_pParent->notifyLayout(
253 m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
254 m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
255 - m_pParent->m_pImg->getWidth() / 2,
256 - m_pParent->m_pImg->getHeight() / 2 );
259 m_pParent->notifyLayout();
263 void CtrlSliderCursor::CmdMove::execute()
265 EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
267 // Get the position of the control
268 const Position *pPos = m_pParent->getPosition();
270 // Compute the resize factors
271 float factorX, factorY;
272 m_pParent->getResizeFactors( factorX, factorY );
274 // Compute the relative position of the centre of the cursor
275 float relX = pEvtMouse->getXPos() - pPos->getLeft() - m_pParent->m_xOffset;
276 float relY = pEvtMouse->getYPos() - pPos->getTop() - m_pParent->m_yOffset;
277 // Ponderate with the resize factors
278 int relXPond = (int)(relX / factorX);
279 int relYPond = (int)(relY / factorY);
281 // Update the position
282 if( m_pParent->m_rCurve.getMinDist( relXPond, relYPond ) < RANGE )
284 float percentage = m_pParent->m_rCurve.getNearestPercent( relXPond,
286 m_pParent->m_rVariable.set( percentage );
290 m_pParent->m_rVariable.set( m_pParent->m_lastPercentage );
294 void CtrlSliderCursor::CmdScroll::execute()
296 EvtScroll *pEvtScroll = (EvtScroll*)m_pParent->m_pEvt;
298 int direction = pEvtScroll->getDirection();
300 float percentage = m_pParent->m_rVariable.get();
301 if( direction == EvtScroll::kUp )
303 percentage += SCROLL_STEP;
307 percentage -= SCROLL_STEP;
310 m_pParent->m_rVariable.set( percentage );
314 void CtrlSliderCursor::getResizeFactors( float &rFactorX,
315 float &rFactorY ) const
317 // Get the position of the control
318 const Position *pPos = getPosition();
323 // Compute the resize factors
326 rFactorX = (float)pPos->getWidth() / (float)m_width;
330 rFactorY = (float)pPos->getHeight() / (float)m_height;
336 CtrlSliderBg::CtrlSliderBg( intf_thread_t *pIntf, CtrlSliderCursor &rCursor,
337 const Bezier &rCurve, VarPercent &rVariable,
338 int thickness, GenericBitmap *pBackground,
339 int nbImages, VarBool *pVisible,
340 const UString &rHelp ):
341 CtrlGeneric( pIntf, rHelp, pVisible ), m_rCursor( rCursor ),
342 m_rVariable( rVariable ), m_thickness( thickness ), m_rCurve( rCurve ),
343 m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() ),
344 m_pImgSeq( NULL ), m_nbImages( nbImages ), m_bgWidth( 0 ),
345 m_bgHeight( 0 ), m_position( 0 )
349 // Build the background image sequence
350 OSFactory *pOsFactory = OSFactory::instance( getIntf() );
351 m_pImgSeq = pOsFactory->createOSGraphics( pBackground->getWidth(),
352 pBackground->getHeight() );
353 m_pImgSeq->drawBitmap( *pBackground, 0, 0 );
355 m_bgWidth = pBackground->getWidth();
356 m_bgHeight = pBackground->getHeight() / nbImages;
358 // Observe the position variable
359 m_rVariable.addObserver( this );
362 m_position = (int)( m_rVariable.get() * (m_nbImages - 1) );
367 CtrlSliderBg::~CtrlSliderBg()
369 m_rVariable.delObserver( this );
374 bool CtrlSliderBg::mouseOver( int x, int y ) const
376 // Compute the resize factors
377 float factorX, factorY;
378 getResizeFactors( factorX, factorY );
380 return (m_rCurve.getMinDist( (int)(x / factorX),
381 (int)(y / factorY) ) < m_thickness );
385 void CtrlSliderBg::draw( OSGraphics &rImage, int xDest, int yDest )
389 // Draw the background image
390 // XXX the "-3" is a hack for winamp skins...
391 rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_bgHeight,
392 xDest, yDest, m_bgWidth, m_bgHeight - 3);
397 void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
399 if( rEvent.getAsString().find( "mouse:left:down" ) != string::npos )
401 // Compute the resize factors
402 float factorX, factorY;
403 getResizeFactors( factorX, factorY );
405 // Get the position of the control
406 const Position *pPos = getPosition();
408 // Get the value corresponding to the position of the mouse
409 EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
410 int x = rEvtMouse.getXPos();
411 int y = rEvtMouse.getYPos();
412 m_rVariable.set( m_rCurve.getNearestPercent(
413 (int)((x - pPos->getLeft()) / factorX),
414 (int)((y - pPos->getTop()) / factorY) ) );
416 // Forward the clic to the cursor
417 EvtMouse evt( getIntf(), x, y, EvtMouse::kLeft, EvtMouse::kDown );
418 TopWindow *pWin = getWindow();
421 EvtEnter evtEnter( getIntf() );
422 // XXX It was not supposed to be implemented like that !!
423 pWin->forwardEvent( evtEnter, m_rCursor );
424 pWin->forwardEvent( evt, m_rCursor );
427 else if( rEvent.getAsString().find( "scroll" ) != string::npos )
429 int direction = ((EvtScroll&)rEvent).getDirection();
431 float percentage = m_rVariable.get();
432 if( direction == EvtScroll::kUp )
434 percentage += SCROLL_STEP;
438 percentage -= SCROLL_STEP;
441 m_rVariable.set( percentage );
446 void CtrlSliderBg::onUpdate( Subject<VarPercent> &rVariable )
448 m_position = (int)( m_rVariable.get() * (m_nbImages - 1) );
449 notifyLayout( m_bgWidth, m_bgHeight );
453 void CtrlSliderBg::getResizeFactors( float &rFactorX, float &rFactorY ) const
455 // Get the position of the control
456 const Position *pPos = getPosition();
461 // Compute the resize factors
464 rFactorX = (float)pPos->getWidth() / (float)m_width;
468 rFactorY = (float)pPos->getHeight() / (float)m_height;