tab_browser.ts 8.4 KB

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