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

205 lines
5.4 KiB
TypeScript
Raw Normal View History

2024-03-11 15:10:18 +01:00
let ui_box_show: bool = false;
let ui_box_draggable: bool = true;
2024-09-03 15:52:05 +02:00
let ui_box_hwnd: ui_handle_t = ui_handle_create();
2024-03-11 15:10:18 +01:00
let ui_box_title: string = "";
let ui_box_text: string = "";
2025-08-15 22:59:45 +02:00
let ui_box_commands: ()=>void = null;
2024-03-11 15:10:18 +01:00
let ui_box_click_to_hide: bool = true;
let ui_box_modalw: i32 = 400;
let ui_box_modalh: i32 = 170;
let ui_box_modal_on_hide: ()=>void = null;
let ui_box_draws: i32 = 0;
let ui_box_copyable: bool = false;
let ui_box_tween_alpha: f32 = 0.0;
function ui_box_init() {
ui_box_hwnd.redraws = 2;
ui_box_hwnd.drag_x = 0;
ui_box_hwnd.drag_y = 0;
ui_box_show = true;
ui_box_draws = 0;
ui_box_click_to_hide = true;
}
function ui_box_render() {
if (!ui_menu_show) {
2024-05-03 21:29:39 +02:00
let in_use: bool = ui.combo_selected_handle != null;
2025-08-15 23:40:53 +02:00
let is_escape: bool = ui.is_escape_down;
2024-03-11 15:10:18 +01:00
if (ui_box_draws > 2 && (ui.input_released || is_escape) && !in_use && !ui.is_typing) {
2025-03-09 17:02:12 +01:00
let appw: i32 = iron_window_width();
let apph: i32 = iron_window_height();
2025-08-15 22:04:07 +02:00
let mw: i32 = math_floor(ui_box_modalw * UI_SCALE());
let mh: i32 = math_floor(ui_box_modalh * UI_SCALE());
2024-03-11 15:10:18 +01:00
let left: f32 = (appw / 2 - mw / 2) + ui_box_hwnd.drag_x;
let right: f32 = (appw / 2 + mw / 2) + ui_box_hwnd.drag_x;
let top: f32 = (apph / 2 - mh / 2) + ui_box_hwnd.drag_y;
let bottom: f32 = (apph / 2 + mh / 2) + ui_box_hwnd.drag_y;
let mx: i32 = mouse_x;
let my: i32 = mouse_y;
if ((ui_box_click_to_hide && (mx < left || mx > right || my < top || my > bottom)) || is_escape) {
ui_box_hide();
}
}
}
if (config_raw.touch_ui) { // Darken bg
2025-06-21 16:49:53 +02:00
draw_begin();
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2025-03-02 21:07:17 +01:00
draw_set_color(color_from_floats(0, 0, 0, ui_box_tween_alpha));
2024-03-11 15:10:18 +01:00
///else
2025-03-02 21:07:17 +01:00
draw_set_color(color_from_floats(0, 0, 0, 0.5));
2024-03-11 15:10:18 +01:00
///end
2025-03-09 17:02:12 +01:00
draw_filled_rect(0, 0, iron_window_width(), iron_window_height());
2025-06-21 16:49:53 +02:00
draw_end();
2024-03-11 15:10:18 +01:00
}
2025-03-09 17:02:12 +01:00
let appw: i32 = iron_window_width();
let apph: i32 = iron_window_height();
2025-08-15 22:04:07 +02:00
let mw: i32 = math_floor(ui_box_modalw * UI_SCALE());
let mh: i32 = math_floor(ui_box_modalh * UI_SCALE());
2024-03-20 16:13:54 +01:00
if (mw > appw) {
mw = appw;
}
if (mh > apph) {
mh = apph;
}
2024-03-11 15:10:18 +01:00
let left: i32 = math_floor(appw / 2 - mw / 2);
let top: i32 = math_floor(apph / 2 - mh / 2);
if (ui_box_commands == null) {
2024-09-03 15:52:05 +02:00
ui_begin(ui);
if (ui_window(ui_box_hwnd, left, top, mw, mh, ui_box_draggable)) {
2024-03-11 15:10:18 +01:00
ui._y += 10;
let tab_vertical: bool = config_raw.touch_ui;
2024-09-03 15:52:05 +02:00
if (ui_tab(ui_handle(__ID__), ui_box_title, tab_vertical)) {
let htext: ui_handle_t = ui_handle(__ID__);
2024-03-11 15:10:18 +01:00
htext.text = ui_box_text;
2024-05-08 21:17:02 +02:00
if (ui_box_copyable) {
2024-09-03 15:52:05 +02:00
ui_text_area(htext, ui_align_t.LEFT, false);
2024-05-08 21:17:02 +02:00
}
else {
2024-09-03 15:52:05 +02:00
ui_text(ui_box_text);
2024-05-08 21:17:02 +02:00
}
2025-08-15 22:04:07 +02:00
ui_end_element();
2024-03-11 15:10:18 +01:00
2024-09-06 21:11:46 +02:00
///if (arm_windows || arm_linux || arm_macos)
2024-03-20 16:13:54 +01:00
if (ui_box_copyable) {
2024-11-08 23:46:39 +01:00
ui_row3();
2024-03-20 16:13:54 +01:00
}
else {
2024-04-25 22:04:43 +02:00
let row: f32[] = [2 / 3, 1 / 3];
2024-09-03 15:52:05 +02:00
ui_row(row);
2024-03-20 16:13:54 +01:00
}
2024-03-11 15:10:18 +01:00
///else
2024-04-25 22:04:43 +02:00
let row: f32[] = [2 / 3, 1 / 3];
2024-09-03 15:52:05 +02:00
ui_row(row);
2024-03-11 15:10:18 +01:00
///end
2025-08-15 22:04:07 +02:00
ui_end_element();
2024-03-11 15:10:18 +01:00
2024-09-06 21:11:46 +02:00
///if (arm_windows || arm_linux || arm_macos)
2024-09-03 15:52:05 +02:00
if (ui_box_copyable && ui_button(tr("Copy"))) {
2025-03-09 17:02:12 +01:00
iron_copy_to_clipboard(ui_box_text);
2024-03-11 15:10:18 +01:00
}
///end
2024-09-03 15:52:05 +02:00
if (ui_button(tr("OK"))) {
2024-03-11 15:10:18 +01:00
ui_box_hide();
}
}
2025-08-15 22:59:45 +02:00
ui_box_window_border();
2024-03-11 15:10:18 +01:00
}
2024-09-03 15:52:05 +02:00
ui_end();
2024-03-11 15:10:18 +01:00
}
else {
2024-09-03 15:52:05 +02:00
ui_begin(ui);
2025-08-15 22:59:45 +02:00
ui.input_enabled = !ui_menu_show && ui.combo_selected_handle == null;
2024-09-03 15:52:05 +02:00
if (ui_window(ui_box_hwnd, left, top, mw, mh, ui_box_draggable)) {
2024-03-11 15:10:18 +01:00
ui._y += 10;
2025-08-15 22:59:45 +02:00
ui_box_commands();
ui_box_window_border();
2024-03-11 15:10:18 +01:00
}
2025-08-15 22:59:45 +02:00
ui.input_enabled = true;
2024-09-03 15:52:05 +02:00
ui_end();
2024-03-11 15:10:18 +01:00
}
ui_box_draws++;
}
function ui_box_show_message(title: string, text: string, copyable: bool = false) {
ui_box_init();
ui_box_modalw = 400;
ui_box_modalh = 210;
ui_box_title = title;
ui_box_text = text;
ui_box_commands = null;
ui_box_copyable = copyable;
ui_box_draggable = true;
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-11 15:10:18 +01:00
ui_box_tween_in();
///end
}
2025-08-15 22:59:45 +02:00
function ui_box_show_custom(commands: ()=>void = null, mw: i32 = 400, mh: i32 = 200, on_hide: ()=>void = null, draggable: bool = true) {
2024-03-11 15:10:18 +01:00
ui_box_init();
ui_box_modalw = mw;
ui_box_modalh = mh;
2024-03-20 16:13:54 +01:00
ui_box_modal_on_hide = on_hide;
2024-03-11 15:10:18 +01:00
ui_box_commands = commands;
ui_box_draggable = draggable;
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-11 15:10:18 +01:00
ui_box_tween_in();
///end
}
function ui_box_hide() {
2024-09-06 21:11:46 +02:00
///if (arm_android || arm_ios)
2024-03-11 15:10:18 +01:00
ui_box_tween_out();
///else
ui_box_hide_internal();
///end
}
function ui_box_hide_internal() {
2024-03-20 16:13:54 +01:00
if (ui_box_modal_on_hide != null) {
ui_box_modal_on_hide();
}
2024-03-11 15:10:18 +01:00
ui_box_show = false;
base_redraw_ui();
}
function ui_box_tween_in() {
2024-08-06 19:53:05 +02:00
tween_reset();
let a: tween_anim_t = { target: ADDRESS(ui_box_tween_alpha), to: 0.5, duration: 0.2, ease: ease_t.EXPO_OUT };
tween_to(a);
2025-03-09 17:02:12 +01:00
ui_box_hwnd.drag_y = math_floor(iron_window_height() / 2);
2024-08-06 19:53:05 +02:00
a = { target: ADDRESS(ui_box_hwnd.drag_y), to: 0.0, duration: 0.2, ease: ease_t.EXPO_OUT, tick: ui_box_tween_tick };
tween_to(a);
2024-03-11 15:10:18 +01:00
}
function ui_box_tween_out() {
2024-08-06 19:53:05 +02:00
let a: tween_anim_t = { target: ADDRESS(ui_box_tween_alpha), to: 0.0, duration: 0.2, ease: ease_t.EXPO_IN, done: ui_box_hide_internal };
tween_to(a);
2025-03-09 17:02:12 +01:00
a = { target: ADDRESS(ui_box_hwnd.drag_y), to: iron_window_height() / 2, duration: 0.2, ease: ease_t.EXPO_IN };
2024-08-06 19:53:05 +02:00
tween_to(a);
}
function ui_box_tween_tick() {
base_redraw_ui();
2024-03-11 15:10:18 +01:00
}
2025-08-15 22:59:45 +02:00
function ui_box_window_border() {
2024-03-11 15:10:18 +01:00
if (ui.scissor) {
ui.scissor = false;
2025-06-19 19:55:16 +02:00
gpu_disable_scissor();
2024-03-11 15:10:18 +01:00
}
// Border
2025-03-02 21:07:17 +01:00
draw_set_color(ui.ops.theme.SEPARATOR_COL);
2025-03-02 19:44:26 +01:00
draw_filled_rect(0, 0, 1, ui._window_h);
draw_filled_rect(0 + ui._window_w - 1, 0, 1, ui._window_h);
draw_filled_rect(0, 0 + ui._window_h - 1, ui._window_w, 1);
2024-03-11 15:10:18 +01:00
}