WelcomeFrame.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.gravity = Atomic.UI_GRAVITY_ALL;
  33. this.recentList = new Atomic.UIListView();
  34. this.recentList.rootList.id = "recentList";
  35. recentProjects.addChild(this.recentList);
  36. var container = <Atomic.UILayout>parent.getWidget("resourceviewcontainer");
  37. container.addChild(this);
  38. this.updateRecentProjects();
  39. this.subscribeToEvent(EditorEvents.CloseProject, () => {
  40. this.updateRecentProjects();
  41. });
  42. this.initExampleBrowser();
  43. }
  44. handleClickedExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  45. var ops = EditorUI.getModelOps();
  46. var env = ToolCore.toolEnvironment;
  47. ops.showCreateProject(example);
  48. }
  49. addExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  50. var fileSystem = Atomic.getFileSystem();
  51. // Verify that at least one of the projects for this example exists, otherwise bounce out
  52. let exists = false;
  53. example.templates.forEach(template => {
  54. if (fileSystem.dirExists(template.folder)) {
  55. exists = true;
  56. }
  57. });
  58. if (!exists) {
  59. return;
  60. }
  61. var exlayout = <Atomic.UILayout>this.getWidget("examples_layout");
  62. if (!this.currentExampleLayout) {
  63. this.currentExampleLayout = new Atomic.UILayout();
  64. this.currentExampleLayout.spacing = 8;
  65. exlayout.addChild(this.currentExampleLayout);
  66. }
  67. // 200x150
  68. var exampleLayout = new Atomic.UILayout();
  69. exampleLayout.skinBg = "StarCondition";
  70. exampleLayout.axis = Atomic.UI_AXIS_Y;
  71. exampleLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  72. exampleLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  73. // IMAGE BUTTON
  74. var id = example.name;
  75. var button = new Atomic.UIButton();
  76. button.skinBg = "StarButton";
  77. button.id = id;
  78. var image = new Atomic.UIImageWidget();
  79. button.onClick = () => {
  80. this.handleClickedExample(example);
  81. };
  82. image.image = example.screenshot;
  83. image.skinBg = "ImageFrame";
  84. var rect = [0, 0, image.imageWidth / 2, image.imageHeight / 2];
  85. image.rect = rect;
  86. // NAME FIELD
  87. var nameField = new Atomic.UITextField();
  88. nameField.skinBg = "ImageCaption";
  89. nameField.text = example.name;
  90. var nameRect = [0, image.imageHeight / 2 - 16, image.imageWidth / 2, image.imageHeight / 2];
  91. nameField.rect = nameRect;
  92. nameField.gravity = Atomic.UI_GRAVITY_BOTTOM;
  93. image.addChild(nameField);
  94. button.addChild(image);
  95. var lp = new Atomic.UILayoutParams();
  96. lp.minWidth = image.imageWidth / 2;
  97. lp.minHeight = image.imageHeight / 2;
  98. button.layoutParams = lp;
  99. button.gravity = Atomic.UI_GRAVITY_LEFT;
  100. exampleLayout.addChild(button);
  101. // DESC TEXT
  102. var descField = new Atomic.UIEditField();
  103. descField.styling = true;
  104. descField.multiline = true;
  105. descField.readOnly = true;
  106. descField.wrapping = true;
  107. descField.skinBg = "AccentColor4";
  108. descField.text = example.desc;
  109. descField.adaptToContentSize = true;
  110. lp.height = 42;
  111. lp.width = image.imageWidth / 2;
  112. descField.layoutParams = lp;
  113. exampleLayout.addChild(descField);
  114. this.currentExampleLayout.addChild(exampleLayout);
  115. this.exampleCount++;
  116. // three across, todo, be smarter about this
  117. if (!(this.exampleCount % 3)) {
  118. this.currentExampleLayout = null;
  119. }
  120. }
  121. initExampleBrowser() {
  122. let examples = ProjectTemplates.getExampleProjectTemplateDefinitions();
  123. for (var i = 0; i < examples.length; i++) {
  124. this.addExample(examples[i]);
  125. }
  126. }
  127. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  128. if (ev.type == Atomic.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  129. if (ev.target.id == "recentList") {
  130. this.openFrameMenu(ev.x, ev.y);
  131. }
  132. }
  133. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  134. var id = ev.target.id;
  135. if (id == "open project") {
  136. var utils = new Editor.FileUtils();
  137. var path = utils.openProjectFileDialog();
  138. if (path) {
  139. this.sendEvent(EditorEvents.LoadProject, { path: path });
  140. }
  141. return true;
  142. }
  143. if (id == "new project") {
  144. var mo = EditorUI.getModelOps();
  145. mo.showNewProject();
  146. return true;
  147. }
  148. if (id == "recentList") {
  149. if (!this.recentList.getSelectedItemID()) {
  150. return;
  151. }
  152. var path: string = this.recent[this.recentList.getSelectedItemID()];
  153. this.sendEvent(EditorEvents.LoadProject, { path: path });
  154. }
  155. if (id == "recentProjectsContextMenu") {
  156. var prefs = Preferences.getInstance();
  157. if (ev.refid == "clear recent projects") {
  158. prefs.deleteRecentProjects();
  159. this.updateRecentProjects();
  160. }
  161. }
  162. }
  163. }
  164. updateRecentProjects() {
  165. this.recentList.deleteAllItems();
  166. var prefs = Preferences.getInstance();
  167. prefs.updateRecentProjects();
  168. this.recent = prefs.recentProjects;
  169. for (var i in this.recent) {
  170. this.recentList.addRootItem(this.recent[i], "Folder.icon", i);
  171. }
  172. }
  173. private openFrameMenu(x: number, y: number) {
  174. var menu = new Atomic.UIMenuWindow(this, "recentProjectsContextMenu");
  175. var menuButtons = new Atomic.UISelectItemSource();
  176. menuButtons.addItem(new Atomic.UISelectItem("Clear Recent Projects", "clear recent projects"));
  177. menu.show(menuButtons, x, y);
  178. }
  179. // examples
  180. exampleCount = 0;
  181. currentExampleLayout: Atomic.UILayout;
  182. recent: string[] = [];
  183. recentList: Atomic.UIListView;
  184. }
  185. export = WelcomeFrame;