SelectionPrefabWidget.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var breakMode = false;
  23. class SelectionPrefabWidget extends Atomic.UILayout {
  24. widgetLayout: Atomic.UILayout;
  25. noticeLayout: Atomic.UILayout;
  26. node: Atomic.Node;
  27. constructor() {
  28. super();
  29. var fd = new Atomic.UIFontDescription();
  30. fd.id = "Vera";
  31. fd.size = 11;
  32. var widgetLayout = this.widgetLayout = new Atomic.UILayout();
  33. var noticeLayout = this.noticeLayout = new Atomic.UILayout();
  34. this.axis = Atomic.UI_AXIS_Y;
  35. widgetLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  36. noticeLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  37. var name = new Atomic.UITextField();
  38. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  39. name.skinBg = "InspectorPrefabTextAttrName";
  40. name.text = "Prefab";
  41. name.fontDescription = fd;
  42. var saveButton = new Atomic.UIButton();
  43. saveButton.text = "Save";
  44. saveButton.fontDescription = fd;
  45. saveButton.onClick = () => {
  46. this.node.scene.sendEvent("SceneEditPrefabSave", {node : this.node});
  47. return true;
  48. };
  49. var undoButton = new Atomic.UIButton();
  50. undoButton.text = "Revert";
  51. undoButton.fontDescription = fd;
  52. undoButton.onClick = () => {
  53. this.node.scene.sendEvent("SceneEditPrefabRevert", {node : this.node});
  54. return true;
  55. };
  56. var breakButton = new Atomic.UIButton();
  57. breakButton.text = "Edit Break";
  58. breakButton.toggleMode = true;
  59. breakButton.value = breakMode ? 1 : 0;
  60. breakButton.fontDescription = fd;
  61. breakButton.tooltip = "Remove prefab connection on edit";
  62. breakButton.onClick = () => {
  63. breakMode = breakButton.value == 1 ? true : false;
  64. return true;
  65. };
  66. this.subscribeToEvent("SceneEditEnd", () => {
  67. if (breakMode && this.node) {
  68. this.node.scene.sendEvent("SceneEditPrefabBreak", {node : this.node});
  69. }
  70. });
  71. var noticeName = new Atomic.UITextField();
  72. noticeName.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  73. noticeName.skinBg = "InspectorTextAttrName";
  74. noticeName.text = "Prefab";
  75. noticeName.fontDescription = fd;
  76. var noticeText = new Atomic.UITextField();
  77. noticeText.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  78. noticeText.skinBg = "InspectorTextAttrName";
  79. noticeText.text = "Multiple Selection";
  80. noticeText.fontDescription = fd;
  81. noticeLayout.addChild(noticeName);
  82. noticeLayout.addChild(noticeText);
  83. widgetLayout.addChild(name);
  84. widgetLayout.addChild(saveButton);
  85. widgetLayout.addChild(undoButton);
  86. widgetLayout.addChild(breakButton);
  87. this.addChild(this.widgetLayout);
  88. this.addChild(this.noticeLayout);
  89. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  90. }
  91. detectPrefab(node: Atomic.Node): boolean {
  92. if (node.getComponent("PrefabComponent"))
  93. return true;
  94. if (node.parent)
  95. return this.detectPrefab(node.parent);
  96. return false;
  97. }
  98. updateSelection(nodes: Atomic.Node[]) {
  99. var hasPrefab = false;
  100. this.node = null;
  101. for (var i in nodes) {
  102. var node = nodes[i];
  103. if (this.detectPrefab(node)) {
  104. hasPrefab = true;
  105. break;
  106. }
  107. }
  108. if (!hasPrefab) {
  109. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  110. return;
  111. }
  112. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  113. if (nodes.length > 1) {
  114. this.noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  115. this.widgetLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  116. return;
  117. }
  118. this.noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  119. this.widgetLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  120. this.node = nodes[0];
  121. }
  122. }
  123. export = SelectionPrefabWidget;