WelcomeFrame.ts 6.2 KB

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