NodeInspector.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import ScriptWidget = require("../ScriptWidget");
  2. import ComponentInspector = require("./ComponentInspector");
  3. import DataBinding = require("./DataBinding");
  4. import CreateComponentButton = require("./CreateComponentButton");
  5. class NodeInspector extends ScriptWidget {
  6. constructor() {
  7. super();
  8. this.subscribeToEvent(this, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  9. this.subscribeToEvent("KeyUp", (data) => this.handleKeyUp(data));
  10. }
  11. handleKeyUp(data) {
  12. if (data.key == 27) {
  13. if (this.nodeLayout) {
  14. this.sendEvent("EditorActiveNodeChange", { node: null });
  15. this.nodeLayout.deleteAllChildren();
  16. this.nodeLayout = null;
  17. }
  18. }
  19. if (data.key == 92) {
  20. // '\' is delete for now
  21. if (this.node) {
  22. this.nodeLayout.deleteAllChildren();
  23. this.nodeLayout = null;
  24. this.node.removeComponents(true, true);
  25. if (this.node.parent)
  26. this.node.parent.removeChild(this.node);
  27. this.node = null;
  28. this.sendEvent("EditorActiveNodeChange", { node: null });
  29. this.sendEvent("EditorUpdateHierarchy", {});
  30. }
  31. }
  32. }
  33. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  34. var handled = false;
  35. for (var i = 0; i < this.bindings.length; i++) {
  36. if (this.bindings[i].handleWidgetEvent(ev)) {
  37. handled = true;
  38. }
  39. }
  40. // return handled
  41. return handled;
  42. }
  43. getPrefabComponent(node: Atomic.Node):Atomic.PrefabComponent {
  44. if (node.parent && node.parent.getComponent("PrefabComponent"))
  45. return <Atomic.PrefabComponent> node.parent.getComponent("PrefabComponent");
  46. if (node.parent)
  47. return this.getPrefabComponent(node.parent);
  48. return null;
  49. }
  50. detectPrefab(node: Atomic.Node): boolean {
  51. if (node.parent && node.parent.getComponent("PrefabComponent"))
  52. return true;
  53. if (node.parent)
  54. return this.detectPrefab(node.parent);
  55. return false;
  56. }
  57. inspect(node: Atomic.Node) {
  58. this.bindings = new Array();
  59. this.node = node;
  60. this.isPrefab = this.detectPrefab(node);
  61. var fd = new Atomic.UIFontDescription();
  62. fd.id = "Vera";
  63. fd.size = 11;
  64. var nlp = new Atomic.UILayoutParams();
  65. nlp.width = 304;
  66. var nodeLayout = this.nodeLayout = new Atomic.UILayout();
  67. nodeLayout.spacing = 4;
  68. nodeLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  69. nodeLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  70. nodeLayout.layoutParams = nlp;
  71. nodeLayout.axis = Atomic.UI_AXIS_Y;
  72. // node attr layout
  73. var nodeSection = new Atomic.UISection();
  74. nodeSection.text = "Node";
  75. nodeSection.value = 1;
  76. nodeLayout.addChild(nodeSection);
  77. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  78. attrsVerticalLayout.spacing = 3;
  79. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  80. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  81. nodeSection.contentRoot.addChild(attrsVerticalLayout);
  82. var attrs = node.getAttributes();
  83. for (var i in attrs) {
  84. var attr = <Atomic.AttributeInfo> attrs[i];
  85. if (attr.mode & Atomic.AM_NOEDIT)
  86. continue;
  87. var binding = DataBinding.createBinding(node, attr);
  88. if (!binding)
  89. continue;
  90. var attrLayout = new Atomic.UILayout();
  91. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  92. var name = new Atomic.UITextField();
  93. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  94. name.skinBg = "InspectorTextAttrName";
  95. if (attr.type == Atomic.VAR_VECTOR3 || attr.type == Atomic.VAR_COLOR ||
  96. attr.type == Atomic.VAR_QUATERNION) {
  97. attrLayout.axis = Atomic.UI_AXIS_Y;
  98. attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  99. attrLayout.skinBg = "InspectorVectorAttrLayout";
  100. }
  101. var bname = attr.name;
  102. if (bname == "Is Enabled")
  103. bname = "Enabled";
  104. name.text = bname;
  105. name.fontDescription = fd;
  106. attrLayout.addChild(name);
  107. attrLayout.addChild(binding.widget);
  108. attrsVerticalLayout.addChild(attrLayout);
  109. this.bindings.push(binding);
  110. }
  111. // PREFAB
  112. if (this.isPrefab) {
  113. var name = new Atomic.UITextField();
  114. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  115. name.skinBg = "InspectorTextAttrName";
  116. name.text = "Prefab"
  117. name.fontDescription = fd;
  118. var prefabLayout = new Atomic.UILayout();
  119. prefabLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  120. var saveButton = new Atomic.UIButton();
  121. saveButton.text = "Save";
  122. saveButton.fontDescription = fd;
  123. saveButton.onClick = function() {
  124. var prefabComponent = this.getPrefabComponent(this.node);
  125. if (prefabComponent) {
  126. prefabComponent.savePrefab();
  127. this.sendEvent("EditorActiveNodeChange", {node:this.node});
  128. return true;
  129. }
  130. }.bind(this);
  131. var undoButton = new Atomic.UIButton();
  132. undoButton.text = "Undo";
  133. undoButton.fontDescription = fd;
  134. undoButton.onClick = function() {
  135. var prefabComponent = this.getPrefabComponent(this.node);
  136. if (prefabComponent) {
  137. prefabComponent.undoPrefab();
  138. this.sendEvent("EditorActiveNodeChange", {node:this.node});
  139. return true;
  140. }
  141. }.bind(this);
  142. prefabLayout.addChild(name);
  143. prefabLayout.addChild(saveButton);
  144. prefabLayout.addChild(undoButton);
  145. attrsVerticalLayout.addChild(prefabLayout);
  146. }
  147. // COMPONENTS
  148. var components = node.getComponents();
  149. for (var i in components) {
  150. var ci = new ComponentInspector();
  151. ci.inspect(components[i]);
  152. nodeLayout.addChild(ci);
  153. }
  154. this.addChild(nodeLayout);
  155. var button = new CreateComponentButton(node);
  156. nodeLayout.addChild(button);
  157. for (var i in this.bindings) {
  158. this.bindings[i].setWidgetValueFromObject();
  159. this.bindings[i].objectLocked = false;
  160. }
  161. }
  162. isPrefab:boolean;
  163. node: Atomic.Node;
  164. nodeLayout: Atomic.UILayout;
  165. bindings: Array<DataBinding>;
  166. }
  167. export = NodeInspector;