SelectionInspector.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import CreateComponentButton = require("./CreateComponentButton");
  2. import ScriptWidget = require("ui/ScriptWidget");
  3. import EditorEvents = require("editor/EditorEvents");
  4. import SerializableEditType = require("./SerializableEditType");
  5. import SelectionSection = require("./SelectionSection");
  6. import SelectionPrefabWidget = require("./SelectionPrefabWidget");
  7. class NodeSection extends SelectionSection {
  8. prefabWidget: SelectionPrefabWidget;
  9. constructor(editType: SerializableEditType) {
  10. super(editType);
  11. this.prefabWidget = new SelectionPrefabWidget();
  12. this.attrLayout.addChild(this.prefabWidget);
  13. }
  14. }
  15. class ComponentSection extends SelectionSection {
  16. constructor(editType: SerializableEditType) {
  17. super(editType);
  18. }
  19. }
  20. // Node Inspector + Component Inspectors
  21. class SelectionInspector extends ScriptWidget {
  22. createComponentButton: CreateComponentButton;
  23. nodeSection: NodeSection;
  24. constructor(sceneEditor: Editor.SceneEditor3D) {
  25. super();
  26. this.sceneEditor = sceneEditor;
  27. var mainLayout = this.mainLayout = new Atomic.UILayout();
  28. mainLayout.spacing = 4;
  29. var lp = new Atomic.UILayoutParams();
  30. lp.width = 304;
  31. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  32. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  33. mainLayout.layoutParams = lp;
  34. mainLayout.axis = Atomic.UI_AXIS_Y;
  35. this.addChild(mainLayout);
  36. this.createComponentButton = new CreateComponentButton();
  37. mainLayout.addChild(this.createComponentButton);
  38. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesBegin", (data) => this.handleSceneEditStateChangesBeginEvent());
  39. this.subscribeToEvent("SceneEditStateChange", (data) => this.handleSceneEditStateChangeEvent(data));
  40. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesEnd", (data) => this.handleSceneEditStateChangesEndEvent());
  41. this.subscribeToEvent(this.createComponentButton, "SelectionCreateComponent", (data) => this.handleSelectionCreateComponent(data));
  42. }
  43. pruneSections() {
  44. var remove: SelectionSection[] = [];
  45. for (var i in this.sections) {
  46. var section = this.sections[i];
  47. var editType = section.editType;
  48. if (editType.typeName == "Node") {
  49. continue;
  50. }
  51. if (!editType.nodes.length) {
  52. remove.push(section);
  53. }
  54. }
  55. if (remove.length) {
  56. for (var i in remove) {
  57. var section = remove[i];
  58. this.removeSection(section);
  59. }
  60. this.suppressSections();
  61. }
  62. }
  63. suppressSections() {
  64. for (var i in this.sections) {
  65. var section = this.sections[i];
  66. var editType = section.editType;
  67. if (editType.typeName == "Node") {
  68. continue;
  69. }
  70. var suppressed = false;
  71. for (var j in this.nodes) {
  72. if (editType.nodes.indexOf(this.nodes[j]) == -1) {
  73. suppressed = true;
  74. break;
  75. }
  76. }
  77. section.suppress(suppressed);
  78. }
  79. }
  80. refresh() {
  81. Atomic.ui.blockChangedEvents = true;
  82. this.pruneSections();
  83. this.suppressSections();
  84. for (var i in this.sections) {
  85. this.sections[i].refresh();
  86. }
  87. if (this.nodeSection) {
  88. this.nodeSection.prefabWidget.updateSelection(this.nodes);
  89. }
  90. Atomic.ui.blockChangedEvents = false;
  91. }
  92. addSection(editType: SerializableEditType) {
  93. var section: SelectionSection;
  94. if (editType.typeName == "Node") {
  95. this.nodeSection = new NodeSection(editType);
  96. section = this.nodeSection;
  97. this.subscribeToEvent(this.nodeSection.prefabWidget, "SelectionPrefabSave", (data) => this.handleSelectionPrefabSave());
  98. this.subscribeToEvent(this.nodeSection.prefabWidget, "SelectionPrefabUndo", (data) => this.handleSelectionPrefabUndo());
  99. this.subscribeToEvent(this.nodeSection.prefabWidget, "SelectionPrefabBreak", (data) => this.handleSelectionPrefabBreak());
  100. } else {
  101. section = new ComponentSection(editType);
  102. }
  103. this.mainLayout.removeChild(this.createComponentButton, false);
  104. this.mainLayout.addChild(section);
  105. // move the create component button down
  106. this.mainLayout.addChild(this.createComponentButton);
  107. this.sections.push(section);
  108. }
  109. removeSection(section: SelectionSection) {
  110. var index = this.sections.indexOf(section);
  111. this.sections.splice(index, 1);
  112. this.mainLayout.removeChild(section);
  113. }
  114. removeSerializable(serial: Atomic.Serializable) {
  115. for (var i in this.sections) {
  116. var section = this.sections[i];
  117. var e = section.editType;
  118. var index = e.objects.indexOf(serial);
  119. if (index != -1) {
  120. e.objects.splice(index, 1);
  121. }
  122. if (serial.typeName == "Node") {
  123. index = e.nodes.indexOf(<Atomic.Node>serial);
  124. if (index != -1) {
  125. e.nodes.splice(index, 1);
  126. }
  127. }
  128. }
  129. }
  130. addSerializable(serial: Atomic.Serializable): SerializableEditType {
  131. var editType = this.getEditType(serial);
  132. // does it already exist?
  133. for (var i in this.sections) {
  134. var section = this.sections[i];
  135. var e = section.editType;
  136. if (e.compareTypes(editType)) {
  137. e.addSerializable(serial);
  138. return e;
  139. }
  140. }
  141. this.addSection(editType);
  142. return editType;
  143. }
  144. getEditType(serial: Atomic.Serializable): SerializableEditType {
  145. var typeName = serial.typeName;
  146. if (SelectionInspector._editTypes[typeName]) {
  147. return new SelectionInspector._editTypes[typeName](serial);
  148. }
  149. return new SerializableEditType(serial);
  150. }
  151. addNode(node: Atomic.Node) {
  152. var index = this.nodes.indexOf(node);
  153. if (index == -1) {
  154. this.nodes.push(node);
  155. this.addSerializable(node);
  156. var components = node.getComponents();
  157. for (var i in components) {
  158. var editType = this.addSerializable(components[i]);
  159. editType.addNode(node);
  160. }
  161. this.refresh();
  162. }
  163. }
  164. removeNode(node: Atomic.Node) {
  165. var index = this.nodes.indexOf(node);
  166. if (index != -1) {
  167. this.nodes.splice(index, 1);
  168. this.removeSerializable(node);
  169. var components = node.getComponents();
  170. for (var i in components) {
  171. this.removeSerializable(components[i]);
  172. }
  173. this.refresh();
  174. }
  175. }
  176. handleSceneEditStateChangesBeginEvent() {
  177. this.stateChangesInProgress = true;
  178. }
  179. handleSelectionCreateComponent(ev) {
  180. for (var i in this.nodes) {
  181. var node = this.nodes[i];
  182. var c = node.createComponent(ev.componentTypeName);
  183. if (!c) {
  184. console.log("ERROR: unable to create component ", ev.componentTypeName);
  185. return;
  186. }
  187. var editType = this.addSerializable(c);
  188. editType.addNode(node);
  189. }
  190. this.refresh();
  191. }
  192. handleSceneEditStateChangeEvent(ev: Editor.SceneEditStateChangeEvent) {
  193. if (!this.stateChangesInProgress)
  194. return;
  195. if (this.stateChanges.indexOf(ev.serializable) == -1) {
  196. this.stateChanges.push(ev.serializable);
  197. }
  198. }
  199. getPrefabComponent(node: Atomic.Node): Atomic.PrefabComponent {
  200. if (node.getComponent("PrefabComponent"))
  201. return <Atomic.PrefabComponent>node.getComponent("PrefabComponent");
  202. if (node.parent)
  203. return this.getPrefabComponent(node.parent);
  204. return null;
  205. }
  206. handleSelectionPrefabSave() {
  207. if (this.nodes.length != 1)
  208. return;
  209. var c = this.getPrefabComponent(this.nodes[0]);
  210. if (!c)
  211. return;
  212. c.savePrefab();
  213. }
  214. handleSelectionPrefabBreak() {
  215. if (this.nodes.length != 1)
  216. return;
  217. var c = this.getPrefabComponent(this.nodes[0]);
  218. if (!c)
  219. return;
  220. c.breakPrefab();
  221. this.refresh();
  222. }
  223. handleSelectionPrefabUndo() {
  224. if (this.nodes.length != 1)
  225. return;
  226. var c = this.getPrefabComponent(this.nodes[0]);
  227. if (!c)
  228. return;
  229. c.undoPrefab();
  230. var node = this.nodes[0];
  231. this.removeNode(node);
  232. this.addNode(node);
  233. }
  234. handleSceneEditStateChangesEndEvent() {
  235. Atomic.ui.blockChangedEvents = true;
  236. var sections: SelectionSection[] = [];
  237. for (var i in this.stateChanges) {
  238. var serial = this.stateChanges[i];
  239. for (var j in this.sections) {
  240. var section = this.sections[j];
  241. if (sections.indexOf(section) != -1)
  242. continue;
  243. if (section.editType.objects.indexOf(serial) != -1) {
  244. sections.push(section);
  245. section.refresh();
  246. }
  247. }
  248. }
  249. Atomic.ui.blockChangedEvents = false;
  250. this.stateChanges = [];
  251. this.stateChangesInProgress = false;
  252. }
  253. mainLayout: Atomic.UILayout;
  254. sceneEditor: Editor.SceneEditor3D;
  255. nodes: Atomic.Node[] = [];
  256. sections: SelectionSection[] = [];
  257. stateChangesInProgress: boolean = false;
  258. stateChanges: Atomic.Serializable[] = [];
  259. // ------------------------------------
  260. static registerEditType(typeName: string, type: typeof SerializableEditType) {
  261. SelectionInspector._editTypes[typeName] = type;
  262. }
  263. private static _editTypes: { [typeName: string]: typeof SerializableEditType } = {};
  264. }
  265. export = SelectionInspector;