Pixel-Composer/scripts/buttonGradient/buttonGradient.gml

261 lines
7.4 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function buttonGradient(_onApply, dialog = noone) : widget() constructor {
2024-01-22 10:26:25 +01:00
onApply = _onApply;
2023-02-28 09:43:01 +01:00
parentDialog = dialog;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
current_gradient = noone;
2024-01-22 10:26:25 +01:00
side_button = noone;
2023-01-17 08:11:55 +01:00
2024-05-25 10:07:11 +02:00
expanded = false;
drag_color_index = -1;
edit_color_index = -1;
edit_color_mx = 0;
edit_color_sx = 0;
2024-05-27 07:12:24 +02:00
hover_index = 0;
function apply(value) { #region
2023-02-28 09:43:01 +01:00
if(!interactable) return;
onApply(value);
} #endregion
2023-02-28 09:43:01 +01:00
static trigger = function() { #region
2023-01-17 08:11:55 +01:00
var dialog = dialogCall(o_dialog_gradient, WIN_W / 2, WIN_H / 2);
2023-07-11 14:18:23 +02:00
dialog.setDefault(current_gradient.clone());
2024-05-25 10:07:11 +02:00
dialog.onApply = apply;
2023-02-28 09:43:01 +01:00
dialog.interactable = interactable;
2024-05-25 10:07:11 +02:00
dialog.drop_target = self;
2023-02-28 09:43:01 +01:00
if(parentDialog)
parentDialog.addChildren(dialog);
} #endregion
2023-01-17 08:11:55 +01:00
2024-05-25 10:07:11 +02:00
static triggerSingle = function(_index) { #region
edit_color_index = _index;
var dialog = dialogCall(o_dialog_color_selector, WIN_W / 2, WIN_H / 2);
dialog.setDefault(edit_color_index.value);
dialog.selector.onApply = editColor;
dialog.onApply = editColor;
dialog.interactable = interactable;
} #endregion
function editColor(col) { #region
if(edit_color_index == -1) return;
edit_color_index.value = col;
apply(current_gradient);
} #endregion
static drawParam = function(params) { return draw(params.x, params.y, params.w, params.h, params.data, params.m); }
2023-07-30 19:56:53 +02:00
static draw = function(_x, _y, _w, _h, _gradient, _m) { #region
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
2023-08-24 11:59:05 +02:00
2024-05-26 04:51:14 +02:00
right_click_block = true;
2024-03-28 14:18:02 +01:00
var _bs = min(_h, ui(32));
hovering = hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h);
if(_w - _bs > ui(100) && side_button && instanceof(side_button) == "buttonClass") {
2024-01-22 10:26:25 +01:00
side_button.setFocusHover(active, hover);
2024-03-28 14:18:02 +01:00
side_button.draw(_x + _w - _bs, _y + _h / 2 - _bs / 2, _bs, _bs, _m, THEME.button_hide);
_w -= _bs + ui(8);
2024-01-22 10:26:25 +01:00
}
2024-05-19 07:55:09 +02:00
var _gw = _w - ui(4);
var _gh = _h - ui(4);
2023-05-28 20:00:51 +02:00
2023-01-17 08:11:55 +01:00
current_gradient = _gradient;
2023-08-24 11:59:05 +02:00
if(is_array(_gradient)) {
if(array_length(_gradient) == 0) return 0;
2024-05-25 10:07:11 +02:00
h = ui(4) + array_length(_gradient) * _gh;
2023-08-24 11:59:05 +02:00
current_gradient = _gradient[0];
} else {
h = _h;
}
if(!is_instanceof(current_gradient, gradientObject))
return 0;
2024-05-25 10:07:11 +02:00
var _drawSingle = !is_array(_gradient) && is_instanceof(_gradient, gradientObject);
var _ggw = _drawSingle? _gw - ui(24) : _w;
var _ggx = _drawSingle? _x + ui(2) + ui(24) : _x;
var hoverRect = ihover && point_in_rectangle(_m[0], _m[1], _ggx, _y, _ggx + _ggw, _y + h);
if(_drawSingle && expanded)
h = _h + ui(22);
if(hoverRect) {
draw_sprite_stretched_ext(THEME.button_def, 1, _x, _y, _w, h, boxColor);
2024-05-25 10:07:11 +02:00
if(mouse_press(mb_left, iactive))
2023-01-17 08:11:55 +01:00
trigger();
2024-05-25 10:07:11 +02:00
2023-04-08 20:06:27 +02:00
if(mouse_click(mb_left, iactive)) {
draw_sprite_stretched_ext(THEME.button_def, 2, _x, _y, _w, h, boxColor);
2024-03-31 05:36:11 +02:00
draw_sprite_stretched_ext(THEME.button_def, 3, _x, _y, _w, h, COLORS._main_accent, 1);
2023-04-08 20:06:27 +02:00
}
2022-01-13 05:24:03 +01:00
} else {
draw_sprite_stretched_ext(THEME.button_def, 0, _x, _y, _w, h, boxColor);
2023-01-17 08:11:55 +01:00
if(mouse_press(mb_left)) deactivate();
2022-01-13 05:24:03 +01:00
}
2024-05-25 10:07:11 +02:00
if(_drawSingle) {
var _ggh = _gh;
var _ggy = _y + ui(2);
2023-08-24 11:59:05 +02:00
2024-05-25 10:07:11 +02:00
var _bbx = _x + ui(12);
var _bby = _y + _ggh / 2 + ui(2);
var _bba = 0.5;
var _bbc = COLORS._main_icon;
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + ui(24), _y + _ggh)) {
_bba = 1;
if(mouse_press(mb_left))
expanded = !expanded;
if(mouse_click(mb_left))
_bbc = COLORS._main_icon_light;
}
draw_sprite_ext(THEME.arrow, expanded? 3 : 0, _bbx, _bby + ui(expanded), 1, 1, 0, _bbc, _bba);
_gradient.draw(_ggx, _ggy, _ggw, _ggh);
2023-08-24 11:59:05 +02:00
2024-05-25 10:07:11 +02:00
if(expanded) {
var _cx = _x + ui(2);
var _cy = _y + _ggh + ui(4);
var _cw = _w - ui(4);
var _ch = h - _ggh - ui(4 + 2);
var _ks = ui(16);
2024-05-26 04:51:14 +02:00
var _hv = noone;
2024-05-27 07:12:24 +02:00
var _hi = noone;
var _hover_index = hover_index;
2024-05-25 10:07:11 +02:00
draw_sprite_stretched_ext(THEME.menu_button_mask, 0, _ggx, _cy, _ggw, _ch, CDEF.main_mdblack, 1);
for (var i = 0, n = array_length(_gradient.keys); i < n; i++) {
var _k = _gradient.keys[i];
var _kx = _ggx + _k.time * _ggw;
var _ky = _ggy + _ggh + _ks - ui(4);
if(drag_color_index == _k || edit_color_index == _k) {
draw_set_color(COLORS._main_accent);
} else {
draw_set_color(c_white);
draw_set_alpha(0.5);
}
draw_line_round(_kx, _ggy + _ggh - ui(8), _kx, _ky, 1);
draw_set_alpha(1);
_kx = clamp(_kx, _ggx + _ks / 2, _ggx + _ggw - _ks / 2);
draw_sprite_stretched_ext(THEME.menu_button_mask, 0, _kx - _ks / 2, _ky - _ks / 2, _ks, _ks, _k.value, 1);
var _ka = 0.3;
if(hover && point_in_rectangle(_m[0], _m[1], _kx - _ks / 2, _ky - _ks / 2, _kx + _ks / 2, _ky + _ks / 2)) {
_ka = 1;
2024-05-26 04:51:14 +02:00
_hv = _k;
2024-05-27 07:12:24 +02:00
_hi = i;
2024-05-25 10:07:11 +02:00
if(mouse_press(mb_left, active)) {
drag_color_index = _k;
edit_color_mx = _m[0];
edit_color_sx = _k.time;
}
if(DOUBLE_CLICK)
triggerSingle(_k);
}
2024-05-27 07:12:24 +02:00
if(_hover_index == i && DRAGGING && DRAGGING.type == "Color") {
draw_sprite_stretched_ext(THEME.menu_button_mask, 1, _kx - _ks / 2, _ky - _ks / 2, _ks, _ks, COLORS._main_value_positive, 1);
if(mouse_release(mb_left)) {
_k.value = DRAGGING.data;
apply(current_gradient);
}
2024-05-25 10:07:11 +02:00
} else {
2024-05-27 07:12:24 +02:00
if(drag_color_index == _k || edit_color_index == _k)
draw_sprite_stretched_ext(THEME.menu_button_mask, 1, _kx - _ks / 2, _ky - _ks / 2, _ks, _ks, COLORS._main_accent, 1);
else
draw_sprite_stretched_add(THEME.menu_button_mask, 1, _kx - _ks / 2, _ky - _ks / 2, _ks, _ks, c_white, _ka);
2024-05-25 10:07:11 +02:00
}
}
2024-05-26 04:51:14 +02:00
if(_hv != noone) {
right_click_block = false;
if(array_length(_gradient.keys) > 1 && mouse_press(mb_right, active)) {
array_remove(_gradient.keys, _hv);
_gradient.refresh();
apply(_gradient);
}
} else if(point_in_rectangle(_m[0], _m[1], _ggx, _cy, _ggx + _ggw, _cy + _ch)) {
if(mouse_press(mb_left, active)) {
var _ti = clamp((_m[0] - _ggx) / _ggw, 0, 1);
var _va = _gradient.eval(_ti);
var _nk = new gradientKey(_ti, _va);
_gradient.add(_nk);
drag_color_index = _nk;
edit_color_mx = _m[0];
edit_color_sx = _ti;
}
}
2024-05-27 07:12:24 +02:00
hover_index = _hi;
2024-05-25 10:07:11 +02:00
}
if(drag_color_index != -1) {
var _val = edit_color_sx + (_m[0] - edit_color_mx) / _ggw;
_val = clamp(_val, 0, 1);
if(drag_color_index.time != _val) {
drag_color_index.time = _val;
_gradient.refresh();
}
if(mouse_release(mb_left)) {
drag_color_index = -1;
_gradient.refresh();
apply(_gradient);
}
}
} else {
for( var i = 0, n = array_length(_gradient); i < n; i++ ) {
var _grad = _gradient[i];
var _gx = _x + ui(2);
var _gy = _y + ui(2) + i * _gh;
if(is_instanceof(_grad, gradientObject))
_grad.draw(_gx, _gy, _gw, _gh);
}
2023-08-24 11:59:05 +02:00
}
2022-01-13 05:24:03 +01:00
2024-05-25 10:07:11 +02:00
if(WIDGET_CURRENT == self || (instance_exists(o_dialog_gradient) && o_dialog_gradient.drop_target == self))
draw_sprite_stretched_ext(THEME.widget_selecting, 0, _x, _y, _w, h, COLORS._main_accent, 1);
2023-01-17 08:11:55 +01:00
2023-03-26 07:13:36 +02:00
if(DRAGGING && DRAGGING.type == "Gradient" && hover && hoverRect) {
draw_sprite_stretched_ext(THEME.ui_panel, 1, _x, _y, _w, h, COLORS._main_value_positive, 1);
2023-03-26 07:13:36 +02:00
if(mouse_release(mb_left))
onApply(DRAGGING.data);
}
2022-01-13 05:24:03 +01:00
2023-03-26 07:13:36 +02:00
resetFocus();
2023-07-30 19:56:53 +02:00
return h;
} #endregion
2024-03-31 11:10:14 +02:00
static clone = function() { #region
var cln = new buttonGradient(onApply, parentDialog);
return cln;
} #endregion
2022-01-13 05:24:03 +01:00
}