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

296 lines
9.4 KiB
TypeScript
Raw Normal View History

2024-03-14 15:31:22 +01:00
2025-06-19 19:55:16 +02:00
let _tab_textures_draw_img: gpu_texture_t;
2024-04-07 09:52:45 +02:00
let _tab_textures_draw_path: string;
let _tab_textures_draw_asset: asset_t;
2024-05-03 21:29:39 +02:00
let _tab_textures_draw_i: i32;
let _tab_textures_draw_is_packed: bool;
2024-04-07 09:52:45 +02:00
2024-09-03 15:52:05 +02:00
function tab_textures_draw(htab: ui_handle_t) {
2024-03-14 15:31:22 +01:00
let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
2025-08-15 22:04:07 +02:00
if (ui_tab(htab, tr("Textures")) && statush > ui_status_default_status_h * UI_SCALE()) {
2024-03-14 15:31:22 +01:00
2024-09-03 15:52:05 +02:00
ui_begin_sticky();
2024-03-14 15:31:22 +01:00
if (config_raw.touch_ui) {
2024-04-25 22:04:43 +02:00
let row: f32[] = [1 / 4, 1 / 4];
2024-09-03 15:52:05 +02:00
ui_row(row);
2024-03-14 15:31:22 +01:00
}
else {
2024-04-25 22:04:43 +02:00
let row: f32[] = [1 / 14, 1 / 14];
2024-09-03 15:52:05 +02:00
ui_row(row);
2024-03-14 15:31:22 +01:00
}
2024-09-03 15:52:05 +02:00
if (ui_button(tr("Import"))) {
2024-05-03 21:29:39 +02:00
ui_files_show(string_array_join(path_texture_formats, ","), false, true, function (path: string) {
2024-03-14 15:31:22 +01:00
import_asset_run(path, -1.0, -1.0, true, false);
ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
});
}
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
2024-09-03 15:52:05 +02:00
ui_tooltip(tr("Import texture file") + " (" + map_get(config_keymap, "file_import_assets") + ")");
2024-03-20 16:13:54 +01:00
}
2024-03-14 15:31:22 +01:00
2024-09-03 15:52:05 +02:00
if (ui_button(tr("2D View"))) {
2024-03-20 16:13:54 +01:00
ui_base_show_2d_view(view_2d_type_t.ASSET);
}
2024-03-14 15:31:22 +01:00
2024-09-03 15:52:05 +02:00
ui_end_sticky();
2024-03-14 15:31:22 +01:00
if (project_assets.length > 0) {
2025-08-07 14:15:45 +02:00
///if is_paint
2025-07-18 23:37:51 +02:00
let statusw: i32 = iron_window_width() - ui_toolbar_w(true) - config_raw.layout[layout_size_t.SIDEBAR_W];
2024-03-14 15:31:22 +01:00
///end
///if is_lab
2025-03-09 17:02:12 +01:00
let statusw: i32 = iron_window_width();
2024-03-14 15:31:22 +01:00
///end
2025-08-15 22:04:07 +02:00
let slotw: i32 = math_floor(52 * UI_SCALE());
2024-03-14 15:31:22 +01:00
let num: i32 = math_floor(statusw / slotw);
2024-11-22 19:45:06 +01:00
if (num == 0) {
return;
}
2024-03-14 15:31:22 +01:00
for (let row: i32 = 0; row < math_floor(math_ceil(project_assets.length / num)); ++row) {
let mult: i32 = config_raw.show_asset_names ? 2 : 1;
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-14 15:31:22 +01:00
ui._x += 2;
2025-08-15 22:04:07 +02:00
let off: f32 = config_raw.show_asset_names ? UI_ELEMENT_OFFSET() * 10.0 : 6;
2024-03-20 16:13:54 +01:00
if (row > 0) {
ui._y += off;
}
2024-03-14 15:31:22 +01:00
for (let j: i32 = 0; j < num; ++j) {
2025-08-15 22:04:07 +02:00
let imgw: i32 = math_floor(50 * UI_SCALE());
2024-03-14 15:31:22 +01:00
let i: i32 = j + row * num;
if (i >= project_assets.length) {
2025-08-15 22:04:07 +02:00
ui_end_element_of_size(imgw);
2024-03-20 16:13:54 +01:00
if (config_raw.show_asset_names) {
2025-08-15 22:04:07 +02:00
ui_end_element_of_size(0);
2024-03-20 16:13:54 +01:00
}
2024-03-14 15:31:22 +01:00
continue;
}
let asset: asset_t = project_assets[i];
2025-06-19 19:55:16 +02:00
let img: gpu_texture_t = project_get_image(asset);
2024-09-24 18:26:51 +02:00
if (img == null) {
let empty_rt: render_target_t = map_get(render_path_render_targets, "empty_black");
img = empty_rt._image;
}
2024-03-14 15:31:22 +01:00
let uix: f32 = ui._x;
let uiy: f32 = ui._y;
let sw: i32 = img.height < img.width ? img.height : 0;
2025-08-04 19:34:53 +02:00
if (ui_sub_image(img, 0xffffffff, slotw, 0, 0, sw, sw) == ui_state_t.STARTED && ui.input_y > ui._window_y) {
2024-03-14 15:31:22 +01:00
base_drag_off_x = -(mouse_x - uix - ui._window_x - 3);
base_drag_off_y = -(mouse_y - uiy - ui._window_y + 1);
base_drag_asset = asset;
context_raw.texture = asset;
2025-03-11 10:02:47 +01:00
if (sys_time() - context_raw.select_time < 0.25) {
2024-03-20 16:13:54 +01:00
ui_base_show_2d_view(view_2d_type_t.ASSET);
}
2025-03-11 10:02:47 +01:00
context_raw.select_time = sys_time();
2024-03-14 15:31:22 +01:00
ui_view2d_hwnd.redraws = 2;
}
if (asset == context_raw.texture) {
let _uix: f32 = ui._x;
let _uiy: f32 = ui._y;
ui._x = uix;
ui._y = uiy;
let off: i32 = i % 2 == 1 ? 1 : 0;
let w: i32 = 50;
2024-09-03 15:52:05 +02:00
ui_fill(0, 0, w + 3, 2, ui.ops.theme.HIGHLIGHT_COL);
ui_fill(0, w - off + 2, w + 3, 2 + off, ui.ops.theme.HIGHLIGHT_COL);
ui_fill(0, 0, 2, w + 3, ui.ops.theme.HIGHLIGHT_COL);
ui_fill(w + 2, 0, 2, w + 4, ui.ops.theme.HIGHLIGHT_COL);
2024-03-14 15:31:22 +01:00
ui._x = _uix;
ui._y = _uiy;
}
let is_packed: bool = project_raw.packed_assets != null && project_packed_asset_exists(project_raw.packed_assets, asset.file);
if (ui.is_hovered) {
2025-03-10 22:13:30 +01:00
ui_tooltip_image(img, 256);
2024-05-03 21:29:39 +02:00
if (is_packed) {
2024-09-03 15:52:05 +02:00
ui_tooltip(asset.name + " " + tr("(packed)"));
2024-05-03 21:29:39 +02:00
}
else {
2024-09-03 15:52:05 +02:00
ui_tooltip(asset.name);
2024-05-03 21:29:39 +02:00
}
2024-03-14 15:31:22 +01:00
}
if (ui.is_hovered && ui.input_released_r) {
context_raw.texture = asset;
2024-04-07 09:52:45 +02:00
_tab_textures_draw_img = img;
2024-05-03 21:29:39 +02:00
_tab_textures_draw_asset = asset;
_tab_textures_draw_i = i;
_tab_textures_draw_is_packed = is_packed;
2024-04-07 09:52:45 +02:00
2025-08-15 22:59:45 +02:00
ui_menu_draw(function () {
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("Export"))) {
2024-03-20 16:13:54 +01:00
ui_files_show("png", true, false, function (path: string) {
2024-04-07 09:52:45 +02:00
_tab_textures_draw_path = path;
2025-03-11 19:49:42 +01:00
sys_notify_on_next_frame(function () {
2025-06-19 19:55:16 +02:00
let img: gpu_texture_t = _tab_textures_draw_img;
2025-08-11 12:07:10 +02:00
let target: gpu_texture_t = gpu_create_render_target(img.width, img.height);
2025-03-10 20:26:45 +01:00
draw_begin(target);
2025-03-03 22:50:40 +01:00
draw_set_pipeline(pipes_copy);
2025-03-02 19:44:26 +01:00
draw_scaled_image(img, 0, 0, target.width, target.height);
2025-03-03 22:50:40 +01:00
draw_set_pipeline(null);
2025-03-10 20:26:45 +01:00
draw_end();
2025-06-19 19:55:16 +02:00
sys_notify_on_next_frame(function (target: gpu_texture_t) {
2024-04-07 09:52:45 +02:00
let path: string = _tab_textures_draw_path;
2024-03-14 15:31:22 +01:00
let f: string = ui_files_filename;
2024-03-20 16:13:54 +01:00
if (f == "") {
f = tr("untitled");
}
if (!ends_with(f, ".png")) {
f += ".png";
}
2025-03-10 20:38:53 +01:00
iron_write_png(path + path_sep + f, gpu_get_texture_pixels(target), target.width, target.height, 0);
2025-07-21 17:14:54 +02:00
iron_delete_texture(target);
2024-05-03 21:29:39 +02:00
}, target);
2024-03-14 15:31:22 +01:00
});
});
}
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("Reimport"))) {
2024-05-03 21:29:39 +02:00
project_reimport_texture(_tab_textures_draw_asset);
2024-03-14 15:31:22 +01:00
}
2025-08-07 14:15:45 +02:00
///if is_paint
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("To Mask"))) {
2025-03-11 19:49:42 +01:00
sys_notify_on_next_frame(function () {
2024-11-10 16:53:36 +01:00
layers_create_image_mask(_tab_textures_draw_asset);
2024-03-14 15:31:22 +01:00
});
}
///end
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("Set as Envmap"))) {
2025-03-11 19:49:42 +01:00
sys_notify_on_next_frame(function () {
2024-04-07 09:52:45 +02:00
import_envmap_run(_tab_textures_draw_asset.file, _tab_textures_draw_img);
2024-03-14 15:31:22 +01:00
});
}
///if is_paint
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("Set as Color ID Map"))) {
2024-05-03 21:29:39 +02:00
context_raw.colorid_handle.position = _tab_textures_draw_i;
2024-03-14 15:31:22 +01:00
context_raw.colorid_picked = false;
ui_toolbar_handle.redraws = 1;
2025-08-07 19:43:11 +02:00
if (context_raw.tool == tool_type_t.COLORID) {
2024-03-14 15:31:22 +01:00
ui_header_handle.redraws = 2;
context_raw.ddirty = 2;
}
}
///end
2025-02-26 08:47:09 +01:00
if (ui_menu_button(tr("Delete"), "delete")) {
2024-05-03 21:29:39 +02:00
tab_textures_delete_texture(_tab_textures_draw_asset);
2024-03-14 15:31:22 +01:00
}
2025-02-26 08:47:09 +01:00
if (!_tab_textures_draw_is_packed && ui_menu_button(tr("Open Containing Directory..."))) {
2024-05-03 21:29:39 +02:00
file_start(substring(_tab_textures_draw_asset.file, 0, string_last_index_of(_tab_textures_draw_asset.file, path_sep)));
2024-03-14 15:31:22 +01:00
}
2025-02-26 08:47:09 +01:00
if (!_tab_textures_draw_is_packed && ui_menu_button(tr("Open in Browser"))) {
2024-05-03 21:29:39 +02:00
tab_browser_show_directory(substring(_tab_textures_draw_asset.file, 0, string_last_index_of(_tab_textures_draw_asset.file, path_sep)));
2024-03-14 15:31:22 +01:00
}
2024-09-11 16:32:12 +02:00
});
2024-03-14 15:31:22 +01:00
}
if (config_raw.show_asset_names) {
ui._x = uix;
ui._y += slotw * 0.9;
2024-09-03 15:52:05 +02:00
ui_text(project_assets[i].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(project_assets[i].name);
2024-03-20 16:13:54 +01:00
}
2024-03-14 15:31:22 +01:00
ui._y -= slotw * 0.9;
if (i == project_assets.length - 1) {
2025-08-15 22:04:07 +02:00
ui._y += j == num - 1 ? imgw : imgw + UI_ELEMENT_H() + UI_ELEMENT_OFFSET();
2024-03-14 15:31:22 +01:00
}
}
}
}
}
else {
2025-06-19 19:55:16 +02:00
let img: gpu_texture_t = resource_get("icons.k");
2024-03-14 15:31:22 +01:00
let r: rect_t = resource_tile50(img, 0, 1);
2025-08-04 19:34:53 +02:00
ui_sub_image(img, ui.ops.theme.BUTTON_COL, r.h, r.x, r.y, r.w, r.h);
2024-03-20 16:13:54 +01:00
if (ui.is_hovered) {
2024-09-03 15:52:05 +02:00
ui_tooltip(tr("Drag and drop files here"));
2024-03-20 16:13:54 +01:00
}
2024-03-14 15:31:22 +01:00
}
let in_focus: bool = ui.input_x > ui._window_x && ui.input_x < ui._window_x + ui._window_w &&
2024-03-20 16:13:54 +01:00
ui.input_y > ui._window_y && ui.input_y < ui._window_y + ui._window_h;
if (in_focus && ui.is_delete_down && project_assets.length > 0 && array_index_of(project_assets, context_raw.texture) >= 0) {
2024-03-14 15:31:22 +01:00
ui.is_delete_down = false;
tab_textures_delete_texture(context_raw.texture);
}
}
}
2024-09-03 15:52:05 +02:00
function tab_textures_update_texture_pointers(nodes: ui_node_t[], i: i32) {
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < nodes.length; ++i) {
2024-09-03 15:52:05 +02:00
let n: ui_node_t = nodes[i];
2024-03-14 15:31:22 +01:00
if (n.type == "TEX_IMAGE") {
2024-04-24 12:14:47 +02:00
if (n.buttons[0].default_value[0] == i) {
n.buttons[0].default_value[0] = 9999; // Texture deleted, use pink now
2024-03-14 15:31:22 +01:00
}
2024-04-24 12:14:47 +02:00
else if (n.buttons[0].default_value[0] > i) {
n.buttons[0].default_value[0]--; // Offset by deleted texture
2024-03-14 15:31:22 +01:00
}
}
}
}
function tab_textures_delete_texture(asset: asset_t) {
2024-03-20 16:13:54 +01:00
let i: i32 = array_index_of(project_assets, asset);
2024-03-14 15:31:22 +01:00
if (project_assets.length > 1) {
context_raw.texture = project_assets[i == project_assets.length - 1 ? i - 1 : i + 1];
}
ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
2025-08-07 19:43:11 +02:00
if (context_raw.tool == tool_type_t.COLORID && i == context_raw.colorid_handle.position) {
2024-03-14 15:31:22 +01:00
ui_header_handle.redraws = 2;
context_raw.ddirty = 2;
context_raw.colorid_picked = false;
ui_toolbar_handle.redraws = 1;
}
2025-08-14 19:57:17 +02:00
if (data_get_image(asset.file) == scene_world._.envmap) {
project_set_default_envmap();
}
2024-03-14 15:31:22 +01:00
data_delete_image(asset.file);
2024-03-20 16:13:54 +01:00
map_delete(project_asset_map, asset.id);
array_splice(project_assets, i, 1);
array_splice(project_asset_names, i, 1);
2025-03-11 19:49:42 +01:00
sys_notify_on_next_frame(function () {
2024-03-14 15:31:22 +01:00
make_material_parse_paint_material();
2025-08-07 14:15:45 +02:00
///if is_paint
2024-03-14 15:31:22 +01:00
util_render_make_material_preview();
ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
///end
2024-04-07 09:52:45 +02:00
});
2024-03-14 15:31:22 +01:00
2025-08-07 14:15:45 +02:00
///if is_paint
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < project_materials.length; ++i) {
let m: slot_material_t = project_materials[i];
2024-03-20 16:13:54 +01:00
tab_textures_update_texture_pointers(m.canvas.nodes, i);
}
2024-10-19 19:44:38 +02:00
2024-03-21 21:06:41 +01:00
for (let i: i32 = 0; i < project_brushes.length; ++i) {
let b: slot_brush_t = project_brushes[i];
2024-03-20 16:13:54 +01:00
tab_textures_update_texture_pointers(b.canvas.nodes, i);
}
2024-03-14 15:31:22 +01:00
///end
}