WelcomeFrame.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("ui/EditorUI");
  23. import ScriptWidget = require("ui/ScriptWidget");
  24. import Preferences = require("editor/Preferences");
  25. import ProjectTemplates = require("resources/ProjectTemplates");
  26. class WelcomeFrame extends ScriptWidget {
  27. constructor(parent: Atomic.UIWidget) {
  28. super();
  29. this.load("AtomicEditor/editor/ui/welcomeframe.tb.txt");
  30. var recentProjects = <Atomic.UILayout>this.getWidget("recentprojects");
  31. this.examplesLayout = <Atomic.UILayout>this.getWidget("examples_layout");
  32. this.examplesCSharp = <Atomic.UIButton>this.getWidget("examples_csharp");
  33. this.examplesJavaScript = <Atomic.UIButton>this.getWidget("examples_javascript");
  34. this.examplesTypeScript = <Atomic.UIButton>this.getWidget("examples_typescript");
  35. this.examplesCSharp.onClick = () => { this.handleExampleFilter(); };
  36. this.examplesJavaScript.onClick = () => { this.handleExampleFilter(); };
  37. this.examplesTypeScript.onClick = () => { this.handleExampleFilter(); };
  38. this.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  39. this.recentList = new Atomic.UIListView();
  40. this.recentList.rootList.id = "recentList";
  41. recentProjects.addChild(this.recentList);
  42. var container = <Atomic.UILayout>parent.getWidget("resourceviewcontainer");
  43. container.addChild(this);
  44. this.updateRecentProjects();
  45. this.subscribeToEvent(Editor.EditorCloseProjectEvent(() => {
  46. this.updateRecentProjects();
  47. }));
  48. this.initExampleBrowser();
  49. }
  50. handleClickedExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  51. var ops = EditorUI.getModelOps();
  52. ops.showCreateProject(example);
  53. }
  54. addExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  55. var fileSystem = Atomic.getFileSystem();
  56. let languages = [];
  57. if (this.examplesCSharp.value) {
  58. languages.push("CSharp");
  59. }
  60. if (this.examplesJavaScript.value) {
  61. languages.push("JavaScript");
  62. }
  63. if (this.examplesTypeScript.value) {
  64. languages.push("TypeScript");
  65. }
  66. // If user doesn't select any languages, show 'em all'
  67. if (!languages.length) {
  68. languages = ["CSharp", "JavaScript", "TypeScript"];
  69. }
  70. let exists = false;
  71. for (var i = 0; i < languages.length; i++) {
  72. if (example.languages.indexOf(languages[i]) != -1) {
  73. exists = true;
  74. break;
  75. }
  76. }
  77. if (!exists) {
  78. return;
  79. }
  80. if (!this.currentExampleLayout) {
  81. this.currentExampleLayout = new Atomic.UILayout();
  82. this.currentExampleLayout.spacing = 8;
  83. this.examplesLayout.addChild(this.currentExampleLayout);
  84. }
  85. // 200x150
  86. var exampleLayout = new Atomic.UILayout();
  87. exampleLayout.skinBg = "StarCondition";
  88. exampleLayout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  89. exampleLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  90. exampleLayout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  91. // IMAGE BUTTON
  92. var id = example.name;
  93. var button = new Atomic.UIButton();
  94. button.skinBg = "StarButton";
  95. button.id = id;
  96. var image = new Atomic.UIImageWidget();
  97. button.onClick = () => {
  98. this.handleClickedExample(example);
  99. };
  100. image.image = example.screenshot;
  101. image.skinBg = "ImageFrame";
  102. var rect = [0, 0, image.imageWidth / 2, image.imageHeight / 2];
  103. image.rect = rect;
  104. // NAME FIELD
  105. var nameField = new Atomic.UITextField();
  106. nameField.skinBg = "ImageCaption";
  107. nameField.text = example.name;
  108. var nameRect = [0, image.imageHeight / 2 - 16, image.imageWidth / 2, image.imageHeight / 2];
  109. nameField.rect = nameRect;
  110. nameField.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_BOTTOM;
  111. image.addChild(nameField);
  112. button.addChild(image);
  113. var lp = new Atomic.UILayoutParams();
  114. lp.minWidth = image.imageWidth / 2;
  115. lp.minHeight = image.imageHeight / 2;
  116. button.layoutParams = lp;
  117. button.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT;
  118. exampleLayout.addChild(button);
  119. // DESC TEXT
  120. var descField = new Atomic.UIEditField();
  121. descField.styling = true;
  122. descField.multiline = true;
  123. descField.readOnly = true;
  124. descField.wrapping = true;
  125. descField.skinBg = "AccentColor4";
  126. descField.text = example.desc;
  127. descField.adaptToContentSize = true;
  128. lp.height = 42;
  129. lp.width = image.imageWidth / 2;
  130. descField.layoutParams = lp;
  131. exampleLayout.addChild(descField);
  132. this.currentExampleLayout.addChild(exampleLayout);
  133. this.exampleCount++;
  134. // three across, todo, be smarter about this
  135. if (!(this.exampleCount % 3)) {
  136. this.currentExampleLayout = null;
  137. }
  138. }
  139. handleExampleFilter() {
  140. this.initExampleBrowser();
  141. }
  142. initExampleBrowser() {
  143. this.exampleCount = 0;
  144. this.examplesLayout.deleteAllChildren();
  145. this.currentExampleLayout = null;
  146. let examples = ProjectTemplates.getExampleProjectTemplateDefinitions();
  147. for (var i = 0; i < examples.length; i++) {
  148. this.addExample(examples[i]);
  149. }
  150. }
  151. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  152. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  153. if (ev.target.id == "recentList") {
  154. this.openFrameMenu(ev.x, ev.y);
  155. }
  156. }
  157. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  158. var id = ev.target.id;
  159. if (id == "open project") {
  160. var utils = new Editor.FileUtils();
  161. var path = utils.openProjectFileDialog();
  162. if (path) {
  163. this.sendEvent(Editor.RequestProjectLoadEventData({ path: path }));
  164. }
  165. return true;
  166. }
  167. if (id == "new project") {
  168. var mo = EditorUI.getModelOps();
  169. mo.showNewProject();
  170. return true;
  171. }
  172. if (id == "recentList") {
  173. if (!this.recentList.getSelectedItemID()) {
  174. return;
  175. }
  176. var path: string = this.recent[this.recentList.getSelectedItemID()];
  177. this.sendEvent(Editor.RequestProjectLoadEventData({ path: path }));
  178. }
  179. if (id == "recentProjectsContextMenu") {
  180. var prefs = Preferences.getInstance();
  181. if (ev.refid == "clear recent projects") {
  182. prefs.deleteRecentProjects();
  183. this.updateRecentProjects();
  184. }
  185. }
  186. }
  187. }
  188. updateRecentProjects() {
  189. this.recentList.deleteAllItems();
  190. var prefs = Preferences.getInstance();
  191. prefs.updateRecentProjects();
  192. this.recent = prefs.recentProjects;
  193. for (var i in this.recent) {
  194. this.recentList.addRootItem(this.recent[i], "Folder.icon", i);
  195. }
  196. }
  197. private openFrameMenu(x: number, y: number) {
  198. var menu = new Atomic.UIMenuWindow(this, "recentProjectsContextMenu");
  199. var menuButtons = new Atomic.UISelectItemSource();
  200. menuButtons.addItem(new Atomic.UISelectItem("Clear Recent Projects", "clear recent projects"));
  201. menu.show(menuButtons, x, y);
  202. }
  203. // examples
  204. exampleCount = 0;
  205. currentExampleLayout: Atomic.UILayout;
  206. examplesLayout:Atomic.UILayout;
  207. examplesCSharp: Atomic.UIButton;
  208. examplesJavaScript: Atomic.UIButton;
  209. examplesTypeScript: Atomic.UIButton;
  210. recent: string[] = [];
  211. recentList: Atomic.UIListView;
  212. }
  213. export = WelcomeFrame;