Pixel-Composer/scripts/sprite_loader/sprite_loader.gml

69 lines
2 KiB
Text
Raw Normal View History

2023-03-21 03:01:53 +01:00
function __initTheme() {
2022-11-22 14:25:39 +01:00
var root = DIRECTORY + "Themes";
if(!directory_exists(root))
directory_create(root);
2023-03-08 12:14:01 +01:00
//var _l = root + "/version";
2023-02-14 02:51:14 +01:00
//if(file_exists(_l)) {
// var res = json_load_struct(_l);
// if(res.version >= VERSION) return;
//}
//json_save_struct(_l, { version: VERSION });
2022-12-19 13:35:30 +01:00
2022-12-18 03:20:38 +01:00
log_message("THEME", "unzipping default theme to DIRECTORY.");
2022-12-10 05:06:01 +01:00
zip_unzip("data/themes/default.zip", root);
2022-11-22 14:25:39 +01:00
}
2022-11-18 03:20:31 +01:00
function _sprite_path(rel, theme) {
2022-11-22 14:25:39 +01:00
return DIRECTORY + "themes/" + theme + "/graphics/" + string_replace_all(rel, "./", "");
2022-11-18 03:20:31 +01:00
}
function _sprite_load_from_struct(str, theme, key) {
var path = _sprite_path(str.path, theme);
2023-01-25 06:49:00 +01:00
var s = sprite_add(path, str.subimages, false, true, str.xorigin, str.yorigin);
2022-11-18 03:20:31 +01:00
if(str.slice) {
var slice = sprite_nineslice_create();
slice.enabled = str.slice.enabled;
slice.left = str.slice.left;
slice.right = str.slice.right;
slice.top = str.slice.top;
slice.bottom = str.slice.bottom;
2022-12-10 05:06:01 +01:00
2022-11-18 03:20:31 +01:00
sprite_set_nineslice(s, slice);
}
return s;
}
function __getGraphicList() {
var path = _sprite_path("./graphics.json", "default");
2023-02-28 09:43:01 +01:00
var s = file_text_read_all(path);
2023-03-05 07:16:44 +01:00
return json_try_parse(s);
2022-11-18 03:20:31 +01:00
}
function loadGraphic(theme = "default") {
var sprDef = __getGraphicList();
var path = _sprite_path("./graphics.json", theme);
if(!file_exists(path)) {
noti_status("Theme not defined at " + path + ", rollback to default theme.");
return;
}
2023-02-28 09:43:01 +01:00
var s = file_text_read_all(path);
2022-11-18 03:20:31 +01:00
var graphics = variable_struct_get_names(sprDef);
2023-03-05 07:16:44 +01:00
var sprStr = json_try_parse(s);
2022-11-18 03:20:31 +01:00
for( var i = 0; i < array_length(graphics); i++ ) {
var key = graphics[i];
if(variable_struct_exists(sprStr, key)) {
var str = variable_struct_get(sprStr, key);
variable_struct_set(THEME, key, _sprite_load_from_struct(str, theme, key));
} else {
noti_status("Graphic resource for " + string(key) + " not found. Rollback to default directory.");
var str = variable_struct_get(sprDef, key);
variable_struct_set(THEME, key, _sprite_load_from_struct(str, "default", key));
}
}
}