Pixel-Composer/scripts/sprite_loader/sprite_loader.gml

121 lines
3.2 KiB
Plaintext
Raw Normal View History

2023-12-11 09:59:58 +01:00
globalvar THEME_DEF;
THEME_DEF = true;
function __initTheme() { #region
2022-11-22 14:25:39 +01:00
var root = DIRECTORY + "Themes";
2022-12-19 13:35:30 +01:00
directory_verify(root);
2024-04-08 07:13:46 +02:00
if(check_version($"{root}/version")) {
log_message("THEME", $"unzipping default theme to {root}.");
2023-12-12 08:20:05 +01:00
zip_unzip("data/Theme.zip", root);
}
2023-10-24 02:09:04 +02:00
2023-12-11 09:59:58 +01:00
var t = get_timer();
loadGraphic(PREFERENCES.theme); print($" > Load graphic | complete in {get_timer() - t}"); t = get_timer();
loadColor(PREFERENCES.theme); print($" > Load color | complete in {get_timer() - t}"); t = get_timer();
} #endregion
2022-11-22 14:25:39 +01:00
function _sprite_path(rel, theme) { #region
2023-11-08 08:38:04 +01:00
INLINE
2023-12-08 03:50:09 +01:00
return $"{DIRECTORY}Themes/{theme}/graphics/{string_replace_all(rel, "./", "")}";
} #endregion
2022-11-18 03:20:31 +01:00
function _sprite_load_from_struct(str, theme, key) { #region
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.s, false, true, str.x, str.y);
if(s < 0) {
log_message("THEME", $"Load sprite {path} failed.");
return 0;
}
2022-12-10 05:06:01 +01:00
if(struct_has(str, "slice")) {
var slice = sprite_nineslice_create();
slice.enabled = true;
2023-06-10 13:59:45 +02:00
if(is_array(str.slice)) {
slice.left = str.slice[0];
slice.right = str.slice[1];
slice.top = str.slice[2];
slice.bottom = str.slice[3];
} else if(is_real(str.slice)) {
slice.left = str.slice;
slice.right = str.slice;
slice.top = str.slice;
slice.bottom = str.slice;
}
2024-07-01 06:23:46 +02:00
if(struct_has(str, "slicemode"))
slice.tilemode = array_create(5, str.slicemode);
sprite_set_nineslice(s, slice);
2022-11-18 03:20:31 +01:00
}
2022-11-18 03:20:31 +01:00
return s;
} #endregion
2022-11-18 03:20:31 +01:00
function loadGraphic(theme = "default") { #region
2023-12-11 09:59:58 +01:00
var path = _sprite_path("./graphics.json", theme);
if(THEME_DEF) {
var sprStr = json_load_struct(path);
var graphics = variable_struct_get_names(sprStr);
var str;
for( var i = 0, n = array_length(graphics); i < n; i++ ) {
var key = graphics[i];
if(struct_has(sprStr, key)) {
str = sprStr[$ key];
THEME[$ key] = _sprite_load_from_struct(str, theme, key);
} else {
noti_status($"Graphic resource for {key} not found. Rollback to default directory.");
str = sprDef[$ key];
THEME[$ key] = _sprite_load_from_struct(str, "default", key);
}
}
return;
}
var sprDef = json_load_struct(_sprite_path("./graphics.json", "default"));
2023-10-27 13:55:31 +02:00
var _metaP = $"{DIRECTORY}Themes/{theme}/meta.json";
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(_metaP))
2023-10-27 13:55:31 +02:00
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.");
}
2022-11-18 03:20:31 +01:00
print($"Loading theme {theme}");
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(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 graphics = variable_struct_get_names(sprDef);
2023-12-11 09:59:58 +01:00
var sprStr = json_load_struct(path);
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];
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
}
}
} #endregion