Pixel-Composer/scripts/save_function/save_function.gml

233 lines
6.7 KiB
Text
Raw Normal View History

2024-05-15 12:42:54 +02:00
globalvar SAVING, IS_SAVING;
SAVING = false;
IS_SAVING = false;
2023-03-11 01:40:17 +01:00
2023-11-23 02:32:26 +01:00
function NEW() { #region
2024-02-06 13:53:08 +01:00
CALL("new");
2024-02-04 07:33:42 +01:00
2023-07-06 19:49:16 +02:00
PROJECT = new Project();
2023-07-30 13:56:22 +02:00
array_push(PROJECTS, PROJECT);
2022-12-10 05:06:01 +01:00
2023-07-06 19:49:16 +02:00
var graph = new Panel_Graph(PROJECT);
PANEL_GRAPH.panel.setContent(graph, true);
PANEL_GRAPH = graph;
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function save_serialize(project = PROJECT, _outMap = false) { #region
2024-02-04 07:33:42 +01:00
var _map = {};
2023-07-06 19:49:16 +02:00
_map.version = SAVE_VERSION;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
var _node_list = [];
2023-07-06 19:49:16 +02:00
var _key = ds_map_find_first(project.nodeMap);
2022-01-13 05:24:03 +01:00
2023-07-06 19:49:16 +02:00
repeat(ds_map_size(project.nodeMap)) {
var _node = project.nodeMap[? _key];
2023-03-11 01:40:17 +01:00
if(_node.active)
2023-06-13 14:42:06 +02:00
array_push(_node_list, _node.serialize());
2022-01-13 05:24:03 +01:00
2023-07-06 19:49:16 +02:00
_key = ds_map_find_next(project.nodeMap, _key);
2022-01-13 05:24:03 +01:00
}
2023-06-13 14:42:06 +02:00
_map.nodes = _node_list;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
var _anim_map = {};
2023-07-06 19:49:16 +02:00
_anim_map.frames_total = project.animator.frames_total;
_anim_map.framerate = project.animator.framerate;
_anim_map.frame_range = project.animator.frame_range;
2023-07-05 15:09:52 +02:00
_map.animator = _anim_map;
2022-01-13 05:24:03 +01:00
2023-11-27 14:50:20 +01:00
_map.metadata = PROJECT.meta.serialize();
2023-07-06 19:49:16 +02:00
_map.global_node = project.globalNode.serialize();
2023-07-08 20:29:23 +02:00
_map.onion_skin = project.onion_skin;
2023-03-07 14:29:47 +01:00
2023-08-01 19:21:51 +02:00
_map.previewGrid = project.previewGrid;
_map.graphGrid = project.graphGrid;
2023-10-06 11:51:11 +02:00
_map.attributes = project.attributes;
2023-08-01 19:21:51 +02:00
2023-10-12 07:07:24 +02:00
_map.timelines = project.timelines.serialize();
2024-04-23 11:53:16 +02:00
_map.notes = array_map(project.notes, function(note) { return note.serialize(); } );
2023-10-12 07:07:24 +02:00
2023-03-21 03:01:53 +01:00
var prev = PANEL_PREVIEW.getNodePreviewSurface();
2023-06-13 14:42:06 +02:00
if(!is_surface(prev)) _map.preview = "";
else _map.preview = surface_encode(surface_size_lim(prev, 128, 128));
2023-03-05 07:16:44 +01:00
2023-06-13 14:42:06 +02:00
var _addon = {};
2023-05-16 21:28:16 +02:00
with(_addon_custom) {
var _ser = lua_call(thread, "serialize");
2023-10-31 05:30:42 +01:00
_addon[$ name] = PREFERENCES.save_file_minify? json_stringify_minify(_ser) : json_stringify(_ser);
2023-05-16 21:28:16 +02:00
}
2023-06-13 14:42:06 +02:00
_map.addon = _addon;
2023-05-16 21:28:16 +02:00
2023-09-26 14:35:25 +02:00
if(_outMap) return _map;
2023-10-31 05:30:42 +01:00
return PREFERENCES.save_file_minify? json_stringify_minify(_map) : json_stringify(_map, true);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SET_PATH(project, path) { #region
if(path == "") {
2023-07-06 19:49:16 +02:00
project.readonly = false;
} else if(!project.readonly) {
ds_list_remove(RECENT_FILES, path);
ds_list_insert(RECENT_FILES, 0, path);
2023-07-28 19:41:57 +02:00
while(ds_list_size(RECENT_FILES) > 64)
ds_list_delete(RECENT_FILES, ds_list_size(RECENT_FILES) - 1);
2023-07-06 19:49:16 +02:00
RECENT_SAVE();
RECENT_REFRESH();
//project.path = filename_name(path);
2022-01-13 05:24:03 +01:00
}
2023-07-06 19:49:16 +02:00
project.path = path;
2023-11-23 02:32:26 +01:00
} #endregion
2023-07-06 19:49:16 +02:00
2023-11-23 02:32:26 +01:00
function SAVE_ALL() { #region
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(PROJECTS); i < n; i++ )
2023-07-06 19:49:16 +02:00
SAVE(PROJECTS[i]);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SAVE(project = PROJECT) { #region
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2023-07-06 19:49:16 +02:00
if(project.path == "" || project.readonly)
return SAVE_AS(project);
return SAVE_AT(project, project.path);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SAVE_AS(project = PROJECT) { #region
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2024-05-16 15:28:45 +02:00
var path = get_save_filename_pxc("Pixel Composer project (.pxc)|*.pxc|Compressed Pixel Composer project (.cpxc)|*.cpxc", "");
2023-02-28 09:43:01 +01:00
key_release();
2022-11-22 14:25:39 +01:00
if(path == "") return false;
2022-01-13 05:24:03 +01:00
2024-04-01 13:32:13 +02:00
if(filename_ext(path) != ".pxc" && filename_ext(path) != ".cpxc")
2022-01-13 05:24:03 +01:00
path += ".pxc";
2023-12-08 03:50:09 +01:00
if(file_exists_empty(path))
2022-11-14 03:16:15 +01:00
log_warning("SAVE", "Overrided file : " + path);
2023-07-06 19:49:16 +02:00
SAVE_AT(project, path);
SET_PATH(project, path);
2022-11-22 14:25:39 +01:00
return true;
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SAVE_AT(project = PROJECT, path = "", log = "save at ") { #region
2024-02-06 13:53:08 +01:00
CALL("save");
2024-02-04 07:33:42 +01:00
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2024-05-15 12:42:54 +02:00
IS_SAVING = true;
SAVING = true;
2023-11-27 14:50:20 +01:00
//if(TESTING && string_char_at(filename_name(path), 1) != "[")
// path = $"{filename_dir(path)}/[{VERSION_STRING}] {filename_name(path)}";
2023-03-11 01:40:17 +01:00
2023-12-08 03:50:09 +01:00
if(file_exists_empty(path)) file_delete(path);
2024-04-01 13:32:13 +02:00
var _ext = filename_ext(path);
if(_ext == ".pxc") file_text_write_all(path, save_serialize(project));
else if(_ext == ".cpxc") buffer_save(buffer_compress_string(save_serialize(project)), path);
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
SAVING = false;
2023-07-06 19:49:16 +02:00
project.readonly = false;
project.modified = false;
2022-01-13 05:24:03 +01:00
2023-02-15 10:04:49 +01:00
log_message("FILE", log + path, THEME.noti_icon_file_save);
2022-12-10 05:06:01 +01:00
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
2022-11-22 14:25:39 +01:00
return true;
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-08-04 13:12:32 +02:00
/////////////////////////////////////////////////////// COLLECTION ///////////////////////////////////////////////////////
2023-11-23 02:32:26 +01:00
function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) { #region
2023-06-13 14:42:06 +02:00
var _content = {};
2023-07-06 19:49:16 +02:00
_content.version = SAVE_VERSION;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
var _nodes = [];
2022-01-13 05:24:03 +01:00
var cx = 0;
var cy = 0;
for(var i = 0; i < ds_list_size(_list); i++) {
cx += _list[| i].x;
cy += _list[| i].y;
}
cx = round((cx / ds_list_size(_list)) / 32) * 32;
cy = round((cy / ds_list_size(_list)) / 32) * 32;
if(save_surface) {
2022-09-21 06:09:40 +02:00
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
2023-02-14 02:51:14 +01:00
if(is_surface(preview_surface)) {
2022-01-13 05:24:03 +01:00
var icon_path = string_copy(_path, 1, string_length(_path) - 5) + ".png";
2023-03-19 09:17:39 +01:00
surface_save_safe(preview_surface, icon_path);
2022-01-13 05:24:03 +01:00
}
}
for(var i = 0; i < ds_list_size(_list); i++)
2023-06-13 14:42:06 +02:00
SAVE_NODE(_nodes, _list[| i], cx, cy, true, context);
_content.nodes = _nodes;
2023-02-14 02:51:14 +01:00
2023-10-31 05:30:42 +01:00
json_save_struct(_path, _content, !PREFERENCES.save_file_minify);
2023-02-14 02:51:14 +01:00
2023-08-04 13:12:32 +02:00
if(metadata != noone) {
var _meta = metadata.serialize();
var _dir = filename_dir(_path);
var _name = filename_name_only(_path);
var _mpath = $"{_dir}/{_name}.meta";
json_save_struct(_mpath, _meta, true);
}
2022-01-13 05:24:03 +01:00
2023-03-12 02:28:21 +01:00
var pane = findPanel("Panel_Collection");
2022-12-10 05:06:01 +01:00
if(pane) pane.refreshContext();
2022-12-21 02:30:23 +01:00
log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save);
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SAVE_COLLECTION(_node, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) { #region
2022-01-13 05:24:03 +01:00
if(save_surface) {
2022-09-21 06:09:40 +02:00
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
2023-02-14 02:51:14 +01:00
if(is_surface(preview_surface)) {
2023-10-01 06:17:39 +02:00
var icon_path = string_replace(_path, filename_ext(_path), "") + ".png";
2023-03-19 09:17:39 +01:00
surface_save_safe(preview_surface, icon_path);
2022-01-13 05:24:03 +01:00
}
}
2023-06-13 14:42:06 +02:00
var _content = {};
2023-07-06 19:49:16 +02:00
_content.version = SAVE_VERSION;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
var _nodes = [];
SAVE_NODE(_nodes, _node, _node.x, _node.y, true, context);
2023-07-05 15:09:52 +02:00
_content.nodes = _nodes;
2023-02-14 02:51:14 +01:00
2023-10-31 05:30:42 +01:00
json_save_struct(_path, _content, !PREFERENCES.save_file_minify);
2023-02-14 02:51:14 +01:00
2023-08-04 13:12:32 +02:00
if(metadata != noone) {
var _meta = metadata.serialize();
var _dir = filename_dir(_path);
var _name = filename_name_only(_path);
var _mpath = $"{_dir}/{_name}.meta";
_meta.version = SAVE_VERSION;
2023-08-04 13:12:32 +02:00
json_save_struct(_mpath, _meta, true);
}
2022-01-13 05:24:03 +01:00
2023-03-12 02:28:21 +01:00
var pane = findPanel("Panel_Collection");
2022-01-25 10:58:11 +01:00
if(pane) pane.refreshContext();
2022-12-21 02:30:23 +01:00
log_message("COLLECTION", "save collection at " + _path, THEME.noti_icon_file_save);
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
2023-11-23 02:32:26 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-11-23 02:32:26 +01:00
function SAVE_NODE(_arr, _node, dx = 0, dy = 0, scale = false, context = PANEL_GRAPH.getCurrentContext()) { #region
2023-06-13 14:42:06 +02:00
if(struct_has(_node, "nodes")) {
2023-03-26 07:13:36 +02:00
for(var i = 0; i < ds_list_size(_node.nodes); i++)
2023-06-13 14:42:06 +02:00
SAVE_NODE(_arr, _node.nodes[| i], dx, dy, scale, context);
2022-01-13 05:24:03 +01:00
}
var m = _node.serialize(scale);
2023-06-13 14:42:06 +02:00
m.x -= dx;
m.y -= dy;
2023-03-26 07:13:36 +02:00
var c = context == noone? noone : context.node_id;
2023-06-13 14:42:06 +02:00
if(m.group == c) m.group = noone;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
array_push(_arr, m);
2023-11-23 02:32:26 +01:00
} #endregion