Pixel-Composer/scripts/slider/slider.gml

111 lines
2.8 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function slider(_min, _max, _step, _onModify = noone, _onRelease = noone) : widget() constructor {
2022-12-21 02:30:23 +01:00
minn = _min; curr_minn = _min;
maxx = _max; curr_maxx = _max;
2022-01-13 05:24:03 +01:00
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;
2023-03-05 07:16:44 +01:00
spr = THEME.slider;
blend = c_white;
handle_w = ui(20);
2022-12-10 05:06:01 +01:00
2023-02-14 05:32:32 +01:00
tb_value = new textBox(TEXTBOX_INPUT.number, onApply);
static setSlideSpeed = function(speed) {
tb_value.slide_speed = speed;
}
2022-01-13 05:24:03 +01:00
2023-01-25 06:49:00 +01:00
static setInteract = function(interactable = noone) {
self.interactable = interactable;
tb_value.interactable = interactable;
}
2023-01-17 08:11:55 +01:00
static register = function(parent = noone) {
tb_value.register(parent);
}
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) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2022-11-03 11:44:49 +01:00
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;
}
2022-12-21 02:30:23 +01:00
var _rang = abs(maxx - minn);
if(!dragging) {
curr_minn = (_data >= minn)? minn : minn - ceil(abs(_data - minn) / _rang) * _rang;
curr_maxx = (_data <= maxx)? maxx : maxx + ceil(abs(_data - maxx) / _rang) * _rang;
}
2023-03-05 07:16:44 +01:00
var sw = _w;
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
if(tb_w > 0) {
sw = _w - (tb_w + ui(16));
tb_value.hover = hover;
tb_value.active = active;
tb_value.draw(_x + sw + ui(16), _y, tb_w, _h, _data, _m);
}
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
draw_sprite_stretched_ext(spr, 0, _x, _y + _h / 2 - ui(4), sw, ui(8), blend, 1);
2022-01-13 05:24:03 +01:00
2022-12-21 02:30:23 +01:00
var _kx = _x + clamp((_data - curr_minn) / (curr_maxx - curr_minn), 0, 1) * sw;
2023-03-05 07:16:44 +01:00
draw_sprite_stretched_ext(spr, 1, _kx - handle_w / 2, _y, handle_w, _h, blend, 1);
2022-01-13 05:24:03 +01:00
if(dragging) {
2023-03-05 07:16:44 +01:00
draw_sprite_stretched_ext(spr, 3, _kx - handle_w / 2, _y, handle_w, _h, blend, 1);
2022-01-13 05:24:03 +01:00
2022-12-21 02:30:23 +01:00
var val = (_m[0] - _x) / sw * (curr_maxx - curr_minn) + curr_minn;
2022-01-13 05:24:03 +01:00
val = round(val / step) * step;
2022-12-21 02:30:23 +01:00
val = clamp(val, curr_minn, curr_maxx);
2023-01-01 02:06:02 +01:00
if(key_mod_press(CTRL))
val = round(val);
2023-02-14 05:32:32 +01:00
if(onModify != noone) {
if(onModify(val))
UNDO_HOLDING = true;
}
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(mouse_release(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 {
2023-03-05 07:16:44 +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 - handle_w / 2, _y, _kx + handle_w / 2, _y + _h))) {
draw_sprite_stretched_ext(spr, 2, _kx - handle_w / 2, _y, handle_w, _h, blend, 1);
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, active)) {
2022-01-13 05:24:03 +01:00
dragging = true;
drag_mx = _m[0];
drag_sx = _data;
}
}
}
2023-01-17 08:11:55 +01:00
resetFocus();
2022-01-13 05:24:03 +01:00
}
}