Pixel-Composer/scripts/directory_object/directory_object.gml

244 lines
7.1 KiB
Text
Raw Normal View History

function FileObject(_name, _path) constructor { #region
2023-08-06 16:00:59 +02:00
static loadThumbnailAsync = false;
2022-09-27 06:37:28 +02:00
name = _name;
path = _path;
2023-10-04 11:28:13 +02:00
spr_path = [];
2023-08-04 13:12:32 +02:00
spr = -1;
sprFetchID = noone;
content = -1;
var _mdir = filename_dir(path);
var _mname = filename_name_only(path);
meta_path = $"{_mdir}/{_mname}.meta";
meta = noone;
type = FILE_TYPE.collection;
2023-03-25 12:53:29 +01:00
2024-06-03 09:34:59 +02:00
switch(filename_ext_raw(path)) { #region
case "png" :
case "jpg" :
case "gif" :
2023-03-25 12:53:29 +01:00
type = FILE_TYPE.assets;
break;
2024-06-03 09:34:59 +02:00
case "pxc" :
case "cpxc" :
2023-03-25 12:53:29 +01:00
type = FILE_TYPE.project;
break;
} #endregion
2023-01-17 08:11:55 +01:00
2023-10-04 11:28:13 +02:00
retrive_data = false;
thumbnail_data = -1;
thumbnail = noone;
size = file_size(path);
2023-03-05 07:16:44 +01:00
2023-06-05 18:27:53 +02:00
static getName = function() { return name; }
static getThumbnail = function() { #region
2024-02-03 13:11:50 +01:00
if(thumbnail != noone && is_surface(thumbnail)) return thumbnail; // Thumbnail loaded
2023-03-05 07:16:44 +01:00
2024-02-03 13:11:50 +01:00
if(size > 100000) return noone; // File too large
if(!retrive_data) getMetadata(); // Metadata not loaded
if(thumbnail_data == -1) return noone; // Metadata does not contains thumbnail
2023-03-05 07:16:44 +01:00
thumbnail = surface_decode(thumbnail_data);
2024-02-03 13:11:50 +01:00
return thumbnail;
} #endregion
2023-03-05 07:16:44 +01:00
static getSpr = function() { #region
2024-06-09 06:27:50 +02:00
if(spr != -1 && sprite_exists(spr))
return spr;
2023-08-04 13:12:32 +02:00
if(sprFetchID != noone) return -1;
2024-06-09 06:27:50 +02:00
if(array_empty(spr_path)) {
2023-08-06 16:00:59 +02:00
if(loadThumbnailAsync) {
sprFetchID = sprite_add_ext(self.path, 0, 0, 0, true);
IMAGE_FETCH_MAP[? sprFetchID] = function(load_result) {
spr = load_result[? "id"];
2023-09-07 20:59:14 +02:00
if(spr) sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
2023-08-06 16:00:59 +02:00
};
} else {
2024-02-03 13:11:50 +01:00
spr = sprite_add(self.path, 0, 0, 0, 0, 0);
2023-09-07 20:59:14 +02:00
if(spr) sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
2023-08-06 16:00:59 +02:00
}
2024-06-09 06:27:50 +02:00
2023-03-26 07:13:36 +02:00
return spr;
}
2024-03-31 05:36:11 +02:00
var path = array_safe_get_fast(spr_path, 0);
var amo = array_safe_get_fast(spr_path, 1);
2023-01-17 08:11:55 +01:00
2024-02-03 13:11:50 +01:00
if(!file_exists_empty(path)) return -1;
2023-08-04 13:12:32 +02:00
2023-08-06 16:00:59 +02:00
if(loadThumbnailAsync) {
sprFetchID = sprite_add_ext(path, amo, 0, 0, true);
IMAGE_FETCH_MAP[? sprFetchID] = function(load_result) {
spr = load_result[? "id"];
2024-03-31 05:36:11 +02:00
if(spr && array_safe_get_fast(spr_path, 2))
2023-08-06 16:00:59 +02:00
sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
2023-09-07 20:59:14 +02:00
}
2023-08-06 16:00:59 +02:00
} else {
2024-02-03 13:11:50 +01:00
spr = sprite_add(path, amo, 0, 0, 0, 0);
2024-03-31 05:36:11 +02:00
if(spr && array_safe_get_fast(spr_path, 2))
2023-09-07 20:59:14 +02:00
sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
2023-08-06 16:00:59 +02:00
}
2023-08-04 13:12:32 +02:00
2023-01-17 08:11:55 +01:00
return spr;
} #endregion
2023-02-14 07:37:13 +01:00
2024-01-10 06:36:33 +01:00
static getMetadata = function(_createnew = false) { #region
2023-03-05 07:16:44 +01:00
retrive_data = true;
2023-02-14 07:37:13 +01:00
2024-02-03 13:11:50 +01:00
if(meta != noone) return meta;
if(meta == undefined) return noone;
if(!file_exists_empty(path)) return noone;
2023-02-14 07:37:13 +01:00
2023-02-15 10:43:24 +01:00
meta = new MetaDataManager();
2023-12-08 03:50:09 +01:00
if(file_exists_empty(meta_path)) {
2023-08-10 11:51:16 +02:00
meta.deserialize(json_load_struct(meta_path));
2023-08-04 13:12:32 +02:00
} else {
2023-08-10 11:51:16 +02:00
var m = json_load_struct(path);
2023-08-04 13:12:32 +02:00
2023-08-12 09:41:13 +02:00
if(struct_has(m, "metadata")) meta.deserialize(m.metadata);
if(struct_has(m, "preview")) thumbnail_data = json_try_parse(m.preview, -1);
2024-01-10 06:36:33 +01:00
if(_createnew) json_save_struct(meta_path, meta);
2023-08-04 13:12:32 +02:00
}
2023-02-14 07:37:13 +01:00
meta.name = name;
2024-06-03 09:34:59 +02:00
switch(filename_ext_raw(path)) {
case "pxc" :
case "cpxc" : meta.type = FILE_TYPE.project; break;
case "pxcc" : meta.type = FILE_TYPE.collection; break;
default : meta.type = FILE_TYPE.assets; break;
2023-02-23 07:02:19 +01:00
}
2023-02-14 07:37:13 +01:00
return meta;
} #endregion
} #endregion
2022-09-27 06:37:28 +02:00
function DirectoryObject(name, path) constructor { #region
2022-09-27 06:37:28 +02:00
self.name = name;
self.path = path;
2024-02-03 13:11:50 +01:00
subDir = ds_list_create();
content = ds_list_create();
open = false;
triggered = false;
2024-02-03 13:11:50 +01:00
scanned = false;
2022-09-27 06:37:28 +02:00
2023-06-05 18:27:53 +02:00
static destroy = function() { ds_list_destroy(subDir); }
static getName = function() { return name; }
2022-09-27 06:37:28 +02:00
static scan = function(file_type) { #region
2023-12-11 09:59:58 +01:00
scanned = true;
2024-02-03 13:11:50 +01:00
2023-11-25 08:54:35 +01:00
var _temp_name = [];
2024-02-03 13:11:50 +01:00
var _file = file_find_first(path + "/*", fa_directory);
2023-11-25 08:54:35 +01:00
while(_file != "") {
array_push(_temp_name, _file);
_file = file_find_next();
2022-09-27 06:37:28 +02:00
}
file_find_close();
ds_list_clear(subDir);
ds_list_clear(content);
2023-11-25 08:54:35 +01:00
array_sort(_temp_name, true);
for( var i = 0; i < array_length(_temp_name); i++ ) {
var file = _temp_name[i];
var _path = path + "/" + file;
2022-09-27 06:37:28 +02:00
2023-11-25 08:54:35 +01:00
if(directory_exists(_path)) {
var _fol_path = _path;
2024-02-03 13:11:50 +01:00
var fol = new DirectoryObject(file, _fol_path);
2022-09-27 06:37:28 +02:00
fol.scan(file_type);
ds_list_add(subDir, fol);
2024-02-03 13:11:50 +01:00
2022-09-27 06:37:28 +02:00
} else if(array_exists(file_type, filename_ext(file))) {
2023-11-25 08:54:35 +01:00
var f = new FileObject(string_replace(file, filename_ext(file), ""), _path);
2022-09-27 06:37:28 +02:00
ds_list_add(content, f);
2023-02-14 07:37:13 +01:00
if(string_lower(filename_ext(file)) == ".png") {
2023-11-25 08:54:35 +01:00
var icon_path = _path;
2022-09-27 06:37:28 +02:00
var amo = 1;
var p = string_pos("strip", icon_path);
2023-07-17 19:58:33 +02:00
if(p) {
var _amo = string_copy(icon_path, p, string_length(icon_path) - p + 1);
_amo = string_digits(_amo);
2024-02-03 13:11:50 +01:00
amo = toNumber(_amo);
2023-07-17 19:58:33 +02:00
}
2023-01-17 08:11:55 +01:00
f.spr_path = [icon_path, amo, false];
2024-02-03 13:11:50 +01:00
2022-09-27 06:37:28 +02:00
} else {
2023-03-08 12:14:01 +01:00
var icon_path = path + "/" + filename_change_ext(file, ".png");
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(icon_path)) continue;
2022-09-27 06:37:28 +02:00
var _temp = sprite_add(icon_path, 0, false, false, 0, 0);
2024-02-03 13:11:50 +01:00
var ww = sprite_get_width(_temp);
var hh = sprite_get_height(_temp);
var amo = safe_mod(ww, hh) == 0? ww / hh : 1;
2022-09-27 06:37:28 +02:00
sprite_delete(_temp);
2022-11-01 03:06:03 +01:00
2023-01-17 08:11:55 +01:00
f.spr_path = [icon_path, amo, true];
2022-09-27 06:37:28 +02:00
}
}
}
} #endregion
2022-09-27 06:37:28 +02:00
2024-05-22 04:46:29 +02:00
static draw = function(parent, _x, _y, _m, _w, _hover, _focus, _homedir, _params = {}) { #region
2022-11-03 11:44:49 +01:00
var hg = ui(28);
2022-09-27 06:37:28 +02:00
var hh = 0;
2024-05-22 04:46:29 +02:00
var font = struct_try_get(_params, "font", f_p0);
2022-11-03 11:44:49 +01:00
if(!ds_list_empty(subDir) && _hover && point_in_rectangle(_m[0], _m[1], _x, _y, ui(32), _y + hg - 1)) {
2023-11-13 15:42:14 +01:00
draw_sprite_stretched_ext(THEME.ui_panel_bg, 0, _x, _y, ui(32), hg, CDEF.main_white, 1);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, _focus))
2022-09-27 06:37:28 +02:00
open = !open;
}
2022-11-03 11:44:49 +01:00
if(_hover && point_in_rectangle(_m[0], _m[1], _x + ui(32), _y, _w, _y + hg - 1)) {
2023-11-13 15:42:14 +01:00
draw_sprite_stretched_ext(THEME.ui_panel_bg, 0, _x + ui(28), _y, _w - ui(36), hg, CDEF.main_white, 1);
if(!triggered && mouse_click(mb_left, _focus)) {
2022-12-10 05:06:01 +01:00
if(!ds_list_empty(subDir))
open = !open;
parent.setContext(parent.context == self? _homedir : self);
triggered = true;
2022-09-27 06:37:28 +02:00
}
2023-11-25 08:54:35 +01:00
} else if(_hover)
triggered = false;
if(triggered && mouse_release(mb_left))
triggered = false;
2022-09-27 06:37:28 +02:00
2023-11-13 07:11:52 +01:00
if(ds_list_empty(subDir)) draw_sprite_ui_uniform(THEME.folder_content, parent.context == self, _x + ui(16), _y + hg / 2 - 1, 1, COLORS.collection_folder_empty);
else draw_sprite_ui_uniform(THEME.folder_content, open, _x + ui(16), _y + hg / 2 - 1, 1, COLORS.collection_folder_nonempty);
2023-06-10 13:59:45 +02:00
2024-05-22 04:46:29 +02:00
draw_set_text(font, fa_left, fa_center, path == parent.context.path? COLORS._main_text_accent : COLORS._main_text_inner);
2023-11-13 15:42:14 +01:00
draw_text_add(_x + ui(32), _y + hg / 2, name);
2022-09-27 06:37:28 +02:00
hh += hg;
_y += hg;
if(open && !ds_list_empty(subDir)) {
var l_y = _y;
for(var i = 0; i < ds_list_size(subDir); i++) {
2024-05-22 04:46:29 +02:00
var _hg = subDir[| i].draw(parent, _x + ui(16), _y, _m, _w - ui(16), _hover, _focus, _homedir, _params);
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.collection_tree_line);
2022-11-03 11:44:49 +01:00
draw_line(_x + ui(12), _y + hg / 2, _x + ui(16), _y + hg / 2);
2022-09-27 06:37:28 +02:00
hh += _hg;
_y += _hg;
}
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS.collection_tree_line);
2022-11-03 11:44:49 +01:00
draw_line(_x + ui(12), l_y, _x + ui(12), _y - hg / 2);
2022-09-27 06:37:28 +02:00
}
return hh;
} #endregion
} #endregion