2022-11-01 03:06:03 +01:00
|
|
|
function slider(_min, _max, _step, _onModify = noone, _onRelease = noone) constructor {
|
2022-01-13 05:24:03 +01:00
|
|
|
active = false;
|
|
|
|
hover = false;
|
|
|
|
|
|
|
|
minn = _min;
|
|
|
|
maxx = _max;
|
|
|
|
step = _step;
|
|
|
|
|
|
|
|
onModify = _onModify;
|
2022-11-01 03:06:03 +01:00
|
|
|
onRelease = _onRelease;
|
|
|
|
onApply = function(val) {
|
2022-11-03 11:44:49 +01:00
|
|
|
if(onModify) onModify(val);
|
|
|
|
if(onRelease) onRelease();
|
2022-11-01 03:06:03 +01:00
|
|
|
}
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
dragging = false;
|
|
|
|
drag_mx = 0;
|
|
|
|
drag_sx = 0;
|
|
|
|
|
2022-11-01 03:06:03 +01:00
|
|
|
tb_value = new textBox(TEXTBOX_INPUT.float, onApply);
|
2022-01-13 05:24:03 +01:00
|
|
|
|
2022-11-03 11:44:49 +01:00
|
|
|
static draw = function(_x, _y, _w, _h, _data, _m, tb_w = 64, halign = fa_left, valign = fa_top) {
|
|
|
|
switch(halign) {
|
|
|
|
case fa_left: _x = _x; break;
|
|
|
|
case fa_center: _x = _x - _w / 2; break;
|
|
|
|
case fa_right: _x = _x - _w; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(valign) {
|
|
|
|
case fa_top: _y = _y; break;
|
|
|
|
case fa_center: _y = _y - _h / 2; break;
|
|
|
|
case fa_bottom: _y = _y - _h; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sw = _w - (tb_w + ui(16));
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
tb_value.hover = hover;
|
|
|
|
tb_value.active = active;
|
2022-11-03 11:44:49 +01:00
|
|
|
tb_value.draw(_x + sw + ui(16), _y, tb_w, _h, _data, _m);
|
2022-01-13 05:24:03 +01:00
|
|
|
|
2022-11-18 03:20:31 +01:00
|
|
|
draw_sprite_stretched(THEME.slider, 0, _x, _y + _h / 2 - ui(4), sw, ui(8));
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
var _kx = _x + clamp((_data - minn) / (maxx - minn), 0, 1) * sw;
|
2022-11-18 03:20:31 +01:00
|
|
|
draw_sprite_stretched(THEME.slider, 1, _kx - ui(10), _y, ui(20), _h);
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
if(dragging) {
|
2022-11-18 03:20:31 +01:00
|
|
|
draw_sprite_stretched(THEME.slider, 3, _kx - ui(10), _y, ui(20), _h);
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
var val = (_m[0] - _x) / sw * (maxx - minn) + minn;
|
|
|
|
val = round(val / step) * step;
|
|
|
|
val = clamp(val, minn, maxx);
|
2022-11-01 03:06:03 +01:00
|
|
|
if(onModify != noone)
|
|
|
|
onModify(val);
|
2022-11-22 14:25:39 +01:00
|
|
|
UNDO_HOLDING = true;
|
2022-01-13 05:24:03 +01:00
|
|
|
|
2022-11-01 03:06:03 +01:00
|
|
|
if(mouse_check_button_released(mb_left)) {
|
2022-01-13 05:24:03 +01:00
|
|
|
dragging = false;
|
2022-11-01 03:06:03 +01:00
|
|
|
if(onRelease != noone)
|
|
|
|
onRelease(val);
|
2022-11-22 14:25:39 +01:00
|
|
|
UNDO_HOLDING = false;
|
2022-11-01 03:06:03 +01:00
|
|
|
}
|
2022-01-13 05:24:03 +01:00
|
|
|
} else {
|
2022-11-03 11:44:49 +01:00
|
|
|
if(hover && (point_in_rectangle(_m[0], _m[1], _x, _y, _x + sw, _y + _h) || point_in_rectangle(_m[0], _m[1], _kx - ui(10), _y, _kx + ui(10), _y + _h))) {
|
2022-11-18 03:20:31 +01:00
|
|
|
draw_sprite_stretched(THEME.slider, 2, _kx - ui(10), _y, ui(20), _h);
|
2022-01-13 05:24:03 +01:00
|
|
|
|
|
|
|
if(active && mouse_check_button_pressed(mb_left)) {
|
|
|
|
dragging = true;
|
|
|
|
drag_mx = _m[0];
|
|
|
|
drag_sx = _data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hover = false;
|
|
|
|
active = false;
|
|
|
|
}
|
|
|
|
}
|