Pixel-Composer/scripts/rotator/rotator.gml

133 lines
3.4 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;
2024-03-26 04:03:45 +01:00
valStep = _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;
2024-03-27 11:51:14 +01:00
tb_value = new textBox(TEXTBOX_INPUT.number, onModify).setSlidable([ 0.1, 15 ], true);
tb_value.hide = true;
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
2024-03-28 14:18:02 +01:00
static isHovering = function() { return dragging || tb_value.hovering; }
2023-12-05 09:51:24 +01:00
static drawParam = function(params) { #region
2024-03-27 11:51:14 +01:00
setParam(params);
tb_value.setParam(params);
2024-03-26 04:03:45 +01: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
2024-03-26 04:03:45 +01:00
static draw = function(_x, _y, _w, _h, _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;
2024-03-26 04:03:45 +01:00
h = _h;
2023-01-17 08:11:55 +01:00
2024-03-26 04:03:45 +01:00
if(!is_real(_data)) return;
2024-03-29 05:20:49 +01:00
var _r = _h;
var _bs = min(_h, ui(32));
var _drawRot = _w - _r > ui(64);
2024-03-29 05:20:49 +01:00
if(_drawRot && side_button) {
2023-12-23 09:39:55 +01:00
side_button.setFocusHover(active, hover);
2024-03-26 04:03:45 +01:00
side_button.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m, THEME.button_hide);
_w -= _bs + ui(4);
}
2023-07-30 19:56:53 +02:00
2024-03-29 05:20:49 +01:00
var _tx = _drawRot? _x + _r + ui(4) : _x;
var _tw = _drawRot? _w - _r - ui(4) : _w;
2022-01-13 05:24:03 +01:00
2024-03-26 04:03:45 +01:00
draw_sprite_stretched_ext(THEME.textbox, 3, _tx, _y, _tw, _h, c_white, 1);
draw_sprite_stretched_ext(THEME.textbox, 0, _tx, _y, _tw, _h, c_white, 0.5 + 0.5 * interactable);
2022-01-13 05:24:03 +01:00
2024-03-26 04:03:45 +01:00
tb_value.setFocusHover(active, hover);
tb_value.draw(_tx, _y, _tw, _h, _data, _m);
2023-06-23 15:39:24 +02:00
2024-03-29 05:20:49 +01:00
if(_drawRot) {
var _kx = _x + _r / 2;
var _ky = _y + _r / 2;
var _kr = (_r - ui(12)) / 2;
var _kc = COLORS._main_icon;
2022-01-13 05:24:03 +01:00
2024-03-29 05:20:49 +01:00
if(dragging) {
_kc = COLORS._main_icon_light;
2024-03-26 04:03:45 +01:00
2024-06-09 07:49:43 +02:00
var real_val = dragging.delta_acc + drag_sv;
var val = key_mod_press(CTRL)? value_snap(real_val, 15) : real_val;
2022-01-13 05:24:03 +01:00
2024-06-09 07:49:43 +02:00
if(valStep != -1) val = value_snap(real_val, valStep);
2022-01-13 05:24:03 +01:00
2024-03-29 05:20:49 +01:00
if(onModify(val))
UNDO_HOLDING = true;
2022-01-13 05:24:03 +01:00
2024-03-29 05:20:49 +01:00
MOUSE_BLOCK = true;
2024-01-16 11:00:39 +01:00
2024-03-29 05:20:49 +01:00
if(mouse_check_button_pressed(mb_right)) {
onModify(drag_sv);
instance_destroy(dragging);
dragging = noone;
UNDO_HOLDING = false;
2024-03-26 04:03:45 +01:00
2024-03-29 05:20:49 +01:00
} else if(mouse_release(mb_left)) {
instance_destroy(dragging);
dragging = noone;
UNDO_HOLDING = false;
}
2022-01-13 05:24:03 +01:00
2024-03-29 05:20:49 +01:00
} else if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _r, _y + _r)) {
_kc = COLORS._main_icon_light;
2024-03-26 04:03:45 +01:00
2024-03-29 05:20:49 +01:00
if(mouse_press(mb_left, active)) {
dragging = instance_create(0, 0, rotator_Rotator).init(_m, _kx, _ky);
drag_sv = _data;
}
2023-11-25 08:54:35 +01:00
2024-03-29 05:20:49 +01:00
if(key_mod_press(SHIFT)) {
var amo = 1;
if(key_mod_press(CTRL)) amo *= 10;
if(key_mod_press(ALT)) amo /= 10;
2024-03-26 04:03:45 +01:00
2024-03-29 05:20:49 +01:00
if(mouse_wheel_down()) onModify(_data + amo * SCROLL_SPEED);
if(mouse_wheel_up()) onModify(_data - amo * SCROLL_SPEED);
}
2023-12-05 04:28:49 +01:00
}
2022-01-13 05:24:03 +01:00
2024-03-29 05:20:49 +01:00
shader_set(sh_widget_rotator);
shader_set_color("color", _kc);
shader_set_f("side", _r);
shader_set_f("angle", degtorad(_data));
2024-03-26 04:03:45 +01:00
2024-03-29 05:20:49 +01:00
draw_sprite_stretched(s_fx_pixel, 0, _x, _y, _r, _r);
shader_reset();
}
2024-03-26 04:03:45 +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
2024-03-31 11:10:14 +02:00
static clone = function() { #region
var cln = new rotator(onModify, valStep);
return cln;
} #endregion
2022-01-13 05:24:03 +01:00
}