Pixel-Composer/scripts/directory_object/directory_object.gml

205 lines
5.7 KiB
Plaintext
Raw Normal View History

2022-09-27 06:37:28 +02:00
function FileObject(_name, _path) constructor {
name = _name;
path = _path;
2023-01-17 08:11:55 +01:00
spr_path = [];
spr = -1;
content = -1;
surface = noone;
2023-02-23 07:02:19 +01:00
meta = noone;
2023-03-25 12:53:29 +01:00
type = FILE_TYPE.collection;
switch(string_lower(filename_ext(path))) {
case ".png" :
case ".jpg" :
case ".gif" :
type = FILE_TYPE.assets;
break;
case ".pxc" :
type = FILE_TYPE.project;
break;
}
2023-01-17 08:11:55 +01:00
2023-03-05 07:16:44 +01:00
retrive_data = false;
thumbnail_data = -1;
thumbnail = noone;
2023-01-17 08:11:55 +01:00
static getSurface = function() {
if(is_surface(surface)) return surface;
var spr = getSpr();
surface = surface_create_from_sprite_ext(spr, 0);
return surface;
}
2023-03-05 07:16:44 +01:00
static getThumbnail = function() {
if(!retrive_data) getMetadata();
if(thumbnail_data == -1) return noone;
if(thumbnail != noone && is_surface(thumbnail)) return thumbnail;
thumbnail = surface_decode(thumbnail_data);
}
2023-01-17 08:11:55 +01:00
static getSpr = function() {
if(sprite_exists(spr)) return spr;
2023-03-26 07:13:36 +02:00
if(array_length(spr_path) == 0) {
spr = sprite_add(self.path, 0, false, false, 0, 0);
sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
return spr;
}
2023-01-17 08:11:55 +01:00
var path = array_safe_get(spr_path, 0);
var amo = array_safe_get(spr_path, 1);
var cent = array_safe_get(spr_path, 2);
if(path == 0) return -1;
spr = sprite_add(path, amo, false, false, 0, 0);
if(cent)
sprite_set_offset(spr, sprite_get_width(spr) / 2, sprite_get_height(spr) / 2);
return spr;
}
2023-02-14 07:37:13 +01:00
static getMetadata = function() {
2023-03-05 07:16:44 +01:00
retrive_data = true;
2023-02-14 07:37:13 +01:00
if(!file_exists(path)) return noone;
if(meta != noone) return meta;
if(meta == undefined) return noone;
2023-02-28 09:43:01 +01:00
var _f = file_text_read_all(path);
2023-02-14 07:37:13 +01:00
var m = json_decode(_f);
2023-02-15 10:43:24 +01:00
meta = new MetaDataManager();
if(ds_map_exists(m, "metadata"))
2023-02-14 07:37:13 +01:00
meta.deserialize(m[? "metadata"]);
2023-05-07 20:55:13 +02:00
if(ds_map_exists(m, "preview"))
2023-03-05 07:16:44 +01:00
thumbnail_data = json_try_parse(m[? "preview"], -1);
2023-02-15 10:43:24 +01:00
2023-02-14 07:37:13 +01:00
meta.version = m[? "version"];
meta.name = name;
2023-02-23 07:02:19 +01:00
switch(filename_ext(path)) {
2023-03-05 07:16:44 +01:00
case ".pxc" : 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
ds_map_destroy(m);
return meta;
}
2022-09-27 06:37:28 +02:00
}
function DirectoryObject(name, path) constructor {
self.name = name;
self.path = path;
2022-11-01 03:06:03 +01:00
subDir = ds_list_create();
2022-09-27 06:37:28 +02:00
content = ds_list_create();
2022-11-01 03:06:03 +01:00
open = false;
2022-09-27 06:37:28 +02:00
static destroy = function() {
ds_list_destroy(subDir);
}
static scan = function(file_type) {
var _temp_name = ds_list_create();
var folder = file_find_first(path + "/*", fa_directory);
while(folder != "") {
ds_list_add(_temp_name, folder);
folder = file_find_next();
}
file_find_close();
ds_list_clear(subDir);
ds_list_clear(content);
ds_list_sort(_temp_name, true);
for( var i = 0; i < ds_list_size(_temp_name); i++ ) {
var file = _temp_name[| i];
2023-03-08 12:14:01 +01:00
if(directory_exists(path + "/" + file)) {
var _fol_path = path + "/" + file;
2022-09-27 06:37:28 +02:00
var fol = new DirectoryObject(file, _fol_path);
fol.scan(file_type);
ds_list_add(subDir, fol);
} else if(array_exists(file_type, filename_ext(file))) {
2023-03-08 12:14:01 +01:00
var f = new FileObject(string_replace(file, filename_ext(file), ""), path + "/" + file);
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-03-08 12:14:01 +01:00
var icon_path = path + "/" + file;
2022-09-27 06:37:28 +02:00
var amo = 1;
var p = string_pos("strip", icon_path);
if(p) amo = toNumber(string_copy(icon_path, p, string_length(icon_path) - p + 1));
2023-01-17 08:11:55 +01:00
f.spr_path = [icon_path, amo, false];
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");
2022-09-27 06:37:28 +02:00
if(!file_exists(icon_path)) continue;
var _temp = sprite_add(icon_path, 0, false, false, 0, 0);
var ww = sprite_get_width(_temp);
var hh = sprite_get_height(_temp);
2023-02-20 10:16:31 +01:00
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
}
}
}
ds_list_destroy(_temp_name);
}
static draw = function(parent, _x, _y, _m, _w, _hover, _focus, _homedir) {
2022-11-03 11:44:49 +01:00
var hg = ui(28);
2022-09-27 06:37:28 +02:00
var hh = 0;
if(path == parent.context.path)
2022-11-18 03:20:31 +01:00
draw_sprite_stretched_ext(THEME.ui_panel_bg, 0, _x + ui(28), _y, _w - ui(28), hg, COLORS.collection_path_current_bg, 1);
2022-09-27 06:37:28 +02:00
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)) {
2022-11-18 03:20:31 +01:00
draw_sprite_stretched_ext(THEME.ui_panel_bg, 0, _x, _y, ui(32), hg, COLORS.collection_path_current_bg, 0.75);
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)) {
2022-11-18 03:20:31 +01:00
draw_sprite_stretched_ext(THEME.ui_panel_bg, 0, _x + ui(28), _y, _w - ui(28), hg, COLORS.collection_path_current_bg, 0.75);
2022-12-10 05:06:01 +01:00
if(mouse_press(mb_left, _focus)) {
if(!ds_list_empty(subDir))
open = !open;
2022-09-27 06:37:28 +02:00
if(parent.context == self)
parent.setContext(_homedir);
else
parent.setContext(self);
}
}
2022-11-18 03:20:31 +01:00
draw_set_text(f_p0, fa_left, fa_center, COLORS._main_text);
2022-09-27 06:37:28 +02:00
if(ds_list_empty(subDir)) {
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.folder_content, parent.context == self, _x + ui(16), _y + hg / 2 - 1, 1, COLORS.collection_folder_empty);
2022-09-27 06:37:28 +02:00
} else {
2022-11-18 03:20:31 +01:00
draw_sprite_ui_uniform(THEME.folder_content, open, _x + ui(16), _y + hg / 2 - 1, 1, COLORS.collection_folder_nonempty);
2022-09-27 06:37:28 +02:00
}
2022-11-03 11:44:49 +01:00
draw_text(_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++) {
2022-11-03 11:44:49 +01:00
var _hg = subDir[| i].draw(parent, _x + ui(16), _y, _m, _w - ui(16), _hover, _focus, _homedir);
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;
}
}