WelcomeFrame.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. class WelcomeFrame extends ScriptWidget {
  12. constructor(parent: Atomic.UIWidget) {
  13. super();
  14. this.load("AtomicEditor/editor/ui/welcomeframe.tb.txt");
  15. var recentProjects = <Atomic.UILayout>this.getWidget("recentprojects");
  16. this.gravity = Atomic.UI_GRAVITY_ALL;
  17. this.recentList = new Atomic.UIListView();
  18. this.recentList.rootList.id = "recentList";
  19. recentProjects.addChild(this.recentList);
  20. var container = <Atomic.UILayout>parent.getWidget("resourceviewcontainer");
  21. container.addChild(this);
  22. this.updateRecentProjects();
  23. this.subscribeToEvent(EditorEvents.CloseProject, () => {
  24. this.updateRecentProjects();
  25. });
  26. this.initExampleBrowser();
  27. }
  28. handleClickedExample(example: ExampleFormat) {
  29. var ops = EditorUI.getModelOps();
  30. var env = ToolCore.toolEnvironment;
  31. ops.showCreateProject(env.toolDataDir + "AtomicExamples/" + example.folder + "/", this.exampleInfoDir + example.screenshot);
  32. }
  33. addExample(example: ExampleFormat) {
  34. var exlayout = <Atomic.UILayout>this.getWidget("examples_layout");
  35. if (!this.currentExampleLayout) {
  36. this.currentExampleLayout = new Atomic.UILayout();
  37. this.currentExampleLayout.spacing = 8;
  38. exlayout.addChild(this.currentExampleLayout);
  39. }
  40. // 200x150
  41. var exampleLayout = new Atomic.UILayout();
  42. exampleLayout.skinBg = "StarCondition";
  43. exampleLayout.axis = Atomic.UI_AXIS_Y;
  44. exampleLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  45. exampleLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  46. // IMAGE BUTTON
  47. var id = example.name;
  48. var button = new Atomic.UIButton();
  49. button.skinBg = "StarButton";
  50. button.id = id;
  51. var image = new Atomic.UIImageWidget();
  52. button.onClick = () => {
  53. this.handleClickedExample(example);
  54. }
  55. image.image = this.exampleInfoDir + example.screenshot;
  56. image.skinBg = "ImageFrame";
  57. var rect = [0, 0, image.imageWidth / 2, image.imageHeight / 2];
  58. image.rect = rect;
  59. // NAME FIELD
  60. var nameField = new Atomic.UITextField();
  61. nameField.skinBg = "ImageCaption";
  62. nameField.text = example.name;
  63. var nameRect = [0, image.imageHeight / 2 - 16, image.imageWidth / 2, image.imageHeight / 2];
  64. nameField.rect = nameRect;
  65. nameField.gravity = Atomic.UI_GRAVITY_BOTTOM;
  66. image.addChild(nameField);
  67. button.addChild(image);
  68. var lp = new Atomic.UILayoutParams();
  69. lp.minWidth = image.imageWidth / 2;
  70. lp.minHeight = image.imageHeight / 2;
  71. button.layoutParams = lp;
  72. button.gravity = Atomic.UI_GRAVITY_LEFT;
  73. exampleLayout.addChild(button);
  74. // DESC TEXT
  75. var descField = new Atomic.UIEditField();
  76. descField.styling = true;
  77. descField.multiline = true;
  78. descField.readOnly = true;
  79. descField.wrapping = true;
  80. var styleDesc = "<color #A9A9A9>" + example.desc + "</color>";
  81. descField.text = styleDesc;
  82. descField.adaptToContentSize = true;
  83. lp.height = 42;
  84. lp.width = image.imageWidth / 2;
  85. descField.layoutParams = lp;
  86. exampleLayout.addChild(descField);
  87. this.currentExampleLayout.addChild(exampleLayout);
  88. this.exampleCount++;
  89. // three across, todo, be smarter about this
  90. if (!(this.exampleCount % 3)) {
  91. this.currentExampleLayout = null;
  92. }
  93. }
  94. initExampleBrowser() {
  95. var env = ToolCore.toolEnvironment;
  96. this.exampleInfoDir =env.toolDataDir + "ExampleInfo/";
  97. var exampleJsonFile = this.exampleInfoDir + "Examples.json";
  98. var jsonFile = new Atomic.File(exampleJsonFile, Atomic.FILE_READ);
  99. if (!jsonFile.isOpen())
  100. return;
  101. var examples = <ExamplesFormat>JSON.parse(jsonFile.readText());
  102. for (var i in examples.examples) {
  103. this.addExample(examples.examples[i]);
  104. }
  105. }
  106. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  107. if (ev.type == Atomic.UI_EVENT_TYPE_RIGHT_POINTER_UP) {
  108. if (ev.target.id == "recentList") {
  109. this.openFrameMenu(ev.x, ev.y);
  110. }
  111. }
  112. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  113. var id = ev.target.id;
  114. if (id == "open project") {
  115. var utils = new Editor.FileUtils();
  116. var path = utils.openProjectFileDialog();
  117. if (path) {
  118. this.sendEvent(EditorEvents.LoadProject, { path: path });
  119. }
  120. return true;
  121. }
  122. if (id == "new project") {
  123. var mo = EditorUI.getModelOps();
  124. mo.showNewProject();
  125. return true;
  126. }
  127. if (id == "recentList") {
  128. var path: string = this.recent[this.recentList.getSelectedItemID()];
  129. this.sendEvent(EditorEvents.LoadProject, { path: path });
  130. }
  131. if (id == "recentProjectsContextMenu") {
  132. var prefs = Preferences.getInstance();
  133. if (ev.refid == "clear recent projects") {
  134. prefs.deleteRecentProjects();
  135. this.updateRecentProjects();
  136. }
  137. }
  138. }
  139. }
  140. updateRecentProjects() {
  141. this.recentList.deleteAllItems();
  142. var prefs = Preferences.getInstance();
  143. prefs.updateRecentProjects();
  144. this.recent = prefs.recentProjects;
  145. for (var i in this.recent) {
  146. this.recentList.addRootItem(this.recent[i], "Folder.icon", i);
  147. }
  148. }
  149. private openFrameMenu(x: number, y: number) {
  150. var menu = new Atomic.UIMenuWindow(this, "recentProjectsContextMenu");
  151. var menuButtons = new Atomic.UISelectItemSource();
  152. menuButtons.addItem(new Atomic.UISelectItem("Clear Recent Projects", "clear recent projects"));
  153. menu.show(menuButtons, x, y);
  154. }
  155. // examples
  156. exampleInfoDir: string;
  157. exampleCount = 0;
  158. currentExampleLayout: Atomic.UILayout;
  159. exampleInfos:[ExampleFormat];
  160. recent: string[] = [];
  161. recentList: Atomic.UIListView;
  162. }
  163. class ExamplesFormat {
  164. examples: [ExampleFormat];
  165. }
  166. class ExampleFormat {
  167. name: string;
  168. desc: string;
  169. screenshot: string;
  170. folder: string;
  171. module: string;
  172. }
  173. export = WelcomeFrame;