BoxProjects.hx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package arm.ui;
  2. import zui.Zui;
  3. import zui.Id;
  4. import arm.io.ImportArm;
  5. import arm.sys.Path;
  6. import arm.sys.File;
  7. @:access(zui.Zui)
  8. class BoxProjects {
  9. public static var htab = Id.handle();
  10. static var hsearch = new Handle();
  11. static var iconMap: Map<String, kha.Image> = null;
  12. public static function show() {
  13. if (iconMap != null) {
  14. for (handle in iconMap.keys()) {
  15. iron.data.Data.deleteImage(handle);
  16. }
  17. iconMap = null;
  18. }
  19. #if (krom_android || krom_ios)
  20. var draggable = false;
  21. #else
  22. var draggable = true;
  23. #end
  24. UIBox.showCustom(function(ui: Zui) {
  25. #if (krom_android || krom_ios)
  26. alignToFullScreen();
  27. #end
  28. #if (krom_android || krom_ios)
  29. projectsTab(ui);
  30. getStartedTab(ui);
  31. #else
  32. recentProjectsTab(ui);
  33. #end
  34. }, 600, 400, null, draggable);
  35. }
  36. static function projectsTab(ui: Zui) {
  37. if (ui.tab(htab, tr("Projects"), true)) {
  38. ui.beginSticky();
  39. drawBadge(ui);
  40. if (ui.button(tr("New"))) {
  41. Project.projectNew();
  42. Viewport.scaleToBounds();
  43. UIBox.hide();
  44. // Pick unique name
  45. var i = 0;
  46. var j = 0;
  47. var title = tr("untitled") + i;
  48. while (j < Config.raw.recent_projects.length) {
  49. var base = Config.raw.recent_projects[j];
  50. base = base.substring(base.lastIndexOf(arm.sys.Path.sep) + 1, base.lastIndexOf("."));
  51. j++;
  52. if (title == base) {
  53. i++;
  54. title = tr("untitled") + i;
  55. j = 0;
  56. }
  57. }
  58. kha.Window.get(0).title = title;
  59. }
  60. ui.endSticky();
  61. ui.separator(3, false);
  62. var slotw = Std.int(150 * ui.SCALE());
  63. var num = Std.int(kha.System.windowWidth() / slotw);
  64. var recent_projects = Config.raw.recent_projects;
  65. var show_asset_names = true;
  66. for (row in 0...Std.int(Math.ceil(recent_projects.length / num))) {
  67. var mult = show_asset_names ? 2 : 1;
  68. ui.row([for (i in 0...num * mult) 1 / num]);
  69. ui._x += 2;
  70. var off = show_asset_names ? ui.ELEMENT_OFFSET() * 16.0 : 6;
  71. if (row > 0) ui._y += off;
  72. for (j in 0...num) {
  73. var imgw = Std.int(128 * ui.SCALE());
  74. var i = j + row * num;
  75. if (i >= recent_projects.length) {
  76. @:privateAccess ui.endElement(imgw);
  77. if (show_asset_names) @:privateAccess ui.endElement(0);
  78. continue;
  79. }
  80. var path = recent_projects[i];
  81. #if krom_ios
  82. var documentDirectory = Krom.saveDialog("", "");
  83. documentDirectory = documentDirectory.substr(0, documentDirectory.length - 8); // Strip /'untitled'
  84. path = documentDirectory + path;
  85. #end
  86. var iconPath = path.substr(0, path.length - 4) + "_icon.png";
  87. if (iconMap == null) iconMap = [];
  88. var icon = iconMap.get(iconPath);
  89. if (icon == null) {
  90. iron.data.Data.getImage(iconPath, function(image: kha.Image) {
  91. icon = image;
  92. iconMap.set(iconPath, icon);
  93. });
  94. }
  95. var uix = ui._x;
  96. if (icon != null) {
  97. ui.fill(0, 0, 128, 128, ui.t.SEPARATOR_COL);
  98. var state = ui.image(icon, 0xffffffff, 128 * ui.SCALE());
  99. if (state == Released) {
  100. var _uix = ui._x;
  101. ui._x = uix;
  102. ui.fill(0, 0, 128, 128, 0x66000000);
  103. ui._x = _uix;
  104. function doImport() {
  105. iron.App.notifyOnInit(function() {
  106. UIBox.hide();
  107. ImportArm.runProject(path);
  108. });
  109. }
  110. #if (krom_android || krom_ios)
  111. arm.App.notifyOnNextFrame(function() {
  112. Console.toast(tr("Opening project"));
  113. arm.App.notifyOnNextFrame(doImport);
  114. });
  115. #else
  116. doImport();
  117. #end
  118. }
  119. var name = path.substring(path.lastIndexOf(arm.sys.Path.sep) + 1, path.lastIndexOf("."));
  120. if (ui.isHovered && ui.inputReleasedR) {
  121. UIMenu.draw(function(ui: Zui) {
  122. // if (UIMenu.menuButton(ui, tr("Duplicate"))) {}
  123. if (UIMenu.menuButton(ui, tr("Delete"))) {
  124. iron.App.notifyOnInit(function() {
  125. arm.sys.File.delete(path);
  126. arm.sys.File.delete(iconPath);
  127. var dataPath = path.substr(0, path.length - 4);
  128. arm.sys.File.delete(dataPath);
  129. recent_projects.splice(i, 1);
  130. });
  131. }
  132. }, 1);
  133. }
  134. if (show_asset_names) {
  135. ui._x = uix - (150 - 128) / 2;
  136. ui._y += slotw * 0.9;
  137. ui.text(name, Center);
  138. if (ui.isHovered) ui.tooltip(name);
  139. ui._y -= slotw * 0.9;
  140. if (i == recent_projects.length - 1) {
  141. ui._y += j == num - 1 ? imgw : imgw + ui.ELEMENT_H() + ui.ELEMENT_OFFSET();
  142. }
  143. }
  144. }
  145. else {
  146. @:privateAccess ui.endElement(0);
  147. if (show_asset_names) @:privateAccess ui.endElement(0);
  148. ui._x = uix;
  149. }
  150. }
  151. ui._y += 150;
  152. }
  153. }
  154. }
  155. static function recentProjectsTab(ui: Zui) {
  156. if (ui.tab(htab, tr("Recent"), true)) {
  157. drawBadge(ui);
  158. ui.enabled = Config.raw.recent_projects.length > 0;
  159. hsearch.text = ui.textInput(hsearch, tr("Search"), Align.Left, true, true);
  160. ui.enabled = true;
  161. for (path in Config.raw.recent_projects) {
  162. var file = path;
  163. #if krom_windows
  164. file = path.replace("/", "\\");
  165. #else
  166. file = path.replace("\\", "/");
  167. #end
  168. file = file.substr(file.lastIndexOf(Path.sep) + 1);
  169. if (file.toLowerCase().indexOf(hsearch.text.toLowerCase()) < 0) continue; // Search filter
  170. if (ui.button(file, Left) && arm.sys.File.exists(path)) {
  171. var current = @:privateAccess kha.graphics2.Graphics.current;
  172. if (current != null) current.end();
  173. ImportArm.runProject(path);
  174. if (current != null) current.begin(false);
  175. UIBox.hide();
  176. }
  177. if (ui.isHovered) ui.tooltip(path);
  178. }
  179. ui.enabled = Config.raw.recent_projects.length > 0;
  180. if (ui.button(tr("Clear"), Left)) {
  181. Config.raw.recent_projects = [];
  182. Config.save();
  183. }
  184. ui.enabled = true;
  185. ui.endElement();
  186. if (ui.button(tr("New Project..."), Left)) Project.projectNewBox();
  187. if (ui.button(tr("Open..."), Left)) Project.projectOpen();
  188. }
  189. }
  190. static function drawBadge(ui: Zui) {
  191. iron.data.Data.getImage("badge.k", function(img) {
  192. ui.image(img);
  193. ui.endElement();
  194. });
  195. }
  196. static function getStartedTab(ui: Zui) {
  197. if (ui.tab(htab, tr("Get Started"), true)) {
  198. if (ui.button(tr("Manual"))) {
  199. File.loadUrl(Manifest.url + "/manual");
  200. }
  201. if (ui.button(tr("How To"))) {
  202. File.loadUrl(Manifest.url + "/howto");
  203. }
  204. if (ui.button(tr("What's New"))) {
  205. File.loadUrl(Manifest.url + "/notes");
  206. }
  207. }
  208. }
  209. static function alignToFullScreen() {
  210. @:privateAccess UIBox.modalW = Std.int(kha.System.windowWidth() / App.uiBox.SCALE());
  211. @:privateAccess UIBox.modalH = Std.int(kha.System.windowHeight() / App.uiBox.SCALE());
  212. var appw = kha.System.windowWidth();
  213. var apph = kha.System.windowHeight();
  214. var mw = appw;
  215. var mh = apph;
  216. UIBox.hwnd.dragX = Std.int(-appw / 2 + mw / 2);
  217. UIBox.hwnd.dragY = Std.int(-apph / 2 + mh / 2);
  218. }
  219. }