Pixel-Composer/scripts/slider/slider.gml

171 lines
5.0 KiB
Plaintext
Raw Normal View History

2023-10-08 04:14:35 +02:00
enum SLIDER_UPDATE {
realtime,
release,
none,
}
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;
2023-09-26 14:35:25 +02:00
stepSize = _step;
2022-01-13 05:24:03 +01:00
2023-10-08 04:14:35 +02:00
current_value = 0;
2023-11-25 08:54:35 +01:00
slide_speed = 1 / 10;
2023-10-08 04:14:35 +02:00
2023-12-22 14:46:54 +01:00
side_button = noone;
onModify = _onModify;
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
2023-10-08 04:14:35 +02:00
update_stat = SLIDER_UPDATE.realtime;
2023-12-05 09:51:24 +01:00
spr = THEME.slider;
blend = c_white;
dragging = noone;
drag_sv = 0;
2023-03-05 07:16:44 +01:00
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);
2024-01-08 08:10:50 +01:00
font = noone;
2023-02-14 05:32:32 +01:00
2023-11-25 08:54:35 +01:00
static modifyValue = function(value) { #region
value = clamp(value, curr_minn, curr_maxx);
onModify(value);
} #endregion
2023-12-05 09:51:24 +01:00
static setSlideSpeed = function(speed) { #region
tb_value.setSlidable(speed);
2023-12-05 09:51:24 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
static setInteract = function(interactable = noone) { #region
2024-01-08 08:10:50 +01:00
self.interactable = interactable;
2023-01-25 06:49:00 +01:00
tb_value.interactable = interactable;
2023-12-05 09:51:24 +01:00
} #endregion
2023-01-25 06:49:00 +01:00
2023-12-05 09:51:24 +01:00
static register = function(parent = noone) { #region
2023-01-17 08:11:55 +01:00
tb_value.register(parent);
2023-12-05 09:51:24 +01:00
} #endregion
2023-01-17 08:11:55 +01:00
2023-12-05 09:51:24 +01:00
static drawParam = function(params) { #region
2023-07-30 19:56:53 +02:00
return draw(params.x, params.y, params.w, params.h, params.data, params.m);
2023-12-05 09:51:24 +01:00
} #endregion
2023-07-30 19:56:53 +02:00
2023-12-05 09:51:24 +01:00
static draw = function(_x, _y, _w, _h, _data, _m, tb_w = 64, halign = fa_left, valign = fa_top) { #region
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2023-06-04 12:38:40 +02:00
if(!is_real(_data)) return;
2023-01-17 08:11:55 +01:00
2023-10-08 04:14:35 +02:00
if(!dragging) current_value = _data;
2023-12-22 14:46:54 +01:00
if(side_button) {
side_button.setFocusHover(active, hover);
side_button.draw(_x + _w - ui(32), _y + _h / 2 - ui(32 / 2), ui(32), ui(32), _m, THEME.button_hide);
_w -= ui(40);
}
2023-12-05 09:51:24 +01:00
switch(halign) { #region
2022-11-03 11:44:49 +01:00
case fa_left: _x = _x; break;
case fa_center: _x = _x - _w / 2; break;
case fa_right: _x = _x - _w; break;
2023-12-05 09:51:24 +01:00
} #endregion
2022-11-03 11:44:49 +01:00
2023-12-05 09:51:24 +01:00
switch(valign) { #region
2022-11-03 11:44:49 +01:00
case fa_top: _y = _y; break;
case fa_center: _y = _y - _h / 2; break;
case fa_bottom: _y = _y - _h; break;
2023-12-05 09:51:24 +01:00
} #endregion
2022-11-03 11:44:49 +01:00
2022-12-21 02:30:23 +01:00
var _rang = abs(maxx - minn);
if(!dragging) {
2023-10-08 04:14:35 +02:00
curr_minn = (current_value >= minn)? minn : minn - ceil(abs(current_value - minn) / _rang) * _rang;
curr_maxx = (current_value <= maxx)? maxx : maxx + ceil(abs(current_value - maxx) / _rang) * _rang;
2022-12-21 02:30:23 +01:00
}
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));
2024-01-08 08:10:50 +01:00
tb_value.font = font;
2023-06-21 20:36:53 +02:00
tb_value.setFocusHover(active, hover);
2023-10-08 04:14:35 +02:00
tb_value.draw(_x + sw + ui(16), _y, tb_w, _h, current_value, _m);
2023-11-25 08:54:35 +01:00
tb_value.setRange(curr_minn, curr_maxx);
2023-03-05 07:16:44 +01:00
}
2022-01-13 05:24:03 +01:00
2023-12-06 05:09:39 +01:00
draw_sprite_stretched_ext(spr, 0, _x - ui(4), _y + _h / 2 - ui(4), sw + ui(8), ui(8), blend, 1);
2023-12-05 09:51:24 +01:00
2023-12-10 14:55:05 +01:00
if(stepSize >= 1 && sw / ((curr_maxx - curr_minn) / stepSize) > ui(16)) {
2023-12-05 09:51:24 +01:00
for( var i = curr_minn; i <= curr_maxx; i += stepSize ) {
var _v = round(i / stepSize) * stepSize;
var _cx = _x + clamp((_v - curr_minn) / (curr_maxx - curr_minn), 0, 1) * sw;
draw_sprite_stretched_ext(spr, 4, _cx - ui(4), _y + _h / 2 - ui(4), ui(8), ui(8), COLORS.widget_slider_step, 1);
}
}
2022-01-13 05:24:03 +01:00
2023-10-08 04:14:35 +02:00
var _pg = clamp((current_value - curr_minn) / (curr_maxx - curr_minn), 0, 1) * sw;
2023-06-10 13:59:45 +02:00
var _kx = _x + _pg;
2024-01-08 08:10:50 +01:00
draw_sprite_stretched_ext(spr, 1, _kx - handle_w / 2, _y, handle_w, _h, blend, interactable * 0.75 + 0.25);
2022-01-13 05:24:03 +01:00
if(dragging) {
2023-12-06 05:09:39 +01:00
draw_sprite_stretched_ext(spr, 3, _kx - handle_w / 2, _y, handle_w, _h, COLORS._main_accent, 1);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
var val = (dragging.drag_sx - dragging.drag_msx) / dragging.drag_sw * (curr_maxx - curr_minn) + curr_minn;
2023-09-26 14:35:25 +02:00
val = round(val / stepSize) * stepSize;
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-10-08 04:14:35 +02:00
current_value = val;
if(update_stat == SLIDER_UPDATE.realtime && onModify != noone && onModify(val))
UNDO_HOLDING = true;
2022-01-13 05:24:03 +01:00
2024-01-16 11:00:39 +01:00
MOUSE_BLOCK = true;
if(mouse_check_button_pressed(mb_right)) {
onModify(drag_sv);
instance_destroy(dragging);
2024-01-16 11:00:39 +01:00
dragging = noone;
UNDO_HOLDING = false;
2024-01-16 11:00:39 +01:00
} else if(mouse_release(mb_left)) {
2023-10-08 04:14:35 +02:00
if(update_stat == SLIDER_UPDATE.release && onModify != noone)
onModify(val);
2023-12-05 09:51:24 +01:00
instance_destroy(dragging);
dragging = noone;
2023-10-08 04:14:35 +02: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))) {
2023-12-06 05:09:39 +01:00
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)) {
2023-12-05 09:51:24 +01:00
dragging = instance_create(0, 0, slider_Slider);
dragging.drag_sx = _m[0];
dragging.drag_msx = _x;
dragging.drag_sw = sw;
drag_sv = current_value;
2022-01-13 05:24:03 +01:00
}
2023-11-25 08:54:35 +01:00
var amo = slide_speed;
if(key_mod_press(CTRL)) amo *= 10;
if(key_mod_press(ALT)) amo /= 10;
2023-12-02 14:30:27 +01:00
if(key_mod_press(SHIFT) && mouse_wheel_down()) modifyValue(_data + amo * SCROLL_SPEED);
if(key_mod_press(SHIFT) && mouse_wheel_up()) modifyValue(_data - amo * SCROLL_SPEED);
2022-01-13 05:24:03 +01:00
}
}
2023-01-17 08:11:55 +01:00
resetFocus();
2023-07-30 19:56:53 +02:00
return h;
2023-12-05 09:51:24 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
}