TimeLineWidget mouse handling is now complete.

This commit is contained in:
Stephen Anthony 2018-02-01 19:03:17 -03:30
parent 8813d5b9e7
commit c18bcbc39b
2 changed files with 11 additions and 58 deletions

View File

@ -25,16 +25,12 @@
#include "TimeLineWidget.hxx" #include "TimeLineWidget.hxx"
// TODO - remove all references to _stepValue__
// - fix posToValue to use _stepValue
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TimeLineWidget::TimeLineWidget(GuiObject* boss, const GUI::Font& font, TimeLineWidget::TimeLineWidget(GuiObject* boss, const GUI::Font& font,
int x, int y, int w, int h, int x, int y, int w, int h,
const string& label, int labelWidth, int cmd) const string& label, int labelWidth, int cmd)
: ButtonWidget(boss, font, x, y, w, h, label, cmd), : ButtonWidget(boss, font, x, y, w, h, label, cmd),
_value(0), _value(0),
_stepValue__(1),
_valueMin(0), _valueMin(0),
_valueMax(100), _valueMax(100),
_isDragging(false), _isDragging(false),
@ -49,7 +45,7 @@ TimeLineWidget::TimeLineWidget(GuiObject* boss, const GUI::Font& font,
_w = w + _labelWidth; _w = w + _labelWidth;
_stepValue.reserve(100); _stepValue.reserve(_valueMax);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -105,8 +101,6 @@ void TimeLineWidget::setStepValues(const IntArray& steps)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TimeLineWidget::handleMouseMoved(int x, int y) void TimeLineWidget::handleMouseMoved(int x, int y)
{ {
// TODO: when the mouse is dragged outside the widget, the slider should
// snap back to the old value.
if(isEnabled() && _isDragging && x >= int(_labelWidth)) if(isEnabled() && _isDragging && x >= int(_labelWidth))
setValue(posToValue(x - _labelWidth)); setValue(posToValue(x - _labelWidth));
} }
@ -136,46 +130,12 @@ void TimeLineWidget::handleMouseWheel(int x, int y, int direction)
if(isEnabled()) if(isEnabled())
{ {
if(direction < 0) if(direction < 0)
handleEvent(Event::UIUp); setValue(_value + 1);
else if(direction > 0) else if(direction > 0)
handleEvent(Event::UIDown); setValue(_value - 1);
} }
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool TimeLineWidget::handleEvent(Event::Type e)
{
if(!isEnabled())
return false;
switch(e)
{
case Event::UIDown:
case Event::UILeft:
case Event::UIPgDown:
setValue(_value - _stepValue__);
break;
case Event::UIUp:
case Event::UIRight:
case Event::UIPgUp:
setValue(_value + _stepValue__);
break;
case Event::UIHome:
setValue(_valueMin);
break;
case Event::UIEnd:
setValue(_valueMax);
break;
default:
return false;
}
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void TimeLineWidget::drawWidget(bool hilite) void TimeLineWidget::drawWidget(bool hilite)
{ {
@ -215,23 +175,17 @@ void TimeLineWidget::drawWidget(bool hilite)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int TimeLineWidget::valueToPos(int value) int TimeLineWidget::valueToPos(int value)
{ {
if(value < _valueMin) value = _valueMin; return _stepValue[BSPF::clamp(value, _valueMin, _valueMax)];
else if(value > _valueMax) value = _valueMax;
int real = _stepValue[BSPF::clamp(value, _valueMin, _valueMax)];
#if 0
int range = std::max(_valueMax - _valueMin, 1); // don't divide by zero
int actual = ((_w - _labelWidth - 4) * (value - _valueMin) / range);
cerr << "i=" << value << " real=" << real << endl << "actual=" << actual << endl << endl;
#endif
return real;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int TimeLineWidget::posToValue(int pos) int TimeLineWidget::posToValue(int pos)
{ {
int value = (pos) * (_valueMax - _valueMin) / (_w - _labelWidth - 4) + _valueMin; // Find the interval in which 'pos' falls, and then the endpoint which
// it is closest to
for(uInt32 i = 0; i < _stepValue.size() - 1; ++i)
if(pos >= _stepValue[i] && pos <= _stepValue[i+1])
return (_stepValue[i+1] - pos) < (_stepValue[i] - pos) ? i+1 : i;
// Scale the position to the correct interval (according to step value) return _valueMax;
return value - (value % _stepValue__);
} }

View File

@ -46,7 +46,6 @@ class TimeLineWidget : public ButtonWidget
void handleMouseDown(int x, int y, MouseButton b, int clickCount) override; void handleMouseDown(int x, int y, MouseButton b, int clickCount) override;
void handleMouseUp(int x, int y, MouseButton b, int clickCount) override; void handleMouseUp(int x, int y, MouseButton b, int clickCount) override;
void handleMouseWheel(int x, int y, int direction) override; void handleMouseWheel(int x, int y, int direction) override;
bool handleEvent(Event::Type event) override;
void drawWidget(bool hilite) override; void drawWidget(bool hilite) override;
@ -54,7 +53,7 @@ class TimeLineWidget : public ButtonWidget
int posToValue(int pos); int posToValue(int pos);
protected: protected:
int _value, _stepValue__; int _value;
int _valueMin, _valueMax; int _valueMin, _valueMax;
bool _isDragging; bool _isDragging;
int _labelWidth; int _labelWidth;