- Add preference option to save and load project layout.

This commit is contained in:
Tanasart 2024-07-01 13:14:55 +07:00
parent 11de0da99f
commit f8c7dab28a
11 changed files with 83 additions and 38 deletions

View file

@ -179,6 +179,15 @@ event_inherited();
ds_list_add(pref_global, __txt("Save/Load")); ds_list_add(pref_global, __txt("Save/Load"));
ds_list_add(pref_global, new __Panel_Linear_Setting_Item_Preference(
__txtx("pref_save_layout", "Save layout"),
"save_layout",
new checkBox(function() {
PREFERENCES.save_layout = !PREFERENCES.save_layout;
PREF_SAVE();
})
));
ds_list_add(pref_global, new __Panel_Linear_Setting_Item_Preference( ds_list_add(pref_global, new __Panel_Linear_Setting_Item_Preference(
__txtx("pref_save_file_minify", "Minify save file"), __txtx("pref_save_file_minify", "Minify save file"),
"save_file_minify", "save_file_minify",

View file

@ -15,7 +15,7 @@ function exportPortable(project = PROJECT) {
var raw_name = filename_name_only(path); var raw_name = filename_name_only(path);
var raw_path = filename_path(path) + raw_name; var raw_path = filename_path(path) + raw_name;
var _proj = save_serialize(project, true); var _proj = project.serialize();
var zip = zip_create(); var zip = zip_create();

View file

@ -18,6 +18,7 @@
"Extrude": new hotkeySimple("E", MOD_KEY.alt), "Extrude": new hotkeySimple("E", MOD_KEY.alt),
"Inset": new hotkeySimple("I", MOD_KEY.alt), "Inset": new hotkeySimple("I", MOD_KEY.alt),
"Skew": new hotkeySimple("S", MOD_KEY.alt), "Skew": new hotkeySimple("S", MOD_KEY.alt),
"Corner": new hotkeySimple("C", MOD_KEY.alt),
}, },
"Node_Mesh_Warp": { "Node_Mesh_Warp": {

View file

@ -253,6 +253,9 @@ function LOAD_AT(path, readonly = false, override = false) { #region
printIf(log, $"========== Load {array_length(PROJECT.allNodes)} nodes completed in {(get_timer() - t0) / 1000} ms =========="); printIf(log, $"========== Load {array_length(PROJECT.allNodes)} nodes completed in {(get_timer() - t0) / 1000} ms ==========");
if(PREFERENCES.save_layout && struct_has(_load_content, "layout"))
LoadPanelStruct(_load_content.layout.panel);
return true; return true;
} #endregion } #endregion

View file

@ -141,6 +141,9 @@
#endregion #endregion
#region node function #region node function
function node_get_id(node) { return node? node.node_id : noone; }
function node_from_id(nid) { return nid && ds_map_exists(PROJECT.nodeMap, nid)? PROJECT.nodeMap[? nid] : noone; }
function nodeLoad(_data, scale = false, _group = noone) { function nodeLoad(_data, scale = false, _group = noone) {
INLINE INLINE
if(!is_struct(_data)) return; if(!is_struct(_data)) return;

View file

@ -77,6 +77,8 @@
return noone; return noone;
} #endregion } #endregion
function LoadPanelStruct(struct) { loadPanelStruct(PANEL_MAIN, struct); }
function loadPanelStruct(panel, str) { #region function loadPanelStruct(panel, str) { #region
var cont = str.content; var cont = str.content;
@ -108,10 +110,10 @@
} }
} #endregion } #endregion
function loadPanel(path, panel) { #region function loadPanel(path) {
CURRENT_PANEL = json_load_struct(path); CURRENT_PANEL = json_load_struct(path);
loadPanelStruct(panel, CURRENT_PANEL.panel); LoadPanelStruct(CURRENT_PANEL.panel);
} #endregion }
function checkPanelValid() { #region function checkPanelValid() { #region
var val = true; var val = true;
@ -180,7 +182,7 @@
var file = $"{DIRECTORY}layouts/{PREFERENCES.panel_layout_file}.json"; var file = $"{DIRECTORY}layouts/{PREFERENCES.panel_layout_file}.json";
if(!file_exists_empty(file)) if(!file_exists_empty(file))
file = DIRECTORY + "layouts/Horizontal.json"; file = DIRECTORY + "layouts/Horizontal.json";
loadPanel(file, PANEL_MAIN); loadPanel(file);
PANEL_MAIN.refresh(); PANEL_MAIN.refresh();
PANEL_MAIN.refreshSize(); PANEL_MAIN.refreshSize();

View file

@ -2590,11 +2590,24 @@ function Panel_Graph(project = PROJECT) : PanelContent() constructor {
return { return {
name: instanceof(self), name: instanceof(self),
project, project,
graph_x,
graph_y,
graph_s,
graph_s_to,
}; };
} }
static deserialize = function(data) { static deserialize = function(data) {
setProject(data.project); setProject(data.project);
graph_x = struct_try_get(data, "graph_x", graph_x);
graph_y = struct_try_get(data, "graph_x", graph_y);
graph_s = struct_try_get(data, "graph_x", graph_s);
graph_s_to = struct_try_get(data, "graph_x", graph_s_to);
return self; return self;
} }

View file

@ -1120,14 +1120,14 @@ function Panel_Inspector() : PanelContent() constructor {
static serialize = function() { static serialize = function() {
return { return {
name: instanceof(self), name: instanceof(self),
inspecting, inspecting : node_get_id(inspecting),
inspectings, inspectings : array_map(inspectings, function(n) { return node_get_id(n) }),
}; };
} }
static deserialize = function(data) { static deserialize = function(data) {
inspecting = data.inspecting; inspecting = node_from_id(data.inspecting);
inspectings = data.inspectings; inspectings = array_map(data.inspectings, function(n) { return node_from_id(n); });
return self; return self;
} }

View file

@ -2155,13 +2155,22 @@ function Panel_Preview() : PanelContent() constructor {
static serialize = function() { static serialize = function() {
return { return {
name: instanceof(self), name: instanceof(self),
preview_node, preview_node : [ node_get_id(preview_node[0]), node_get_id(preview_node[1]) ],
canvas_x,
canvas_y,
canvas_s,
}; };
} }
static deserialize = function(data) { static deserialize = function(data) {
preview_node = data.preview_node; if(struct_has(data, "preview_node"))
preview_node = [ node_from_id(data.preview_node[0]), node_from_id(data.preview_node[1]) ];
canvas_x = struct_try_get(data, "canvas_x", canvas_x);
canvas_y = struct_try_get(data, "canvas_y", canvas_y);
canvas_s = struct_try_get(data, "canvas_s", canvas_s);
run_in(1, fullView) run_in(1, fullView)
return self; return self;

View file

@ -124,10 +124,16 @@
#endregion #endregion
#region //////////////////////////////////////////////////////////////////////// MISC ////////////////////////////////////////////////////////////////////////// #region //////////////////////////////////////////////////////////////////////// SAVE //////////////////////////////////////////////////////////////////////////
PREFERENCES.save_file_minify = true; PREFERENCES.save_file_minify = true;
PREFERENCES.save_backup = 1; PREFERENCES.save_backup = 1;
PREFERENCES.save_layout = false;
#endregion
#region //////////////////////////////////////////////////////////////////////// MISC //////////////////////////////////////////////////////////////////////////
PREFERENCES.render_all_export = true; PREFERENCES.render_all_export = true;
PREFERENCES.clear_temp_on_close = true; PREFERENCES.clear_temp_on_close = true;

View file

@ -2,7 +2,7 @@ globalvar SAVING, IS_SAVING;
SAVING = false; SAVING = false;
IS_SAVING = false; IS_SAVING = false;
function NEW() { #region function NEW() {
CALL("new"); CALL("new");
PROJECT = new Project(); PROJECT = new Project();
@ -11,16 +11,16 @@ function NEW() { #region
var graph = new Panel_Graph(PROJECT); var graph = new Panel_Graph(PROJECT);
PANEL_GRAPH.panel.setContent(graph, true); PANEL_GRAPH.panel.setContent(graph, true);
PANEL_GRAPH = graph; PANEL_GRAPH = graph;
} #endregion }
function save_serialize(project = PROJECT, _outMap = false) { #region function SERIALIZE_PROJECT(project = PROJECT) {
var _map = project.serialize(); var _map = project.serialize();
_map.layout = panelSerialize();
if(_outMap) return _map;
return PREFERENCES.save_file_minify? json_stringify_minify(_map) : json_stringify(_map, true); return PREFERENCES.save_file_minify? json_stringify_minify(_map) : json_stringify(_map, true);
} #endregion }
function SET_PATH(project, path) { #region function SET_PATH(project, path) {
if(path == "") { if(path == "") {
project.readonly = false; project.readonly = false;
} else if(!project.readonly) { } else if(!project.readonly) {
@ -34,23 +34,23 @@ function SET_PATH(project, path) { #region
} }
project.path = path; project.path = path;
} #endregion }
function SAVE_ALL() { #region function SAVE_ALL() {
for( var i = 0, n = array_length(PROJECTS); i < n; i++ ) for( var i = 0, n = array_length(PROJECTS); i < n; i++ )
SAVE(PROJECTS[i]); SAVE(PROJECTS[i]);
} #endregion }
function SAVE(project = PROJECT) { #region function SAVE(project = PROJECT) {
if(DEMO) return false; if(DEMO) return false;
if(project.path == "" || project.readonly || path_is_backup(project.path)) if(project.path == "" || project.readonly || path_is_backup(project.path))
return SAVE_AS(project); return SAVE_AS(project);
return SAVE_AT(project, project.path); return SAVE_AT(project, project.path);
} #endregion }
function SAVE_AS(project = PROJECT) { #region function SAVE_AS(project = PROJECT) {
if(DEMO) return false; if(DEMO) return false;
var path = get_save_filename_pxc("Pixel Composer project (.pxc)|*.pxc|Compressed Pixel Composer project (.cpxc)|*.cpxc", ""); var path = get_save_filename_pxc("Pixel Composer project (.pxc)|*.pxc|Compressed Pixel Composer project (.cpxc)|*.cpxc", "");
@ -66,17 +66,15 @@ function SAVE_AS(project = PROJECT) { #region
SET_PATH(project, path); SET_PATH(project, path);
return true; return true;
} #endregion }
function SAVE_AT(project = PROJECT, path = "", log = "save at ") { #region function SAVE_AT(project = PROJECT, path = "", log = "save at ") {
CALL("save"); CALL("save");
if(DEMO) return false; if(DEMO) return false;
IS_SAVING = true; IS_SAVING = true;
SAVING = true; SAVING = true;
//if(TESTING && string_char_at(filename_name(path), 1) != "[")
// path = $"{filename_dir(path)}/[{VERSION_STRING}] {filename_name(path)}";
if(PREFERENCES.save_backup) { if(PREFERENCES.save_backup) {
for(var i = PREFERENCES.save_backup - 1; i >= 0; i--) { for(var i = PREFERENCES.save_backup - 1; i >= 0; i--) {
@ -90,9 +88,10 @@ function SAVE_AT(project = PROJECT, path = "", log = "save at ") { #region
if(file_exists_empty(path)) file_delete(path); if(file_exists_empty(path)) file_delete(path);
var _ext = filename_ext_raw(path); var _ext = filename_ext_raw(path);
var _prj = SERIALIZE_PROJECT(project);
if(_ext == "pxc") file_text_write_all(path, save_serialize(project)); if(_ext == "pxc") file_text_write_all(path, _prj);
else if(_ext == "cpxc") buffer_save(buffer_compress_string(save_serialize(project)), path); else if(_ext == "cpxc") buffer_save(buffer_compress_string(_prj), path);
SAVING = false; SAVING = false;
project.readonly = false; project.readonly = false;
@ -102,11 +101,11 @@ function SAVE_AT(project = PROJECT, path = "", log = "save at ") { #region
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save); PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
return true; return true;
} #endregion }
/////////////////////////////////////////////////////// COLLECTION /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// COLLECTION ///////////////////////////////////////////////////////
function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) { #region function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) {
var _content = {}; var _content = {};
_content.version = SAVE_VERSION; _content.version = SAVE_VERSION;
@ -151,9 +150,9 @@ function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, c
log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save); log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save);
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save); PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
} #endregion }
function SAVE_COLLECTION(_node, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) { #region function SAVE_COLLECTION(_node, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) {
if(save_surface) { if(save_surface) {
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface(); var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
if(is_surface(preview_surface)) { if(is_surface(preview_surface)) {
@ -186,9 +185,9 @@ function SAVE_COLLECTION(_node, _path, save_surface = true, metadata = noone, co
log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save); log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save);
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save); PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
} #endregion }
function SAVE_NODE(_arr, _node, dx = 0, dy = 0, scale = false, context = PANEL_GRAPH.getCurrentContext()) { #region function SAVE_NODE(_arr, _node, dx = 0, dy = 0, scale = false, context = PANEL_GRAPH.getCurrentContext()) {
if(struct_has(_node, "nodes")) { if(struct_has(_node, "nodes")) {
for(var i = 0; i < array_length(_node.nodes); i++) for(var i = 0; i < array_length(_node.nodes); i++)
SAVE_NODE(_arr, _node.nodes[i], dx, dy, scale, context); SAVE_NODE(_arr, _node.nodes[i], dx, dy, scale, context);
@ -202,4 +201,4 @@ function SAVE_NODE(_arr, _node, dx = 0, dy = 0, scale = false, context = PANEL_G
if(m.group == c) m.group = noone; if(m.group == c) m.group = noone;
array_push(_arr, m); array_push(_arr, m);
} #endregion }