Pixel-Composer/scripts/sprite_loader/sprite_loader.gml

97 lines
2.6 KiB
Plaintext
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-04-08 20:06:27 +02:00
var _l = root + "/version";
if(file_exists(_l))
2023-05-07 20:55:13 +02:00
var res = json_load_struct(_l);
2023-05-08 10:50:42 +02:00
json_save_struct(_l, { version: BUILD_NUMBER });
2022-12-19 13:35:30 +01:00
2023-10-18 14:58:55 +02:00
log_message("THEME", $"unzipping default theme to {root}.");
2023-04-11 13:34:01 +02:00
zip_unzip("data/themes/default.zip", root);
2023-10-24 02:09:04 +02:00
2023-10-31 05:30:42 +01:00
loadGraphic(PREFERENCES.theme);
loadColor(PREFERENCES.theme);
2022-11-22 14:25:39 +01:00
}
2022-11-18 03:20:31 +01:00
function _sprite_path(rel, theme) {
2023-11-08 08:38:04 +01:00
INLINE
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) {
2023-11-08 08:38:04 +01:00
INLINE
2022-11-18 03:20:31 +01:00
var path = _sprite_path(str.path, theme);
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
2023-06-10 13:59:45 +02:00
if(struct_has(str.slice, "tilemode"))
slice.tilemode = str.slice.tilemode;
2023-09-07 20:59:14 +02:00
if(s >= 0) sprite_set_nineslice(s, slice);
else log_message("THEME", $"Load sprite {path} failed.");
2022-11-18 03:20:31 +01:00
}
return s;
}
function __getGraphicList() {
2023-11-08 08:38:04 +01:00
INLINE
2022-11-18 03:20:31 +01:00
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
}
2023-10-24 02:09:04 +02:00
function loadGraphic(theme = "default") {
2022-11-18 03:20:31 +01:00
var sprDef = __getGraphicList();
2023-10-27 13:55:31 +02:00
var _metaP = $"{DIRECTORY}Themes/{theme}/meta.json";
if(!file_exists(_metaP))
noti_warning("Loading theme made for older version.");
else {
var _meta = json_load_struct(_metaP);
if(_meta[$ "version"] < VERSION)
noti_warning("Loading theme made for older version.");
}
var path = _sprite_path("./graphics.json", theme);
2022-11-18 03:20:31 +01:00
print($"Loading theme {theme}");
2022-11-18 03:20:31 +01:00
if(!file_exists(path)) {
2023-10-27 13:55:31 +02:00
print("Theme not defined at " + path + ", rollback to default theme.");
2022-11-18 03:20:31 +01:00
return;
}
var s = file_text_read_all(path);
2022-11-18 03:20:31 +01:00
var graphics = variable_struct_get_names(sprDef);
var sprStr = json_try_parse(s);
var str;
2022-11-18 03:20:31 +01:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(graphics); i < n; i++ ) {
2022-11-18 03:20:31 +01:00
var key = graphics[i];
2023-10-27 13:55:31 +02:00
//if(struct_has(THEME, key) && sprite_exists(THEME[$ key]))
// sprite_delete(THEME[$ key]);
if(struct_has(sprStr, key)) {
str = sprStr[$ key];
THEME[$ key] = _sprite_load_from_struct(str, theme, key);
2022-11-18 03:20:31 +01:00
} else {
noti_status($"Graphic resource for {key} not found. Rollback to default directory.");
2022-11-18 03:20:31 +01:00
str = sprDef[$ key];
THEME[$ key] = _sprite_load_from_struct(str, "default", key);
2022-11-18 03:20:31 +01:00
}
//print($"{key}: {THEME[$ key]} [{sprite_exists(THEME[$ key])}]");
2022-11-18 03:20:31 +01:00
}
}