Files
armorpaint/base/sources/ts/box_projects.ts
T

273 lines
7.5 KiB
TypeScript
Raw Normal View History

2024-03-13 23:50:11 +01:00
2024-09-03 15:52:05 +02:00
let box_projects_htab: ui_handle_t = ui_handle_create();
let box_projects_hsearch: ui_handle_t = ui_handle_create();
2025-03-10 20:38:53 +01:00
let box_projects_icon_map: map_t<string, iron_gpu_texture_t> = null;
2024-03-13 23:50:11 +01:00
2024-11-08 23:46:39 +01:00
let _box_projects_path: string;
let _box_projects_icon_path: string;
let _box_projects_i: i32;
2024-03-13 23:50:11 +01:00
function box_projects_show() {
if (box_projects_icon_map != null) {
2024-04-12 12:51:40 +02:00
let keys: string[] = map_keys(box_projects_icon_map);
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < keys.length; ++i) {
let handle: string = keys[i];
2024-03-13 23:50:11 +01:00
data_delete_image(handle);
}
box_projects_icon_map = null;
}
let draggable: bool;
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-13 23:50:11 +01:00
draggable = false;
///else
draggable = true;
///end
2024-09-03 15:52:05 +02:00
ui_box_show_custom(function (ui: ui_t) {
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-13 23:50:11 +01:00
box_projects_align_to_fullscreen();
///end
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-13 23:50:11 +01:00
box_projects_tab(ui);
box_projects_get_started_tab(ui);
///else
box_projects_recent_tab(ui);
///end
}, 600, 400, null, draggable);
}
2024-09-03 15:52:05 +02:00
function box_projects_tab(ui: ui_t) {
if (ui_tab(box_projects_htab, tr("Projects"), true)) {
ui_begin_sticky();
2024-03-13 23:50:11 +01:00
box_projects_draw_badge(ui);
2024-09-03 15:52:05 +02:00
if (ui_button(tr("New"))) {
2024-03-13 23:50:11 +01:00
project_new();
viewport_scale_to_bounds();
ui_box_hide();
// Pick unique name
let i: i32 = 0;
let j: i32 = 0;
let title: string = tr("untitled") + i;
while (j < config_raw.recent_projects.length) {
let base: string = config_raw.recent_projects[j];
2024-03-20 16:13:54 +01:00
base = substring(base, string_last_index_of(base, path_sep) + 1, string_last_index_of(base, "."));
2024-03-13 23:50:11 +01:00
j++;
if (title == base) {
i++;
title = tr("untitled") + i;
j = 0;
}
}
sys_title_set(title);
}
2024-09-03 15:52:05 +02:00
ui_end_sticky();
ui_separator(3, false);
2024-03-13 23:50:11 +01:00
2024-09-03 15:52:05 +02:00
let slotw: i32 = math_floor(150 * ui_SCALE(ui));
2025-03-09 17:02:12 +01:00
let num: i32 = math_floor(iron_window_width() / slotw);
2024-11-22 19:45:06 +01:00
if (num == 0) {
return;
}
2024-03-13 23:50:11 +01:00
let recent_projects: string[] = config_raw.recent_projects;
let show_asset_names: bool = true;
for (let row: i32 = 0; row < math_ceil(recent_projects.length / num); ++row) {
2024-05-03 21:29:39 +02:00
let mult: i32 = show_asset_names ? 2 : 1;
2024-03-13 23:50:11 +01:00
let ar: f32[] = [];
2024-03-20 16:13:54 +01:00
for (let i: i32 = 0; i < num * mult; ++i) {
array_push(ar, 1 / num);
}
2024-09-03 15:52:05 +02:00
ui_row(ar);
2024-03-13 23:50:11 +01:00
ui._x += 2;
2024-09-03 15:52:05 +02:00
let off: f32 = show_asset_names ? ui_ELEMENT_OFFSET(ui) * 16.0 : 6;
2024-03-20 16:13:54 +01:00
if (row > 0) {
ui._y += off;
}
2024-03-13 23:50:11 +01:00
for (let j: i32 = 0; j < num; ++j) {
2024-09-03 15:52:05 +02:00
let imgw: i32 = math_floor(128 * ui_SCALE(ui));
2024-03-13 23:50:11 +01:00
let i: i32 = j + row * num;
if (i >= recent_projects.length) {
2024-09-03 15:52:05 +02:00
_ui_end_element(imgw);
2024-03-20 16:13:54 +01:00
if (show_asset_names) {
2024-09-03 15:52:05 +02:00
_ui_end_element(0);
2024-03-20 16:13:54 +01:00
}
2024-03-13 23:50:11 +01:00
continue;
}
let path: string = recent_projects[i];
2024-09-06 21:11:46 +02:00
///if arm_ios
2024-09-03 16:39:16 +02:00
let document_directory: string = iron_save_dialog("", "");
2024-03-22 14:17:49 +01:00
document_directory = substring(document_directory, 0, document_directory.length - 8); // Strip /"untitled"
2024-03-13 23:50:11 +01:00
path = document_directory + path;
///end
2024-03-20 16:13:54 +01:00
let icon_path: string = substring(path, 0, path.length - 4) + "_icon.png";
if (box_projects_icon_map == null) {
box_projects_icon_map = map_create();
}
2025-03-10 20:38:53 +01:00
let icon: iron_gpu_texture_t = map_get(box_projects_icon_map, icon_path);
2024-03-13 23:50:11 +01:00
if (icon == null) {
2025-03-10 20:38:53 +01:00
let image: iron_gpu_texture_t = data_get_image(icon_path);
2024-03-13 23:50:11 +01:00
icon = image;
2024-03-20 16:13:54 +01:00
map_set(box_projects_icon_map, icon_path, icon);
2024-03-13 23:50:11 +01:00
}
let uix: i32 = ui._x;
if (icon != null) {
2024-09-03 15:52:05 +02:00
ui_fill(0, 0, 128, 128, ui.ops.theme.SEPARATOR_COL);
2024-03-13 23:50:11 +01:00
2024-09-03 15:52:05 +02:00
let state: i32 = _ui_image(icon, 0xffffffff, 128 * ui_SCALE(ui));
if (state == ui_state_t.RELEASED) {
2024-03-13 23:50:11 +01:00
let _uix: i32 = ui._x;
ui._x = uix;
2024-09-03 15:52:05 +02:00
ui_fill(0, 0, 128, 128, 0x66000000);
2024-03-13 23:50:11 +01:00
ui._x = _uix;
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-04-07 09:52:45 +02:00
console_toast(tr("Opening project"));
2024-03-13 23:50:11 +01:00
///end
2025-03-11 19:49:42 +01:00
sys_notify_on_init(function (path: string) {
2024-04-07 09:52:45 +02:00
ui_box_hide();
import_arm_run_project(path);
}, path);
2024-03-13 23:50:11 +01:00
}
2024-03-20 16:13:54 +01:00
let name: string = substring(path, string_last_index_of(path, path_sep) + 1, string_last_index_of(path, "."));
2024-03-13 23:50:11 +01:00
if (ui.is_hovered && ui.input_released_r) {
2024-04-07 09:52:45 +02:00
_box_projects_path = path;
_box_projects_icon_path = icon_path;
2024-05-03 21:29:39 +02:00
_box_projects_i = i;
2024-09-03 15:52:05 +02:00
ui_menu_draw(function (ui: ui_t) {
2025-02-26 08:47:09 +01:00
// if (ui_menu_button(tr("Duplicate"))) {}
if (ui_menu_button(tr("Delete"))) {
2025-03-11 19:49:42 +01:00
sys_notify_on_init(function () {
2024-04-07 09:52:45 +02:00
file_delete(_box_projects_path);
file_delete(_box_projects_icon_path);
2024-05-03 21:29:39 +02:00
let data_path: string = substring(_box_projects_path, 0, _box_projects_path.length - 4);
2024-03-13 23:50:11 +01:00
file_delete(data_path);
2024-04-07 09:52:45 +02:00
let recent_projects: string[] = config_raw.recent_projects;
2024-05-03 21:29:39 +02:00
array_splice(recent_projects, _box_projects_i, 1);
2024-03-13 23:50:11 +01:00
});
}
2024-09-11 16:32:12 +02:00
});
2024-03-13 23:50:11 +01:00
}
if (show_asset_names) {
ui._x = uix - (150 - 128) / 2;
ui._y += slotw * 0.9;
2024-09-03 15:52:05 +02:00
ui_text(name, ui_align_t.CENTER);
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
2024-09-03 15:52:05 +02:00
ui_tooltip(name);
2024-03-20 16:13:54 +01:00
}
2024-03-13 23:50:11 +01:00
ui._y -= slotw * 0.9;
if (i == recent_projects.length - 1) {
2024-09-03 15:52:05 +02:00
ui._y += j == num - 1 ? imgw : imgw + ui_ELEMENT_H(ui) + ui_ELEMENT_OFFSET(ui);
2024-03-13 23:50:11 +01:00
}
}
}
else {
2024-09-03 15:52:05 +02:00
_ui_end_element(0);
2024-03-20 16:13:54 +01:00
if (show_asset_names) {
2024-09-03 15:52:05 +02:00
_ui_end_element(0);
2024-03-20 16:13:54 +01:00
}
2024-03-13 23:50:11 +01:00
ui._x = uix;
}
}
ui._y += 150;
}
}
}
2024-09-03 15:52:05 +02:00
function box_projects_recent_tab(ui: ui_t) {
if (ui_tab(box_projects_htab, tr("Recent"), true)) {
2024-03-13 23:50:11 +01:00
box_projects_draw_badge(ui);
ui.enabled = config_raw.recent_projects.length > 0;
2024-09-03 15:52:05 +02:00
box_projects_hsearch.text = ui_text_input(box_projects_hsearch, tr("Search"), ui_align_t.LEFT, true, true);
2024-03-13 23:50:11 +01:00
ui.enabled = true;
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < config_raw.recent_projects.length; ++i) {
let path: string = config_raw.recent_projects[i];
2024-09-06 21:11:46 +02:00
///if arm_windows
2024-09-22 17:48:17 +02:00
path = string_replace_all(path, "/", "\\");
2024-03-13 23:50:11 +01:00
///else
2024-09-22 17:48:17 +02:00
path = string_replace_all(path, "\\", "/");
2024-03-13 23:50:11 +01:00
///end
2024-09-22 17:48:17 +02:00
let file: string = substring(path, string_last_index_of(path, path_sep) + 1, path.length);
2024-03-13 23:50:11 +01:00
2024-03-20 16:13:54 +01:00
if (string_index_of(to_lower_case(file), to_lower_case(box_projects_hsearch.text)) < 0) {
continue; // Search filter
}
2024-03-13 23:50:11 +01:00
2024-09-03 15:52:05 +02:00
if (ui_button(file, ui_align_t.LEFT) && file_exists(path)) {
2025-03-10 20:38:53 +01:00
let current: iron_gpu_texture_t = _draw_current;
2025-03-10 20:26:45 +01:00
let g2_in_use: bool = _draw_in_use;
if (g2_in_use) draw_end();
2024-03-13 23:50:11 +01:00
import_arm_run_project(path);
2025-03-10 20:26:45 +01:00
if (g2_in_use) draw_begin(current);
2024-03-13 23:50:11 +01:00
ui_box_hide();
}
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
2024-09-03 15:52:05 +02:00
ui_tooltip(path);
2024-03-20 16:13:54 +01:00
}
2024-03-13 23:50:11 +01:00
}
ui.enabled = config_raw.recent_projects.length > 0;
2024-09-03 15:52:05 +02:00
if (ui_button(tr("Clear"), ui_align_t.LEFT)) {
2024-03-13 23:50:11 +01:00
config_raw.recent_projects = [];
config_save();
}
ui.enabled = true;
2024-09-03 15:52:05 +02:00
_ui_end_element();
2024-11-08 23:46:39 +01:00
if (ui_button(tr("New Project..."), ui_align_t.LEFT)) {
2024-03-20 16:13:54 +01:00
project_new_box();
}
2024-09-03 15:52:05 +02:00
if (ui_button(tr("Open..."), ui_align_t.LEFT)) {
2024-03-20 16:13:54 +01:00
project_open();
}
2024-03-13 23:50:11 +01:00
}
}
2024-09-03 15:52:05 +02:00
function box_projects_draw_badge(ui: ui_t) {
2025-03-10 20:38:53 +01:00
let img: iron_gpu_texture_t = data_get_image("badge.k");
2024-09-03 15:52:05 +02:00
_ui_image(img);
_ui_end_element();
2024-03-13 23:50:11 +01:00
}
2024-09-03 15:52:05 +02:00
function box_projects_get_started_tab(ui: ui_t) {
if (ui_tab(box_projects_htab, tr("Get Started"), true)) {
if (ui_button(tr("Manual"))) {
2024-03-13 23:50:11 +01:00
file_load_url(manifest_url + "/manual");
}
2024-09-03 15:52:05 +02:00
if (ui_button(tr("How To"))) {
2024-03-13 23:50:11 +01:00
file_load_url(manifest_url + "/howto");
}
2024-09-03 15:52:05 +02:00
if (ui_button(tr("What's New"))) {
2024-03-13 23:50:11 +01:00
file_load_url(manifest_url + "/notes");
}
}
}
function box_projects_align_to_fullscreen() {
2025-03-09 17:02:12 +01:00
ui_box_modalw = math_floor(iron_window_width() / ui_SCALE(base_ui_box));
ui_box_modalh = math_floor(iron_window_height() / ui_SCALE(base_ui_box));
let appw: i32 = iron_window_width();
let apph: i32 = iron_window_height();
2024-03-13 23:50:11 +01:00
let mw: i32 = appw;
let mh: i32 = apph;
ui_box_hwnd.drag_x = math_floor(-appw / 2 + mw / 2);
ui_box_hwnd.drag_y = math_floor(-apph / 2 + mh / 2);
}