SelectionPrefabWidget.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 EditorUI = require("../../EditorUI");
  23. var breakMode = true;
  24. class SelectionPrefabWidget extends Atomic.UILayout {
  25. widgetLayout: Atomic.UILayout;
  26. noticeLayout: Atomic.UILayout;
  27. node: Atomic.Node;
  28. constructor() {
  29. super();
  30. var fd = new Atomic.UIFontDescription();
  31. fd.id = "Vera";
  32. fd.size = 11;
  33. var widgetLayout = this.widgetLayout = new Atomic.UILayout();
  34. var noticeLayout = this.noticeLayout = new Atomic.UILayout();
  35. this.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  36. widgetLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  37. noticeLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  38. var name = new Atomic.UITextField();
  39. name.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_LEFT;
  40. name.skinBg = "InspectorPrefabTextAttrName";
  41. name.text = "Prefab";
  42. name.fontDescription = fd;
  43. var saveButton = new Atomic.UIButton();
  44. saveButton.text = "Save";
  45. saveButton.fontDescription = fd;
  46. saveButton.onClick = () => {
  47. this.node.scene.sendEvent(Editor.SceneEditPrefabSaveEventData({node : this.node}));
  48. return true;
  49. };
  50. var undoButton = new Atomic.UIButton();
  51. undoButton.text = "Revert";
  52. undoButton.fontDescription = fd;
  53. undoButton.onClick = () => {
  54. this.node.scene.sendEvent(Editor.SceneEditPrefabRevertEventData({node : this.node}));
  55. return true;
  56. };
  57. var breakButton = new Atomic.UIButton();
  58. breakButton.text = "Edit Break";
  59. breakButton.toggleMode = true;
  60. breakButton.value = breakMode ? 1 : 0;
  61. breakButton.fontDescription = fd;
  62. breakButton.tooltip = "Prompt to remove prefab connection";
  63. breakButton.onClick = () => {
  64. breakMode = breakButton.value == 1 ? true : false;
  65. return true;
  66. };
  67. this.subscribeToEvent("ComponentEditEnd", () => {
  68. if (breakMode && this.node) {
  69. var window = new ConfirmPrefabBreak(this.node);
  70. window.show();
  71. }
  72. });
  73. var noticeName = new Atomic.UITextField();
  74. noticeName.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_LEFT;
  75. noticeName.skinBg = "InspectorTextAttrName";
  76. noticeName.text = "Prefab";
  77. noticeName.fontDescription = fd;
  78. var noticeText = new Atomic.UITextField();
  79. noticeText.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_LEFT;
  80. noticeText.skinBg = "InspectorTextAttrName";
  81. noticeText.text = "Multiple Selection";
  82. noticeText.fontDescription = fd;
  83. noticeLayout.addChild(noticeName);
  84. noticeLayout.addChild(noticeText);
  85. widgetLayout.addChild(name);
  86. widgetLayout.addChild(saveButton);
  87. widgetLayout.addChild(undoButton);
  88. widgetLayout.addChild(breakButton);
  89. this.addChild(this.widgetLayout);
  90. this.addChild(this.noticeLayout);
  91. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  92. }
  93. detectPrefab(node: Atomic.Node): boolean {
  94. if (node.getComponent("PrefabComponent"))
  95. return true;
  96. if (node.parent)
  97. return this.detectPrefab(node.parent);
  98. return false;
  99. }
  100. updateSelection(nodes: Atomic.Node[]) {
  101. var hasPrefab = false;
  102. this.node = null;
  103. for (var i in nodes) {
  104. var node = nodes[i];
  105. if (this.detectPrefab(node)) {
  106. hasPrefab = true;
  107. break;
  108. }
  109. }
  110. if (!hasPrefab) {
  111. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  112. return;
  113. }
  114. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE;
  115. if (nodes.length > 1) {
  116. this.noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE;
  117. this.widgetLayout.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  118. return;
  119. }
  120. this.noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  121. this.widgetLayout.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE;
  122. this.node = nodes[0];
  123. }
  124. }
  125. class ConfirmPrefabBreak extends Atomic.UIWindow {
  126. constructor(node:Atomic.Node) {
  127. super();
  128. this.node = node;
  129. this.settings = Atomic.UI_WINDOW_SETTINGS.UI_WINDOW_SETTINGS_DEFAULT & ~Atomic.UI_WINDOW_SETTINGS.UI_WINDOW_SETTINGS_CLOSE_BUTTON;
  130. this.text = "Break Prefab Connection";
  131. this.load("AtomicEditor/editor/ui/breakprefab.tb.txt");
  132. var message = <Atomic.UIEditField>this.getWidget("message");
  133. message.text = "Editing this node will break the prefab connection.\nThis operation cannot be undone, do you want to continue?";
  134. this.resizeToFitContent();
  135. this.center();
  136. this.dimmer = new Atomic.UIDimmer();
  137. this.subscribeToEvent(Atomic.UIWidgetEvent((ev) => { this.handleWidgetEvent(ev); }));
  138. }
  139. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  140. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  141. var id = ev.target.id;
  142. if (id == "breakprefab") {
  143. this.hide();
  144. this.node.scene.sendEvent(Editor.SceneEditPrefabBreakEventData({node : this.node}));
  145. return true;
  146. }
  147. if (id == "cancel") {
  148. this.hide();
  149. this.node.scene.sendEvent(Editor.SceneEditPrefabRevertEventData({node : this.node}));
  150. return true;
  151. }
  152. }
  153. }
  154. show() {
  155. var view = EditorUI.getView();
  156. view.addChild(this.dimmer);
  157. view.addChild(this);
  158. }
  159. hide() {
  160. if (this.dimmer.parent)
  161. this.dimmer.parent.removeChild(this.dimmer, false);
  162. if (this.parent)
  163. this.parent.removeChild(this, false);
  164. }
  165. node: Atomic.Node;
  166. dimmer: Atomic.UIDimmer;
  167. }
  168. export = SelectionPrefabWidget;