Pixel-Composer/scripts/sprite_loader/sprite_loader.gml

81 lines
2.4 KiB
Text
Raw Normal View History

2024-07-16 17:38:05 +07:00
globalvar THEME_DEF; THEME_DEF = true;
2023-12-11 15:59:58 +07:00
2024-07-16 17:38:05 +07:00
function __initTheme() {
2022-11-22 20:25:39 +07:00
var root = DIRECTORY + "Themes";
2024-07-16 17:38:05 +07:00
var t = get_timer();
2022-12-19 19:35:30 +07:00
directory_verify(root);
2024-04-08 12:13:46 +07:00
if(check_version($"{root}/version")) {
2024-07-16 17:38:05 +07:00
zip_unzip("data/Theme.zip", root); print($" > Unzip theme | complete in {get_timer() - t}"); t = get_timer();
}
2023-10-24 07:09:04 +07:00
2024-07-16 17:38:05 +07:00
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();
}
2022-11-22 20:25:39 +07:00
2024-07-16 17:38:05 +07:00
function _sprite_path(rel, theme) { INLINE return $"{DIRECTORY}Themes/{theme}/graphics/{string_replace_all(rel, "./", "")}"; }
2022-11-18 09:20:31 +07:00
2024-07-16 17:38:05 +07:00
function _sprite_load_from_struct(str, theme, key) {
2022-11-18 09:20:31 +07:00
var path = _sprite_path(str.path, theme);
2024-07-16 17:38:05 +07:00
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 11:06:01 +07: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 11:23:46 +07:00
if(struct_has(str, "slicemode"))
slice.tilemode = array_create(5, str.slicemode);
sprite_set_nineslice(s, slice);
2022-11-18 09:20:31 +07:00
}
2022-11-18 09:20:31 +07:00
return s;
2024-07-16 17:38:05 +07:00
}
2022-11-18 09:20:31 +07:00
2024-07-16 17:38:05 +07:00
function loadGraphic(theme = "default") {
THEME = new Theme();
if(theme == "default") return;
2023-12-11 15:59:58 +07:00
2024-07-16 17:38:05 +07:00
var path = _sprite_path("./graphics.json", theme);
2023-12-11 15:59:58 +07:00
var sprDef = json_load_struct(_sprite_path("./graphics.json", "default"));
2023-10-27 18:55:31 +07:00
var _metaP = $"{DIRECTORY}Themes/{theme}/meta.json";
2023-12-08 09:50:09 +07:00
if(!file_exists_empty(_metaP))
2023-10-27 18:55:31 +07: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 09:20:31 +07:00
print($"Loading theme {theme}");
2024-07-16 17:38:05 +07:00
if(!file_exists_empty(path)) { print($"Theme not defined at {path}, rollback to default theme."); return; }
2022-11-18 09:20:31 +07:00
2023-12-11 15:59:58 +07:00
var sprStr = json_load_struct(path);
2024-07-16 17:38:05 +07:00
var graphics = variable_struct_get_names(sprStr);
var str, key;
2022-11-18 09:20:31 +07:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(graphics); i < n; i++ ) {
2024-07-16 17:38:05 +07:00
key = graphics[i];
str = sprStr[$ key];
THEME[$ key] = _sprite_load_from_struct(str, theme, key);
2022-11-18 09:20:31 +07:00
}
2024-07-16 17:38:05 +07:00
}