SelectionInspector.ts 13 KB

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