Ticket #8387: slider.patch

File slider.patch, 4.1 KB (added by salty-horse, 19 years ago)
  • gui/widget.cpp

    RCS file: /cvsroot/scummvm/scummvm/gui/widget.cpp,v
    retrieving revision 1.38
    diff -u -r1.38 gui/widget.cpp
     
    1818 * $Header: /cvsroot/scummvm/scummvm/gui/widget.cpp,v 1.38 2004/03/13 13:03:25 fingolfin Exp $
    1919 */
    2020
     21#include <math.h>
     22
    2123#include "stdafx.h"
    2224#include "common/util.h"
    2325#include "gui/widget.h"
     
    208210}
    209211
    210212void SliderWidget::handleMouseMoved(int x, int y, int button) {
    211         // TODO: when the mouse is dragged outside the widget, the slider should
    212         // snap back to the old value.
    213213        if (isEnabled() && _isDragging && x >= (int)_labelWidth) {
    214214                int newValue = posToValue(x - _labelWidth);
    215215                if (newValue < _valueMin)
     
    228228void SliderWidget::handleMouseDown(int x, int y, int button, int clickCount) {
    229229        if (isEnabled()) {
    230230                _isDragging = true;
     231                 _valueDragStart = _value;
    231232                handleMouseMoved(x, y, button);
    232233        }
    233234}
    234235
    235236void SliderWidget::handleMouseUp(int x, int y, int button, int clickCount) {
     237        if (isEnabled() && _isDragging)
     238                sendCommand(_cmd, _value);
     239 
     240        _isDragging = false;
     241}
     242
     243void SliderWidget::handleMouseLeft(int button) {
     244        ButtonWidget::handleMouseLeft(button);
    236245        if (isEnabled() && _isDragging) {
     246                _value = _valueDragStart;
     247                draw();
    237248                sendCommand(_cmd, _value);
    238249        }
    239250        _isDragging = false;
     
    249260        // Draw the box
    250261        gui->box(_x + _labelWidth, _y, _w - _labelWidth, _h, gui->_color, gui->_shadowcolor);
    251262
    252         // Draw the 'bar'
    253         gui->fillRect(_x + _labelWidth + 2, _y + 2, valueToPos(_value), _h - 4,
    254                                 !isEnabled() ? gui->_color :
    255                                 hilite ? gui->_textcolorhi : gui->_textcolor);
     263        OverlayColor color = !isEnabled() ? gui->_color : hilite ? gui->_textcolorhi : gui->_textcolor;
     264
     265        // Draw handle
     266        int handlePos = valueToPos(_value);
     267        gui->vLine(_x + handlePos + _labelWidth + 2, _y + 4, _y + _h - 6, color);
     268        gui->vLine(_x + handlePos + _labelWidth + 1, _y + 3, _y + _h - 5, color);
     269        gui->vLine(_x + handlePos + _labelWidth - 1, _y + 3, _y + _h - 5, color);
     270        gui->vLine(_x + handlePos + _labelWidth - 2, _y + 4, _y + _h - 6, color);
     271
     272        // Draw Tracks
     273        if (handlePos > 5) {
     274                gui->hLine(_x + _labelWidth + 3, _y + _h / 2 - 2, _x + _labelWidth + handlePos - 4, color);
     275                gui->hLine(_x + _labelWidth + 3, _y + _h / 2, _x + _labelWidth + handlePos - 4, color);
     276        }
     277 
     278        if ((uint)handlePos < _w - _labelWidth - 11) {
     279                gui->hLine(_x + _labelWidth + handlePos + 4, _y + _h / 2 - 2, _x + _w - 4, color);
     280                gui->hLine(_x + _labelWidth + handlePos + 4, _y + _h / 2, _x + _w - 4, color);
     281        }
    256282}
    257283
    258284int SliderWidget::valueToPos(int value) {
    259         return ((_w - _labelWidth - 4) * (value - _valueMin) / (_valueMax - _valueMin));
     285        return ((_w - _labelWidth - 11) * (value - _valueMin) / (_valueMax - _valueMin) + 5);
    260286}
    261287
    262288int SliderWidget::posToValue(int pos) {
    263         return (pos) * (_valueMax - _valueMin) / (_w - _labelWidth - 4) + _valueMin;
     289        // Align pos to middle of handle
     290        if (pos < 5)
     291                pos = 5;
     292        else if ((uint)pos > _w - _labelWidth - 5)
     293                pos = _w - _labelWidth - 5;
     294
     295        return int(((float)((pos - 5) * (_valueMax - _valueMin)) / (_w - _labelWidth - 11) + _valueMin) + 0.5);
    264296}
    265297
    266298} // End of namespace GUI
  • gui/widget.h

    RCS file: /cvsroot/scummvm/scummvm/gui/widget.h,v
    retrieving revision 1.36
    diff -u -r1.36 gui/widget.h
     
    185185/* SliderWidget */
    186186class SliderWidget : public ButtonWidget {
    187187protected:
    188         int             _value, _oldValue;
     188        int             _value, _oldValue, _valueDragStart;
    189189        int             _valueMin, _valueMax;
    190190        bool    _isDragging;
    191191        uint    _labelWidth;
     
    200200        int getMaxValue() const         { return _valueMax; }
    201201
    202202        void handleMouseMoved(int x, int y, int button);
     203        void handleMouseLeft(int button);
    203204        void handleMouseDown(int x, int y, int button, int clickCount);
    204205        void handleMouseUp(int x, int y, int button, int clickCount);