WelcomeFrame.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import EditorEvents = require("editor/EditorEvents");
  8. import EditorUI = require("ui/EditorUI");
  9. import ScriptWidget = require("ui/ScriptWidget");
  10. import Preferences = require("editor/Preferences");
  11. import ProjectTemplates = require("resources/ProjectTemplates");
  12. class WelcomeFrame extends ScriptWidget {
  13. constructor(parent: Atomic.UIWidget) {
  14. super();
  15. this.load("AtomicEditor/editor/ui/welcomeframe.tb.txt");
  16. var recentProjects = <Atomic.UILayout>this.getWidget("recentprojects");
  17. this.gravity = Atomic.UI_GRAVITY_ALL;
  18. this.recentList = new Atomic.UIListView();
  19. this.recentList.rootList.id = "recentList";
  20. recentProjects.addChild(this.recentList);
  21. var container = <Atomic.UILayout>parent.getWidget("resourceviewcontainer");
  22. container.addChild(this);
  23. this.updateRecentProjects();
  24. this.subscribeToEvent(EditorEvents.CloseProject, () => {
  25. this.updateRecentProjects();
  26. });
  27. this.initExampleBrowser();
  28. }
  29. handleClickedExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  30. var ops = EditorUI.getModelOps();
  31. var env = ToolCore.toolEnvironment;
  32. ops.showCreateProject(example);
  33. }
  34. addExample(example: ProjectTemplates.ProjectTemplateDefinition) {
  35. var fileSystem = Atomic.getFileSystem();
  36. // Verify that at least one of the projects for this example exists, otherwise bounce out
  37. let exists = false;
  38. example.templates.forEach(template => {
  39. if (fileSystem.dirExists(template.folder)) {
  40. exists = true;
  41. }
  42. });
  43. if (!exists) {
  44. return;
  45. }
  46. var exlayout = <Atomic.UILayout>this.getWidget("examples_layout");
  47. if (!this.currentExampleLayout) {
  48. this.currentExampleLayout = new Atomic.UILayout();
  49. this.currentExampleLayout.spacing = 8;
  50. exlayout.addChild(this.currentExampleLayout);
  51. }
  52. // 200x150
  53. var exampleLayout = new Atomic.UILayout();
  54. exampleLayout.skinBg = "StarCondition";
  55. exampleLayout.axis = Atomic.UI_AXIS_Y;
  56. exampleLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  57. exampleLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  58. // IMAGE BUTTON
  59. var id = example.name;
  60. var button = new Atomic.UIButton();
  61. button.skinBg = "StarButton";
  62. button.id = id;
  63. var image = new Atomic.UIImageWidget();
  64. button.onClick = () => {
  65. this.handleClickedExample(example);
  66. };
  67. image.image = example.screenshot;
  68. image.skinBg = "ImageFrame";
  69. var rect = [0, 0, image.imageWidth / 2, image.imageHeight / 2];
  70. image.rect = rect;
  71. // NAME FIELD
  72. var nameField = new Atomic.UITextField();
  73. nameField.skinBg = "ImageCaption";
  74. nameField.text = example.name;
  75. var nameRect = [0, image.imageHeight / 2 - 16, image.imageWidth / 2, image.imageHeight / 2];
  76. nameField.rect = nameRect;
  77. nameField.gravity = Atomic.UI_GRAVITY_BOTTOM;
  78. image.addChild(nameField);
  79. button.addChild(image);
  80. var lp = new Atomic.UILayoutParams();
  81. lp.minWidth = image.imageWidth / 2;
  82. lp.minHeight = image.imageHeight / 2;
  83. button.layoutParams = lp;
  84. button.gravity = Atomic.UI_GRAVITY_LEFT;
  85. exampleLayout.addChild(button);
  86. // DESC TEXT
  87. var descField = new Atomic.UIEditField();
  88. descField.styling = true;
  89. descField.multiline = true;
  90. descField.readOnly = true;
  91. descField.wrapping = true;
  92. var styleDesc = "<color #A9A9A9>" + example.desc + "</color>";
  93. descField.text = styleDesc;
  94. descField.adaptToContentSize = true;
  95. lp.height = 42;
  96. lp.width = image.imageWidth / 2;
  97. descField.layoutParams = lp;
  98. exampleLayout.addChild(descField);
  99. this.currentExampleLayout.addChild(exampleLayout);
  100. this.exampleCount++;
  101. // three across, todo, be smarter about this
  102. if (!(this.exampleCount % 3)) {
  103. this.currentExampleLayout = null;
  104. }
  105. }
  106. initExampleBrowser() {
  107. let examples = ProjectTemplates.getExampleProjectTemplateDefinitions();
  108. for (var i = 0; i < examples.length; i++) {
  109. this.addExample(examples[i]);
  110. }
  111. }
  112. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  113. if (ev.type == Atomic.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  114. if (ev.target.id == "recentList") {
  115. this.openFrameMenu(ev.x, ev.y);
  116. }
  117. }
  118. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  119. var id = ev.target.id;
  120. if (id == "open project") {
  121. var utils = new Editor.FileUtils();
  122. var path = utils.openProjectFileDialog();
  123. if (path) {
  124. this.sendEvent(EditorEvents.LoadProject, { path: path });
  125. }
  126. return true;
  127. }
  128. if (id == "new project") {
  129. var mo = EditorUI.getModelOps();
  130. mo.showNewProject();
  131. return true;
  132. }
  133. if (id == "recentList") {
  134. var path: string = this.recent[this.recentList.getSelectedItemID()];
  135. this.sendEvent(EditorEvents.LoadProject, { path: path });
  136. }
  137. if (id == "recentProjectsContextMenu") {
  138. var prefs = Preferences.getInstance();
  139. if (ev.refid == "clear recent projects") {
  140. prefs.deleteRecentProjects();
  141. this.updateRecentProjects();
  142. }
  143. }
  144. }
  145. }
  146. updateRecentProjects() {
  147. this.recentList.deleteAllItems();
  148. var prefs = Preferences.getInstance();
  149. prefs.updateRecentProjects();
  150. this.recent = prefs.recentProjects;
  151. for (var i in this.recent) {
  152. this.recentList.addRootItem(this.recent[i], "Folder.icon", i);
  153. }
  154. }
  155. private openFrameMenu(x: number, y: number) {
  156. var menu = new Atomic.UIMenuWindow(this, "recentProjectsContextMenu");
  157. var menuButtons = new Atomic.UISelectItemSource();
  158. menuButtons.addItem(new Atomic.UISelectItem("Clear Recent Projects", "clear recent projects"));
  159. menu.show(menuButtons, x, y);
  160. }
  161. // examples
  162. exampleCount = 0;
  163. currentExampleLayout: Atomic.UILayout;
  164. recent: string[] = [];
  165. recentList: Atomic.UIListView;
  166. }
  167. export = WelcomeFrame;