Pixel-Composer/scripts/rotatorRange/rotatorRange.gml

160 lines
4.7 KiB
Text
Raw Normal View History

2023-01-17 08:11:55 +01:00
function rotatorRange(_onModify) : widget() constructor {
2022-01-13 05:24:03 +01:00
onModify = _onModify;
2023-12-05 09:51:24 +01:00
dragging_index = -1;
dragging = noone;
2022-01-13 05:24:03 +01:00
drag_sv = 0;
drag_dat = [ 0, 0 ];
2022-01-13 05:24:03 +01:00
tb_min = new textBox(TEXTBOX_INPUT.number, function(val) { return onModify(0, val); } ).setSlidable();
2023-01-17 08:11:55 +01:00
tb_max = new textBox(TEXTBOX_INPUT.number, function(val) { return onModify(1, val); } ).setSlidable();
2023-01-17 08:11:55 +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_min.interactable = interactable;
tb_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-25 06:49:00 +01:00
tb_min.register(parent);
tb_max.register(parent);
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 drawParam = function(params) { #region
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) { #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
2023-07-30 19:56:53 +02:00
_x += _w / 2;
2023-06-04 12:38:40 +02:00
if(!is_real(_data[0])) return;
if(!is_real(_data[1])) return;
2023-08-24 11:59:05 +02:00
2023-06-23 15:39:24 +02:00
var knob_y = _y + h / 2;
var _r = ui(28);
2022-01-13 05:24:03 +01:00
2023-06-21 20:36:53 +02:00
tb_min.setFocusHover(active, hover);
tb_max.setFocusHover(active, hover);
2023-01-17 08:11:55 +01:00
tb_min.draw(_x - ui(40 + 16 + 80), knob_y - TEXTBOX_HEIGHT / 2, ui(80), TEXTBOX_HEIGHT, array_safe_get(_data, 0), _m);
tb_max.draw(_x + ui(40 + 16), knob_y - TEXTBOX_HEIGHT / 2, ui(80), TEXTBOX_HEIGHT, array_safe_get(_data, 1), _m);
2023-06-23 15:39:24 +02:00
var px, py;
for(var i = 0; i < 2; i++) {
px[i] = _x + lengthdir_x(_r, _data[i]);
py[i] = knob_y + lengthdir_y(_r, _data[i]);
}
draw_sprite(THEME.rotator_bg, 0, round(_x), round(knob_y));
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[0]) - 1, knob_y + lengthdir_y(ui(20), _data[0]) - 1);
draw_line(_x, knob_y, _x + lengthdir_x(ui(20), _data[1]) - 1, knob_y + lengthdir_y(ui(20), _data[1]) - 1);
2022-01-13 05:24:03 +01:00
#region draw arc
var hover_arc = false;
var diss = point_distance(_m[0], _m[1], _x, knob_y);
2023-12-05 09:51:24 +01:00
if(abs(diss - _r) < 6 || dragging_index == 2)
2022-01-13 05:24:03 +01:00
hover_arc = true;
2023-12-05 09:51:24 +01:00
2023-06-23 15:39:24 +02:00
for(var i = 0; i < 2; i++) {
if(point_in_circle(_m[0], _m[1], px[i], py[i], ui(20)))
hover_arc = false;
2022-01-13 05:24:03 +01:00
}
2023-12-05 09:51:24 +01:00
2023-06-23 15:39:24 +02:00
draw_set_color(hover_arc? COLORS.widget_rotator_range_hover : COLORS.widget_rotator_range);
2023-12-05 09:51:24 +01:00
draw_arc_forward(_x, knob_y, _r, 3, _data[0], _data[1]);
2022-01-13 05:24:03 +01:00
#endregion
2023-06-23 15:39:24 +02:00
for(var i = 0; i < 2; i++)
draw_sprite(THEME.rotator_knob, 0, px[i], py[i]);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(dragging_index > -1) { #region
2022-01-13 05:24:03 +01:00
var val = point_direction(_x, knob_y, _m[0], _m[1]);
2022-12-22 03:09:55 +01:00
if(key_mod_press(CTRL)) val = round(val / 15) * 15;
2022-01-13 05:24:03 +01:00
var val, real_val;
2023-12-05 09:51:24 +01:00
if(dragging_index == 2) {
2023-02-14 05:32:32 +01:00
var modi = false;
2023-12-05 09:51:24 +01:00
real_val[0] = round(dragging.delta_acc + drag_sv[0]);
real_val[1] = round(dragging.delta_acc + drag_sv[1]);
val = key_mod_press(CTRL)? round(real_val[0] / 15) * 15 : real_val[0];
2023-02-14 05:32:32 +01:00
modi |= onModify(0, val);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
val = key_mod_press(CTRL)? round(real_val[1] / 15) * 15 : real_val[1];
2023-02-14 05:32:32 +01:00
modi |= onModify(1, val);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(modi) UNDO_HOLDING = true;
2022-01-13 05:24:03 +01:00
} else {
2023-12-05 09:51:24 +01:00
var _o = _data[dragging_index];
real_val = round(dragging.delta_acc + drag_sv);
2022-12-22 03:09:55 +01:00
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
draw_sprite(THEME.rotator_knob, 1, px[dragging_index], py[dragging_index]);
2022-01-13 05:24:03 +01:00
2023-12-05 09:51:24 +01:00
if(_data[dragging_index] != val) {
2023-02-14 05:32:32 +01:00
var modi = false;
2023-12-05 09:51:24 +01:00
modi |= onModify(dragging_index, val);
2022-11-21 06:38:44 +01:00
2023-02-14 05:32:32 +01:00
if(key_mod_press(ALT)) {
2022-11-21 06:38:44 +01:00
var dt = val - _o;
2023-12-05 09:51:24 +01:00
modi |= onModify(!dragging_index, _data[!dragging_index] - dt);
2022-11-21 06:38:44 +01:00
}
2023-12-05 09:51:24 +01:00
if(modi) 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)) {
for( var i = 0; i < 2; i++ ) onModify(i, drag_dat[i]);
instance_destroy(rotator_Rotator);
dragging = noone;
dragging_index = -1;
UNDO_HOLDING = false;
2024-01-16 11:00:39 +01:00
} else if(mouse_release(mb_left)) {
instance_destroy(rotator_Rotator);
dragging = noone;
2023-12-05 09:51:24 +01:00
dragging_index = -1;
UNDO_HOLDING = false;
2022-01-13 05:24:03 +01:00
}
2023-12-05 09:51:24 +01:00
#endregion
} else if(hover) { #region
2022-01-13 05:24:03 +01:00
for(var i = 0; i < 2; i++) {
2023-06-23 15:39:24 +02:00
if(point_in_circle(_m[0], _m[1], px[i], py[i], ui(20))) {
draw_sprite(THEME.rotator_knob, 1, px[i], py[i]);
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_index = i;
2022-01-13 05:24:03 +01:00
drag_sv = _data[i];
drag_dat = [ _data[0], _data[1] ];
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
}
}
}
2023-12-05 09:51:24 +01:00
if(dragging_index == -1 && hover_arc && mouse_press(mb_left, active)) {
dragging_index = 2;
drag_sv = [ _data[0], _data[1] ];
drag_dat = [ _data[0], _data[1] ];
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
}
2023-12-05 09:51:24 +01:00
} #endregion
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
}