Pixel-Composer/scripts/buttonPalette/buttonPalette.gml

83 lines
2.1 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
function buttonPalette(_onApply) {
return new buttonPaletteClass(_onApply);
}
2023-01-17 08:11:55 +01:00
function buttonPaletteClass(_onApply) : widget() constructor {
2022-01-13 05:24:03 +01:00
onApply = _onApply;
2023-01-17 08:11:55 +01:00
current_palette = noone;
static trigger = function() {
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-01-17 08:11:55 +01:00
dialog.onApply = onApply;
}
2022-01-13 05:24:03 +01:00
2022-01-24 02:21:25 +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;
current_palette = _color;
2022-01-13 05:24:03 +01:00
var click = false;
2023-02-14 02:51:14 +01:00
if(ihover && point_in_rectangle(_m[0], _m[1], _x, _y, _x + _w, _y + _h)) {
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.button, 1, _x, _y, _w, _h);
2023-02-14 02:51:14 +01:00
if(mouse_press(mb_left, iactive)) {
2023-01-17 08:11:55 +01:00
trigger();
2022-01-13 05:24:03 +01:00
click = true;
}
2023-02-14 02:51:14 +01:00
if(mouse_click(mb_left, iactive))
2022-11-18 03:20:31 +01:00
draw_sprite_stretched(THEME.button, 2, _x, _y, _w, _h);
2022-01-13 05:24:03 +01:00
} else {
2022-11-18 03:20:31 +01: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
}
2022-11-03 11:44:49 +01:00
drawPalette(_color, _x + ui(6), _y + ui(6), _w - ui(12), _h - ui(12));
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
resetFocus();
if(WIDGET_CURRENT == self)
draw_sprite_stretched(THEME.widget_selecting, 0, _x - ui(3), _y - ui(3), _w + ui(6), _h + ui(6));
2022-01-13 05:24:03 +01:00
return click;
}
}
2023-02-14 02:51:14 +01:00
function drawPalette(_pal, _x, _y, _w, _h) {
2022-01-13 05:24:03 +01:00
var ww = _w / array_length(_pal);
for(var i = 0; i < array_length(_pal); i++) {
2023-02-14 02:51:14 +01:00
if(!is_real(_pal[i])) continue;
2022-01-13 05:24:03 +01:00
draw_set_color(_pal[i]);
var _x0 = _x + i * ww;
var _x1 = _x0 + ww;
draw_rectangle(_x0, _y, _x1, _y + _h, false);
}
}
function drawPaletteGrid(_pal, _x, _y, _w, _gs = 24, c_color = -1) {
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);
}
if(c_color > -1) {
for(var i = 0; i < array_length(_pal); i++) {
if(c_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_set_color(c_white);
draw_rectangle_border(_x0, _y0 + 1, _x0 + _gs, _y0 + _gs, 2);
}
}
}
}