HierarchyFrameMenu.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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, editor: Editor.SceneEditor3D): 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. editor.selection.delete();
  75. return true;
  76. } else if (refid == "duplicate_node") {
  77. if (node instanceof Atomic.Scene)
  78. return;
  79. var newnode = node.clone();
  80. node.scene.sendEvent("SceneEditNodeCreated", { node: newnode });
  81. return true;
  82. }
  83. // Let plugins handle context
  84. return ServiceLocator.uiServices.hierarchyContextItemClicked(node, refid);
  85. }
  86. return false;
  87. }
  88. createNodeContextMenu(parent: Atomic.UIWidget, node: Atomic.Node, x: number, y: number) {
  89. var menu = new Atomic.UIMenuWindow(parent, "node context menu");
  90. menu["node"] = node;
  91. var srcName: string = "node context general";
  92. var src = MenuItemSources.getMenuItemSource(srcName);
  93. menu.show(src, x, y);
  94. }
  95. createPluginItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  96. return MenuItemSources.createSubMenuItemSource(this.contextMenuItemSource , id, items);
  97. }
  98. removePluginItemSource(id: string) {
  99. this.contextMenuItemSource.removeItemWithStr(id);
  100. }
  101. }
  102. export = HierarchyFrameMenus;
  103. // initialization
  104. var StringID = strings.StringID;
  105. var createItems = {
  106. Node: ["create_node", undefined, "Folder.icon"],
  107. "-1": null,
  108. "3D": {
  109. Light: ["create_light", undefined, "JavascriptBitmap"]
  110. }
  111. };
  112. var nodeGeneralContextItems = {
  113. "Duplicate": ["duplicate_node", undefined, ""],
  114. "-1": null,
  115. "Delete": ["delete_node", undefined, ""]
  116. };