2018-01-21 21:07:07 +00:00
|
|
|
//============================================================================
|
|
|
|
//
|
|
|
|
// SSSS tt lll lll
|
|
|
|
// SS SS tt ll ll
|
|
|
|
// SS tttttt eeee ll ll aaaa
|
|
|
|
// SSSS tt ee ee ll ll aa
|
|
|
|
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
|
|
|
|
// SS SS tt ee ll ll aa aa
|
|
|
|
// SSSS ttt eeeee llll llll aaaaa
|
|
|
|
//
|
2021-01-05 22:12:43 +00:00
|
|
|
// Copyright (c) 1995-2021 by Bradford W. Mott, Stephen Anthony
|
2018-01-21 21:07:07 +00:00
|
|
|
// and the Stella Team
|
|
|
|
//
|
|
|
|
// See the file "License.txt" for information on usage and redistribution of
|
|
|
|
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include "bspf.hxx"
|
|
|
|
#include "Command.hxx"
|
|
|
|
#include "Dialog.hxx"
|
|
|
|
#include "Font.hxx"
|
|
|
|
#include "FBSurface.hxx"
|
|
|
|
#include "GuiObject.hxx"
|
|
|
|
#include "OSystem.hxx"
|
|
|
|
|
|
|
|
#include "TimeLineWidget.hxx"
|
|
|
|
|
2018-02-07 13:55:43 +00:00
|
|
|
const int HANDLE_W = 3;
|
|
|
|
const int HANDLE_H = 3; // size above/below the slider
|
|
|
|
|
2018-01-21 21:07:07 +00:00
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
TimeLineWidget::TimeLineWidget(GuiObject* boss, const GUI::Font& font,
|
|
|
|
int x, int y, int w, int h,
|
2018-02-04 14:45:42 +00:00
|
|
|
const string& label, uInt32 labelWidth, int cmd)
|
2018-01-21 21:07:07 +00:00
|
|
|
: ButtonWidget(boss, font, x, y, w, h, label, cmd),
|
2020-12-21 00:38:00 +00:00
|
|
|
_labelWidth{labelWidth}
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
2020-11-10 22:29:56 +00:00
|
|
|
_flags = Widget::FLAG_ENABLED | Widget::FLAG_TRACK_MOUSE
|
2020-11-11 07:56:11 +00:00
|
|
|
| Widget::FLAG_CLEARBG | Widget::FLAG_NOBG;
|
2020-11-10 22:29:56 +00:00
|
|
|
|
2018-01-21 21:07:07 +00:00
|
|
|
_bgcolor = kDlgColor;
|
|
|
|
_bgcolorhi = kDlgColor;
|
|
|
|
|
|
|
|
if(!_label.empty() && _labelWidth == 0)
|
|
|
|
_labelWidth = _font.getStringWidth(_label);
|
|
|
|
|
|
|
|
_w = w + _labelWidth;
|
2018-01-28 23:41:21 +00:00
|
|
|
|
2018-02-04 14:45:42 +00:00
|
|
|
_stepValue.reserve(100);
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2018-02-04 14:45:42 +00:00
|
|
|
void TimeLineWidget::setValue(uInt32 value)
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
2018-02-04 14:45:42 +00:00
|
|
|
value = BSPF::clamp(value, _valueMin, _valueMax);
|
2018-01-21 21:07:07 +00:00
|
|
|
|
|
|
|
if(value != _value)
|
|
|
|
{
|
|
|
|
_value = value;
|
|
|
|
setDirty();
|
|
|
|
sendCommand(_cmd, _value, _id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2018-02-04 14:45:42 +00:00
|
|
|
void TimeLineWidget::setMinValue(uInt32 value)
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
|
|
|
_valueMin = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2018-02-04 14:45:42 +00:00
|
|
|
void TimeLineWidget::setMaxValue(uInt32 value)
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
|
|
|
_valueMax = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2018-01-28 23:41:21 +00:00
|
|
|
void TimeLineWidget::setStepValues(const IntArray& steps)
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
2018-01-28 23:41:21 +00:00
|
|
|
_stepValue.clear();
|
|
|
|
|
2018-01-30 11:48:20 +00:00
|
|
|
// If no steps are defined, just use the maximum value
|
|
|
|
if(steps.size() > 0)
|
|
|
|
{
|
|
|
|
// Try to allocate as infrequently as possible
|
|
|
|
if(steps.size() > _stepValue.capacity())
|
|
|
|
_stepValue.reserve(2 * steps.size());
|
|
|
|
|
2020-11-10 22:29:56 +00:00
|
|
|
double scale = (_w - _labelWidth - 2 - HANDLE_W) / double(steps.back());
|
2018-01-28 23:41:21 +00:00
|
|
|
|
2018-01-30 11:48:20 +00:00
|
|
|
// Skip the very last value; we take care of it outside the end of the loop
|
|
|
|
for(uInt32 i = 0; i < steps.size() - 1; ++i)
|
|
|
|
_stepValue.push_back(int(steps[i] * scale));
|
2018-01-28 23:41:21 +00:00
|
|
|
|
2018-02-04 15:44:23 +00:00
|
|
|
// Due to integer <-> double conversion, the last value is sometimes
|
|
|
|
// slightly less than the maximum value; we assign it manually to fix this
|
2020-11-10 22:29:56 +00:00
|
|
|
_stepValue.push_back(_w - _labelWidth - 2 - HANDLE_W);
|
2018-02-04 15:44:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_stepValue.push_back(0);
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
void TimeLineWidget::handleMouseMoved(int x, int y)
|
|
|
|
{
|
|
|
|
if(isEnabled() && _isDragging && x >= int(_labelWidth))
|
|
|
|
setValue(posToValue(x - _labelWidth));
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
void TimeLineWidget::handleMouseDown(int x, int y, MouseButton b, int clickCount)
|
|
|
|
{
|
|
|
|
if(isEnabled() && b == MouseButton::LEFT)
|
|
|
|
{
|
|
|
|
_isDragging = true;
|
|
|
|
handleMouseMoved(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
void TimeLineWidget::handleMouseUp(int x, int y, MouseButton b, int clickCount)
|
|
|
|
{
|
|
|
|
if(isEnabled() && _isDragging)
|
|
|
|
sendCommand(_cmd, _value, _id);
|
|
|
|
|
|
|
|
_isDragging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
void TimeLineWidget::handleMouseWheel(int x, int y, int direction)
|
|
|
|
{
|
|
|
|
if(isEnabled())
|
|
|
|
{
|
2018-02-04 14:45:42 +00:00
|
|
|
if(direction < 0 && _value < _valueMax)
|
2018-02-01 22:33:17 +00:00
|
|
|
setValue(_value + 1);
|
2018-02-04 14:45:42 +00:00
|
|
|
else if(direction > 0 && _value > _valueMin)
|
2018-02-01 22:33:17 +00:00
|
|
|
setValue(_value - 1);
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
|
|
void TimeLineWidget::drawWidget(bool hilite)
|
|
|
|
{
|
|
|
|
FBSurface& s = _boss->dialog().surface();
|
|
|
|
|
|
|
|
// Draw the label, if any
|
|
|
|
if(_labelWidth > 0)
|
|
|
|
s.drawString(_font, _label, _x, _y + 2, _labelWidth,
|
2018-02-06 11:46:27 +00:00
|
|
|
isEnabled() ? kTextColor : kColor, TextAlign::Left);
|
|
|
|
|
2018-02-07 13:55:43 +00:00
|
|
|
// Frame the handle
|
|
|
|
const int HANDLE_W2 = (HANDLE_W + 1) / 2;
|
2020-11-10 22:29:56 +00:00
|
|
|
int p = valueToPos(_value),
|
|
|
|
x = _x + _labelWidth + HANDLE_W2,
|
|
|
|
w = _w - _labelWidth - HANDLE_W;
|
2018-02-07 13:55:43 +00:00
|
|
|
s.hLine(x + p - HANDLE_W2, _y + 0, x + p - HANDLE_W2 + HANDLE_W, kColorInfo);
|
|
|
|
s.vLine(x + p - HANDLE_W2, _y + 1, _y + _h - 2, kColorInfo);
|
|
|
|
s.hLine(x + p - HANDLE_W2 + 1, _y + _h - 1, x + p - HANDLE_W2 + 1 + HANDLE_W, kBGColor);
|
|
|
|
s.vLine(x + p - HANDLE_W2 + 1 + HANDLE_W, _y + 1, _y + _h - 2, kBGColor);
|
|
|
|
// Frame the box
|
|
|
|
s.hLine(x, _y + HANDLE_H, x + w - 2, kColorInfo);
|
|
|
|
s.vLine(x, _y + HANDLE_H, _y + _h - 2 - HANDLE_H, kColorInfo);
|
|
|
|
s.hLine(x + 1, _y + _h - 1 - HANDLE_H, x + w - 1, kBGColor);
|
|
|
|
s.vLine(x + w - 1, _y + 1 + HANDLE_H, _y + _h - 2 - HANDLE_H, kBGColor);
|
2018-01-21 21:07:07 +00:00
|
|
|
|
|
|
|
// Fill the box
|
2018-02-07 13:55:43 +00:00
|
|
|
s.fillRect(x + 1, _y + 1 + HANDLE_H, w - 2, _h - 2 - HANDLE_H * 2,
|
2018-02-06 11:46:27 +00:00
|
|
|
!isEnabled() ? kSliderBGColorLo : hilite ? kSliderBGColorHi : kSliderBGColor);
|
2018-01-21 21:07:07 +00:00
|
|
|
// Draw the 'bar'
|
2018-02-07 13:55:43 +00:00
|
|
|
s.fillRect(x + 1, _y + 1 + HANDLE_H, p, _h - 2 - HANDLE_H * 2,
|
2018-01-21 21:07:07 +00:00
|
|
|
!isEnabled() ? kColor : hilite ? kSliderColorHi : kSliderColor);
|
2018-02-02 12:05:41 +00:00
|
|
|
|
2018-02-07 13:55:43 +00:00
|
|
|
// Add 4 tickmarks for 5 intervals
|
2018-02-02 12:05:41 +00:00
|
|
|
int numTicks = std::min(5, int(_stepValue.size()));
|
|
|
|
for(int i = 1; i < numTicks; ++i)
|
|
|
|
{
|
2018-02-04 00:14:46 +00:00
|
|
|
int idx = int((_stepValue.size() * i + numTicks / 2) / numTicks);
|
2018-02-02 12:05:41 +00:00
|
|
|
if(idx > 1)
|
|
|
|
{
|
2018-02-06 11:46:27 +00:00
|
|
|
int xt = x + valueToPos(idx - 1);
|
2018-08-08 15:39:10 +00:00
|
|
|
ColorId color = kNone;
|
2018-02-06 11:46:27 +00:00
|
|
|
|
|
|
|
if(isEnabled())
|
|
|
|
{
|
|
|
|
if(xt > x + p)
|
|
|
|
color = hilite ? kSliderColorHi : kSliderColor;
|
|
|
|
else
|
|
|
|
color = hilite ? kSliderBGColorHi : kSliderBGColor;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(xt > x + p)
|
|
|
|
color = kColor;
|
|
|
|
else
|
|
|
|
color = kSliderBGColorLo;
|
|
|
|
}
|
2018-02-07 13:55:43 +00:00
|
|
|
s.vLine(xt, _y + _h / 2, _y + _h - 2 - HANDLE_H, color);
|
2018-02-02 12:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-07 13:55:43 +00:00
|
|
|
// Draw the handle
|
|
|
|
s.fillRect(x + p + 1 - HANDLE_W2, _y + 1, HANDLE_W, _h - 2,
|
|
|
|
!isEnabled() ? kColor : hilite ? kSliderColorHi : kSliderColor);
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2020-12-21 00:38:00 +00:00
|
|
|
uInt32 TimeLineWidget::valueToPos(uInt32 value) const
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
2018-02-04 14:45:42 +00:00
|
|
|
return _stepValue[BSPF::clamp(value, _valueMin, _valueMax)];
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
2020-12-21 00:38:00 +00:00
|
|
|
uInt32 TimeLineWidget::posToValue(uInt32 pos) const
|
2018-01-21 21:07:07 +00:00
|
|
|
{
|
2018-02-01 22:33:17 +00:00
|
|
|
// 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])
|
2018-02-01 23:22:32 +00:00
|
|
|
return (_stepValue[i+1] - pos) < (pos - _stepValue[i]) ? i+1 : i;
|
2018-01-21 21:07:07 +00:00
|
|
|
|
2018-02-01 22:33:17 +00:00
|
|
|
return _valueMax;
|
2018-01-21 21:07:07 +00:00
|
|
|
}
|