box_projects.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. let box_projects_htab: ui_handle_t = ui_handle_create();
  2. let box_projects_hsearch: ui_handle_t = ui_handle_create();
  3. let box_projects_icon_map: map_t<string, gpu_texture_t> = null;
  4. let _box_projects_path: string;
  5. let _box_projects_icon_path: string;
  6. let _box_projects_i: i32;
  7. function box_projects_show() {
  8. if (box_projects_icon_map != null) {
  9. let keys: string[] = map_keys(box_projects_icon_map);
  10. for (let i: i32 = 0; i < keys.length; ++i) {
  11. let handle: string = keys[i];
  12. data_delete_image(handle);
  13. }
  14. box_projects_icon_map = null;
  15. }
  16. let draggable: bool;
  17. /// if (arm_android || arm_ios)
  18. draggable = false;
  19. /// else
  20. draggable = true;
  21. /// end
  22. ui_box_show_custom(function() {
  23. /// if (arm_android || arm_ios)
  24. box_projects_align_to_fullscreen();
  25. /// end
  26. /// if (arm_android || arm_ios)
  27. box_projects_tab();
  28. box_projects_get_started_tab();
  29. /// else
  30. box_projects_recent_tab();
  31. /// end
  32. }, 600, 400, null, draggable);
  33. }
  34. function box_projects_tab() {
  35. if (ui_tab(box_projects_htab, tr("Projects"), true)) {
  36. ui_begin_sticky();
  37. box_projects_draw_badge();
  38. if (ui_button(tr("New"))) {
  39. project_new();
  40. ui_box_hide();
  41. // Pick unique name
  42. let i: i32 = 0;
  43. let j: i32 = 0;
  44. let title: string = "" + tr("untitled") + i;
  45. while (j < config_raw.recent_projects.length) {
  46. let base: string = config_raw.recent_projects[j];
  47. base = substring(base, string_last_index_of(base, path_sep) + 1, string_last_index_of(base, "."));
  48. j++;
  49. if (title == base) {
  50. i++;
  51. title = "" + tr("untitled") + i;
  52. j = 0;
  53. }
  54. }
  55. sys_title_set(title);
  56. }
  57. ui_end_sticky();
  58. ui_separator(3, false);
  59. let slotw: i32 = math_floor(150 * UI_SCALE());
  60. let num: i32 = math_floor(iron_window_width() / slotw);
  61. if (num == 0) {
  62. return;
  63. }
  64. let recent_projects: string[] = config_raw.recent_projects;
  65. let show_asset_names: bool = true;
  66. for (let row: i32 = 0; row < math_ceil(recent_projects.length / num); ++row) {
  67. let mult: i32 = show_asset_names ? 2 : 1;
  68. let ar: f32[] = [];
  69. for (let i: i32 = 0; i < num * mult; ++i) {
  70. array_push(ar, 1 / num);
  71. }
  72. ui_row(ar);
  73. ui._x += 2;
  74. let off: f32 = show_asset_names ? UI_ELEMENT_OFFSET() * 16.0 : 6;
  75. if (row > 0) {
  76. ui._y += off;
  77. }
  78. for (let j: i32 = 0; j < num; ++j) {
  79. let imgw: i32 = math_floor(128 * UI_SCALE());
  80. let i: i32 = j + row * num;
  81. if (i >= recent_projects.length) {
  82. ui_end_element_of_size(imgw);
  83. if (show_asset_names) {
  84. ui_end_element_of_size(0);
  85. }
  86. continue;
  87. }
  88. let path: string = recent_projects[i];
  89. /// if arm_ios
  90. let document_directory: string = iron_save_dialog("", "");
  91. document_directory = substring(document_directory, 0, document_directory.length - 8); // Strip /"untitled"
  92. path = document_directory + path;
  93. /// end
  94. let icon_path: string = substring(path, 0, path.length - 4) + "_icon.png";
  95. if (box_projects_icon_map == null) {
  96. box_projects_icon_map = map_create();
  97. }
  98. let icon: gpu_texture_t = map_get(box_projects_icon_map, icon_path);
  99. if (icon == null) {
  100. let image: gpu_texture_t = data_get_image(icon_path);
  101. icon = image;
  102. map_set(box_projects_icon_map, icon_path, icon);
  103. }
  104. let uix: i32 = ui._x;
  105. if (icon != null) {
  106. ui_fill(0, 0, 128, 128, ui.ops.theme.SEPARATOR_COL);
  107. let state: i32 = ui_image(icon, 0xffffffff, 128 * UI_SCALE());
  108. if (state == ui_state_t.RELEASED) {
  109. let _uix: i32 = ui._x;
  110. ui._x = uix;
  111. ui_fill(0, 0, 128, 128, 0x66000000);
  112. ui._x = _uix;
  113. /// if (arm_android || arm_ios)
  114. console_toast(tr("Opening project"));
  115. /// end
  116. sys_notify_on_next_frame(function(path: string) {
  117. ui_box_hide();
  118. import_arm_run_project(path);
  119. }, path);
  120. }
  121. let name: string = substring(path, string_last_index_of(path, path_sep) + 1, string_last_index_of(path, "."));
  122. if (ui.is_hovered && ui.input_released_r) {
  123. _box_projects_path = path;
  124. _box_projects_icon_path = icon_path;
  125. _box_projects_i = i;
  126. ui_menu_draw(function() {
  127. // if (ui_menu_button(tr("Duplicate"), "", icon_t.DUPLICATE)) {}
  128. if (ui_menu_button(tr("Delete"), "", icon_t.DELETE)) {
  129. sys_notify_on_next_frame(function() {
  130. iron_delete_file(_box_projects_path);
  131. iron_delete_file(_box_projects_icon_path);
  132. let data_path: string = substring(_box_projects_path, 0, _box_projects_path.length - 4);
  133. iron_delete_file(data_path);
  134. let recent_projects: string[] = config_raw.recent_projects;
  135. array_splice(recent_projects, _box_projects_i, 1);
  136. });
  137. }
  138. });
  139. }
  140. if (show_asset_names) {
  141. ui._x = uix - (150 - 128) / 2;
  142. ui._y += slotw * 0.9;
  143. ui_text(name, ui_align_t.CENTER);
  144. if (ui.is_hovered) {
  145. ui_tooltip(name);
  146. }
  147. ui._y -= slotw * 0.9;
  148. if (i == recent_projects.length - 1) {
  149. ui._y += j == num - 1 ? imgw : imgw + UI_ELEMENT_H() + UI_ELEMENT_OFFSET();
  150. }
  151. }
  152. }
  153. else {
  154. ui_end_element_of_size(0);
  155. if (show_asset_names) {
  156. ui_end_element_of_size(0);
  157. }
  158. ui._x = uix;
  159. }
  160. }
  161. ui._y += 150;
  162. }
  163. }
  164. }
  165. function box_projects_recent_tab() {
  166. if (ui_tab(box_projects_htab, tr("Recent"), true)) {
  167. box_projects_draw_badge();
  168. ui.enabled = config_raw.recent_projects.length > 0;
  169. box_projects_hsearch.text = ui_text_input(box_projects_hsearch, tr("Search"), ui_align_t.LEFT, true, true);
  170. ui.enabled = true;
  171. for (let i: i32 = 0; i < config_raw.recent_projects.length; ++i) {
  172. let path: string = config_raw.recent_projects[i];
  173. /// if arm_windows
  174. path = string_replace_all(path, "/", "\\");
  175. /// else
  176. path = string_replace_all(path, "\\", "/");
  177. /// end
  178. let file: string = substring(path, string_last_index_of(path, path_sep) + 1, path.length);
  179. if (string_index_of(to_lower_case(file), to_lower_case(box_projects_hsearch.text)) < 0) {
  180. continue; // Search filter
  181. }
  182. if (ui_button(file, ui_align_t.LEFT) && iron_file_exists(path)) {
  183. let current: gpu_texture_t = _draw_current;
  184. let in_use: bool = gpu_in_use;
  185. if (in_use)
  186. draw_end();
  187. import_arm_run_project(path);
  188. if (in_use)
  189. draw_begin(current);
  190. ui_box_hide();
  191. }
  192. if (ui.is_hovered) {
  193. ui_tooltip(path);
  194. }
  195. }
  196. ui.enabled = config_raw.recent_projects.length > 0;
  197. if (ui_button(tr("Clear"), ui_align_t.LEFT)) {
  198. config_raw.recent_projects = [];
  199. config_save();
  200. }
  201. ui.enabled = true;
  202. ui_end_element();
  203. if (ui_button(tr("New Project..."), ui_align_t.LEFT)) {
  204. project_new_box();
  205. }
  206. if (ui_button(tr("Open..."), ui_align_t.LEFT)) {
  207. project_open();
  208. }
  209. }
  210. }
  211. function box_projects_draw_badge() {
  212. let img: gpu_texture_t = data_get_image("badge.k");
  213. ui_image(img);
  214. ui_end_element();
  215. }
  216. function box_projects_get_started_tab() {
  217. if (ui_tab(box_projects_htab, tr("Get Started"), true)) {
  218. if (ui_button(tr("Manual"))) {
  219. iron_load_url(manifest_url + "/manual");
  220. }
  221. if (ui_button(tr("How To"))) {
  222. iron_load_url(manifest_url + "/howto");
  223. }
  224. if (ui_button(tr("What's New"))) {
  225. iron_load_url(manifest_url + "/notes");
  226. }
  227. }
  228. }
  229. function box_projects_align_to_fullscreen() {
  230. ui_box_modalw = math_floor(iron_window_width() / UI_SCALE());
  231. ui_box_modalh = math_floor(iron_window_height() / UI_SCALE());
  232. let appw: i32 = iron_window_width();
  233. let apph: i32 = iron_window_height();
  234. let mw: i32 = appw;
  235. let mh: i32 = apph;
  236. ui_box_hwnd.drag_x = math_floor(-appw / 2 + mw / 2);
  237. ui_box_hwnd.drag_y = math_floor(-apph / 2 + mh / 2);
  238. }