ctrl_slider.cpp 12.1 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * ctrl_slider.cpp
 *****************************************************************************
 * Copyright (C) 2003 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *          Olivier Teulire <ipkiss@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include "ctrl_slider.hpp"
#include "../events/evt_enter.hpp"
#include "../events/evt_mouse.hpp"
#include "../events/evt_scroll.hpp"
#include "../src/generic_bitmap.hpp"
30
#include "../src/top_window.hpp"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "../src/os_factory.hpp"
#include "../src/os_graphics.hpp"
#include "../utils/position.hpp"
#include "../utils/var_percent.hpp"


#define RANGE 40
#define SCROLL_STEP 0.05


CtrlSliderCursor::CtrlSliderCursor( intf_thread_t *pIntf,
                                    const GenericBitmap &rBmpUp,
                                    const GenericBitmap &rBmpOver,
                                    const GenericBitmap &rBmpDown,
                                    const Bezier &rCurve,
                                    VarPercent &rVariable,
                                    VarBool *pVisible,
                                    const UString &rTooltip,
                                    const UString &rHelp ):
50 51
    CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ),
    m_rVariable( rVariable ), m_tooltip( rTooltip ),
52 53 54 55 56 57
    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_lastPercentage( 0 ), m_xOffset( 0 ), m_yOffset( 0 ),
58
    m_pEvt( NULL ), m_rCurve( rCurve )
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
{
    // Build the images of the cursor
    OSFactory *pOsFactory = OSFactory::instance( getIntf() );
    m_pImgUp = pOsFactory->createOSGraphics( rBmpUp.getWidth(),
                                             rBmpUp.getHeight() );
    m_pImgUp->drawBitmap( rBmpUp, 0, 0 );
    m_pImgDown = pOsFactory->createOSGraphics( rBmpDown.getWidth(),
                                               rBmpDown.getHeight() );
    m_pImgDown->drawBitmap( rBmpDown, 0, 0 );
    m_pImgOver = pOsFactory->createOSGraphics( rBmpOver.getWidth(),
                                               rBmpOver.getHeight() );
    m_pImgOver->drawBitmap( rBmpOver, 0, 0 );

    // States
    m_fsm.addState( "up" );
    m_fsm.addState( "over" );
    m_fsm.addState( "down" );

    // Transitions
    m_fsm.addTransition( "over", "mouse:left:down", "down",
                         &m_cmdOverDown );
    m_fsm.addTransition( "down", "mouse:left:up", "over",
                         &m_cmdDownOver );
    m_fsm.addTransition( "over", "leave", "up", &m_cmdOverUp );
    m_fsm.addTransition( "up", "enter", "over", &m_cmdUpOver );
    m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );
    m_fsm.addTransition( "over", "scroll", "over", &m_cmdScroll );

    // Initial state
    m_fsm.setState( "up" );
    m_pImg = m_pImgUp;

    // Observe the position variable
    m_rVariable.addObserver( this );

    // Initial position of the cursor
    m_lastPercentage = m_rVariable.get();
}


CtrlSliderCursor::~CtrlSliderCursor()
{
    m_rVariable.delObserver( this );
    SKINS_DELETE( m_pImgUp );
    SKINS_DELETE( m_pImgDown );
    SKINS_DELETE( m_pImgOver );
}


void CtrlSliderCursor::handleEvent( EvtGeneric &rEvent )
{
    // Save the event to use it in callbacks
    m_pEvt = &rEvent;

    m_fsm.handleTransition( rEvent.getAsString() );
}


bool CtrlSliderCursor::mouseOver( int x, int y ) const
{
    if( m_pImg )
    {
        // Compute the position of the cursor
        int xPos, yPos;
123
        m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
124 125

        // Compute the resize factors
126
        float factorX, factorY;
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        getResizeFactors( factorX, factorY );
        xPos = (int)(xPos * factorX);
        yPos = (int)(yPos * factorY);

        return m_pImg->hit( x - xPos + m_pImg->getWidth() / 2,
                            y - yPos + m_pImg->getHeight() / 2 );
    }
    else
    {
        return false;
    }
}


void CtrlSliderCursor::draw( OSGraphics &rImage, int xDest, int yDest )
{
143
    if( m_pImg )
144 145 146
    {
        // Compute the position of the cursor
        int xPos, yPos;
147
        m_rCurve.getPoint( m_rVariable.get(), xPos, yPos );
148 149

        // Compute the resize factors
150
        float factorX, factorY;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        getResizeFactors( factorX, factorY );
        xPos = (int)(xPos * factorX);
        yPos = (int)(yPos * factorY);

        // Draw the current image
        rImage.drawGraphics( *m_pImg, 0, 0,
                             xDest + xPos - m_pImg->getWidth() / 2,
                             yDest + yPos - m_pImg->getHeight() / 2 );
    }
}


void CtrlSliderCursor::onUpdate( Subject<VarPercent> &rVariable )
{
    // The position has changed
166
    notifyLayout( m_rCurve.getWidth(), m_rCurve.getHeight() );
167 168 169 170 171 172 173 174 175
}


void CtrlSliderCursor::transOverDown( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;

    // Compute the resize factors
176
    float factorX, factorY;
177 178
    pThis->getResizeFactors( factorX, factorY );

179 180 181
    // Get the position of the control
    const Position *pPos = pThis->getPosition();

182 183
    // Compute the offset
    int tempX, tempY;
184
    pThis->m_rCurve.getPoint( pThis->m_rVariable.get(), tempX, tempY );
185 186 187 188
    pThis->m_xOffset = pEvtMouse->getXPos() - pPos->getLeft()
                       - (int)(tempX * factorX);
    pThis->m_yOffset = pEvtMouse->getYPos() - pPos->getTop()
                       - (int)(tempY * factorY);
189 190 191

    pThis->captureMouse();
    pThis->m_pImg = pThis->m_pImgDown;
192 193
    pThis->notifyLayout( pThis->m_rCurve.getWidth(),
                         pThis->m_rCurve.getHeight() );
194 195 196 197 198 199 200 201 202 203 204 205
}


void CtrlSliderCursor::transDownOver( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;

    // Save the position
    pThis->m_lastPercentage = pThis->m_rVariable.get();

    pThis->releaseMouse();
    pThis->m_pImg = pThis->m_pImgUp;
206 207
    pThis->notifyLayout( pThis->m_rCurve.getWidth(),
                         pThis->m_rCurve.getHeight() );
208 209 210 211 212 213 214 215
}


void CtrlSliderCursor::transUpOver( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;

    pThis->m_pImg = pThis->m_pImgOver;
216 217
    pThis->notifyLayout( pThis->m_rCurve.getWidth(),
                         pThis->m_rCurve.getHeight() );
218 219 220 221 222 223 224 225
}


void CtrlSliderCursor::transOverUp( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;

    pThis->m_pImg = pThis->m_pImgUp;
226 227
    pThis->notifyLayout( pThis->m_rCurve.getWidth(),
                         pThis->m_rCurve.getHeight() );
228 229 230 231 232 233 234 235 236 237 238 239
}


void CtrlSliderCursor::transMove( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;

    // Get the position of the control
    const Position *pPos = pThis->getPosition();

    // Compute the resize factors
240
    float factorX, factorY;
241 242
    pThis->getResizeFactors( factorX, factorY );

243 244 245 246 247 248 249 250
    // Compute the relative position of the centre of the cursor
    float relX = pEvtMouse->getXPos() - pPos->getLeft() - pThis->m_xOffset;
    float relY = pEvtMouse->getYPos() - pPos->getTop() - pThis->m_yOffset;
    // Ponderate with the resize factors
    int relXPond = (int)(relX / factorX);
    int relYPond = (int)(relY / factorY);

    // Update the position
251
    if( pThis->m_rCurve.getMinDist( relXPond, relYPond ) < RANGE )
252
    {
253 254
        float percentage = pThis->m_rCurve.getNearestPercent( relXPond,
                                                              relYPond );
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        pThis->m_rVariable.set( percentage );
    }
    else
    {
        pThis->m_rVariable.set( pThis->m_lastPercentage );
    }
}

void CtrlSliderCursor::transScroll( SkinObject *pCtrl )
{
    CtrlSliderCursor *pThis = (CtrlSliderCursor*)pCtrl;
    EvtScroll *pEvtScroll = (EvtScroll*)pThis->m_pEvt;

    int direction = pEvtScroll->getDirection();

Cyril Deguet's avatar
Cyril Deguet committed
270
    float percentage = pThis->m_rVariable.get();
271 272 273 274 275 276 277 278 279 280 281 282 283
    if( direction == EvtScroll::kUp )
    {
        percentage += SCROLL_STEP;
    }
    else
    {
        percentage -= SCROLL_STEP;
    }

    pThis->m_rVariable.set( percentage );
}


Cyril Deguet's avatar
Cyril Deguet committed
284 285
void CtrlSliderCursor::getResizeFactors( float &rFactorX,
                                         float &rFactorY ) const
286 287 288 289 290 291 292 293 294 295
{
    // Get the position of the control
    const Position *pPos = getPosition();

    rFactorX = 1.0;
    rFactorY = 1.0;

    // Compute the resize factors
    if( m_width > 0 )
    {
Cyril Deguet's avatar
Cyril Deguet committed
296
        rFactorX = (float)pPos->getWidth() / (float)m_width;
297 298 299
    }
    if( m_height > 0 )
    {
Cyril Deguet's avatar
Cyril Deguet committed
300
        rFactorY = (float)pPos->getHeight() / (float)m_height;
301 302 303 304 305 306 307 308 309
    }
}



CtrlSliderBg::CtrlSliderBg( intf_thread_t *pIntf, CtrlSliderCursor &rCursor,
                            const Bezier &rCurve, VarPercent &rVariable,
                            int thickness, VarBool *pVisible,
                            const UString &rHelp ):
310
    CtrlGeneric( pIntf, rHelp, pVisible ), m_rCursor( rCursor ),
311
    m_rVariable( rVariable ), m_thickness( thickness ), m_rCurve( rCurve ),
312 313 314 315 316 317 318 319
    m_width( rCurve.getWidth() ), m_height( rCurve.getHeight() )
{
}


bool CtrlSliderBg::mouseOver( int x, int y ) const
{
    // Compute the resize factors
320
    float factorX, factorY;
321 322
    getResizeFactors( factorX, factorY );

323 324
    return (m_rCurve.getMinDist( (int)(x / factorX),
                                 (int)(y / factorY) ) < m_thickness );
325 326 327 328 329 330 331 332
}


void CtrlSliderBg::handleEvent( EvtGeneric &rEvent )
{
    if( rEvent.getAsString().find( "mouse:left:down" ) != string::npos )
    {
        // Compute the resize factors
333
        float factorX, factorY;
334 335 336 337 338 339 340 341 342
        getResizeFactors( factorX, factorY );

        // Get the position of the control
        const Position *pPos = getPosition();

        // Get the value corresponding to the position of the mouse
        EvtMouse &rEvtMouse = (EvtMouse&)rEvent;
        int x = rEvtMouse.getXPos();
        int y = rEvtMouse.getYPos();
343
        m_rVariable.set( m_rCurve.getNearestPercent(
344 345 346 347 348
                            (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 );
349
        TopWindow *pWin = getWindow();
350 351 352 353 354 355 356 357 358 359 360 361
        if( pWin )
        {
            EvtEnter evtEnter( getIntf() );
            // XXX It was not supposed to be implemented like that !!
            pWin->forwardEvent( evtEnter, m_rCursor );
            pWin->forwardEvent( evt, m_rCursor );
        }
    }
    else if( rEvent.getAsString().find( "scroll" ) != string::npos )
    {
        int direction = ((EvtScroll&)rEvent).getDirection();

Cyril Deguet's avatar
Cyril Deguet committed
362
        float percentage = m_rVariable.get();
363 364 365 366 367 368 369 370 371 372 373 374 375 376
        if( direction == EvtScroll::kUp )
        {
            percentage += SCROLL_STEP;
        }
        else
        {
            percentage -= SCROLL_STEP;
        }

        m_rVariable.set( percentage );
    }
}


Cyril Deguet's avatar
Cyril Deguet committed
377
void CtrlSliderBg::getResizeFactors( float &rFactorX, float &rFactorY ) const
378 379 380 381 382 383 384 385 386 387
{
    // Get the position of the control
    const Position *pPos = getPosition();

    rFactorX = 1.0;
    rFactorY = 1.0;

    // Compute the resize factors
    if( m_width > 0 )
    {
Cyril Deguet's avatar
Cyril Deguet committed
388
        rFactorX = (float)pPos->getWidth() / (float)m_width;
389 390 391
    }
    if( m_height > 0 )
    {
Cyril Deguet's avatar
Cyril Deguet committed
392
        rFactorY = (float)pPos->getHeight() / (float)m_height;
393 394 395
    }
}