tab_textures.ts 9.5 KB

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