SelectionInspector.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import EditorUI = require("../../EditorUI");
  8. import CreateComponentButton = require("./CreateComponentButton");
  9. import ScriptWidget = require("ui/ScriptWidget");
  10. import EditorEvents = require("editor/EditorEvents");
  11. import SerializableEditType = require("./SerializableEditType");
  12. import SelectionSection = require("./SelectionSection");
  13. import SelectionPrefabWidget = require("./SelectionPrefabWidget");
  14. import AttributeInfoEdit = require("./AttributeInfoEdit");
  15. class NodeSection extends SelectionSection {
  16. prefabWidget: SelectionPrefabWidget;
  17. transformEdits: AttributeInfoEdit[] = [];
  18. updateDelta: number = 0.0;
  19. constructor(editType: SerializableEditType) {
  20. super(editType);
  21. this.prefabWidget = new SelectionPrefabWidget();
  22. this.attrLayout.addChild(this.prefabWidget);
  23. this.transformEdits.push(this.attrEdits["Position"]);
  24. this.transformEdits.push(this.attrEdits["Rotation"]);
  25. this.transformEdits.push(this.attrEdits["Scale"]);
  26. this.subscribeToEvent("Update", (ev) => this.handleUpdate(ev));
  27. }
  28. handleUpdate(ev) {
  29. this.updateDelta -= ev.timeStep;
  30. if (this.updateDelta < 0.0) {
  31. this.updateDelta = 0.1;
  32. Atomic.ui.blockChangedEvents = true;
  33. for (var i in this.transformEdits) {
  34. this.transformEdits[i].refresh();
  35. }
  36. Atomic.ui.blockChangedEvents = false;
  37. }
  38. }
  39. }
  40. class ComponentSection extends SelectionSection {
  41. constructor(editType: SerializableEditType, inspector: SelectionInspector) {
  42. super(editType);
  43. var deleteButton = new Atomic.UIButton();
  44. deleteButton.text = "Delete Component";
  45. deleteButton.fontDescription = SelectionSection.fontDesc;
  46. deleteButton.onClick = () => {
  47. inspector.onComponentDelete(editType);
  48. return true;
  49. }
  50. this.attrLayout.addChild(deleteButton);;
  51. }
  52. }
  53. class SceneSection extends SelectionSection {
  54. constructor(editType: SerializableEditType) {
  55. super(editType);
  56. }
  57. }
  58. interface AttributeEditResourceChangedEvent {
  59. attrInfoEdit: AttributeInfoEdit;
  60. resource: Atomic.Resource;
  61. }
  62. class JSComponentSection extends ComponentSection {
  63. constructor(editType: SerializableEditType, inspector: SelectionInspector) {
  64. super(editType, inspector);
  65. this.hasDynamicAttr = true;
  66. this.subscribeToEvent(this, "AttributeEditResourceChanged", (ev) => this.handleAttributeEditResourceChanged(ev));
  67. }
  68. private handleAttributeEditResourceChanged(ev: AttributeEditResourceChangedEvent) {
  69. var jsc = <Atomic.JSComponent>this.editType.getFirstObject();
  70. if (!jsc)
  71. return;
  72. var attrInfos = jsc.getAttributes();
  73. this.updateDynamicAttrInfos(attrInfos);
  74. }
  75. }
  76. // Node Inspector + Component Inspectors
  77. class SelectionInspector extends ScriptWidget {
  78. constructor(sceneEditor: Editor.SceneEditor3D) {
  79. super();
  80. this.sceneEditor = sceneEditor;
  81. var mainLayout = this.mainLayout = new Atomic.UILayout();
  82. mainLayout.spacing = 4;
  83. var lp = new Atomic.UILayoutParams();
  84. lp.width = 304;
  85. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  86. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  87. mainLayout.layoutParams = lp;
  88. mainLayout.axis = Atomic.UI_AXIS_Y;
  89. this.addChild(mainLayout);
  90. var noticeLayout = this.multipleSelectNotice = new Atomic.UILayout();
  91. noticeLayout.axis = Atomic.UI_AXIS_Y;
  92. noticeLayout.layoutParams = lp;
  93. var noticeText = new Atomic.UITextField();
  94. noticeText.textAlign = Atomic.UI_TEXT_ALIGN_CENTER;
  95. noticeText.skinBg = "InspectorTextAttrName";
  96. noticeText.text = "Multiple Selection - Some components are hidden";
  97. noticeText.fontDescription = SelectionSection.fontDesc;
  98. noticeText.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  99. noticeText.layoutParams = lp;
  100. noticeLayout.addChild(noticeText);
  101. noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  102. mainLayout.addChild(noticeLayout);
  103. this.createComponentButton = new CreateComponentButton();
  104. mainLayout.addChild(this.createComponentButton);
  105. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesBegin", (data) => this.handleSceneEditStateChangesBeginEvent());
  106. this.subscribeToEvent("SceneEditStateChange", (data) => this.handleSceneEditStateChangeEvent(data));
  107. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesEnd", (data) => this.handleSceneEditStateChangesEndEvent());
  108. this.subscribeToEvent(sceneEditor.scene, "SceneEditNodeRemoved", (ev: Editor.SceneEditNodeRemovedEvent) => this.handleSceneEditNodeRemoved(ev));
  109. this.subscribeToEvent(sceneEditor.scene, "SceneEditComponentAddedRemoved", (ev) => this.handleSceneEditComponentAddedRemovedEvent(ev));
  110. this.subscribeToEvent(this.createComponentButton, "SelectionCreateComponent", (data) => this.handleSelectionCreateComponent(data));
  111. }
  112. pruneSections() {
  113. var remove: SelectionSection[] = [];
  114. for (var i in this.sections) {
  115. var section = this.sections[i];
  116. var editType = section.editType;
  117. if (editType.typeName == "Node") {
  118. continue;
  119. }
  120. if (editType.typeName == "Scene") {
  121. var gotone = false;
  122. for (var j in this.nodes) {
  123. if (this.nodes[j].typeName == "Scene") {
  124. gotone = true;
  125. break;
  126. }
  127. }
  128. if (gotone)
  129. continue;
  130. }
  131. if (!editType.nodes.length) {
  132. remove.push(section);
  133. }
  134. }
  135. if (remove.length) {
  136. for (var i in remove) {
  137. var section = remove[i];
  138. this.removeSection(section);
  139. }
  140. this.suppressSections();
  141. }
  142. }
  143. suppressSections() {
  144. this.multipleSelectNotice.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  145. for (var i in this.sections) {
  146. var section = this.sections[i];
  147. var editType = section.editType;
  148. if (editType.typeName == "Node" || editType.typeName == "Scene") {
  149. continue;
  150. }
  151. var suppressed = false;
  152. for (var j in this.nodes) {
  153. if (editType.nodes.indexOf(this.nodes[j]) == -1) {
  154. suppressed = true;
  155. break;
  156. }
  157. }
  158. if (suppressed)
  159. this.multipleSelectNotice.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  160. section.suppress(suppressed);
  161. }
  162. }
  163. refresh() {
  164. Atomic.ui.blockChangedEvents = true;
  165. this.pruneSections();
  166. this.suppressSections();
  167. for (var i in this.sections) {
  168. this.sections[i].refresh();
  169. }
  170. if (this.nodeSection) {
  171. this.nodeSection.prefabWidget.updateSelection(this.nodes);
  172. }
  173. Atomic.ui.blockChangedEvents = false;
  174. }
  175. addSection(editType: SerializableEditType) {
  176. var section: SelectionSection;
  177. if (editType.typeName == "Node") {
  178. this.nodeSection = new NodeSection(editType);
  179. section = this.nodeSection;
  180. } else if (editType.typeName == "Scene") {
  181. section = new SceneSection(editType);
  182. } else if (editType.typeName == "JSComponent") {
  183. section = new JSComponentSection(editType, this);
  184. }
  185. else {
  186. section = new ComponentSection(editType, this);
  187. }
  188. section.value = SelectionInspector.sectionStates[editType.typeName] ? 1 : 0;
  189. this.mainLayout.removeChild(this.createComponentButton, false);
  190. this.mainLayout.removeChild(this.multipleSelectNotice, false);
  191. // sort it in alphabetically
  192. this.sections.push(section);
  193. this.sections.sort(function(a, b) {
  194. if (a.editType.typeName == "Node" && b.editType.typeName == "Scene")
  195. return -1;
  196. if (a.editType.typeName == "Scene" && b.editType.typeName == "Node")
  197. return 1;
  198. if (a.editType.typeName == "Node" || a.editType.typeName == "Scene")
  199. return -1;
  200. if (b.editType.typeName == "Node" || b.editType.typeName == "Scene")
  201. return 1;
  202. return a.editType.typeName.localeCompare(b.editType.typeName);
  203. });
  204. var idx = this.sections.indexOf(section);
  205. if (idx == 0) {
  206. if (this.sections.length == 1) {
  207. this.mainLayout.addChild(section);
  208. } else {
  209. this.mainLayout.addChildBefore(section, this.sections[1]);
  210. }
  211. }
  212. else if (idx == this.sections.length - 1) {
  213. this.mainLayout.addChild(section);
  214. }
  215. else {
  216. this.mainLayout.addChildAfter(section, this.sections[idx - 1]);
  217. }
  218. // move the create component button down
  219. this.mainLayout.addChild(this.multipleSelectNotice);
  220. this.mainLayout.addChild(this.createComponentButton);
  221. }
  222. removeSection(section: SelectionSection) {
  223. SelectionInspector.sectionStates[section.editType.typeName] = section.value ? true : false;
  224. var index = this.sections.indexOf(section);
  225. this.sections.splice(index, 1);
  226. this.mainLayout.removeChild(section);
  227. }
  228. removeSerializable(serial: Atomic.Serializable) {
  229. for (var i in this.sections) {
  230. var section = this.sections[i];
  231. var e = section.editType;
  232. var index = e.objects.indexOf(serial);
  233. if (index != -1) {
  234. e.objects.splice(index, 1);
  235. }
  236. if (serial.typeName == "Node") {
  237. index = e.nodes.indexOf(<Atomic.Node>serial);
  238. if (index != -1) {
  239. e.nodes.splice(index, 1);
  240. }
  241. }
  242. }
  243. }
  244. addSerializable(serial: Atomic.Serializable): SerializableEditType {
  245. var editType = this.getEditType(serial);
  246. // does it already exist?
  247. for (var i in this.sections) {
  248. var section = this.sections[i];
  249. var e = section.editType;
  250. if (e.compareTypes(editType, this.nodes.length > 1)) {
  251. e.addSerializable(serial);
  252. return e;
  253. }
  254. }
  255. this.addSection(editType);
  256. return editType;
  257. }
  258. getEditType(serial: Atomic.Serializable): SerializableEditType {
  259. var typeName = serial.typeName;
  260. if (SelectionInspector._editTypes[typeName]) {
  261. return new SelectionInspector._editTypes[typeName](serial);
  262. }
  263. return new SerializableEditType(serial);
  264. }
  265. addNode(node: Atomic.Node) {
  266. var index = this.nodes.indexOf(node);
  267. if (index == -1) {
  268. this.nodes.push(node);
  269. this.addSerializable(node);
  270. var components = node.getComponents();
  271. for (var i in components) {
  272. if (this.filterComponent(components[i]))
  273. continue;
  274. var editType = this.addSerializable(components[i]);
  275. editType.addNode(node);
  276. }
  277. this.refresh();
  278. }
  279. }
  280. removeNode(node: Atomic.Node) {
  281. var index = this.nodes.indexOf(node);
  282. if (index != -1) {
  283. this.nodes.splice(index, 1);
  284. this.removeSerializable(node);
  285. var components = node.getComponents();
  286. for (var i in components) {
  287. if (this.filterComponent(components[i]))
  288. continue;
  289. this.removeSerializable(components[i]);
  290. }
  291. this.refresh();
  292. }
  293. // save node section state
  294. if (!this.nodes.length && this.nodeSection)
  295. SelectionInspector.sectionStates["Node"] = this.nodeSection.value ? true : false;
  296. }
  297. handleSceneEditStateChangesBeginEvent() {
  298. this.stateChangesInProgress = true;
  299. }
  300. handleSceneEditNodeRemoved(ev: Editor.SceneEditNodeRemovedEvent) {
  301. this.removeNode(ev.node);
  302. }
  303. handleSceneEditComponentAddedRemovedEvent(ev: Editor.SceneEditComponentAddedRemovedEvent) {
  304. if (this.filterComponent(ev.component)) {
  305. // still refresh as may affect UI (for example PrefabComponents)
  306. this.refresh();
  307. return;
  308. }
  309. if (!ev.removed) {
  310. for (var i in this.sections) {
  311. var section = this.sections[i];
  312. if (section.contains(ev.component)) {
  313. // this happens on a prefab break/unbreak
  314. // when only temporary is changing
  315. return;
  316. }
  317. }
  318. editType = this.addSerializable(ev.component);
  319. editType.addNode(ev.node);
  320. } else {
  321. for (var i in this.sections) {
  322. var section = this.sections[i];
  323. var editType = section.editType;
  324. var index = editType.objects.indexOf(ev.component);
  325. if (index != -1) {
  326. editType.objects.splice(index, 1);
  327. index = editType.nodes.indexOf(ev.node);
  328. if (index != -1) {
  329. editType.nodes.splice(index, 1);
  330. }
  331. break;
  332. }
  333. }
  334. }
  335. this.refresh();
  336. }
  337. onComponentDelete(editType: SerializableEditType) {
  338. var removed: Atomic.Component[] = [];
  339. for (var i in editType.objects) {
  340. var c = <Atomic.Component>editType.objects[i];
  341. removed.push(c);
  342. }
  343. for (var i in removed) {
  344. var c = removed[i];
  345. var node = c.node;
  346. c.remove();
  347. this.removeSerializable(removed[i]);
  348. var index = editType.nodes.indexOf(node);
  349. if (index != -1) {
  350. editType.nodes.splice(index, 1);
  351. }
  352. }
  353. if (removed.length) {
  354. this.sceneEditor.scene.sendEvent("SceneEditEnd");
  355. this.refresh();
  356. }
  357. }
  358. handleSelectionCreateComponent(ev) {
  359. var valid = true;
  360. if (ev.componentTypeName != "JSComponent") {
  361. for (var i in this.nodes) {
  362. var node = this.nodes[i];
  363. if (node.getComponent(ev.componentTypeName, false)) {
  364. valid = false;
  365. break;
  366. }
  367. }
  368. }
  369. if (!valid) {
  370. EditorUI.showModalError("Component Create", "Unable to create component, a node with an existing " + ev.componentTypeName + " component is selected");
  371. return;
  372. }
  373. for (var i in this.nodes) {
  374. var node = this.nodes[i];
  375. var c = node.createComponent(ev.componentTypeName);
  376. if (!c) {
  377. console.log("ERROR: unable to create component ", ev.componentTypeName);
  378. return;
  379. }
  380. var editType = this.addSerializable(c);
  381. editType.addNode(node);
  382. for (var i in this.sections) {
  383. if (this.sections[i].editType == editType) {
  384. this.sections[i].value = 1;
  385. break;
  386. }
  387. }
  388. }
  389. this.refresh();
  390. }
  391. handleSceneEditStateChangeEvent(ev: Editor.SceneEditStateChangeEvent) {
  392. if (!this.stateChangesInProgress)
  393. return;
  394. if (this.stateChanges.indexOf(ev.serializable) == -1) {
  395. this.stateChanges.push(ev.serializable);
  396. }
  397. }
  398. getPrefabComponent(node: Atomic.Node): Atomic.PrefabComponent {
  399. if (node.getComponent("PrefabComponent"))
  400. return <Atomic.PrefabComponent>node.getComponent("PrefabComponent");
  401. if (node.parent)
  402. return this.getPrefabComponent(node.parent);
  403. return null;
  404. }
  405. filterComponent(component: Atomic.Component): boolean {
  406. if (component.typeName == "PrefabComponent") {
  407. return true;
  408. }
  409. return false;
  410. }
  411. handleSceneEditStateChangesEndEvent() {
  412. Atomic.ui.blockChangedEvents = true;
  413. var sections: SelectionSection[] = [];
  414. for (var i in this.stateChanges) {
  415. var serial = this.stateChanges[i];
  416. for (var j in this.sections) {
  417. var section = this.sections[j];
  418. if (sections.indexOf(section) != -1)
  419. continue;
  420. if (section.editType.objects.indexOf(serial) != -1) {
  421. sections.push(section);
  422. if (section.hasDynamicAttr) {
  423. var object = section.editType.getFirstObject();
  424. if (object) {
  425. var attrInfos = object.getAttributes();
  426. section.updateDynamicAttrInfos(attrInfos);
  427. }
  428. }
  429. section.refresh();
  430. }
  431. }
  432. }
  433. Atomic.ui.blockChangedEvents = false;
  434. this.stateChanges = [];
  435. this.stateChangesInProgress = false;
  436. }
  437. mainLayout: Atomic.UILayout;
  438. multipleSelectNotice: Atomic.UILayout;
  439. sceneEditor: Editor.SceneEditor3D;
  440. nodes: Atomic.Node[] = [];
  441. sections: SelectionSection[] = [];
  442. createComponentButton: CreateComponentButton;
  443. nodeSection: NodeSection;
  444. stateChangesInProgress: boolean = false;
  445. stateChanges: Atomic.Serializable[] = [];
  446. // ------------------------------------
  447. static registerEditType(typeName: string, type: typeof SerializableEditType) {
  448. SelectionInspector._editTypes[typeName] = type;
  449. }
  450. private static sectionStates: { [typeName: string]: boolean } = {};
  451. private static _editTypes: { [typeName: string]: typeof SerializableEditType } = {};
  452. private static Ctor = (() => {
  453. SelectionInspector.sectionStates["Node"] = true;
  454. })();
  455. }
  456. export = SelectionInspector;