WelcomeFrame.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 EditorEvents = require("editor/EditorEvents");
  23. import EditorUI = require("ui/EditorUI");
  24. import ScriptWidget = require("ui/ScriptWidget");
  25. import Preferences = require("editor/Preferences");
  26. import ProjectTemplates = require("resources/ProjectTemplates");
  27. class WelcomeFrame extends ScriptWidget {
  28. constructor(parent: Atomic.UIWidget) {
  29. super();
  30. this.load("AtomicEditor/editor/ui/welcomeframe.tb.txt");
  31. var recentProjects = <Atomic.UILayout>this.getWidget("recentprojects");
  32. this.examplesLayout = <Atomic.UILayout>this.getWidget("examples_layout");
  33. this.examplesCSharp = <Atomic.UIButton>this.getWidget("examples_csharp");
  34. this.examplesJavaScript = <Atomic.UIButton>this.getWidget("examples_javascript");
  35. this.examplesTypeScript = <Atomic.UIButton>this.getWidget("examples_typescript");
  36. this.examplesCSharp.onClick = () => { this.handleExampleFilter(); };
  37. this.examplesJavaScript.onClick = () => { this.handleExampleFilter(); };
  38. this.examplesTypeScript.onClick = () => { this.handleExampleFilter(); };
  39. this.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_ALL;
  40. this.recentList = new Atomic.UIListView();
  41. this.recentList.rootList.id = "recentList";
  42. recentProjects.addChild(this.recentList);
  43. var container = <Atomic.UILayout>parent.getWidget("resourceviewcontainer");
  44. container.addChild(this);
  45. this.updateRecentProjects();
  46. this.subscribeToEvent(Editor.EditorCloseProjectEvent(() => {
  47. this.updateRecentProjects();
  48. }));
  49. this.initExampleBrowser();
  50. }
  51. handleClickedExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  52. var ops = EditorUI.getModelOps();
  53. ops.showCreateProject(example);
  54. }
  55. addExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  56. var fileSystem = Atomic.getFileSystem();
  57. let languages = [];
  58. if (this.examplesCSharp.value) {
  59. languages.push("CSharp");
  60. }
  61. if (this.examplesJavaScript.value) {
  62. languages.push("JavaScript");
  63. }
  64. if (this.examplesTypeScript.value) {
  65. languages.push("TypeScript");
  66. }
  67. // If user doesn't select any languages, show 'em all'
  68. if (!languages.length) {
  69. languages = ["CSharp", "JavaScript", "TypeScript"];
  70. }
  71. let exists = false;
  72. for (var i = 0; i < languages.length; i++) {
  73. if (example.languages.indexOf(languages[i]) != -1) {
  74. exists = true;
  75. break;
  76. }
  77. }
  78. if (!exists) {
  79. return;
  80. }
  81. if (!this.currentExampleLayout) {
  82. this.currentExampleLayout = new Atomic.UILayout();
  83. this.currentExampleLayout.spacing = 8;
  84. this.examplesLayout.addChild(this.currentExampleLayout);
  85. }
  86. // 200x150
  87. var exampleLayout = new Atomic.UILayout();
  88. exampleLayout.skinBg = "StarCondition";
  89. exampleLayout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  90. exampleLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  91. exampleLayout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  92. // IMAGE BUTTON
  93. var id = example.name;
  94. var button = new Atomic.UIButton();
  95. button.skinBg = "StarButton";
  96. button.id = id;
  97. var image = new Atomic.UIImageWidget();
  98. button.onClick = () => {
  99. this.handleClickedExample(example);
  100. };
  101. image.image = example.screenshot;
  102. image.skinBg = "ImageFrame";
  103. var rect = [0, 0, image.imageWidth / 2, image.imageHeight / 2];
  104. image.rect = rect;
  105. // NAME FIELD
  106. var nameField = new Atomic.UITextField();
  107. nameField.skinBg = "ImageCaption";
  108. nameField.text = example.name;
  109. var nameRect = [0, image.imageHeight / 2 - 16, image.imageWidth / 2, image.imageHeight / 2];
  110. nameField.rect = nameRect;
  111. nameField.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_BOTTOM;
  112. image.addChild(nameField);
  113. button.addChild(image);
  114. var lp = new Atomic.UILayoutParams();
  115. lp.minWidth = image.imageWidth / 2;
  116. lp.minHeight = image.imageHeight / 2;
  117. button.layoutParams = lp;
  118. button.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT;
  119. exampleLayout.addChild(button);
  120. // DESC TEXT
  121. var descField = new Atomic.UIEditField();
  122. descField.styling = true;
  123. descField.multiline = true;
  124. descField.readOnly = true;
  125. descField.wrapping = true;
  126. descField.skinBg = "AccentColor4";
  127. descField.text = example.desc;
  128. descField.adaptToContentSize = true;
  129. lp.height = 42;
  130. lp.width = image.imageWidth / 2;
  131. descField.layoutParams = lp;
  132. exampleLayout.addChild(descField);
  133. this.currentExampleLayout.addChild(exampleLayout);
  134. this.exampleCount++;
  135. // three across, todo, be smarter about this
  136. if (!(this.exampleCount % 3)) {
  137. this.currentExampleLayout = null;
  138. }
  139. }
  140. handleExampleFilter() {
  141. this.initExampleBrowser();
  142. }
  143. initExampleBrowser() {
  144. this.exampleCount = 0;
  145. this.examplesLayout.deleteAllChildren();
  146. this.currentExampleLayout = null;
  147. let examples = ProjectTemplates.getExampleProjectTemplateDefinitions();
  148. for (var i = 0; i < examples.length; i++) {
  149. this.addExample(examples[i]);
  150. }
  151. }
  152. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  153. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  154. if (ev.target.id == "recentList") {
  155. this.openFrameMenu(ev.x, ev.y);
  156. }
  157. }
  158. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  159. var id = ev.target.id;
  160. if (id == "open project") {
  161. var utils = new Editor.FileUtils();
  162. var path = utils.openProjectFileDialog();
  163. if (path) {
  164. this.sendEvent(EditorEvents.RequestProjectLoad, { path: path });
  165. }
  166. return true;
  167. }
  168. if (id == "new project") {
  169. var mo = EditorUI.getModelOps();
  170. mo.showNewProject();
  171. return true;
  172. }
  173. if (id == "recentList") {
  174. if (!this.recentList.getSelectedItemID()) {
  175. return;
  176. }
  177. var path: string = this.recent[this.recentList.getSelectedItemID()];
  178. this.sendEvent(EditorEvents.RequestProjectLoad, { path: path });
  179. }
  180. if (id == "recentProjectsContextMenu") {
  181. var prefs = Preferences.getInstance();
  182. if (ev.refid == "clear recent projects") {
  183. prefs.deleteRecentProjects();
  184. this.updateRecentProjects();
  185. }
  186. }
  187. }
  188. }
  189. updateRecentProjects() {
  190. this.recentList.deleteAllItems();
  191. var prefs = Preferences.getInstance();
  192. prefs.updateRecentProjects();
  193. this.recent = prefs.recentProjects;
  194. for (var i in this.recent) {
  195. this.recentList.addRootItem(this.recent[i], "Folder.icon", i);
  196. }
  197. }
  198. private openFrameMenu(x: number, y: number) {
  199. var menu = new Atomic.UIMenuWindow(this, "recentProjectsContextMenu");
  200. var menuButtons = new Atomic.UISelectItemSource();
  201. menuButtons.addItem(new Atomic.UISelectItem("Clear Recent Projects", "clear recent projects"));
  202. menu.show(menuButtons, x, y);
  203. }
  204. // examples
  205. exampleCount = 0;
  206. currentExampleLayout: Atomic.UILayout;
  207. examplesLayout:Atomic.UILayout;
  208. examplesCSharp: Atomic.UIButton;
  209. examplesJavaScript: Atomic.UIButton;
  210. examplesTypeScript: Atomic.UIButton;
  211. recent: string[] = [];
  212. recentList: Atomic.UIListView;
  213. }
  214. export = WelcomeFrame;