2022-09-26 07:58:24 +00:00
|
|
|
# Slider.nas : show a user-draggable slider
|
|
|
|
# with optional tick marks and value display
|
|
|
|
# SPDX-FileCopyrightText: (C) 2022 James Turner <james@flightgear.org>
|
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2022-06-30 10:40:33 +00:00
|
|
|
gui.widgets.Slider = {
|
|
|
|
new: func(parent, style, cfg)
|
|
|
|
{
|
|
|
|
var cfg = Config.new(cfg);
|
|
|
|
var m = gui.Widget.new(gui.widgets.Slider);
|
|
|
|
m._focus_policy = m.StrongFocus;
|
|
|
|
m._down = 0;
|
|
|
|
m._minValue = 0;
|
2022-09-26 07:58:24 +00:00
|
|
|
m._maxValue = cfg.get("max-value", 100);
|
2022-06-30 10:40:33 +00:00
|
|
|
m._value = 50;
|
2022-09-26 07:58:24 +00:00
|
|
|
m._pageStep = cfg.get("page-step", 0);
|
|
|
|
m._numTicks = cfg.get("tick-count", 0);
|
|
|
|
|
|
|
|
m._tickStyle = cfg.get("ticks-style", 0);
|
|
|
|
m._valueDisplayStyle = cfg.get("value-style", 0);
|
2022-06-30 10:40:33 +00:00
|
|
|
|
2022-09-26 07:58:24 +00:00
|
|
|
# TODO : select where value is shown
|
|
|
|
# TODO : select where tick marks are shown
|
2022-06-30 10:40:33 +00:00
|
|
|
|
2023-01-10 13:30:39 +00:00
|
|
|
m._setView( style.createWidget(parent, cfg.get("type", "slider"), cfg) );
|
|
|
|
m._view.updateRanges(m._minValue, m._maxValue, m._numTicks);
|
2022-06-30 10:40:33 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
},
|
|
|
|
|
|
|
|
setValue: func(val)
|
|
|
|
{
|
2023-01-10 13:30:39 +00:00
|
|
|
me._value = val;
|
2022-06-30 10:40:33 +00:00
|
|
|
if( me._view != nil ) {
|
2023-01-10 13:30:39 +00:00
|
|
|
me._view.setNormValue(me, me._normValue());
|
2022-06-30 10:40:33 +00:00
|
|
|
}
|
|
|
|
return me;
|
|
|
|
},
|
|
|
|
|
2023-01-10 13:30:39 +00:00
|
|
|
setDown: func(down = 1)
|
|
|
|
{
|
|
|
|
if (me._down == down )
|
|
|
|
return me;
|
|
|
|
|
|
|
|
me._down = down;
|
|
|
|
me._onStateChange();
|
|
|
|
return me;
|
|
|
|
},
|
2022-06-30 10:40:33 +00:00
|
|
|
|
|
|
|
# protected:
|
|
|
|
_setView: func(view)
|
|
|
|
{
|
|
|
|
call(gui.Widget._setView, [view], me);
|
|
|
|
|
2023-01-10 13:30:39 +00:00
|
|
|
var el = view._root;
|
|
|
|
el.addEventListener("mousedown", func if( me._enabled ) me.setDown(1));
|
|
|
|
el.addEventListener("mouseup", func if( me._enabled ) me.setDown(0));
|
2022-06-30 10:40:33 +00:00
|
|
|
# el.addEventListener("click", func if( me._enabled ) me.toggle());
|
2023-01-10 13:30:39 +00:00
|
|
|
el.addEventListener("mouseleave",func me.setDown(0));
|
|
|
|
|
|
|
|
view._thumb.addEventListener("drag", func(e) {
|
|
|
|
me._dragThumb(e);
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
},
|
2022-06-30 10:40:33 +00:00
|
|
|
|
2023-01-10 13:30:39 +00:00
|
|
|
_dragThumb: func(event)
|
|
|
|
{
|
|
|
|
var vr = me._view._root;
|
|
|
|
var viewPosX = vr.canvasToLocal([event.clientX, event.clientY])[0];
|
|
|
|
var width = me._size[0];
|
|
|
|
|
|
|
|
if (viewPosX < 0) {
|
|
|
|
me.setValue(me._minValue);
|
|
|
|
} elsif (viewPosX > width) {
|
|
|
|
me.setValue(me._maxValue);
|
|
|
|
} else {
|
|
|
|
var norm = viewPosX / width;
|
|
|
|
me.setValue(norm * ( me._maxValue - me._minValue));
|
|
|
|
}
|
2022-06-30 10:40:33 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
# return value as its normalised equivalent
|
|
|
|
_normValue: func
|
|
|
|
{
|
|
|
|
var range = me._maxValue - me._minValue;
|
|
|
|
var v = math.clamp(me._value, me._minValue, me._maxValue) - me._minValue;
|
|
|
|
return v / range;
|
|
|
|
}
|
|
|
|
};
|