tab_browser.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. let tab_browser_hpath: ui_handle_t = ui_handle_create();
  2. let tab_browser_hsearch: ui_handle_t = ui_handle_create();
  3. let tab_browser_known: bool = false;
  4. let tab_browser_last_path: string = "";
  5. let _tab_browser_draw_file: string;
  6. let _tab_browser_draw_b: string;
  7. let tab_browser_refresh: bool = false;
  8. function tab_browser_show_directory(directory: string) {
  9. tab_browser_hpath.text = directory;
  10. tab_browser_hsearch.text = "";
  11. ui_base_htabs[tab_area_t.STATUS].i = 0;
  12. config_raw.layout_tabs[tab_area_t.STATUS] = 0;
  13. }
  14. function tab_browser_draw(htab: ui_handle_t) {
  15. if (ui_tab(htab, tr("Browser")) && ui._window_h > ui_statusbar_default_h * UI_SCALE()) {
  16. if (config_raw.bookmarks == null) {
  17. config_raw.bookmarks = [];
  18. }
  19. let bookmarks_w: i32 = math_floor(100 * UI_SCALE());
  20. let show_full: bool = ui._w > (500 * UI_SCALE());
  21. if (tab_browser_hpath.text == "" && config_raw.bookmarks.length > 0) { // Init to first bookmark
  22. tab_browser_hpath.text = config_raw.bookmarks[0];
  23. /// if arm_windows
  24. tab_browser_hpath.text = string_replace_all(tab_browser_hpath.text, "/", "\\");
  25. /// end
  26. }
  27. ui_begin_sticky();
  28. if (show_full) {
  29. let step: f32 = (1.0 - bookmarks_w / ui._w);
  30. if (tab_browser_hsearch.text != "") {
  31. let row: f32[] = [ bookmarks_w / ui._w, step * 0.07, step * 0.07, step * 0.66, step * 0.17, step * 0.03 ];
  32. ui_row(row);
  33. }
  34. else {
  35. let row: f32[] = [ bookmarks_w / ui._w, step * 0.07, step * 0.07, step * 0.66, step * 0.2 ];
  36. ui_row(row);
  37. }
  38. // Bookmark
  39. if (ui_icon_button(tr("Bookmark"), icon_t.PLUS, ui_align_t.LEFT)) {
  40. let bookmark: string = tab_browser_hpath.text;
  41. /// if arm_windows
  42. bookmark = string_replace_all(bookmark, "\\", "/");
  43. /// end
  44. array_push(config_raw.bookmarks, bookmark);
  45. config_save();
  46. }
  47. // Refresh
  48. let in_focus: bool =
  49. 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;
  50. if (ui_icon_button(tr("Refresh"), icon_t.REFRESH) || (in_focus && ui.is_key_pressed && ui.key_code == key_code_t.F5)) {
  51. tab_browser_refresh = true;
  52. }
  53. }
  54. else {
  55. // Up, Refresh
  56. let row: f32[] = [ 1 / 4, 3 / 4 ];
  57. ui_row(row);
  58. }
  59. // Previous folder
  60. let text: string = tab_browser_hpath.text;
  61. let i1: i32 = string_index_of(text, path_sep);
  62. let nested: bool = i1 > -1 && text.length - 1 > i1;
  63. /// if arm_windows
  64. // Server addresses like \\server are not nested
  65. nested = nested && !(text.length >= 2 && char_at(text, 0) == path_sep && char_at(text, 1) == path_sep && string_last_index_of(text, path_sep) == 1);
  66. /// end
  67. ui.enabled = nested;
  68. if (ui_icon_button("", icon_t.CHEVRON_LEFT)) {
  69. ui_files_go_up(tab_browser_hpath);
  70. }
  71. ui.enabled = true;
  72. if (ui.is_hovered) {
  73. ui_tooltip(tr("Previous folder"));
  74. }
  75. /// if arm_android
  76. let stripped: bool = false;
  77. let strip: string = "/storage/emulated/0/";
  78. if (starts_with(tab_browser_hpath.text, strip)) {
  79. tab_browser_hpath.text = substring(tab_browser_hpath.text, strip.length - 1, tab_browser_hpath.text.length);
  80. stripped = true;
  81. }
  82. /// end
  83. tab_browser_hpath.text = ui_text_input(tab_browser_hpath, tr("Path"));
  84. /// if arm_android
  85. if (stripped) {
  86. tab_browser_hpath.text = "/storage/emulated/0" + tab_browser_hpath.text;
  87. }
  88. /// end
  89. if (show_full) {
  90. tab_browser_hsearch.text = ui_text_input(tab_browser_hsearch, tr("Search"), ui_align_t.LEFT, true, true);
  91. if (ui.is_hovered) {
  92. ui_tooltip(tr("ctrl+f to search") + "\n" + tr("esc to cancel"));
  93. }
  94. if (ui.is_ctrl_down && ui.is_key_pressed && ui.key_code == key_code_t.F) { // Start searching via ctrl+f
  95. ui_start_text_edit(tab_browser_hsearch);
  96. }
  97. if (tab_browser_hsearch.text != "" && (ui_button(tr("X")) || ui.is_escape_down)) {
  98. tab_browser_hsearch.text = "";
  99. }
  100. }
  101. ui_end_sticky();
  102. if (tab_browser_last_path != tab_browser_hpath.text) {
  103. tab_browser_hsearch.text = "";
  104. }
  105. tab_browser_last_path = tab_browser_hpath.text;
  106. let _y: f32 = ui._y;
  107. if (show_full) {
  108. ui._x = bookmarks_w;
  109. ui._w -= bookmarks_w;
  110. }
  111. ui_files_file_browser(tab_browser_hpath, true, tab_browser_hsearch.text, tab_browser_refresh, function(file: string) {
  112. _tab_browser_draw_file = file;
  113. // Context menu
  114. ui_menu_draw(function() {
  115. let file: string = _tab_browser_draw_file;
  116. if (ui_menu_button(tr("Import"), "", icon_t.IMPORT)) {
  117. import_asset_run(file);
  118. }
  119. if (path_is_texture(file)) {
  120. if (ui_menu_button(tr("Set as Envmap"), "", icon_t.LANDSCAPE)) {
  121. import_asset_run(file, -1.0, -1.0, true, true, function() {
  122. sys_notify_on_next_frame(function() {
  123. let file: string = _tab_browser_draw_file;
  124. let asset_index: i32 = -1;
  125. for (let i: i32 = 0; i < project_assets.length; ++i) {
  126. if (project_assets[i].file == file) {
  127. asset_index = i;
  128. break;
  129. }
  130. }
  131. if (asset_index != -1) {
  132. import_envmap_run(file, project_get_image(project_assets[asset_index]));
  133. }
  134. });
  135. });
  136. }
  137. if (ui_menu_button(tr("Set as Mask"), "", icon_t.MASK)) {
  138. import_asset_run(file, -1.0, -1.0, true, true, function() {
  139. sys_notify_on_next_frame(function() {
  140. let file: string = _tab_browser_draw_file;
  141. let asset_index: i32 = -1;
  142. for (let i: i32 = 0; i < project_assets.length; ++i) {
  143. if (project_assets[i].file == file) {
  144. asset_index = i;
  145. break;
  146. }
  147. }
  148. if (asset_index != -1) {
  149. layers_create_image_mask(project_assets[asset_index]);
  150. }
  151. });
  152. });
  153. }
  154. if (ui_menu_button(tr("Set as Color ID Map"), "", icon_t.COLOR_ID)) {
  155. import_asset_run(file, -1.0, -1.0, true, true, function() {
  156. sys_notify_on_next_frame(function() {
  157. let file: string = _tab_browser_draw_file;
  158. let asset_index: i32 = -1;
  159. for (let i: i32 = 0; i < project_assets.length; ++i) {
  160. if (project_assets[i].file == file) {
  161. asset_index = i;
  162. break;
  163. }
  164. }
  165. if (asset_index != -1) {
  166. context_raw.colorid_handle.i = asset_index;
  167. context_raw.colorid_picked = false;
  168. ui_toolbar_handle.redraws = 1;
  169. if (context_raw.tool == tool_type_t.COLORID) {
  170. ui_header_handle.redraws = 2;
  171. context_raw.ddirty = 2;
  172. }
  173. }
  174. });
  175. });
  176. }
  177. }
  178. if (ui_menu_button(tr("Open Externally"))) {
  179. file_start(file);
  180. }
  181. });
  182. });
  183. tab_browser_refresh = false;
  184. if (tab_browser_known) {
  185. let path: string = tab_browser_hpath.text;
  186. sys_notify_on_next_frame(function(path: string) {
  187. import_asset_run(path);
  188. }, path);
  189. tab_browser_hpath.text = substring(tab_browser_hpath.text, 0, string_last_index_of(tab_browser_hpath.text, path_sep));
  190. }
  191. let hpath_text: string = tab_browser_hpath.text;
  192. tab_browser_known =
  193. string_index_of(substring(tab_browser_hpath.text, string_last_index_of(tab_browser_hpath.text, path_sep), hpath_text.length), ".") > 0;
  194. /// if arm_android
  195. if (ends_with(tab_browser_hpath.text, "." + to_lower_case(manifest_title))) {
  196. tab_browser_known = false;
  197. }
  198. /// end
  199. if (show_full) {
  200. let bottom_y: i32 = ui._y;
  201. ui._x = 0;
  202. ui._y = _y;
  203. ui._w = bookmarks_w;
  204. if (ui_icon_button(tr("Cloud"), icon_t.CLOUD, ui_align_t.LEFT)) {
  205. tab_browser_hpath.text = "cloud";
  206. }
  207. if (ui_icon_button(tr("Disk"), icon_t.STORAGE, ui_align_t.LEFT)) {
  208. /// if arm_android
  209. ui_menu_draw(function() {
  210. if (ui_menu_button(tr("Download"), "", icon_t.FOLDER)) {
  211. tab_browser_hpath.text = ui_files_default_path;
  212. }
  213. if (ui_menu_button(tr("Pictures"), "", icon_t.FOLDER)) {
  214. tab_browser_hpath.text = "/storage/emulated/0/Pictures";
  215. }
  216. if (ui_menu_button(tr("Camera"), "", icon_t.FOLDER)) {
  217. tab_browser_hpath.text = "/storage/emulated/0/DCIM/Camera";
  218. }
  219. if (ui_menu_button(tr("Projects"), "", icon_t.FOLDER)) {
  220. tab_browser_hpath.text = iron_internal_save_path();
  221. }
  222. });
  223. /// else
  224. tab_browser_hpath.text = ui_files_default_path;
  225. /// end
  226. }
  227. for (let i: i32 = 0; i < config_raw.bookmarks.length; ++i) {
  228. let b: string = config_raw.bookmarks[i];
  229. let folder: string = substring(b, string_last_index_of(b, "/") + 1, b.length);
  230. if (ui_icon_button(folder, icon_t.FOLDER)) {
  231. tab_browser_hpath.text = b;
  232. /// if arm_windows
  233. tab_browser_hpath.text = string_replace_all(tab_browser_hpath.text, "/", "\\");
  234. /// end
  235. }
  236. if (ui.is_hovered && ui.input_released_r) {
  237. _tab_browser_draw_b = b;
  238. ui_menu_draw(function() {
  239. if (ui_menu_button(tr("Delete"), "", icon_t.DELETE)) {
  240. array_remove(config_raw.bookmarks, _tab_browser_draw_b);
  241. config_save();
  242. }
  243. });
  244. }
  245. }
  246. if (ui._y < bottom_y) {
  247. ui._y = bottom_y;
  248. }
  249. }
  250. }
  251. }