NodeInspector.ts 6.5 KB

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