Pixel-Composer/scripts/buttonPalette/buttonPalette.gml

308 lines
8.3 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function buttonPalette(_onApply, dialog = noone) : widget() constructor {
onApply = _onApply;
2023-02-28 09:43:01 +01:00
parentDialog = dialog;
2023-10-01 06:17:39 +02:00
current_palette = [];
side_button = noone;
2023-01-17 08:11:55 +01:00
2024-05-25 10:07:11 +02:00
expanded = false;
edit_color_index = -1;
2024-10-29 07:01:22 +01:00
function apply(value) {
2023-02-28 09:43:01 +01:00
if(!interactable) return;
onApply(value);
2024-10-29 07:01:22 +01:00
}
2023-02-28 09:43:01 +01:00
2024-10-29 07:01:22 +01:00
static trigger = function() {
2023-01-17 08:11:55 +01:00
var dialog = dialogCall(o_dialog_palette, WIN_W / 2, WIN_H / 2);
2023-02-14 02:51:14 +01:00
dialog.setDefault(current_palette);
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);
2024-10-29 07:01:22 +01:00
}
2022-01-13 05:24:03 +01:00
2024-10-29 07:01:22 +01:00
static triggerSingle = function(_index) {
2024-05-25 10:07:11 +02:00
edit_color_index = _index;
current_palette = array_clone(current_palette);
var dialog = dialogCall(o_dialog_color_selector, WIN_W / 2, WIN_H / 2);
dialog.setDefault(current_palette[edit_color_index]);
dialog.selector.onApply = editColor;
dialog.onApply = editColor;
dialog.interactable = interactable;
2024-10-29 07:01:22 +01:00
}
2024-05-25 10:07:11 +02:00
2024-10-29 07:01:22 +01:00
function editColor(col) {
2024-05-25 10:07:11 +02:00
if(edit_color_index == -1) return;
current_palette[edit_color_index] = col;
apply(current_palette);
2024-10-29 07:01:22 +01:00
}
2024-05-25 10:07:11 +02:00
static drawParam = function(params) { return draw(params.x, params.y, params.w, params.h, params.data, params.m); }
2023-07-21 12:40:20 +02:00
2024-10-29 07:01:22 +01:00
static draw = function(_x, _y, _w, _h, _color, _m) {
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2023-08-24 11:59:05 +02:00
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") {
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-05-19 07:55:09 +02:00
var _pw = _w - ui(4);
var _ph = _h - ui(4);
2023-08-24 11:59:05 +02:00
2023-01-17 08:11:55 +01:00
current_palette = _color;
2023-08-24 11:59:05 +02:00
if(array_length(_color) > 0 && is_array(_color[0])) {
if(array_length(_color[0]) == 0) return 0;
2024-05-25 10:07:11 +02:00
h = ui(4) + array_length(_color) * _ph;
2023-08-24 11:59:05 +02:00
current_palette = _color[0];
} else {
h = _h;
}
2023-10-01 06:17:39 +02:00
if(!is_array(current_palette) || array_empty(current_palette) || is_array(current_palette[0]))
2023-08-24 11:59:05 +02:00
return 0;
2024-05-25 10:07:11 +02:00
var _colr_h = ui(16);
var _drawSingle = !is_array(_color[0]);
var _ppw = _drawSingle? _pw - ui(24) : _w;
var _ppx = _drawSingle? _x + ui(2) + ui(24) : _x;
var hoverRect = ihover && point_in_rectangle(_m[0], _m[1], _ppx, _y, _ppx + _ppw, _y + h);
if(_drawSingle && expanded)
h = _h + array_length(_color) * _colr_h + ui(2);
if(hoverRect) {
draw_sprite_stretched_ext(THEME.button_def, 1, _x, _y, _w, h, boxColor);
2023-07-21 12:40:20 +02:00
if(mouse_press(mb_left, iactive))
2023-01-17 08:11:55 +01:00
trigger();
2023-07-21 12:40:20 +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 _pph = _ph;
var _ppy = _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 + _pph / 2 + ui(2);
2023-08-24 11:59:05 +02:00
2024-05-25 10:07:11 +02:00
var _bba = 0.5;
var _bbc = COLORS._main_icon;
if(hover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + ui(24), _y + _pph)) {
_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);
if(expanded) {
var _cx = _x + ui(2);
var _cy = _y + _pph + ui(4);
var _cw = _w - ui(4);
var _ch = h - _pph - ui(4 + 2);
2024-05-26 04:51:14 +02:00
var _pd = ui(2);
var _pd2 = _pd / 2;
2024-05-25 10:07:11 +02:00
draw_sprite_stretched_ext(THEME.s_box_r2, 0, _cx, _cy, _cw, _ch, CDEF.main_mdblack, 1);
2024-05-25 10:07:11 +02:00
for (var i = 0, n = array_length(_color); i < n; i++) {
var _c = _color[i];
var _ccx = _cx;
var _ccy = _cy + i * _colr_h;
var _ccw = _cw;
var _cch = _colr_h;
2024-05-26 04:51:14 +02:00
draw_sprite_stretched_ext(THEME.palette_mask, 1, _ccx + _pd2, _ccy + _pd2, _ccw - _pd, _cch - _pd, _c, 1);
2024-05-25 10:07:11 +02:00
if(hover && point_in_rectangle(_m[0], _m[1], _ccx, _ccy, _ccx + _ccw, _ccy + _cch - 1)) {
2024-05-27 07:12:24 +02:00
if(DRAGGING && DRAGGING.type == "Color") {
draw_sprite_stretched_ext(THEME.s_box_r2, 1, _ccx + _pd2, _ccy + _pd2, _ccw - _pd, _cch - _pd, COLORS._main_value_positive, 1);
2024-05-27 07:12:24 +02:00
if(mouse_release(mb_left)) {
current_palette[i] = DRAGGING.data;
apply(current_palette);
}
} else {
draw_sprite_stretched_add(THEME.s_box_r2, 1, _ccx + _pd2, _ccy + _pd2, _ccw - _pd, _cch - _pd, c_white, .3);
2024-05-27 07:12:24 +02:00
if(mouse_press(mb_left, active))
triggerSingle(i);
}
2024-05-25 10:07:11 +02:00
}
}
}
drawPalette(_color, _ppx, _ppy, _ppw, _pph);
} else {
expanded = false;
for( var i = 0, n = array_length(_color); i < n; i++ ) {
var _pal = _color[i];
var _px = _x + ui(2);
var _py = _y + ui(2) + i * _ph;
if(is_array(_pal)) drawPalette(_pal, _px, _py, _pw, _ph);
}
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_palette) && o_dialog_palette.drop_target == self))
draw_sprite_stretched_ext(THEME.widget_selecting, 0, _x, _y, _w, h, COLORS._main_accent, 1);
2022-01-13 05:24:03 +01:00
2023-03-25 12:27:04 +01:00
if(DRAGGING && DRAGGING.type == "Palette" && hover && hoverRect) {
draw_sprite_stretched_ext(THEME.ui_panel, 1, _x, _y, _w, h, COLORS._main_value_positive, 1);
2023-03-25 12:27:04 +01:00
if(mouse_release(mb_left))
onApply(DRAGGING.data);
}
resetFocus();
2023-07-30 19:56:53 +02:00
2023-08-24 11:59:05 +02:00
return h;
2024-10-29 07:01:22 +01:00
}
2024-03-31 11:10:14 +02:00
2024-10-29 07:01:22 +01:00
static clone = function() {
2024-03-31 11:10:14 +02:00
var cln = new buttonPalette(onApply, parentDialog);
return cln;
2024-10-29 07:01:22 +01:00
}
2022-01-13 05:24:03 +01:00
}
2024-10-29 07:01:22 +01:00
function drawPalette(_pal, _x, _y, _w, _h, _a = 1) {
var am = array_length(_pal);
2023-12-29 14:30:54 +01:00
2024-10-29 07:01:22 +01:00
if(am == 1) {
2023-12-29 14:30:54 +01:00
draw_sprite_stretched_ext(THEME.palette_mask, 1, _x, _y, _w, _h, _pal[0], _a);
return;
}
2024-10-29 07:01:22 +01:00
var aa = min(am, 64);
var st = am / aa;
2023-12-29 14:30:54 +01:00
var ww = _w / aa;
var _x0 = _x;
2024-01-09 03:39:40 +01:00
var _in;
2023-12-29 14:30:54 +01:00
2024-10-29 07:01:22 +01:00
for(var i = 0; i < am; i += st) {
var _p = _pal[floor(i)];
if(!is_numeric(_p)) continue;
2023-12-29 14:30:54 +01:00
2024-01-09 03:39:40 +01:00
if(i == 0) _in = 2;
else if(i == aa - 1) _in = 3;
else _in = 0;
2023-12-29 14:30:54 +01:00
2024-10-29 07:01:22 +01:00
var _ca = _color_get_alpha(_p);
2023-12-29 14:30:54 +01:00
2024-01-09 03:39:40 +01:00
if(_ca == 1) {
2024-10-29 07:01:22 +01:00
draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h, _p, _a);
2024-01-09 03:39:40 +01:00
} else {
2024-10-29 07:01:22 +01:00
draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h - ui(8), _p, _a);
2024-01-09 03:39:40 +01:00
draw_sprite_stretched_ext(THEME.palette_mask, 1, floor(_x0), _y + _h - ui(6), ceil(ww), ui(6), c_black, _a);
draw_sprite_stretched_ext(THEME.palette_mask, 1, floor(_x0), _y + _h - ui(6), ceil(ww) * _ca, ui(6), c_white, _a);
}
2023-12-29 14:30:54 +01:00
_x0 += ww;
2022-01-13 05:24:03 +01:00
}
2024-10-29 07:01:22 +01:00
}
2022-01-13 05:24:03 +01:00
2024-10-29 07:01:22 +01:00
function drawPaletteGrid(_pal, _x, _y, _w, _gs = 24, params = {}) {
2024-05-29 04:41:08 +02:00
var c_color = struct_try_get(params, "color", -1);
2024-07-30 13:02:53 +02:00
var _stretch = struct_try_get(params, "stretch", true);
2024-05-29 04:41:08 +02:00
var _mx = struct_try_get(params, "mx", -1);
var _my = struct_try_get(params, "my", -1);
2022-01-13 05:24:03 +01:00
var amo = array_length(_pal);
var col = floor(_w / _gs);
var row = ceil(amo / col);
2024-05-19 15:11:45 +02:00
var cx = -1, cy = -1;
var _h = row * _gs;
2022-01-13 05:24:03 +01:00
2024-05-29 04:41:08 +02:00
var _gw = _stretch? _w / (min(col, amo)) : _gs;
var _hov = noone;
var _hcc = noone;
2024-05-31 09:18:09 +02:00
var hvx = 0;
var hvy = 0;
var hvw = 0;
var hvh = 0;
2024-05-27 07:12:24 +02:00
for(var i = 0; i < amo; i++) {
var _cc = safe_mod(i, col);
var _rr = floor(i / col);
2024-05-29 04:41:08 +02:00
var _x0 = _x + _cc * _gw;
2024-05-27 07:12:24 +02:00
var _y0 = _y + _rr * _gs;
var _i = 0;
if(amo == 1) {
_i = 1;
} else {
if(row == 1) {
if(i == 0) _i = 2;
else if(i == amo - 1) _i = 3;
} else {
if(i == 0) _i = 6;
2024-05-31 09:18:09 +02:00
else if(_cc == col - 1 && i + col >= amo) {
2024-05-27 07:12:24 +02:00
if(_rr == 0) _i = 3;
else _i = 9;
}
else if(_rr == 0 && _cc == col - 1) _i = 7;
else if(_rr == row - 1 && _cc == 0) _i = 8;
else if(i == amo - 1) _i = 9;
}
}
2024-04-16 10:34:05 +02:00
2024-05-31 09:18:09 +02:00
draw_sprite_stretched_ext(THEME.palette_mask, _i, _x0, _y0, _cc == col - 1? _gw : ceil(_gw), _gs, _pal[i], 1);
2024-05-29 04:41:08 +02:00
if(point_in_rectangle(_mx, _my, _x0, _y0, _x0 + _gw, _y0 + _gs)) {
_hov = i;
_hcc = _pal[i];
2024-05-31 09:18:09 +02:00
hvx = _x0;
hvy = _y0;
hvw = _gw;
hvh = _gs;
2024-05-29 04:41:08 +02:00
}
2024-04-16 10:34:05 +02:00
2024-05-27 09:08:23 +02:00
var _same = (c_color & 0x00FFFFFF) == (_pal[i] & 0x00FFFFFF);
if(c_color >= 0 && _same) {
2024-04-16 10:34:05 +02:00
cx = _x0;
cy = _y0;
}
2022-01-13 05:24:03 +01:00
}
2024-05-27 07:12:24 +02:00
if(cx != -1) {
var _pd = ui(5);
2024-05-31 09:18:09 +02:00
draw_sprite_stretched_ext(THEME.palette_selecting, 0, cx - _pd, cy - _pd, _gw + _pd * 2, _gs + _pd * 2);
2024-05-27 07:12:24 +02:00
}
2023-07-25 20:12:40 +02:00
2024-05-29 04:41:08 +02:00
return {
height: _h,
hoverIndex: _hov,
hoverColor: _hcc,
2024-05-31 09:18:09 +02:00
hoverBBOX: [ hvx, hvy, hvw, hvh ],
2024-05-29 04:41:08 +02:00
};
2024-10-29 07:01:22 +01:00
}