Pixel-Composer/scripts/save_function/save_function.gml

214 lines
5.4 KiB
Plaintext
Raw Normal View History

2023-03-11 01:40:17 +01:00
globalvar SAVING;
SAVING = false;
2022-01-13 05:24:03 +01:00
function NEW() {
2023-02-23 07:02:19 +01:00
if(MODIFIED && !READONLY) {
var dia = dialogCall(o_dialog_load);
dia.newFile = true;
} else
__NEW();
}
function __NEW() {
2022-01-19 06:11:17 +01:00
nodeCleanUp();
2022-01-13 05:24:03 +01:00
setPanel();
2023-02-14 13:44:46 +01:00
instance_destroy(_p_dialog);
2023-02-23 07:02:19 +01:00
//room_restart();
2022-12-10 05:06:01 +01:00
2022-01-13 05:24:03 +01:00
gc_collect();
2022-01-19 06:11:17 +01:00
SET_PATH("");
2023-02-14 02:51:14 +01:00
2023-02-23 07:02:19 +01:00
MODIFIED = false;
2023-02-14 02:51:14 +01:00
SAFE_MODE = false;
2022-01-13 05:24:03 +01:00
}
function save_serialize() {
2023-06-13 14:42:06 +02:00
var _map = {};
_map.version = SAVEFILE_VERSION;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
var _node_list = [];
2022-01-13 05:24:03 +01:00
var _key = ds_map_find_first(NODE_MAP);
repeat(ds_map_size(NODE_MAP)) {
var _node = NODE_MAP[? _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
_key = ds_map_find_next(NODE_MAP, _key);
}
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 = {};
_anim_map.frames_total = ANIMATOR.frames_total;
_anim_map.framerate = ANIMATOR.framerate;
_map.animator = _anim_map;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
_map.metadata = METADATA.serialize();
_map.global = GLOBAL.serialize();
2023-03-07 14:29:47 +01: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-06-13 14:42:06 +02:00
_addon[$ name] = PREF_MAP[? "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-06-13 14:42:06 +02:00
var val = PREF_MAP[? "save_file_minify"]? json_stringify_minify(_map) : json_stringify(_map, true);
2022-01-13 05:24:03 +01:00
return val;
}
function SET_PATH(path) {
if(path == "") {
READONLY = false;
2023-02-14 02:51:14 +01:00
} else if(!READONLY) {
var index = ds_list_find_index(RECENT_FILES, path);
if(CURRENT_PATH != path) {
if(index != -1)
ds_list_delete(RECENT_FILES, index);
ds_list_insert(RECENT_FILES, 0, path);
RECENT_SAVE();
2023-03-05 07:16:44 +01:00
RECENT_REFRESH();
2022-01-13 05:24:03 +01:00
}
2023-02-14 02:51:14 +01:00
CURRENT_PATH = filename_name(path);
2022-01-13 05:24:03 +01:00
}
CURRENT_PATH = path;
}
function SAVE() {
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2022-09-21 06:09:40 +02:00
if(CURRENT_PATH == "" || READONLY)
2022-11-22 14:25:39 +01:00
return SAVE_AS();
return SAVE_AT(CURRENT_PATH);
2022-01-13 05:24:03 +01:00
}
function SAVE_AS() {
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2023-02-28 09:43:01 +01:00
var path = get_save_filename("Pixel Composer project (.pxc)|*.pxc", "");
key_release();
2022-11-22 14:25:39 +01:00
if(path == "") return false;
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
if(filename_ext(path) != ".pxc")
2022-01-13 05:24:03 +01:00
path += ".pxc";
if(file_exists(path))
2022-11-14 03:16:15 +01:00
log_warning("SAVE", "Overrided file : " + path);
2022-01-13 05:24:03 +01:00
SAVE_AT(path);
SET_PATH(path);
2022-11-22 14:25:39 +01:00
return true;
2022-01-13 05:24:03 +01:00
}
2023-02-15 10:04:49 +01:00
function SAVE_AT(path, log = "save at ") {
2023-02-14 02:51:14 +01:00
if(DEMO) return false;
2023-03-11 01:40:17 +01:00
SAVING = true;
2022-01-13 05:24:03 +01:00
if(file_exists(path))
file_delete(path);
var file = file_text_open_write(path);
file_text_write_string(file, save_serialize());
file_text_close(file);
2023-03-11 01:40:17 +01:00
SAVING = false;
2022-01-13 05:24:03 +01:00
READONLY = false;
2022-09-27 06:37:28 +02:00
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;
2022-01-13 05:24:03 +01:00
}
2023-03-26 07:13:36 +02:00
function SAVE_COLLECTIONS(_list, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) {
2023-06-13 14:42:06 +02:00
var _content = {};
_content.version = SAVEFILE_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
if(metadata != noone)
2023-06-13 14:42:06 +02:00
_content.metadata = metadata.serialize();
2023-02-14 02:51:14 +01:00
var file = file_text_open_write(_path);
2023-06-13 14:42:06 +02:00
file_text_write_string(file, PREF_MAP[? "save_file_minify"]? json_stringify_minify(_content) : json_stringify(_content, true));
2022-01-13 05:24:03 +01:00
file_text_close(file);
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);
2022-01-13 05:24:03 +01:00
}
2023-03-26 07:13:36 +02:00
function SAVE_COLLECTION(_node, _path, save_surface = true, metadata = noone, context = PANEL_GRAPH.getCurrentContext()) {
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)) {
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
}
}
2023-06-13 14:42:06 +02:00
var _content = {};
_content.version = SAVEFILE_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);
_content.nodes = nodes;
2023-02-14 02:51:14 +01:00
if(metadata != noone)
2023-06-13 14:42:06 +02:00
_content.metadata = metadata.serialize();
2023-02-14 02:51:14 +01:00
var file = file_text_open_write(_path);
2023-06-13 14:42:06 +02:00
file_text_write_string(file, PREF_MAP[? "save_file_minify"]? json_stringify_minify(_content) : json_stringify(_content, true));
2022-01-13 05:24:03 +01:00
file_text_close(file);
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);
2022-01-13 05:24:03 +01:00
}
2023-06-13 14:42:06 +02:00
function SAVE_NODE(_arr, _node, dx = 0, dy = 0, scale = false, context = PANEL_GRAPH.getCurrentContext()) {
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);
2022-01-13 05:24:03 +01:00
}