Pixel-Composer/scripts/buttonPalette/buttonPalette.gml

153 lines
4.2 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
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_palette, WIN_W / 2, WIN_H / 2);
2023-02-14 02:51:14 +01:00
dialog.setDefault(current_palette);
2023-02-28 09:43:01 +01:00
dialog.onApply = apply;
dialog.interactable = interactable;
if(parentDialog)
parentDialog.addChildren(dialog);
} #endregion
2022-01-13 05:24:03 +01: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
static draw = function(_x, _y, _w, _h, _color, _m) { #region
2023-01-17 08:11:55 +01:00
x = _x;
y = _y;
w = _w;
h = _h;
2023-08-24 11:59:05 +02:00
if(side_button && instanceof(side_button) == "buttonClass") {
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);
}
2023-12-29 14:30:54 +01:00
var _pw = _w - ui(8);
var _ph = _h - ui(8);
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;
h = ui(12) + array_length(_color) * _ph;
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;
var hoverRect = point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + h);
2023-03-25 12:27:04 +01:00
if(ihover && hoverRect) {
2023-08-24 11:59:05 +02:00
draw_sprite_stretched(THEME.button, 1, _x, _y, _w, h);
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)) {
2023-08-24 11:59:05 +02:00
draw_sprite_stretched(THEME.button, 2, _x, _y, _w, h);
draw_sprite_stretched_ext(THEME.button, 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 {
2023-08-24 11:59:05 +02:00
draw_sprite_stretched(THEME.button, 0, _x, _y, _w, h);
2023-01-17 08:11:55 +01:00
if(mouse_press(mb_left)) deactivate();
2022-01-13 05:24:03 +01:00
}
2023-08-24 11:59:05 +02:00
if(!is_array(_color[0])) _color = [ _color ];
for( var i = 0, n = array_length(_color); i < n; i++ ) {
var _pal = _color[i];
2023-12-29 14:30:54 +01:00
var _px = _x + ui(4);
var _py = _y + ui(4) + i * _ph;
2023-08-24 11:59:05 +02:00
if(is_array(_pal))
drawPalette(_pal, _px, _py, _pw, _ph);
}
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
if(WIDGET_CURRENT == self)
2023-08-24 11:59:05 +02:00
draw_sprite_stretched_ext(THEME.widget_selecting, 0, _x - ui(3), _y - ui(3), _w + ui(6), h + ui(6), 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) {
2023-08-24 11:59:05 +02:00
draw_sprite_stretched_ext(THEME.ui_panel_active, 0, _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;
} #endregion
2022-01-13 05:24:03 +01:00
}
2023-12-29 14:30:54 +01:00
function drawPalette(_pal, _x, _y, _w, _h, _a = 1) { #region
var aa = array_length(_pal);
if(aa == 1) {
draw_sprite_stretched_ext(THEME.palette_mask, 1, _x, _y, _w, _h, _pal[0], _a);
return;
}
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
for(var i = 0; i < aa; i++) {
2024-01-09 03:39:40 +01:00
if(!is_numeric(_pal[i])) 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-01-09 03:39:40 +01:00
var _ca = _color_get_alpha(_pal[i]);
2023-12-29 14:30:54 +01:00
2024-01-09 03:39:40 +01:00
if(_ca == 1) {
draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h, _pal[i], _a);
} else {
draw_sprite_stretched_ext(THEME.palette_mask, _in, floor(_x0), _y, ceil(ww), _h - ui(8), _pal[i], _a);
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
}
2023-12-29 14:30:54 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-12-29 14:30:54 +01:00
function drawPaletteGrid(_pal, _x, _y, _w, _gs = 24, c_color = -1) { #region
2022-01-13 05:24:03 +01:00
var amo = array_length(_pal);
var col = floor(_w / _gs);
var row = ceil(amo / col);
for(var i = 0; i < array_length(_pal); i++) {
draw_set_color(_pal[i]);
2023-02-20 10:16:31 +01:00
var _x0 = _x + safe_mod(i, col) * _gs;
2022-01-13 05:24:03 +01:00
var _y0 = _y + floor(i / col) * _gs;
draw_rectangle(_x0, _y0 + 1, _x0 + _gs, _y0 + _gs, false);
}
2023-07-25 20:12:40 +02:00
if(c_color == -1) return;
for(var i = 0; i < array_length(_pal); i++) {
if(c_color != _pal[i]) continue;
var _x0 = _x + safe_mod(i, col) * _gs;
var _y0 = _y + floor(i / col) * _gs;
2022-01-13 05:24:03 +01:00
2023-07-25 20:12:40 +02:00
draw_set_color(c_white);
draw_rectangle_border(_x0, _y0 + 1, _x0 + _gs, _y0 + _gs, 2);
2022-01-13 05:24:03 +01:00
}
2023-12-29 14:30:54 +01:00
} #endregion