Pixel-Composer/scripts/save_function/save_function.gml
2024-06-09 11:27:50 +07:00

205 lines
No EOL
5.7 KiB
Text

globalvar SAVING, IS_SAVING;
SAVING = false;
IS_SAVING = false;
function NEW() { #region
CALL("new");
PROJECT = new Project();
array_push(PROJECTS, PROJECT);
var graph = new Panel_Graph(PROJECT);
PANEL_GRAPH.panel.setContent(graph, true);
PANEL_GRAPH = graph;
} #endregion
function save_serialize(project = PROJECT, _outMap = false) { #region
var _map = project.serialize();
if(_outMap) return _map;
return PREFERENCES.save_file_minify? json_stringify_minify(_map) : json_stringify(_map, true);
} #endregion
function SET_PATH(project, path) { #region
if(path == "") {
project.readonly = false;
} else if(!project.readonly) {
ds_list_remove(RECENT_FILES, path);
ds_list_insert(RECENT_FILES, 0, path);
while(ds_list_size(RECENT_FILES) > 64)
ds_list_delete(RECENT_FILES, ds_list_size(RECENT_FILES) - 1);
RECENT_SAVE();
RECENT_REFRESH();
//project.path = filename_name(path);
}
project.path = path;
} #endregion
function SAVE_ALL() { #region
for( var i = 0, n = array_length(PROJECTS); i < n; i++ )
SAVE(PROJECTS[i]);
} #endregion
function SAVE(project = PROJECT) { #region
if(DEMO) return false;
if(project.path == "" || project.readonly || path_is_backup(project.path))
return SAVE_AS(project);
return SAVE_AT(project, project.path);
} #endregion
function SAVE_AS(project = PROJECT) { #region
if(DEMO) return false;
var path = get_save_filename_pxc("Pixel Composer project (.pxc)|*.pxc|Compressed Pixel Composer project (.cpxc)|*.cpxc", "");
key_release();
if(path == "") return false;
if(!path_is_project(path, false))
path = filename_name_only(path) + ".pxc";
if(file_exists_empty(path))
log_warning("SAVE", "Overrided file : " + path);
SAVE_AT(project, path);
SET_PATH(project, path);
return true;
} #endregion
function SAVE_AT(project = PROJECT, path = "", log = "save at ") { #region
CALL("save");
if(DEMO) return false;
IS_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) {
for(var i = PREFERENCES.save_backup - 1; i >= 0; i--) {
var _p = path;
if(i) _p = $"{path}{string(i)}"
if(file_exists(_p))
file_rename(_p, $"{path}{string(i + 1)}");
}
}
if(file_exists_empty(path)) file_delete(path);
var _ext = filename_ext_raw(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);
SAVING = false;
project.readonly = false;
project.modified = false;
log_message("FILE", log + path, THEME.noti_icon_file_save);
PANEL_MENU.setNotiIcon(THEME.noti_icon_file_save);
return true;
} #endregion
/////////////////////////////////////////////////////// COLLECTION ///////////////////////////////////////////////////////
function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) { #region
var _content = {};
_content.version = SAVE_VERSION;
var _nodes = [];
var cx = 0;
var cy = 0;
var amo = array_length(_list);
for(var i = 0; i < amo; i++) {
cx += _list[i].x;
cy += _list[i].y;
}
cx = round((cx / amo) / 32) * 32;
cy = round((cy / amo) / 32) * 32;
if(save_surface) {
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
if(is_surface(preview_surface)) {
var icon_path = string_copy(_path, 1, string_length(_path) - 5) + ".png";
surface_save_safe(preview_surface, icon_path);
}
}
for(var i = 0; i < amo; i++)
SAVE_NODE(_nodes, _list[i], cx, cy, true, context);
_content.nodes = _nodes;
json_save_struct(_path, _content, !PREFERENCES.save_file_minify);
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);
}
var pane = findPanel("Panel_Collection");
if(pane) pane.refreshContext();
log_message("COLLECTION", "save collection at " + _path, 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
if(save_surface) {
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
if(is_surface(preview_surface)) {
var icon_path = string_replace(_path, filename_ext(_path), "") + ".png";
surface_save_safe(preview_surface, icon_path);
}
}
var _content = {};
_content.version = SAVE_VERSION;
var _nodes = [];
SAVE_NODE(_nodes, _node, _node.x, _node.y, true, context);
_content.nodes = _nodes;
json_save_struct(_path, _content, !PREFERENCES.save_file_minify);
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;
json_save_struct(_mpath, _meta, true);
}
var pane = findPanel("Panel_Collection");
if(pane) pane.refreshContext();
log_message("COLLECTION", "save collection at " + _path, 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
if(struct_has(_node, "nodes")) {
for(var i = 0; i < array_length(_node.nodes); i++)
SAVE_NODE(_arr, _node.nodes[i], dx, dy, scale, context);
}
var m = _node.serialize(scale);
m.x -= dx;
m.y -= dy;
var c = context == noone? noone : context.node_id;
if(m.group == c) m.group = noone;
array_push(_arr, m);
} #endregion