HierarchyFrameMenu.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 strings = require("ui/EditorStrings");
  23. import EditorEvents = require("editor/EditorEvents");
  24. import EditorUI = require("ui/EditorUI");
  25. import MenuItemSources = require("./MenuItemSources");
  26. import ServiceLocator from "../../../hostExtensions/ServiceLocator";
  27. class HierarchyFrameMenus extends Atomic.ScriptObject {
  28. contentFolder: string;
  29. private contextMenuItemSource: Atomic.UIMenuItemSource = null;
  30. constructor() {
  31. super();
  32. MenuItemSources.createMenuItemSource("hierarchy create items", createItems);
  33. this.contextMenuItemSource = MenuItemSources.createMenuItemSource("node context general", nodeGeneralContextItems);
  34. this.subscribeToEvent(EditorEvents.ContentFolderChanged, (ev: EditorEvents.ContentFolderChangedEvent) => {
  35. this.contentFolder = ev.path;
  36. });
  37. }
  38. handlePopupMenu(target: Atomic.UIWidget, refid: string, node: Atomic.Node): boolean {
  39. if (!target || !refid) return false;
  40. if (target.id == "create popup") {
  41. var child: Atomic.Node;
  42. if (refid == "create_node") {
  43. if (node) {
  44. child = node.createChild("Node");
  45. }
  46. }
  47. else if (refid == "create_light") {
  48. if (node) {
  49. child = node.createChild("Light");
  50. child.createComponent("Light");
  51. }
  52. }
  53. if (child) {
  54. child.scene.sendEvent("SceneEditNodeCreated", { node: child });
  55. }
  56. return true;
  57. }
  58. return false;
  59. }
  60. handleNodeContextMenu(target: Atomic.UIWidget, refid: string): boolean {
  61. if (target.id == "node context menu") {
  62. var node = <Atomic.Node>target["node"];
  63. if (!node) {
  64. return false;
  65. }
  66. if (refid == "delete_node") {
  67. if (node instanceof Atomic.Scene)
  68. return;
  69. var scene = node.scene;
  70. scene.sendEvent("SceneEditAddRemoveNodes", { end: false });
  71. scene.sendEvent("SceneEditNodeRemoved", { node: node, parent: node.parent, scene: scene });
  72. node.remove();
  73. scene.sendEvent("SceneEditAddRemoveNodes", { end: true });
  74. return true;
  75. } else if (refid == "duplicate_node") {
  76. if (node instanceof Atomic.Scene)
  77. return;
  78. var newnode = node.clone();
  79. node.scene.sendEvent("SceneEditNodeCreated", { node: newnode });
  80. return true;
  81. }
  82. // Let plugins handle context
  83. return ServiceLocator.uiServices.hierarchyContextItemClicked(node, refid);
  84. }
  85. return false;
  86. }
  87. createNodeContextMenu(parent: Atomic.UIWidget, node: Atomic.Node, x: number, y: number) {
  88. var menu = new Atomic.UIMenuWindow(parent, "node context menu");
  89. menu["node"] = node;
  90. var srcName: string = "node context general";
  91. var src = MenuItemSources.getMenuItemSource(srcName);
  92. menu.show(src, x, y);
  93. }
  94. createPluginItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  95. return MenuItemSources.createSubMenuItemSource(this.contextMenuItemSource , id, items);
  96. }
  97. removePluginItemSource(id: string) {
  98. this.contextMenuItemSource.removeItemWithStr(id);
  99. }
  100. }
  101. export = HierarchyFrameMenus;
  102. // initialization
  103. var StringID = strings.StringID;
  104. var createItems = {
  105. Node: ["create_node", undefined, "Folder.icon"],
  106. "-1": null,
  107. "3D": {
  108. Light: ["create_light", undefined, "JavascriptBitmap"]
  109. }
  110. };
  111. var nodeGeneralContextItems = {
  112. "Duplicate": ["duplicate_node", undefined, ""],
  113. "-1": null,
  114. "Delete": ["delete_node", undefined, ""]
  115. };