ctrl_radialslider.cpp 5.19 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * ctrl_radialslider.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 30 31 32 33 34 35 36 37
 *
 * 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 <math.h>
#include "ctrl_radialslider.hpp"
#include "../events/evt_mouse.hpp"
#include "../src/generic_bitmap.hpp"
#include "../src/generic_window.hpp"
#include "../src/os_factory.hpp"
#include "../src/os_graphics.hpp"
#include "../utils/position.hpp"
#include "../utils/var_percent.hpp"


CtrlRadialSlider::CtrlRadialSlider( intf_thread_t *pIntf,
                                    const GenericBitmap &rBmpSeq, int numImg,
Cyril Deguet's avatar
Cyril Deguet committed
38
                                    VarPercent &rVariable, float minAngle,
39 40 41
                                    float maxAngle, const UString &rHelp,
                                    VarBool *pVisible ):
    CtrlGeneric( pIntf, rHelp, pVisible ), m_fsm( pIntf ), m_numImg( numImg ),
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 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
    m_rVariable( rVariable ), m_minAngle( minAngle ), m_maxAngle( maxAngle ),
    m_cmdUpDown( this, &transUpDown ), m_cmdDownUp( this, &transDownUp ),
    m_cmdMove( this, &transMove ), m_position( 0 ), m_lastPos( 0 )
{
    // Build the images of the sequence
    OSFactory *pOsFactory = OSFactory::instance( getIntf() );
    m_pImgSeq = pOsFactory->createOSGraphics( rBmpSeq.getWidth(),
                                              rBmpSeq.getHeight() );
    m_pImgSeq->drawBitmap( rBmpSeq, 0, 0 );

    m_width = rBmpSeq.getWidth();
    m_height = rBmpSeq.getHeight() / numImg;

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

    // Transitions
    m_fsm.addTransition( "up", "mouse:left:down", "down", &m_cmdUpDown );
    m_fsm.addTransition( "down", "mouse:left:up", "up", &m_cmdDownUp );
    m_fsm.addTransition( "down", "motion", "down", &m_cmdMove );

    // Initial state
    m_fsm.setState( "up" );

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


CtrlRadialSlider::~CtrlRadialSlider()
{
    m_rVariable.delObserver( this );
    SKINS_DELETE( m_pImgSeq );
}


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

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


bool CtrlRadialSlider::mouseOver( int x, int y ) const
{
    return m_pImgSeq->hit( x, y + m_position * m_height );
}


void CtrlRadialSlider::draw( OSGraphics &rImage, int xDest, int yDest )
{
    rImage.drawGraphics( *m_pImgSeq, 0, m_position * m_height, xDest, yDest,
                         m_width, m_height );
}


void CtrlRadialSlider::onUpdate( Subject<VarPercent> &rVariable )
{
    m_position = (int)( m_rVariable.get() * m_numImg );
104
    notifyLayout( m_width, m_height );
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}


void CtrlRadialSlider::transUpDown( SkinObject *pCtrl )
{
    CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;

    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;

    // Change the position of the cursor, in non-blocking mode
    pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), false );

    pThis->captureMouse();
}


void CtrlRadialSlider::transDownUp( SkinObject *pCtrl )
{
    CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;

    pThis->releaseMouse();
}


void CtrlRadialSlider::transMove( SkinObject *pCtrl )
{
    CtrlRadialSlider *pThis = (CtrlRadialSlider*)pCtrl;

    EvtMouse *pEvtMouse = (EvtMouse*)pThis->m_pEvt;

    // Change the position of the cursor, in blocking mode
    pThis->setCursor( pEvtMouse->getXPos(), pEvtMouse->getYPos(), true );
}


void CtrlRadialSlider::setCursor( int posX, int posY, bool blocking )
{
    // Get the position of the control
    const Position *pPos = getPosition();
    if( !pPos )
    {
        return;
    }

    // Compute the position relative to the center
    int x = posX - pPos->getLeft() - m_width / 2;
    int y = posY - pPos->getTop() - m_width / 2;

    // Compute the polar coordinates. angle is -(-j,OM)
Cyril Deguet's avatar
Cyril Deguet committed
154
    float r = sqrt(x*x + y*y);
155 156 157 158
    if( r == 0 )
    {
        return;
    }
Cyril Deguet's avatar
Cyril Deguet committed
159
    float angle = acos(y/r);
160 161 162 163 164 165 166
    if( x > 0 )
    {
        angle = 2*M_PI - angle;
    }

    if( angle >= m_minAngle && angle <= m_maxAngle )
    {
Cyril Deguet's avatar
Cyril Deguet committed
167
        float newVal = (angle - m_minAngle) / (m_maxAngle - m_minAngle);
168 169 170 171 172 173 174 175
        // Avoid too fast moves of the cursor if blocking mode
        if( !blocking || fabs( m_rVariable.get() - newVal ) < 0.5 )
        {
            m_rVariable.set( newVal );
        }
    }
}