WelcomeFrame.ts 9.0 KB

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