Pixel-Composer/scripts/save_function/save_function.gml

172 lines
4.3 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
function NEW() {
2022-01-19 06:11:17 +01:00
nodeCleanUp();
2022-01-13 05:24:03 +01:00
setPanel();
room_restart();
gc_collect();
2022-01-19 06:11:17 +01:00
SET_PATH("");
2022-01-13 05:24:03 +01:00
}
function save_serialize() {
var _map = ds_map_create();
_map[? "version"] = SAVEFILE_VERSION;
var _node_list = ds_list_create();
var _key = ds_map_find_first(NODE_MAP);
repeat(ds_map_size(NODE_MAP)) {
var _node = NODE_MAP[? _key];
if(_node.active) {
ds_list_add_map(_node_list, _node.serialize());
}
_key = ds_map_find_next(NODE_MAP, _key);
}
ds_map_add_list(_map, "nodes", _node_list);
var _anim_map = ds_map_create();
_anim_map[? "frames_total"] = ANIMATOR.frames_total;
_anim_map[? "framerate"] = ANIMATOR.framerate;
ds_map_add_map(_map, "animator", _anim_map);
var val = json_encode(_map);
ds_map_destroy(_map);
return val;
}
function SET_PATH(path) {
if(path == "") {
READONLY = false;
window_set_caption("Pixel Composer");
} else {
if(READONLY)
window_set_caption("[READ ONLY] " + filename_name(path) + " - Pixel Composer");
else {
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();
}
2022-09-27 06:37:28 +02:00
CURRENT_PATH = filename_name(path);
window_set_caption(CURRENT_PATH + " - Pixel Composer");
2022-01-13 05:24:03 +01:00
}
}
CURRENT_PATH = path;
}
function SAVE() {
2022-09-21 06:09:40 +02:00
if(CURRENT_PATH == "" || READONLY)
2022-01-13 05:24:03 +01:00
SAVE_AS();
2022-09-21 06:09:40 +02:00
else
2022-01-13 05:24:03 +01:00
SAVE_AT(CURRENT_PATH);
}
function SAVE_AS() {
var path = get_save_filename(".pxc", "");
if(path == "") return;
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))
PANEL_MENU.addNotiExtra("Overrided file : " + path);
SAVE_AT(path);
SET_PATH(path);
}
function SAVE_AT(path) {
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);
READONLY = false;
2022-09-27 06:37:28 +02:00
MODIFIED = false;
2022-01-13 05:24:03 +01:00
log_message("FILE", "save at " + path);
2022-01-19 03:05:13 +01:00
PANEL_MENU.showNoti("File saved", s_noti_icon_file_save);
2022-01-13 05:24:03 +01:00
}
function SAVE_COLLECTIONS(_list, _path, save_surface = true) {
var file = file_text_open_write(_path);
var _map = ds_map_create();
_map[? "version"] = SAVEFILE_VERSION;
var _node_list = ds_list_create();
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();
if(preview_surface && is_surface(preview_surface)) {
2022-01-13 05:24:03 +01:00
var icon_path = string_copy(_path, 1, string_length(_path) - 5) + ".png";
2022-09-21 06:09:40 +02:00
surface_save(preview_surface, icon_path);
2022-01-13 05:24:03 +01:00
}
}
for(var i = 0; i < ds_list_size(_list); i++)
SAVE_NODE(_node_list, _list[| i], cx, cy, true);
ds_map_add_list(_map, "nodes", _node_list);
file_text_write_string(file, json_encode(_map));
file_text_close(file);
ds_map_destroy(_map);
var pane = findPanel("Panel_Collection", PANEL_MAIN, noone);
if(pane) pane.searchContent();
2022-01-13 05:24:03 +01:00
}
function SAVE_COLLECTION(_node, _path, save_surface = true) {
if(save_surface) {
2022-09-21 06:09:40 +02:00
var preview_surface = PANEL_PREVIEW.getNodePreviewSurface();
if(preview_surface && is_surface(preview_surface)) {
2022-01-13 05:24:03 +01:00
var icon_path = string_copy(_path, 1, string_length(_path) - 5) + ".png";
2022-09-21 06:09:40 +02:00
surface_save(preview_surface, icon_path);
2022-01-13 05:24:03 +01:00
}
}
var file = file_text_open_write(_path);
var _map = ds_map_create();
_map[? "version"] = SAVEFILE_VERSION;
var _node_list = ds_list_create();
SAVE_NODE(_node_list, _node, _node.x, _node.y, true);
ds_map_add_list(_map, "nodes", _node_list);
file_text_write_string(file, json_encode(_map));
file_text_close(file);
ds_map_destroy(_map);
var pane = findPanel("Panel_Collection", PANEL_MAIN, noone);
2022-01-25 10:58:11 +01:00
if(pane) pane.refreshContext();
2022-01-13 05:24:03 +01:00
}
function SAVE_NODE(_list, _node, dx = 0, dy = 0, scale = false) {
if(variable_struct_exists(_node, "nodes")) {
for(var i = 0; i < ds_list_size(_node.nodes); i++) {
var _n = _node.nodes[| i];
SAVE_NODE(_list, _n, dx, dy, scale);
}
}
var m = _node.serialize(scale);
m[? "x"] -= dx;
m[? "y"] -= dy;
var c = PANEL_GRAPH.getCurrentContext();
if(c != -1) c = c.node_id;
if(m[? "group"] == c)
m[? "group"] = -1;
ds_list_add(_list, m);
ds_list_mark_as_map(_list, ds_list_size(_list) - 1);
}