tab_browser.ts 8.4 KB

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