Pixel-Composer/scripts/panel_workspace/panel_workspace.gml

146 lines
4.2 KiB
Plaintext
Raw Normal View History

2024-08-09 13:30:09 +02:00
#macro CHECK_PANEL_WORKSPACE if(!is_instanceof(FOCUS_CONTENT, Panel_Workspace)) return;
function panel_workspace_apply() { CHECK_PANEL_WORKSPACE CALL("panel_workspace_apply"); FOCUS_CONTENT.apply_space(); }
function panel_workspace_replace() { CHECK_PANEL_WORKSPACE CALL("panel_workspace_replace"); FOCUS_CONTENT.replace_space(); }
function panel_workspace_delete() { CHECK_PANEL_WORKSPACE CALL("panel_workspace_delete"); FOCUS_CONTENT.delete_space(); }
2023-03-12 02:28:21 +01:00
function Panel_Workspace() : PanelContent() constructor {
2024-08-09 13:30:09 +02:00
title = "Workspace";
2023-03-12 02:28:21 +01:00
workspaces = [];
w = ui(480);
2024-09-10 11:48:51 +02:00
h = ui(32);
2023-03-13 10:45:56 +01:00
scroll = 0;
scroll_to = 0;
scroll_max = 0;
2024-08-09 13:30:09 +02:00
hori = false;
2023-03-12 02:28:21 +01:00
2023-03-28 06:58:28 +02:00
layout_selecting = "";
2024-08-09 13:30:09 +02:00
registerFunction("Workspace", "Apply", "", MOD_KEY.none, panel_workspace_apply);
registerFunction("Workspace", "Replace", "", MOD_KEY.none, panel_workspace_replace);
registerFunction("Workspace", "Delete", "", MOD_KEY.none, panel_workspace_delete);
function apply_space() {
if(layout_selecting == "") return;
PREFERENCES.panel_layout_file = layout_selecting;
PREF_SAVE();
setPanel();
}
function replace_space() {
if(layout_selecting == "") return;
var cont = panelSerialize();
json_save_struct(DIRECTORY + "layouts/" + layout_selecting + ".json", cont);
}
function delete_space() {
if(layout_selecting == "") return;
file_delete(DIRECTORY + "layouts/" + layout_selecting + ".json");
refreshContent();
}
2023-03-12 02:28:21 +01:00
function refreshContent() {
workspaces = [];
var f = file_find_first(DIRECTORY + "layouts/*", 0);
2023-03-12 02:28:21 +01:00
while(f != "") {
2023-11-20 05:10:55 +01:00
if(filename_ext(f) == ".json")
array_push(workspaces, filename_name_only(f));
2023-03-12 02:28:21 +01:00
f = file_find_next();
}
2024-08-09 13:30:09 +02:00
} refreshContent();
2023-03-12 02:28:21 +01:00
2023-03-13 10:45:56 +01:00
function onFocusBegin() { refreshContent(); }
2023-03-12 02:28:21 +01:00
function drawContent(panel) {
2024-05-27 07:12:24 +02:00
draw_clear_alpha(COLORS.panel_bg_clear, 1);
2023-03-12 02:28:21 +01:00
2023-03-13 10:45:56 +01:00
var _hori = hori;
hori = w > h;
if(hori != _hori) scroll_to = 0;
2023-03-12 02:28:21 +01:00
2024-09-10 11:48:51 +02:00
var x0 = ui(8) + scroll, x1;
var y0, y1;
var cx = w / 2;
var cy = h / 2;
2023-03-13 10:45:56 +01:00
var ww = 0;
var hh = 0;
2023-03-28 06:58:28 +02:00
var amo = array_length(workspaces);
2023-03-12 02:28:21 +01:00
2024-09-10 11:48:51 +02:00
draw_set_text(f_p1, fa_center, fa_center, COLORS._main_text_sub);
2023-03-12 02:28:21 +01:00
2023-03-28 06:58:28 +02:00
for( var i = 0; i <= amo; i++ ) {
var str = i == amo? "+" : workspaces[i];
2024-09-10 11:48:51 +02:00
var tw = string_width(str) + ui(16);
2023-03-28 06:58:28 +02:00
var th = string_height(str) + ui(8);
2023-03-12 02:28:21 +01:00
x1 = x0 + tw;
2024-09-10 11:48:51 +02:00
y0 = cy - th / 2;
2023-03-12 02:28:21 +01:00
y1 = y0 + th;
if(pHOVER && point_in_rectangle(mx, my, x0, y0, x1, y1)) {
2024-09-10 11:48:51 +02:00
draw_sprite_stretched(THEME.button_hide_fill, 1, x0, y0, tw, th);
2023-03-12 02:28:21 +01:00
if(mouse_press(mb_left, pFOCUS)) {
2023-03-28 06:58:28 +02:00
if(i == amo) {
var dia = dialogCall(o_dialog_file_name, mouse_mx + ui(8), mouse_my + ui(8));
2023-10-31 05:30:42 +01:00
dia.name = PREFERENCES.panel_layout_file;
2023-03-28 06:58:28 +02:00
dia.onModify = function(name) {
var cont = panelSerialize();
2023-10-31 05:30:42 +01:00
json_save_struct($"{DIRECTORY}layouts/{name}.json", cont);
2023-03-28 06:58:28 +02:00
2023-10-31 05:30:42 +01:00
PREFERENCES.panel_layout_file = name;
2023-03-28 06:58:28 +02:00
PREF_SAVE();
refreshContent();
};
} else {
2023-10-31 05:30:42 +01:00
PREFERENCES.panel_layout_file = str;
2023-03-28 06:58:28 +02:00
PREF_SAVE();
setPanel();
}
}
if(mouse_press(mb_right, pFOCUS)) {
layout_selecting = str;
2024-08-10 11:04:14 +02:00
menuCall("workspace_menu", [
2024-08-10 07:30:41 +02:00
menuItem(__txt("Select"), apply_space),
menuItem(__txtx("workspace_replace_current", "Replace with current"), replace_space),
menuItem(__txt("Delete"), delete_space, THEME.cross),
2023-03-28 06:58:28 +02:00
]);
2023-03-12 02:28:21 +01:00
}
}
2024-09-10 11:48:51 +02:00
if(PREFERENCES.panel_layout_file == str) draw_set_color(COLORS._main_text);
else draw_set_color(COLORS._main_text_sub);
2023-03-12 02:28:21 +01:00
2024-09-10 11:48:51 +02:00
var _tx = x0 + tw / 2;
var _ty = cy;
draw_text_add(_tx, _ty, str);
x0 += tw + ui(4);
ww += tw + ui(4);
2023-03-13 10:45:56 +01:00
}
scroll = lerp_float(scroll, scroll_to, 5);
if(hori) {
scroll_max = max(ww - w + ui(16), 0);
if(pHOVER) {
2023-04-07 21:25:27 +02:00
if(mouse_wheel_down()) scroll_to = clamp(scroll_to - ui(128) * SCROLL_SPEED, -scroll_max, 0);
if(mouse_wheel_up()) scroll_to = clamp(scroll_to + ui(128) * SCROLL_SPEED, -scroll_max, 0);
2023-03-13 10:45:56 +01:00
}
} else {
scroll_max = max(hh - h + ui(16), 0);
if(pHOVER) {
2023-04-07 21:25:27 +02:00
if(mouse_wheel_down()) scroll_to = clamp(scroll_to - ui(32) * SCROLL_SPEED, -scroll_max, 0);
if(mouse_wheel_up()) scroll_to = clamp(scroll_to + ui(32) * SCROLL_SPEED, -scroll_max, 0);
2023-03-13 10:45:56 +01:00
}
2023-03-12 02:28:21 +01:00
}
}
}