SelectionInspector.ts 11 KB

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