Pixel-Composer/scripts/rotator/rotator.gml

116 lines
2.9 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function rotator(_onModify, _step = -1) : widget() constructor {
2022-01-13 05:24:03 +01:00
onModify = _onModify;
2022-12-16 09:18:09 +01:00
step = _step;
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
scale = 1;
2023-12-05 09:51:24 +01:00
dragging = noone;
2022-01-13 05:24:03 +01:00
drag_sv = 0;
real_val = 0;
2023-11-25 08:54:35 +01:00
slide_speed = 1 / 10;
2023-12-23 09:39:55 +01:00
side_button = noone;
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
spr_bg = THEME.rotator_bg;
spr_knob = THEME.rotator_knob;
tb_value = new textBox(TEXTBOX_INPUT.number, onModify).setSlidable();
2022-01-13 05:24:03 +01:00
halign = fa_center;
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.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
halign = params.halign;
2023-07-30 19:56:53 +02:00
return draw(params.x, params.y, params.w, 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, _data, _m, draw_tb = true) { #region
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
2023-07-30 19:56:53 +02:00
w = _w;
2023-06-23 15:39:24 +02:00
h = ui(64);
2023-01-17 08:11:55 +01:00
var _r = ui(28);
2023-12-23 09:39:55 +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);
}
switch(halign) {
case fa_left : _x += _r; break;
case fa_center : _x += _w / 2; break;
}
2023-07-30 19:56:53 +02:00
2023-06-04 12:38:40 +02:00
if(!is_real(_data)) return;
2023-06-23 15:39:24 +02:00
var knob_y = _y + h / 2;
2022-01-13 05:24:03 +01:00
2023-03-05 07:16:44 +01:00
if(draw_tb) {
2023-06-21 20:36:53 +02:00
tb_value.setFocusHover(active, hover);
2023-03-05 07:16:44 +01:00
tb_value.draw(_x + ui(64), knob_y - ui(17), ui(64), TEXTBOX_HEIGHT, _data, _m);
}
2022-01-13 05:24:03 +01:00
draw_sprite(spr_bg, 0, round(_x), round(knob_y));
2022-01-13 05:24:03 +01:00
2023-06-23 15:39:24 +02:00
draw_set_color(COLORS.widget_rotator_guide);
draw_line(_x, knob_y, _x + lengthdir_x(ui(20), _data) - 1, knob_y + lengthdir_y(ui(20), _data) - 1);
var px = _x + lengthdir_x(_r, _data);
var py = knob_y + lengthdir_y(_r, _data);
2022-01-13 05:24:03 +01:00
if(dragging) {
2023-12-05 09:51:24 +01:00
var real_val = round(dragging.delta_acc + drag_sv);
var val = key_mod_press(CTRL)? round(real_val / 15) * 15 : real_val;
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(step != -1) val = round(real_val / step) * step;
2022-12-16 09:18:09 +01:00
2023-06-23 15:39:24 +02:00
draw_sprite(spr_knob, 1, px, py);
2022-01-13 05:24:03 +01:00
if(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;
} else if(mouse_release(mb_left)) {
2023-12-05 09:51:24 +01:00
instance_destroy(dragging);
2024-01-16 11:00:39 +01:00
dragging = noone;
2022-12-16 09:18:09 +01:00
UNDO_HOLDING = false;
2022-01-13 05:24:03 +01:00
}
2023-06-23 15:39:24 +02:00
} else if(hover && point_in_circle(_m[0], _m[1], _x, knob_y, _r + ui(16))) {
draw_sprite(spr_knob, 1, px, py);
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, rotator_Rotator).init(_m, _x, knob_y);
2022-01-13 05:24:03 +01:00
drag_sv = _data;
}
2023-11-25 08:54:35 +01:00
var amo = 1;
if(key_mod_press(CTRL)) amo *= 10;
if(key_mod_press(ALT)) amo /= 10;
2023-12-05 04:28:49 +01:00
if(key_mod_press(SHIFT)) {
if(mouse_wheel_down()) onModify(_data + amo * SCROLL_SPEED);
if(mouse_wheel_up()) onModify(_data - amo * SCROLL_SPEED);
}
2022-01-13 05:24:03 +01:00
} else {
2023-06-23 15:39:24 +02:00
draw_sprite(spr_knob, 0, px, py);
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
}