]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/controls/ctrl_slider.cpp
* all: the skin sliders can now have a background image, which
[vlc] / modules / gui / skins2 / controls / ctrl_slider.cpp
index 9564d39a08dfa887f7a0fdc1e8d4e2343c2cc686..73edd8cb2e5833e098bec80f8259ab0718a5ddd7 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * ctrl_slider.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
- * $Id: ctrl_slider.cpp,v 1.1 2004/01/03 23:31:33 asmax Exp $
+ * Copyright (C) 2003 the VideoLAN team
+ * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  *          Olivier Teulière <ipkiss@via.ecp.fr>
@@ -27,7 +27,7 @@
 #include "../events/evt_mouse.hpp"
 #include "../events/evt_scroll.hpp"
 #include "../src/generic_bitmap.hpp"
-#include "../src/generic_window.hpp"
+#include "../src/top_window.hpp"
 #include "../src/os_factory.hpp"
 #include "../src/os_graphics.hpp"
 #include "../utils/position.hpp"
@@ -35,7 +35,7 @@
 
 
 #define RANGE 40
-#define SCROLL_STEP 0.05
+#define SCROLL_STEP 0.05f
 
 
 CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
@@ -47,15 +47,14 @@ CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
                                     VarBool *pVisible,
                                     const UString &rTooltip,
                                     const UString &rHelp ):
-    CtrlGeneric( pIntf, rHelp ), m_fsm( pIntf ), m_rVariable( rVariable ),
-    m_pVisible( pVisible ), m_tooltip( rTooltip ),
+    CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
+    m_rVariable( rVariable ), m_tooltip( rTooltip ),
     m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() ),
-    m_cmdOverDown( this, &transOverDown ),
-    m_cmdDownOver( this, &transDownOver ), m_cmdOverUp( this, &transOverUp ),
-    m_cmdUpOver( this, &transUpOver ), m_cmdMove( this, &transMove ),
-    m_cmdScroll( this, &transScroll ),
+    m_cmdOverDown( this ), m_cmdDownOver( this ),
+    m_cmdOverUp( this ), m_cmdUpOver( this ),
+    m_cmdMove( this ), m_cmdScroll( this ),
     m_lastPercentage( 0 ), m_xOffset( 0 ), m_yOffset( 0 ),
-    m_pEvt( NULL ), m_curve( rCurve )
+    m_pEvt( NULL ), m_rCurve( rCurve )
 {
     // Build the images of the cursor
     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
@@ -91,12 +90,6 @@ CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
     // Observe the position variable
     m_rVariable.addObserver( this );
 
-    // Observe the visibility variable
-    if( m_pVisible )
-    {
-        m_pVisible->addObserver( this );
-    }
-
     // Initial position of the cursor
     m_lastPercentage = m_rVariable.get();
 }
@@ -105,10 +98,6 @@ CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
 CtrlSliderCursor::~CtrlSliderCursor()
 {
     m_rVariable.delObserver( this );
-    if( m_pVisible )
-    {
-        m_pVisible->delObserver( this );
-    }
     SKINS_DELETE( m_pImgUp );
     SKINS_DELETE( m_pImgDown );
     SKINS_DELETE( m_pImgOver );
@@ -130,10 +119,10 @@ bool CtrlSliderCursor::mouseOver( int x, int y ) const
     {
         // Compute the position of the cursor
         int xPos, yPos;
-        m_curve.getPoint( m_rVariable.get(), xPos, yPos );
+        m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
 
         // Compute the resize factors
-        double factorX = 0, factorY = 0;
+        float factorX, factorY;
         getResizeFactors( factorX, factorY );
         xPos = (int)(xPos * factorX);
         yPos = (int)(yPos * factorY);
@@ -150,14 +139,14 @@ bool CtrlSliderCursor::mouseOver( int x, int y ) const
 
 void CtrlSliderCursor::draw( OSGraphics &rImage, int xDest, int yDest )
 {
-    if( m_pImg && (!m_pVisible || m_pVisible->get()) )
+    if( m_pImg )
     {
         // Compute the position of the cursor
         int xPos, yPos;
-        m_curve.getPoint( m_rVariable.get(), xPos, yPos );
+        m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
 
         // Compute the resize factors
-        double factorX = 0, factorY = 0;
+        float factorX, factorY;
         getResizeFactors( factorX, factorY );
         xPos = (int)(xPos * factorX);
         yPos = (int)(yPos * factorY);
@@ -173,105 +162,142 @@ void CtrlSliderCursor::draw( OSGraphics &rImage, int xDest, int yDest )
 void CtrlSliderCursor::onUpdate( Subject<VarPercent> &rVariable )
 {
     // The position has changed
-    notifyLayout();
-}
-
-
-void CtrlSliderCursor::onUpdate( Subject<VarBool> &rVariable )
-{
-    // The visibility variable has changed
-    notifyLayout();
+    if( m_pImg )
+    {
+        notifyLayout( m_rCurve.getWidth() + m_pImg->getWidth(),
+                      m_rCurve.getHeight() + m_pImg->getHeight(),
+                      - m_pImg->getWidth() / 2,
+                      - m_pImg->getHeight() / 2 );
+    }
+    else
+        notifyLayout();
 }
 
 
-void CtrlSliderCursor::transOverDown( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdOverDown::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
+    EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
 
     // Compute the resize factors
-    double factorX = 0, factorY = 0;
-    pThis->getResizeFactors( factorX, factorY );
+    float factorX, factorY;
+    m_pParent->getResizeFactors( factorX, factorY );
+
+    // Get the position of the control
+    const Position *pPos = m_pParent->getPosition();
 
     // Compute the offset
     int tempX, tempY;
-    pThis->m_curve.getPoint( pThis->m_rVariable.get(), tempX, tempY );
-    pThis->m_xOffset = pEvtMouse->getXPos() - (int)(tempX * factorX);
-    pThis->m_yOffset = pEvtMouse->getYPos() - (int)(tempY * factorY);
-
-    pThis->captureMouse();
-    pThis->m_pImg = pThis->m_pImgDown;
-    pThis->notifyLayout();
+    m_pParent->m_rCurve.getPoint( m_pParent->m_rVariable.get(), tempX, tempY );
+    m_pParent->m_xOffset = pEvtMouse->getXPos() - pPos->getLeft()
+                       - (int)(tempX * factorX);
+    m_pParent->m_yOffset = pEvtMouse->getYPos() - pPos->getTop()
+                       - (int)(tempY * factorY);
+
+    m_pParent->captureMouse();
+    m_pParent->m_pImg = m_pParent->m_pImgDown;
+    if( m_pParent->m_pImg )
+    {
+        m_pParent->notifyLayout(
+            m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
+            m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
+            - m_pParent->m_pImg->getWidth() / 2,
+            - m_pParent->m_pImg->getHeight() / 2 );
+    }
+    else
+        m_pParent->notifyLayout();
 }
 
 
-void CtrlSliderCursor::transDownOver( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdDownOver::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-
     // Save the position
-    pThis->m_lastPercentage = pThis->m_rVariable.get();
+    m_pParent->m_lastPercentage = m_pParent->m_rVariable.get();
 
-    pThis->releaseMouse();
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayout();
+    m_pParent->releaseMouse();
+    m_pParent->m_pImg = m_pParent->m_pImgUp;
+    if( m_pParent->m_pImg )
+    {
+        m_pParent->notifyLayout(
+            m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
+            m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
+            - m_pParent->m_pImg->getWidth() / 2,
+            - m_pParent->m_pImg->getHeight() / 2 );
+    }
+    else
+        m_pParent->notifyLayout();
 }
 
 
-void CtrlSliderCursor::transUpOver( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdUpOver::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-
-    pThis->m_pImg = pThis->m_pImgOver;
-    pThis->notifyLayout();
+    m_pParent->m_pImg = m_pParent->m_pImgOver;
+    if( m_pParent->m_pImg )
+    {
+        m_pParent->notifyLayout(
+            m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
+            m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
+            - m_pParent->m_pImg->getWidth() / 2,
+            - m_pParent->m_pImg->getHeight() / 2 );
+    }
+    else
+        m_pParent->notifyLayout();
 }
 
 
-void CtrlSliderCursor::transOverUp( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdOverUp::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-
-    pThis->m_pImg = pThis->m_pImgUp;
-    pThis->notifyLayout();
+    m_pParent->m_pImg = m_pParent->m_pImgUp;
+    if( m_pParent->m_pImg )
+    {
+        m_pParent->notifyLayout(
+            m_pParent->m_rCurve.getWidth() + m_pParent->m_pImg->getWidth(),
+            m_pParent->m_rCurve.getHeight() + m_pParent->m_pImg->getHeight(),
+            - m_pParent->m_pImg->getWidth() / 2,
+            - m_pParent->m_pImg->getHeight() / 2 );
+    }
+    else
+        m_pParent->notifyLayout();
 }
 
 
-void CtrlSliderCursor::transMove( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdMove::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;
+    EvtMouse *pEvtMouse = (EvtMouse*)m_pParent->m_pEvt;
 
     // Get the position of the control
-    const Position *pPos = pThis->getPosition();
+    const Position *pPos = m_pParent->getPosition();
 
     // Compute the resize factors
-    double factorX = 0, factorY = 0;
-    pThis->getResizeFactors( factorX, factorY );
-
-    // XXX: This could be optimized a little bit
-    if( pThis->m_curve.getMinDist(
-        (int)((pEvtMouse->getXPos() - pPos->getLeft()) / factorX),
-        (int)((pEvtMouse->getYPos() - pPos->getTop()) / factorY) ) < RANGE )
+    float factorX, factorY;
+    m_pParent->getResizeFactors( factorX, factorY );
+
+    // Compute the relative position of the centre of the cursor
+    float relX = pEvtMouse->getXPos() - pPos->getLeft() - m_pParent->m_xOffset;
+    float relY = pEvtMouse->getYPos() - pPos->getTop() - m_pParent->m_yOffset;
+    // Ponderate with the resize factors
+    int relXPond = (int)(relX / factorX);
+    int relYPond = (int)(relY / factorY);
+
+    // Update the position
+    if( m_pParent->m_rCurve.getMinDist( relXPond, relYPond ) < RANGE )
     {
-        double percentage = pThis->m_curve.getNearestPercent(
-            (int)((pEvtMouse->getXPos() - pThis->m_xOffset) / factorX),
-            (int)((pEvtMouse->getYPos() - pThis->m_yOffset) / factorY) );
-        pThis->m_rVariable.set( percentage );
+        float percentage = m_pParent->m_rCurve.getNearestPercent( relXPond,
+                                                              relYPond );
+        m_pParent->m_rVariable.set( percentage );
     }
     else
     {
-        pThis->m_rVariable.set( pThis->m_lastPercentage );
+        m_pParent->m_rVariable.set( m_pParent->m_lastPercentage );
     }
 }
 
-void CtrlSliderCursor::transScroll( SkinObject *pCtrl )
+void CtrlSliderCursor::CmdScroll::execute()
 {
-    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
-    EvtScroll *pEvtScroll = (EvtScroll*)pThis->m_pEvt;
+    EvtScroll *pEvtScroll = (EvtScroll*)m_pParent->m_pEvt;
 
     int direction = pEvtScroll->getDirection();
 
-    double percentage = pThis->m_rVariable.get();
+    float percentage = m_pParent->m_rVariable.get();
     if( direction == EvtScroll::kUp )
     {
         percentage += SCROLL_STEP;
@@ -281,12 +307,12 @@ void CtrlSliderCursor::transScroll( SkinObject *pCtrl )
         percentage -= SCROLL_STEP;
     }
 
-    pThis->m_rVariable.set( percentage );
+    m_pParent->m_rVariable.set( percentage );
 }
 
 
-void CtrlSliderCursor::getResizeFactors( double &rFactorX,
-                                         double &rFactorY ) const
+void CtrlSliderCursor::getResizeFactors( float &rFactorX,
+                                         float &rFactorY ) const
 {
     // Get the position of the control
     const Position *pPos = getPosition();
@@ -297,11 +323,11 @@ void CtrlSliderCursor::getResizeFactors( double &rFactorX,
     // Compute the resize factors
     if( m_width > 0 )
     {
-        rFactorX = (double)pPos->getWidth() / (double)m_width;
+        rFactorX = (float)pPos->getWidth() / (float)m_width;
     }
     if( m_height > 0 )
     {
-        rFactorY = (double)pPos->getHeight() / (double)m_height;
+        rFactorY = (float)pPos->getHeight() / (float)m_height;
     }
 }
 
@@ -309,28 +335,62 @@ void CtrlSliderCursor::getResizeFactors( double &rFactorX,
 
 CtrlSliderBg::CtrlSliderBg( intf_thread_t *pIntf, CtrlSliderCursor &rCursor,
                             const Bezier &rCurve, VarPercent &rVariable,
-                            int thickness, VarBool *pVisible,
+                            int thickness, GenericBitmap *pBackground,
+                            int nbImages, VarBool *pVisible,
                             const UString &rHelp ):
-    CtrlGeneric( pIntf, rHelp ), m_rCursor( rCursor ), m_rVariable( rVariable ),
-    m_thickness( thickness ), m_pVisible( pVisible ), m_curve( rCurve ),
-    m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() )
+    CtrlGeneric( pIntf, rHelp, pVisible ), m_rCursor( rCursor ),
+    m_rVariable( rVariable ), m_thickness( thickness ), m_rCurve( rCurve ),
+    m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() ),
+    m_pImgSeq( NULL ), m_nbImages( nbImages ), m_bgWidth( 0 ),
+    m_bgHeight( 0 ), m_position( 0 )
 {
+    if( pBackground )
+    {
+        // Build the background image sequence
+        OSFactory *pOsFactory = OSFactory::instance( getIntf() );
+        m_pImgSeq = pOsFactory->createOSGraphics( pBackground->getWidth(),
+                                                  pBackground->getHeight() );
+        m_pImgSeq->drawBitmap( *pBackground, 0, 0 );
+
+        m_bgWidth = pBackground->getWidth();
+        m_bgHeight = pBackground->getHeight() / nbImages;
+
+        // Observe the position variable
+        m_rVariable.addObserver( this );
+
+        // Initial position
+        m_position = (int)( m_rVariable.get() * (m_nbImages - 1) );
+    }
 }
 
 
-bool CtrlSliderBg::mouseOver( int x, int y ) const
+CtrlSliderBg::~CtrlSliderBg()
 {
-    if( m_pVisible && !m_pVisible->get() )
-    {
-        return false;
-    }
+    m_rVariable.delObserver( this );
+    delete m_pImgSeq;
+}
 
+
+bool CtrlSliderBg::mouseOver( int x, int y ) const
+{
     // Compute the resize factors
-    double factorX = 0, factorY = 1.0;
+    float factorX, factorY;
     getResizeFactors( factorX, factorY );
 
-    return (m_curve.getMinDist( (int)(x / factorY),
-                                (int)(y / factorY) ) < m_thickness );
+    return (m_rCurve.getMinDist( (int)(x / factorX),
+                                 (int)(y / factorY) ) < m_thickness );
+}
+
+
+void CtrlSliderBg::draw( OSGraphics &rImage, int xDest, int yDest )
+{
+    if( m_pImgSeq )
+    {
+        // Draw the background image
+        // XXX the "-3" is a hack for winamp skins...
+        rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_bgHeight,
+                             xDest, yDest, m_bgWidth, m_bgHeight - 3);
+    }
 }
 
 
@@ -339,7 +399,7 @@ void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
     if( rEvent.getAsString().find( "mouse:left:down" ) != string::npos )
     {
         // Compute the resize factors
-        double factorX = 0, factorY = 1.0;
+        float factorX, factorY;
         getResizeFactors( factorX, factorY );
 
         // Get the position of the control
@@ -349,13 +409,13 @@ void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
         EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
         int x = rEvtMouse.getXPos();
         int y = rEvtMouse.getYPos();
-        m_rVariable.set( m_curve.getNearestPercent(
+        m_rVariable.set( m_rCurve.getNearestPercent(
                             (int)((x - pPos->getLeft()) / factorX),
                             (int)((y - pPos->getTop()) / factorY) ) );
 
         // Forward the clic to the cursor
         EvtMouse evt( getIntf(), x, y, EvtMouse::kLeft, EvtMouse::kDown );
-        GenericWindow *pWin = getWindow();
+        TopWindow *pWin = getWindow();
         if( pWin )
         {
             EvtEnter evtEnter( getIntf() );
@@ -368,7 +428,7 @@ void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
     {
         int direction = ((EvtScroll&)rEvent).getDirection();
 
-        double percentage = m_rVariable.get();
+        float percentage = m_rVariable.get();
         if( direction == EvtScroll::kUp )
         {
             percentage += SCROLL_STEP;
@@ -383,14 +443,14 @@ void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
 }
 
 
-void CtrlSliderBg::onUpdate( Subject<VarBool> &rVariable )
+void CtrlSliderBg::onUpdate( Subject<VarPercent> &rVariable )
 {
-    // The visibility variable has changed
-    notifyLayout();
+    m_position = (int)( m_rVariable.get() * (m_nbImages - 1) );
+    notifyLayout( m_bgWidth, m_bgHeight );
 }
 
 
-void CtrlSliderBg::getResizeFactors( double &rFactorX, double &rFactorY ) const
+void CtrlSliderBg::getResizeFactors( float &rFactorX, float &rFactorY ) const
 {
     // Get the position of the control
     const Position *pPos = getPosition();
@@ -401,11 +461,11 @@ void CtrlSliderBg::getResizeFactors( double &rFactorX, double &rFactorY ) const
     // Compute the resize factors
     if( m_width > 0 )
     {
-        rFactorX = (double)pPos->getWidth() / (double)m_width;
+        rFactorX = (float)pPos->getWidth() / (float)m_width;
     }
     if( m_height > 0 )
     {
-        rFactorY = (double)pPos->getHeight() / (double)m_height;
+        rFactorY = (float)pPos->getHeight() / (float)m_height;
     }
 }