Pixel-Composer/scripts/sliderRange/sliderRange.gml

118 lines
3.8 KiB
Plaintext
Raw Normal View History

2023-01-17 08:11:55 +01:00
function sliderRange(_min, _max, _step, _onModify) : widget() constructor {
2022-01-13 05:24:03 +01:00
minn = _min;
maxx = _max;
2023-09-26 14:35:25 +02:00
stepSize = _step;
2022-01-13 05:24:03 +01:00
2023-04-08 20:06:27 +02:00
spr = THEME.slider;
2022-01-13 05:24:03 +01:00
onModify = _onModify;
2023-12-05 09:51:24 +01:00
dragging = noone;
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
tb_value_min = new textBox(TEXTBOX_INPUT.number, function(val) { return onModify(0, clamp(val, minn, maxx)); });
tb_value_max = new textBox(TEXTBOX_INPUT.number, function(val) { return onModify(1, clamp(val, minn, maxx)); });
2022-12-27 04:00:50 +01:00
tb_value_min.slidable = true;
tb_value_max.slidable = true;
2023-02-14 05:32:32 +01:00
2023-12-05 09:51:24 +01:00
static setSlideSpeed = function(speed) { #region
tb_value_min.setSlidable(speed);
tb_value_max.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
2023-01-25 06:49:00 +01:00
self.interactable = interactable;
tb_value_min.interactable = interactable;
tb_value_max.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_min.register(parent);
tb_value_max.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) { #region
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2023-12-05 09:51:24 +01:00
if(!is_real(_data[0])) return h;
if(!is_real(_data[1])) return h;
2023-01-17 08:11:55 +01:00
2022-11-03 11:44:49 +01:00
var tb_w = ui(64);
var sw = _w - (tb_w + ui(16)) * 2;
2022-01-13 05:24:03 +01:00
2023-06-21 20:36:53 +02:00
tb_value_min.setFocusHover(active, hover);
2022-11-03 11:44:49 +01:00
tb_value_min.draw(_x, _y, tb_w, TEXTBOX_HEIGHT, _data[0], _m);
2022-01-13 05:24:03 +01:00
2023-06-21 20:36:53 +02:00
tb_value_max.setFocusHover(active, hover);
2022-11-03 11:44:49 +01:00
tb_value_max.draw(_x + _w - tb_w, _y, tb_w, TEXTBOX_HEIGHT, _data[1], _m);
2022-01-13 05:24:03 +01:00
2022-11-03 11:44:49 +01:00
var _x0 = _x + tb_w + ui(16);
2023-04-08 20:06:27 +02:00
draw_sprite_stretched(spr, 0, _x0, _y + _h / 2 - ui(4), sw, ui(8));
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(stepSize >= 1 && sw / ((maxx - minn) / stepSize) > ui(8)) {
for( var i = minn; i <= maxx; i += stepSize ) {
var _v = round(i / stepSize) * stepSize;
var _cx = _x + clamp((_v - minn) / (maxx - 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
var _slider_x0 = _x0 + clamp((_data[0] - minn) / (maxx - minn), 0, 1) * sw;
var _slider_x1 = _x0 + clamp((_data[1] - minn) / (maxx - minn), 0, 1) * sw;
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 4, min(_slider_x0, _slider_x1), _y + _h / 2 - ui(4), abs(_slider_x1 - _slider_x0), ui(8), COLORS._main_accent, 1);
draw_sprite_stretched(spr, 1, _slider_x0 - ui(10), _y, ui(20), _h);
draw_sprite_stretched(spr, 1, _slider_x1 - ui(10), _y, ui(20), _h);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(dragging) {
if(dragging_index == 0)
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 3, _slider_x0 - ui(10), _y, ui(20), _h, COLORS._main_accent, 1);
2023-12-05 09:51:24 +01:00
else if(dragging_index == 1)
2023-04-08 20:06:27 +02:00
draw_sprite_stretched_ext(spr, 3, _slider_x1 - ui(10), _y, ui(20), _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 * (maxx - minn) + minn;
2023-09-26 14:35:25 +02:00
val = round(val / stepSize) * stepSize;
2022-01-13 05:24:03 +01:00
val = clamp(val, minn, maxx);
2023-01-01 02:06:02 +01:00
if(key_mod_press(CTRL))
val = round(val);
2023-12-05 09:51:24 +01:00
if(onModify(dragging_index, val))
2023-02-14 05:32:32 +01:00
UNDO_HOLDING = true;
2022-01-13 05:24:03 +01:00
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left)) {
2023-12-05 09:51:24 +01:00
instance_destroy(dragging);
dragging = noone;
2022-11-22 14:25:39 +01:00
UNDO_HOLDING = false;
}
2023-01-01 02:06:02 +01:00
} else if(hover) {
var _hover = -1;
2023-12-05 09:51:24 +01:00
2023-01-01 02:06:02 +01:00
if(point_in_rectangle(_m[0], _m[1], _slider_x0 - ui(10), _y, _slider_x0 + ui(10), _y + _h)) {
2023-04-08 20:06:27 +02:00
draw_sprite_stretched(spr, 2, _slider_x0 - ui(10), _y, ui(20), _h);
2023-01-01 02:06:02 +01:00
_hover = 0;
}
2023-12-05 09:51:24 +01:00
2023-01-01 02:06:02 +01:00
if(point_in_rectangle(_m[0], _m[1], _slider_x1 - ui(10), _y, _slider_x1 + ui(10), _y + _h)) {
2023-04-08 20:06:27 +02:00
draw_sprite_stretched(spr, 2, _slider_x1 - ui(10), _y, ui(20), _h);
2023-01-01 02:06:02 +01:00
_hover = 1;
}
2023-12-05 09:51:24 +01:00
2023-01-01 02:06:02 +01:00
if(_hover > -1 && mouse_press(mb_left, active)) {
2023-12-05 09:51:24 +01:00
dragging = instance_create(0, 0, slider_Slider);
dragging_index = _hover;
dragging.drag_sx = _m[0];
dragging.drag_msx = _x0;
dragging.drag_sw = sw;
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
}