tab_textures.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. let _tab_textures_draw_img: gpu_texture_t;
  2. let _tab_textures_draw_path: string;
  3. let _tab_textures_draw_asset: asset_t;
  4. let _tab_textures_draw_i: i32;
  5. let _tab_textures_draw_is_packed: bool;
  6. function tab_textures_draw(htab: ui_handle_t) {
  7. let ui: ui_t = ui_base_ui;
  8. let statush: i32 = config_raw.layout[layout_size_t.STATUS_H];
  9. if (ui_tab(htab, tr("Textures")) && statush > ui_status_default_status_h * ui_SCALE(ui)) {
  10. ui_begin_sticky();
  11. if (config_raw.touch_ui) {
  12. let row: f32[] = [1 / 4, 1 / 4];
  13. ui_row(row);
  14. }
  15. else {
  16. let row: f32[] = [1 / 14, 1 / 14];
  17. ui_row(row);
  18. }
  19. if (ui_button(tr("Import"))) {
  20. ui_files_show(string_array_join(path_texture_formats, ","), false, true, function (path: string) {
  21. import_asset_run(path, -1.0, -1.0, true, false);
  22. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  23. });
  24. }
  25. if (ui.is_hovered) {
  26. ui_tooltip(tr("Import texture file") + " (" + map_get(config_keymap, "file_import_assets") + ")");
  27. }
  28. if (ui_button(tr("2D View"))) {
  29. ui_base_show_2d_view(view_2d_type_t.ASSET);
  30. }
  31. ui_end_sticky();
  32. if (project_assets.length > 0) {
  33. ///if is_paint
  34. let statusw: i32 = iron_window_width() - ui_toolbar_w(true) - config_raw.layout[layout_size_t.SIDEBAR_W];
  35. ///end
  36. ///if is_lab
  37. let statusw: i32 = iron_window_width();
  38. ///end
  39. let slotw: i32 = math_floor(52 * ui_SCALE(ui));
  40. let num: i32 = math_floor(statusw / slotw);
  41. if (num == 0) {
  42. return;
  43. }
  44. for (let row: i32 = 0; row < math_floor(math_ceil(project_assets.length / num)); ++row) {
  45. let mult: i32 = config_raw.show_asset_names ? 2 : 1;
  46. let ar: f32[] = [];
  47. for (let i: i32 = 0; i < num * mult; ++i) {
  48. array_push(ar, 1 / num);
  49. }
  50. ui_row(ar);
  51. ui._x += 2;
  52. let off: f32 = config_raw.show_asset_names ? ui_ELEMENT_OFFSET(ui) * 10.0 : 6;
  53. if (row > 0) {
  54. ui._y += off;
  55. }
  56. for (let j: i32 = 0; j < num; ++j) {
  57. let imgw: i32 = math_floor(50 * ui_SCALE(ui));
  58. let i: i32 = j + row * num;
  59. if (i >= project_assets.length) {
  60. _ui_end_element(imgw);
  61. if (config_raw.show_asset_names) {
  62. _ui_end_element(0);
  63. }
  64. continue;
  65. }
  66. let asset: asset_t = project_assets[i];
  67. let img: gpu_texture_t = project_get_image(asset);
  68. if (img == null) {
  69. let empty_rt: render_target_t = map_get(render_path_render_targets, "empty_black");
  70. img = empty_rt._image;
  71. }
  72. let uix: f32 = ui._x;
  73. let uiy: f32 = ui._y;
  74. let sw: i32 = img.height < img.width ? img.height : 0;
  75. if (ui_sub_image(img, 0xffffffff, slotw, 0, 0, sw, sw) == ui_state_t.STARTED && ui.input_y > ui._window_y) {
  76. base_drag_off_x = -(mouse_x - uix - ui._window_x - 3);
  77. base_drag_off_y = -(mouse_y - uiy - ui._window_y + 1);
  78. base_drag_asset = asset;
  79. context_raw.texture = asset;
  80. if (sys_time() - context_raw.select_time < 0.25) {
  81. ui_base_show_2d_view(view_2d_type_t.ASSET);
  82. }
  83. context_raw.select_time = sys_time();
  84. ui_view2d_hwnd.redraws = 2;
  85. }
  86. if (asset == context_raw.texture) {
  87. let _uix: f32 = ui._x;
  88. let _uiy: f32 = ui._y;
  89. ui._x = uix;
  90. ui._y = uiy;
  91. let off: i32 = i % 2 == 1 ? 1 : 0;
  92. let w: i32 = 50;
  93. ui_fill(0, 0, w + 3, 2, ui.ops.theme.HIGHLIGHT_COL);
  94. ui_fill(0, w - off + 2, w + 3, 2 + off, ui.ops.theme.HIGHLIGHT_COL);
  95. ui_fill(0, 0, 2, w + 3, ui.ops.theme.HIGHLIGHT_COL);
  96. ui_fill(w + 2, 0, 2, w + 4, ui.ops.theme.HIGHLIGHT_COL);
  97. ui._x = _uix;
  98. ui._y = _uiy;
  99. }
  100. let is_packed: bool = project_raw.packed_assets != null && project_packed_asset_exists(project_raw.packed_assets, asset.file);
  101. if (ui.is_hovered) {
  102. ui_tooltip_image(img, 256);
  103. if (is_packed) {
  104. ui_tooltip(asset.name + " " + tr("(packed)"));
  105. }
  106. else {
  107. ui_tooltip(asset.name);
  108. }
  109. }
  110. if (ui.is_hovered && ui.input_released_r) {
  111. context_raw.texture = asset;
  112. _tab_textures_draw_img = img;
  113. _tab_textures_draw_asset = asset;
  114. _tab_textures_draw_i = i;
  115. _tab_textures_draw_is_packed = is_packed;
  116. ui_menu_draw(function (ui: ui_t) {
  117. if (ui_menu_button(tr("Export"))) {
  118. ui_files_show("png", true, false, function (path: string) {
  119. _tab_textures_draw_path = path;
  120. sys_notify_on_next_frame(function () {
  121. let img: gpu_texture_t = _tab_textures_draw_img;
  122. let target: gpu_texture_t = gpu_create_render_target(tab_textures_to_pow2(img.width), tab_textures_to_pow2(img.height));
  123. draw_begin(target);
  124. draw_set_pipeline(pipes_copy);
  125. draw_scaled_image(img, 0, 0, target.width, target.height);
  126. draw_set_pipeline(null);
  127. draw_end();
  128. sys_notify_on_next_frame(function (target: gpu_texture_t) {
  129. let path: string = _tab_textures_draw_path;
  130. let f: string = ui_files_filename;
  131. if (f == "") {
  132. f = tr("untitled");
  133. }
  134. if (!ends_with(f, ".png")) {
  135. f += ".png";
  136. }
  137. iron_write_png(path + path_sep + f, gpu_get_texture_pixels(target), target.width, target.height, 0);
  138. iron_delete_texture(target);
  139. }, target);
  140. });
  141. });
  142. }
  143. if (ui_menu_button(tr("Reimport"))) {
  144. project_reimport_texture(_tab_textures_draw_asset);
  145. }
  146. ///if is_paint
  147. if (ui_menu_button(tr("To Mask"))) {
  148. sys_notify_on_next_frame(function () {
  149. layers_create_image_mask(_tab_textures_draw_asset);
  150. });
  151. }
  152. ///end
  153. if (ui_menu_button(tr("Set as Envmap"))) {
  154. sys_notify_on_next_frame(function () {
  155. import_envmap_run(_tab_textures_draw_asset.file, _tab_textures_draw_img);
  156. });
  157. }
  158. ///if is_paint
  159. if (ui_menu_button(tr("Set as Color ID Map"))) {
  160. context_raw.colorid_handle.position = _tab_textures_draw_i;
  161. context_raw.colorid_picked = false;
  162. ui_toolbar_handle.redraws = 1;
  163. if (context_raw.tool == workspace_tool_t.COLORID) {
  164. ui_header_handle.redraws = 2;
  165. context_raw.ddirty = 2;
  166. }
  167. }
  168. ///end
  169. if (ui_menu_button(tr("Delete"), "delete")) {
  170. tab_textures_delete_texture(_tab_textures_draw_asset);
  171. }
  172. if (!_tab_textures_draw_is_packed && ui_menu_button(tr("Open Containing Directory..."))) {
  173. file_start(substring(_tab_textures_draw_asset.file, 0, string_last_index_of(_tab_textures_draw_asset.file, path_sep)));
  174. }
  175. if (!_tab_textures_draw_is_packed && ui_menu_button(tr("Open in Browser"))) {
  176. tab_browser_show_directory(substring(_tab_textures_draw_asset.file, 0, string_last_index_of(_tab_textures_draw_asset.file, path_sep)));
  177. }
  178. });
  179. }
  180. if (config_raw.show_asset_names) {
  181. ui._x = uix;
  182. ui._y += slotw * 0.9;
  183. ui_text(project_assets[i].name, ui_align_t.CENTER);
  184. if (ui.is_hovered) {
  185. ui_tooltip(project_assets[i].name);
  186. }
  187. ui._y -= slotw * 0.9;
  188. if (i == project_assets.length - 1) {
  189. ui._y += j == num - 1 ? imgw : imgw + ui_ELEMENT_H(ui) + ui_ELEMENT_OFFSET(ui);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. else {
  196. let img: gpu_texture_t = resource_get("icons.k");
  197. let r: rect_t = resource_tile50(img, 0, 1);
  198. ui_sub_image(img, ui.ops.theme.BUTTON_COL, r.h, r.x, r.y, r.w, r.h);
  199. if (ui.is_hovered) {
  200. ui_tooltip(tr("Drag and drop files here"));
  201. }
  202. }
  203. let in_focus: bool = ui.input_x > ui._window_x && ui.input_x < ui._window_x + ui._window_w &&
  204. ui.input_y > ui._window_y && ui.input_y < ui._window_y + ui._window_h;
  205. if (in_focus && ui.is_delete_down && project_assets.length > 0 && array_index_of(project_assets, context_raw.texture) >= 0) {
  206. ui.is_delete_down = false;
  207. tab_textures_delete_texture(context_raw.texture);
  208. }
  209. }
  210. }
  211. function tab_textures_to_pow2(i: i32): i32 {
  212. i--;
  213. i |= i >> 1;
  214. i |= i >> 2;
  215. i |= i >> 4;
  216. i |= i >> 8;
  217. i |= i >> 16;
  218. i++;
  219. return i;
  220. }
  221. function tab_textures_update_texture_pointers(nodes: ui_node_t[], i: i32) {
  222. for (let i: i32 = 0; i < nodes.length; ++i) {
  223. let n: ui_node_t = nodes[i];
  224. if (n.type == "TEX_IMAGE") {
  225. if (n.buttons[0].default_value[0] == i) {
  226. n.buttons[0].default_value[0] = 9999; // Texture deleted, use pink now
  227. }
  228. else if (n.buttons[0].default_value[0] > i) {
  229. n.buttons[0].default_value[0]--; // Offset by deleted texture
  230. }
  231. }
  232. }
  233. }
  234. function tab_textures_delete_texture(asset: asset_t) {
  235. let i: i32 = array_index_of(project_assets, asset);
  236. if (project_assets.length > 1) {
  237. context_raw.texture = project_assets[i == project_assets.length - 1 ? i - 1 : i + 1];
  238. }
  239. ui_base_hwnds[tab_area_t.STATUS].redraws = 2;
  240. if (context_raw.tool == workspace_tool_t.COLORID && i == context_raw.colorid_handle.position) {
  241. ui_header_handle.redraws = 2;
  242. context_raw.ddirty = 2;
  243. context_raw.colorid_picked = false;
  244. ui_toolbar_handle.redraws = 1;
  245. }
  246. data_delete_image(asset.file);
  247. map_delete(project_asset_map, asset.id);
  248. array_splice(project_assets, i, 1);
  249. array_splice(project_asset_names, i, 1);
  250. sys_notify_on_next_frame(function () {
  251. make_material_parse_paint_material();
  252. ///if is_paint
  253. util_render_make_material_preview();
  254. ui_base_hwnds[tab_area_t.SIDEBAR1].redraws = 2;
  255. ///end
  256. });
  257. ///if is_paint
  258. for (let i: i32 = 0; i < project_materials.length; ++i) {
  259. let m: slot_material_t = project_materials[i];
  260. tab_textures_update_texture_pointers(m.canvas.nodes, i);
  261. }
  262. for (let i: i32 = 0; i < project_brushes.length; ++i) {
  263. let b: slot_brush_t = project_brushes[i];
  264. tab_textures_update_texture_pointers(b.canvas.nodes, i);
  265. }
  266. ///end
  267. }