Pixel-Composer/objects/o_dialog_palette/Draw_64.gml

411 lines
13 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
/// @description init
if !ready exit;
if palette == 0 exit;
#region dropper
2023-03-02 07:59:14 +01:00
selector.interactable = interactable;
2022-11-18 03:20:31 +01:00
if(selector.dropper_active) {
selector.drawDropper(self);
2022-01-13 05:24:03 +01:00
exit;
}
#endregion
#region base UI
var presets_x = dialog_x;
2022-11-03 11:44:49 +01:00
var presets_w = ui(240);
2022-01-13 05:24:03 +01:00
2022-11-03 11:44:49 +01:00
var content_x = dialog_x + presets_w + ui(16);
var content_w = dialog_w - presets_w - ui(16);
2022-01-13 05:24:03 +01:00
2023-11-13 15:42:14 +01:00
var p = DIALOG_PAD;
var p2 = DIALOG_PAD * 2;
2022-01-13 05:24:03 +01:00
draw_sprite_stretched(THEME.dialog, 0, presets_x - p, dialog_y - p, presets_w + p2, dialog_h + p2);
if(sFOCUS) draw_sprite_stretched_ext(THEME.dialog, 1, presets_x - p, dialog_y - p, presets_w + p2, dialog_h + p2, COLORS._main_accent, 1);
2023-11-13 15:42:14 +01:00
draw_sprite_stretched(THEME.dialog, 0, content_x - p, dialog_y - p, content_w + p2, dialog_h + p2);
if(sFOCUS) draw_sprite_stretched_ext(THEME.dialog, 1, content_x - p, dialog_y - p, content_w + p2, dialog_h + p2, COLORS._main_accent, 1);
2022-01-13 05:24:03 +01:00
2023-06-10 13:59:45 +02:00
draw_set_text(f_p0, fa_left, fa_top, COLORS._main_text);
2023-06-05 18:27:53 +02:00
draw_text(presets_x + ui(24), dialog_y + ui(16), __txt("Presets"));
2023-03-02 07:59:14 +01:00
draw_text(content_x + (!interactable * ui(32)) + ui(24), dialog_y + ui(16), name);
if(!interactable)
draw_sprite_ui(THEME.lock, 0, content_x + ui(24 + 12), dialog_y + ui(16 + 12),,,, COLORS._main_icon);
2022-01-13 05:24:03 +01:00
#endregion
#region presets
2024-07-30 13:02:53 +02:00
draw_sprite_stretched(THEME.ui_panel_bg, 1, presets_x + pal_padding, dialog_y + ui(48), ui(240) - pal_padding * 2, dialog_h - ui(48) - pal_padding);
2022-01-13 05:24:03 +01:00
2023-06-21 20:36:53 +02:00
sp_presets.setFocusHover(sFOCUS, sHOVER);
2024-07-30 13:02:53 +02:00
sp_presets.draw(presets_x + pal_padding + ui(4), dialog_y + ui(48) + ui(4));
2022-01-13 05:24:03 +01:00
2022-11-03 11:44:49 +01:00
var bx = presets_x + presets_w - ui(44);
2023-02-23 07:02:19 +01:00
var by = dialog_y + ui(12);
2023-12-29 14:30:54 +01:00
var bs = ui(28);
2022-01-13 05:24:03 +01:00
2023-12-29 14:30:54 +01:00
if(buttonInstant(THEME.button_hide, bx, by, bs, bs, mouse_ui, sFOCUS, sHOVER, __txtx("add_preset", "Add to preset"), THEME.add_20) == 2) {
2022-11-03 11:44:49 +01:00
var dia = dialogCall(o_dialog_file_name, mouse_mx + ui(8), mouse_my + ui(8));
2022-01-13 05:24:03 +01:00
dia.onModify = function (txt) {
var file = file_text_open_write(txt + ".hex");
for(var i = 0; i < array_length(palette); i++) {
var cc = palette[i];
var r = number_to_hex(color_get_red(cc));
var g = number_to_hex(color_get_green(cc));
var b = number_to_hex(color_get_blue(cc));
2024-01-09 03:39:40 +01:00
var a = number_to_hex(color_get_alpha(cc));
2022-01-13 05:24:03 +01:00
2024-01-09 03:39:40 +01:00
file_text_write_string(file, $"{r}{g}{b}{a}\n");
2022-01-13 05:24:03 +01:00
}
file_text_close(file);
__initPalette();
2022-01-13 05:24:03 +01:00
};
dia.path = DIRECTORY + "Palettes/"
}
2022-11-03 11:44:49 +01:00
bx -= ui(32);
2022-01-13 05:24:03 +01:00
2023-12-29 14:30:54 +01:00
if(buttonInstant(THEME.button_hide, bx, by, bs, bs, mouse_ui, sFOCUS, sHOVER, __txt("Refresh"), THEME.refresh_20) == 2)
__initPalette();
2022-11-03 11:44:49 +01:00
bx -= ui(32);
2022-01-13 05:24:03 +01:00
2023-12-29 14:30:54 +01:00
if(buttonInstant(THEME.button_hide, bx, by, bs, bs, mouse_ui, sFOCUS, sHOVER, __txtx("color_selector_open_palette", "Open palette folder"), THEME.path_open_20) == 2) {
2023-05-30 11:09:15 +02:00
var _realpath = DIRECTORY + "Palettes";
shellOpenExplorer(_realpath)
2022-01-13 05:24:03 +01:00
}
2023-12-29 14:30:54 +01:00
draw_sprite_ui_uniform(THEME.path_open_20, 1, bx + bs / 2, by + bs / 2, 1, c_white);
2022-11-03 11:44:49 +01:00
bx -= ui(32);
2022-01-13 05:24:03 +01:00
#endregion
#region palette
2024-07-30 13:02:53 +02:00
2022-01-13 05:24:03 +01:00
#region tools
2022-11-03 11:44:49 +01:00
var bx = content_x + content_w - ui(50);
var by = dialog_y + ui(16);
var bc = index_selecting[1] < 2? COLORS._main_icon : merge_color(COLORS._main_icon, COLORS._main_accent, 0.5);
2022-01-13 05:24:03 +01:00
var _txt = index_selecting[1] < 2? __txtx("palette_editor_sort", "Sort palette") : __txtx("palette_editor_sort_selected", "Sort selected");
var b = buttonInstant(THEME.button_hide, bx, by, ui(28), ui(28), mouse_ui, interactable && sFOCUS, sHOVER, _txt, THEME.sort, 0, bc);
if(b) mouse_draggable = false;
if(b == 2) {
2023-05-03 21:42:17 +02:00
menuCall("palette_window_sort_menu", bx + ui(32), by, [
2024-08-10 07:30:41 +02:00
menuItem(__txtx("palette_editor_sort_brighter", "Brighter"), function() { sortPalette(__sortBright); }),
menuItem(__txtx("palette_editor_sort_darker", "Darker"), function() { sortPalette(__sortDark); }),
2023-02-17 04:48:54 +01:00
-1,
2024-08-10 07:30:41 +02:00
menuItem(__txtx("palette_editor_sort_hue", "Hue"), function() { sortPalette(__sortHue); }),
menuItem(__txtx("palette_editor_sort_sat", "Saturation"), function() { sortPalette(__sortSat); }),
menuItem(__txtx("palette_editor_sort_val", "Value"), function() { sortPalette(__sortVal); }),
],, palette);
2022-01-13 05:24:03 +01:00
}
2023-02-17 04:48:54 +01:00
bx -= ui(32);
2022-01-13 05:24:03 +01:00
var _txt = index_selecting[1] < 2? __txtx("palette_editor_reverse", "Reverse palette") : __txtx("palette_editor_reverse_selected", "Reverse selected");
var b = buttonInstant(THEME.button_hide, bx, by, ui(28), ui(28), mouse_ui, interactable && sFOCUS, sHOVER, _txt, THEME.reverse, 0, bc);
if(b) mouse_draggable = false;
if(b == 2) {
if(index_selecting[1] < 2)
palette = array_reverse(palette);
else {
var _arr = array_create(index_selecting[1]);
for(var i = 0; i < index_selecting[1]; i++)
_arr[i] = palette[index_selecting[0] + i];
_arr = array_reverse(_arr);
for(var i = 0; i < index_selecting[1]; i++)
palette[index_selecting[0] + i] = _arr[i];
}
2023-02-17 04:48:54 +01:00
onApply(palette);
}
2022-11-03 11:44:49 +01:00
bx -= ui(32);
2022-01-13 05:24:03 +01:00
#endregion
2024-07-30 13:02:53 +02:00
var pl_x = content_x + ui(60);
var pl_y = dialog_y + ui(54);
var pl_w = content_w - ui(154);
var hh = ui(24);
var pd = ui(0);
var _len = array_length(palette);
var min_col = 8;
var col = min(_len, min_col);
var row = ceil(_len / col);
if(row > 8) {
col = 16;
row = ceil(_len / col);
}
var ww = pl_w / col;
var pl_h = hh * row;
dialog_h = ui(408) + pl_h;
var pdd = ui(6);
var pl_sx = pl_x - pdd;
var pl_sy = pl_y - pdd;
var pl_sw = pl_w + pdd * 2;
var pl_sh = pl_h + pdd * 2;
draw_sprite_stretched(THEME.textbox, 3, pl_sx, pl_sy, pl_sw, pl_sh);
draw_sprite_stretched(THEME.textbox, 0, pl_sx, pl_sy, pl_sw, pl_sh);
selection_surface = surface_verify(selection_surface, pl_sw, pl_sh);
2022-12-10 05:06:01 +01:00
var hover = -1, hvx, hvy;
2024-07-30 13:02:53 +02:00
var _pw = ceil(ww - pd * 2);
var _ph = ceil(hh - pd * 2);
var _spx = pl_x - pdd;
var _spy = pl_y - pdd;
var ppos = palette_positions;
var _hedge = false;
var _clrRep = {};
var _palInd = [];
2024-01-09 03:39:40 +01:00
2022-01-13 05:24:03 +01:00
for(var i = 0; i < row; i++)
for(var j = 0; j < col; j++) {
var index = i * col + j;
2024-07-30 13:02:53 +02:00
if(index >= _len) break;
2024-01-09 03:39:40 +01:00
2022-01-13 05:24:03 +01:00
var _p = palette[index];
2024-01-09 03:39:40 +01:00
var _pa = _color_get_alpha(_p);
2022-01-13 05:24:03 +01:00
var _kx = pl_x + j * ww;
2024-07-30 13:02:53 +02:00
var _ky = pl_y + i * hh;
2022-01-13 05:24:03 +01:00
2024-07-30 13:02:53 +02:00
var _px = floor(_kx + pd);
var _py = floor(_ky + pd);
2024-01-09 03:39:40 +01:00
2024-07-30 13:02:53 +02:00
var _k = string(_p);
var _ii = 0;
var _selecting = index >= index_selecting[0] && index < index_selecting[0] + index_selecting[1];
2024-07-30 13:02:53 +02:00
while(struct_has(_clrRep, _k)) {
_k = $"{_p}{_ii}";
_ii++;
2024-01-09 03:39:40 +01:00
}
2024-07-30 13:02:53 +02:00
_clrRep[$ _k] = 1;
2022-01-13 05:24:03 +01:00
2024-07-30 13:02:53 +02:00
if(struct_has(ppos, _k)) {
ppos[$ _k][0] = (ppos[$ _k][0] == 0 || !_selecting)? _px - dialog_x : lerp_float(ppos[$ _k][0], _px - dialog_x, 4);
ppos[$ _k][1] = (ppos[$ _k][1] == 0 || !_selecting)? _py - dialog_y : lerp_float(ppos[$ _k][1], _py - dialog_y, 4);
2024-07-30 13:02:53 +02:00
} else {
ppos[$ _k] = [ _px - dialog_x, _py - dialog_y ];
2024-07-30 13:02:53 +02:00
}
var _pdx = dialog_x + ppos[$ _k][0];
var _pdy = dialog_y + ppos[$ _k][1];
2024-07-30 13:02:53 +02:00
var _ind = 0;
if(row == 1) {
if(j == 0) _ind = 2;
else if(j == col - 1) _ind = 3;
} else {
if(index == 0) _ind = 6;
else if(i == 0 && j == col - 1) _ind = 7;
else if(i == row - 2) {
if(j == col - 1 && _len - 1 < index + col) _ind = 9;
} else if(i == row - 1) {
if(j == 0) _ind = 8;
if(j == col - 1) _ind = 7;
if(index == _len - 1) _ind = 9;
}
}
_palInd[index] = _ind;
drawColor(_p, _pdx, _pdy, _pw, _ph, true, _ind);
2024-07-30 13:02:53 +02:00
if(sHOVER && point_in_rectangle(mouse_mx, mouse_my, _kx, _ky, _kx + ww, _ky + hh)) {
2022-01-13 05:24:03 +01:00
hover = index;
2022-12-10 05:06:01 +01:00
hvx = _kx;
hvy = _ky;
2024-07-30 13:02:53 +02:00
if(_selecting && !point_in_rectangle(mouse_mx, mouse_my, _kx + 4, _ky + 4, _kx + ww - 8, _ky + hh - 8))
2024-07-30 13:02:53 +02:00
_hedge = true;
2022-01-13 05:24:03 +01:00
}
}
2024-07-30 13:02:53 +02:00
surface_set_target(selection_surface);
DRAW_CLEAR
for(var i = 0; i < row; i++)
for(var j = 0; j < col; j++) {
var index = i * col + j;
if(index >= _len) break;
if(index >= index_selecting[0] && index < index_selecting[0] + index_selecting[1]) {
var _p = palette[index];
var _px = dialog_x + ppos[$ _p][0] - pl_sx;
var _py = dialog_y + ppos[$ _p][1] - pl_sy;
2024-07-30 13:02:53 +02:00
drawColor(_p, _px, _py, _pw, _ph, true, _palInd[index]);
2024-07-30 13:02:53 +02:00
}
}
surface_reset_target();
shader_set(sh_dialog_palette_selector);
shader_set_f("dimension", pl_sw, pl_sh);
shader_set_i("edge", (_hedge && !mouse_click(mb_left)) || index_dragging != noone);
2024-07-30 13:02:53 +02:00
shader_set_color("edgeColor", COLORS._main_accent);
2024-01-09 03:39:40 +01:00
2024-07-30 13:02:53 +02:00
draw_surface(selection_surface, pl_sx, pl_sy);
shader_reset();
if(index_dragging != noone) {
2022-12-10 05:06:01 +01:00
if(hover > -1 && hover != index_dragging) {
2024-07-30 13:02:53 +02:00
var prea = [];
var cont = [];
var posa = [];
var _0 = index_selecting[0];
var _1 = index_selecting[0] + index_selecting[1];
var _2 = array_length(palette);
for(var i = 0; i < _2; i++) {
if(i < _0) array_push(prea, palette[i]);
else if(i < _1) array_push(cont, palette[i]);
else array_push(posa, palette[i]);
}
var _shf = clamp(hover - index_dragging, -index_selecting[0], _2 - (index_selecting[0] + index_selecting[1]));
var _pal = [];
if(_shf < 0) {
for (var i = 0, n = array_length(prea) + _shf; i < n; i++) array_push(_pal, prea[i]);
for (var i = 0, n = array_length(cont); i < n; i++) array_push(_pal, cont[i]);
for (var i = array_length(prea) + _shf, n = array_length(prea); i < n; i++) array_push(_pal, prea[i]);
for (var i = 0, n = array_length(posa); i < n; i++) array_push(_pal, posa[i]);
2022-01-13 05:24:03 +01:00
2024-07-30 13:02:53 +02:00
palette = _pal;
2023-02-21 07:38:25 +01:00
2024-07-30 13:02:53 +02:00
} else if(_shf > 0) {
for (var i = 0, n = array_length(prea); i < n; i++) array_push(_pal, prea[i]);
for (var i = 0, n = _shf; i < n; i++) array_push(_pal, posa[i]);
for (var i = 0, n = array_length(cont); i < n; i++) array_push(_pal, cont[i]);
for (var i = _shf, n = array_length(posa); i < n; i++) array_push(_pal, posa[i]);
palette = _pal;
}
index_selecting[0] += _shf;
index_dragging = hover;
2024-01-09 03:39:40 +01:00
onApply(palette);
2022-01-13 05:24:03 +01:00
}
2022-12-10 05:06:01 +01:00
2024-07-30 13:02:53 +02:00
if(mouse_release(mb_left)) {
index_selecting = [ 0, 0 ];
index_dragging = noone;
}
2024-01-09 03:39:40 +01:00
} else {
index_drag_x = 0;
index_drag_y = 0;
index_drag_w = 0;
index_drag_h = 0;
2022-01-13 05:24:03 +01:00
2024-07-30 13:02:53 +02:00
if(hover > -1) {
if(mouse_press(mb_left, sFOCUS)) {
if(interactable) {
if(_hedge) index_dragging = hover;
else {
index_selecting = [ hover, 1 ];
selector.setColor(palette[hover]);
}
} else if(!interactable) {
index_selecting = [ hover, 1 ];
selector.setColor(palette[hover]);
}
2024-07-31 06:56:49 +02:00
mouse_interact = true;
2024-07-30 13:02:53 +02:00
index_sel_start = hover;
2024-07-31 06:56:49 +02:00
} else if(mouse_click(mb_left, sFOCUS) && mouse_interact) {
2024-07-30 13:02:53 +02:00
if(hover > index_sel_start) {
index_selecting[0] = index_sel_start;
index_selecting[1] = hover - index_sel_start + 1;
} else if(hover < index_sel_start) {
index_selecting[0] = hover;
index_selecting[1] = index_sel_start - hover + 1;
} else {
index_selecting[0] = hover;
index_selecting[1] = 1;
}
2024-07-31 06:56:49 +02:00
}
2024-07-30 13:02:53 +02:00
}
2022-01-13 05:24:03 +01:00
}
2024-07-31 06:56:49 +02:00
if(mouse_release(mb_left)) mouse_interact = false;
selector.current_colors = noone;
if(index_selecting[1] > 1) {
colors_selecting = array_verify(colors_selecting, index_selecting[1]);
for(var i = 0; i < index_selecting[1]; i++)
colors_selecting[i] = palette[index_selecting[0] + i];
selector.current_colors = colors_selecting;
}
2022-11-03 11:44:49 +01:00
var bx = content_x + content_w - ui(50);
var by = pl_y - ui(2);
2022-01-13 05:24:03 +01:00
if(array_length(palette) > 1) {
2024-01-09 03:39:40 +01:00
if(buttonInstant(THEME.button_hide, bx, by, ui(28), ui(28), mouse_ui, interactable && sFOCUS, sHOVER, "", THEME.minus) == 2) {
2024-07-30 13:02:53 +02:00
array_delete(palette, index_selecting[0], index_selecting[1]);
index_selecting = [ 0, 0 ];
2022-11-18 03:20:31 +01:00
onApply(palette);
2022-01-13 05:24:03 +01:00
}
} else {
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.minus, 0, bx + ui(14), by + ui(14), 1, COLORS._main_icon, 0.5);
2022-01-13 05:24:03 +01:00
}
2022-11-03 11:44:49 +01:00
bx -= ui(32);
2024-01-09 03:39:40 +01:00
if(buttonInstant(THEME.button_hide, bx, by, ui(28), ui(28), mouse_ui, interactable && sFOCUS, sHOVER, "", THEME.add) == 2) {
2022-01-13 05:24:03 +01:00
palette[array_length(palette)] = c_black;
2024-07-30 13:02:53 +02:00
index_selecting = [ array_length(palette), 1 ];
2022-11-18 03:20:31 +01:00
onApply(palette);
2022-01-13 05:24:03 +01:00
}
2022-11-03 11:44:49 +01:00
bx = content_x + ui(18);
2024-01-09 03:39:40 +01:00
if(buttonInstant(THEME.button_hide, bx, by, ui(28), ui(28), mouse_ui, interactable && sFOCUS, sHOVER, __txtx("palette_editor_load", "Load palette file") + " (.hex)", THEME.file) == 2) {
2024-05-16 15:28:45 +02:00
var path = get_open_filename_pxc("HEX palette|*.hex", "");
2023-02-28 09:43:01 +01:00
key_release();
if(isPaletteFile(path)) {
2022-01-13 05:24:03 +01:00
palette = loadPalette(path);
2022-11-18 03:20:31 +01:00
onApply(palette);
}
2022-01-13 05:24:03 +01:00
}
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.file, 0, bx + ui(14), by + ui(14), 1, COLORS._main_icon);
2022-01-13 05:24:03 +01:00
#endregion
2022-11-18 03:20:31 +01:00
#region selector
2022-11-03 11:44:49 +01:00
var col_x = content_x + ui(20);
2024-07-30 13:02:53 +02:00
var col_y = dialog_y + ui(70) + pl_h;
2022-01-13 05:24:03 +01:00
2022-11-18 03:20:31 +01:00
selector.draw(col_x, col_y, sFOCUS, sHOVER);
2022-01-13 05:24:03 +01:00
#endregion
#region controls
2022-11-03 11:44:49 +01:00
var bx = content_x + content_w - ui(36);
var by = dialog_y + dialog_h - ui(36);
2023-01-25 06:49:00 +01:00
b_apply.register();
2023-06-21 20:36:53 +02:00
b_apply.setFocusHover(sFOCUS, sHOVER);
2023-01-25 06:49:00 +01:00
b_apply.draw(bx - ui(18), by - ui(18), ui(36), ui(36), mouse_ui, THEME.button_lime);
2023-02-14 13:44:46 +01:00
bx -= ui(48);
b_cancel.register();
2023-06-21 20:36:53 +02:00
b_cancel.setFocusHover(sFOCUS, sHOVER);
2023-02-14 13:44:46 +01:00
b_cancel.draw(bx - ui(18), by - ui(18), ui(36), ui(36), mouse_ui, THEME.button_hide);
2022-01-13 05:24:03 +01:00
#endregion