tab_textures.ts 9.5 KB

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