SelectionInspector.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("../../EditorUI");
  23. import CreateComponentButton = require("./CreateComponentButton");
  24. import ScriptWidget = require("ui/ScriptWidget");
  25. import EditorEvents = require("editor/EditorEvents");
  26. import SerializableEditType = require("./SerializableEditType");
  27. import SelectionSection = require("./SelectionSection");
  28. import SelectionPrefabWidget = require("./SelectionPrefabWidget");
  29. import AttributeInfoEdit = require("./AttributeInfoEdit");
  30. class NodeSection extends SelectionSection {
  31. prefabWidget: SelectionPrefabWidget;
  32. transformEdits: AttributeInfoEdit[] = [];
  33. updateDelta: number = 0.0;
  34. constructor(editType: SerializableEditType) {
  35. super(editType);
  36. this.prefabWidget = new SelectionPrefabWidget();
  37. this.attrLayout.addChild(this.prefabWidget);
  38. this.transformEdits.push(this.attrEdits["Position"]);
  39. this.transformEdits.push(this.attrEdits["Rotation"]);
  40. this.transformEdits.push(this.attrEdits["Scale"]);
  41. this.subscribeToEvent("Update", (ev) => this.handleUpdate(ev));
  42. }
  43. handleUpdate(ev) {
  44. this.updateDelta -= ev.timeStep;
  45. if (this.updateDelta < 0.0) {
  46. this.updateDelta = 0.1;
  47. Atomic.ui.blockChangedEvents = true;
  48. for (var i in this.transformEdits) {
  49. this.transformEdits[i].refresh();
  50. }
  51. Atomic.ui.blockChangedEvents = false;
  52. }
  53. }
  54. }
  55. class ComponentSection extends SelectionSection {
  56. constructor(editType: SerializableEditType, inspector: SelectionInspector) {
  57. super(editType);
  58. var deleteButton = new Atomic.UIButton();
  59. deleteButton.text = "Delete Component";
  60. deleteButton.fontDescription = SelectionSection.fontDesc;
  61. deleteButton.onClick = () => {
  62. inspector.onComponentDelete(editType);
  63. return true;
  64. };
  65. var copyButton = new Atomic.UIButton();
  66. copyButton.text = "Copy Settings";
  67. copyButton.onClick = () => {
  68. inspector.onComponentCopy(editType);
  69. return true;
  70. };
  71. var pasteButton = new Atomic.UIButton();
  72. pasteButton.text = "Paste Settings";
  73. pasteButton.onClick = () => {
  74. inspector.onComponentPaste(editType);
  75. return true;
  76. };
  77. this.attrLayout.addChild(deleteButton);
  78. this.attrLayout.addChild(copyButton);
  79. this.attrLayout.addChild(pasteButton);
  80. }
  81. }
  82. class SceneSection extends SelectionSection {
  83. constructor(editType: SerializableEditType) {
  84. super(editType);
  85. }
  86. }
  87. interface AttributeEditResourceChangedEvent {
  88. attrInfoEdit: AttributeInfoEdit;
  89. resource: Atomic.Resource;
  90. }
  91. class JSComponentSection extends ComponentSection {
  92. constructor(editType: SerializableEditType, inspector: SelectionInspector) {
  93. super(editType, inspector);
  94. this.hasDynamicAttr = true;
  95. this.subscribeToEvent(this, "AttributeEditResourceChanged", (ev) => this.handleAttributeEditResourceChanged(ev));
  96. }
  97. private handleAttributeEditResourceChanged(ev: AttributeEditResourceChangedEvent) {
  98. var jsc = <Atomic.JSComponent>this.editType.getFirstObject();
  99. if (!jsc)
  100. return;
  101. var attrInfos = jsc.getAttributes();
  102. this.updateDynamicAttrInfos(attrInfos);
  103. }
  104. }
  105. class CSComponentSection extends ComponentSection {
  106. constructor(editType: SerializableEditType, inspector: SelectionInspector) {
  107. super(editType, inspector);
  108. this.hasDynamicAttr = true;
  109. this.subscribeToEvent(this, "AttributeEditResourceChanged", (ev) => this.handleAttributeEditResourceChanged(ev));
  110. this.subscribeToEvent("CSComponentAssemblyChanged", (ev) => this.handleCSComponentAssemblyChanged(ev));
  111. }
  112. private handleCSComponentAssemblyChanged(ev) {
  113. var csc = <AtomicNETScript.CSComponent>this.editType.getFirstObject();
  114. if (!csc)
  115. return;
  116. if (csc.assemblyFile == <AtomicNETScript.CSComponentAssembly> ev.resource) {
  117. var attrInfos = csc.getAttributes();
  118. this.updateDynamicAttrInfos(attrInfos);
  119. }
  120. }
  121. private handleAttributeEditResourceChanged(ev: AttributeEditResourceChangedEvent) {
  122. var csc = <AtomicNETScript.CSComponent>this.editType.getFirstObject();
  123. if (!csc)
  124. return;
  125. var attrInfos = csc.getAttributes();
  126. this.updateDynamicAttrInfos(attrInfos);
  127. }
  128. }
  129. // Node Inspector + Component Inspectors
  130. class SelectionInspector extends ScriptWidget {
  131. component: Atomic.Component;
  132. constructor(sceneEditor: Editor.SceneEditor3D) {
  133. super();
  134. this.sceneEditor = sceneEditor;
  135. var mainLayout = this.mainLayout = new Atomic.UILayout();
  136. mainLayout.spacing = 4;
  137. var lp = new Atomic.UILayoutParams();
  138. lp.width = 304;
  139. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  140. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  141. mainLayout.layoutParams = lp;
  142. mainLayout.axis = Atomic.UI_AXIS_Y;
  143. this.addChild(mainLayout);
  144. var noticeLayout = this.multipleSelectNotice = new Atomic.UILayout();
  145. noticeLayout.axis = Atomic.UI_AXIS_Y;
  146. noticeLayout.layoutParams = lp;
  147. var noticeText = new Atomic.UITextField();
  148. noticeText.textAlign = Atomic.UI_TEXT_ALIGN_CENTER;
  149. noticeText.skinBg = "InspectorTextAttrName";
  150. noticeText.text = "Multiple Selection - Some components are hidden";
  151. noticeText.fontDescription = SelectionSection.fontDesc;
  152. noticeText.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  153. noticeText.layoutParams = lp;
  154. noticeLayout.addChild(noticeText);
  155. noticeLayout.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  156. mainLayout.addChild(noticeLayout);
  157. this.createComponentButton = new CreateComponentButton();
  158. mainLayout.addChild(this.createComponentButton);
  159. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesBegin", (data) => this.handleSceneEditStateChangesBeginEvent());
  160. this.subscribeToEvent("SceneEditStateChange", (data) => this.handleSceneEditStateChangeEvent(data));
  161. this.subscribeToEvent(sceneEditor.scene, "SceneEditStateChangesEnd", (data) => this.handleSceneEditStateChangesEndEvent());
  162. this.subscribeToEvent(sceneEditor.scene, "SceneEditNodeRemoved", (ev: Editor.SceneEditNodeRemovedEvent) => this.handleSceneEditNodeRemoved(ev));
  163. this.subscribeToEvent(sceneEditor.scene, "SceneEditComponentAddedRemoved", (ev) => this.handleSceneEditComponentAddedRemovedEvent(ev));
  164. this.subscribeToEvent(this.createComponentButton, "SelectionCreateComponent", (data) => this.handleSelectionCreateComponent(data));
  165. }
  166. pruneSections() {
  167. var remove: SelectionSection[] = [];
  168. for (var i in this.sections) {
  169. var section = this.sections[i];
  170. var editType = section.editType;
  171. if (editType.typeName == "Node") {
  172. continue;
  173. }
  174. if (editType.typeName == "Scene") {
  175. var gotone = false;
  176. for (var j in this.nodes) {
  177. if (this.nodes[j].typeName == "Scene") {
  178. gotone = true;
  179. break;
  180. }
  181. }
  182. if (gotone)
  183. continue;
  184. }
  185. if (!editType.nodes.length) {
  186. remove.push(section);
  187. }
  188. }
  189. if (remove.length) {
  190. for (var i in remove) {
  191. var section = remove[i];
  192. this.removeSection(section);
  193. }
  194. this.suppressSections();
  195. }
  196. }
  197. suppressSections() {
  198. this.multipleSelectNotice.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  199. for (var i in this.sections) {
  200. var section = this.sections[i];
  201. var editType = section.editType;
  202. if (editType.typeName == "Node" || editType.typeName == "Scene") {
  203. continue;
  204. }
  205. var suppressed = false;
  206. for (var j in this.nodes) {
  207. if (editType.nodes.indexOf(this.nodes[j]) == -1) {
  208. suppressed = true;
  209. break;
  210. }
  211. }
  212. if (suppressed)
  213. this.multipleSelectNotice.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  214. section.suppress(suppressed);
  215. }
  216. }
  217. refresh() {
  218. Atomic.ui.blockChangedEvents = true;
  219. this.pruneSections();
  220. this.suppressSections();
  221. for (var i in this.sections) {
  222. this.sections[i].refresh();
  223. }
  224. if (this.nodeSection) {
  225. this.nodeSection.prefabWidget.updateSelection(this.nodes);
  226. }
  227. Atomic.ui.blockChangedEvents = false;
  228. }
  229. addSection(editType: SerializableEditType) {
  230. var section: SelectionSection;
  231. if (editType.typeName == "Node") {
  232. this.nodeSection = new NodeSection(editType);
  233. section = this.nodeSection;
  234. } else if (editType.typeName == "Scene") {
  235. section = new SceneSection(editType);
  236. } else if (editType.typeName == "JSComponent") {
  237. section = new JSComponentSection(editType, this);
  238. } else if (editType.typeName == "CSComponent") {
  239. section = new CSComponentSection(editType, this);
  240. }
  241. else {
  242. section = new ComponentSection(editType, this);
  243. }
  244. section.value = SelectionInspector.sectionStates[editType.typeName] ? 1 : 0;
  245. this.mainLayout.removeChild(this.createComponentButton, false);
  246. this.mainLayout.removeChild(this.multipleSelectNotice, false);
  247. // sort it in alphabetically
  248. this.sections.push(section);
  249. this.sections.sort(function(a, b) {
  250. if (a.editType.typeName == "Node" && b.editType.typeName == "Scene")
  251. return -1;
  252. if (a.editType.typeName == "Scene" && b.editType.typeName == "Node")
  253. return 1;
  254. if (a.editType.typeName == "Node" || a.editType.typeName == "Scene")
  255. return -1;
  256. if (b.editType.typeName == "Node" || b.editType.typeName == "Scene")
  257. return 1;
  258. return a.editType.typeName.localeCompare(b.editType.typeName);
  259. });
  260. var idx = this.sections.indexOf(section);
  261. if (idx == 0) {
  262. if (this.sections.length == 1) {
  263. this.mainLayout.addChild(section);
  264. } else {
  265. this.mainLayout.addChildBefore(section, this.sections[1]);
  266. }
  267. }
  268. else if (idx == this.sections.length - 1) {
  269. this.mainLayout.addChild(section);
  270. }
  271. else {
  272. this.mainLayout.addChildAfter(section, this.sections[idx - 1]);
  273. }
  274. // move the create component button down
  275. this.mainLayout.addChild(this.multipleSelectNotice);
  276. this.mainLayout.addChild(this.createComponentButton);
  277. }
  278. removeSection(section: SelectionSection) {
  279. SelectionInspector.sectionStates[section.editType.typeName] = section.value ? true : false;
  280. var index = this.sections.indexOf(section);
  281. this.sections.splice(index, 1);
  282. this.mainLayout.removeChild(section);
  283. }
  284. removeSerializable(serial: Atomic.Serializable) {
  285. for (var i in this.sections) {
  286. var section = this.sections[i];
  287. var e = section.editType;
  288. var index = e.objects.indexOf(serial);
  289. if (index != -1) {
  290. e.objects.splice(index, 1);
  291. }
  292. if (serial.typeName == "Node") {
  293. index = e.nodes.indexOf(<Atomic.Node>serial);
  294. if (index != -1) {
  295. e.nodes.splice(index, 1);
  296. }
  297. }
  298. }
  299. }
  300. addSerializable(serial: Atomic.Serializable): SerializableEditType {
  301. var editType = this.getEditType(serial);
  302. // does it already exist?
  303. for (var i in this.sections) {
  304. var section = this.sections[i];
  305. var e = section.editType;
  306. if (e.compareTypes(editType, this.nodes.length > 1)) {
  307. e.addSerializable(serial);
  308. return e;
  309. }
  310. }
  311. this.addSection(editType);
  312. return editType;
  313. }
  314. getEditType(serial: Atomic.Serializable): SerializableEditType {
  315. var typeName = serial.typeName;
  316. if (SelectionInspector._editTypes[typeName]) {
  317. return new SelectionInspector._editTypes[typeName](serial);
  318. }
  319. return new SerializableEditType(serial);
  320. }
  321. addNode(node: Atomic.Node) {
  322. var index = this.nodes.indexOf(node);
  323. if (index == -1) {
  324. this.nodes.push(node);
  325. this.addSerializable(node);
  326. var components = node.getComponents();
  327. for (var i in components) {
  328. if (this.filterComponent(components[i]))
  329. continue;
  330. var editType = this.addSerializable(components[i]);
  331. editType.addNode(node);
  332. }
  333. this.refresh();
  334. }
  335. }
  336. removeNode(node: Atomic.Node) {
  337. var index = this.nodes.indexOf(node);
  338. if (index != -1) {
  339. this.nodes.splice(index, 1);
  340. this.removeSerializable(node);
  341. var components = node.getComponents();
  342. for (var i in components) {
  343. if (this.filterComponent(components[i]))
  344. continue;
  345. this.removeSerializable(components[i]);
  346. }
  347. this.refresh();
  348. }
  349. // save node section state
  350. if (!this.nodes.length && this.nodeSection)
  351. SelectionInspector.sectionStates["Node"] = this.nodeSection.value ? true : false;
  352. }
  353. handleSceneEditStateChangesBeginEvent() {
  354. this.stateChangesInProgress = true;
  355. }
  356. handleSceneEditNodeRemoved(ev: Editor.SceneEditNodeRemovedEvent) {
  357. this.removeNode(ev.node);
  358. }
  359. handleSceneEditComponentAddedRemovedEvent(ev: Editor.SceneEditComponentAddedRemovedEvent) {
  360. if (this.filterComponent(ev.component)) {
  361. // still refresh as may affect UI (for example PrefabComponents)
  362. this.refresh();
  363. return;
  364. }
  365. var editType;
  366. if (!ev.removed) {
  367. editType = this.addSerializable(ev.component);
  368. editType.addNode(ev.node);
  369. } else {
  370. for (var i in this.sections) {
  371. var section = this.sections[i];
  372. editType = section.editType;
  373. var index = editType.objects.indexOf(ev.component);
  374. if (index != -1) {
  375. editType.objects.splice(index, 1);
  376. index = editType.nodes.indexOf(ev.node);
  377. if (index != -1) {
  378. editType.nodes.splice(index, 1);
  379. }
  380. break;
  381. }
  382. }
  383. }
  384. this.refresh();
  385. }
  386. onComponentDelete(editType: SerializableEditType) {
  387. var removed: Atomic.Component[] = [];
  388. for (var i in editType.objects) {
  389. var c = <Atomic.Component>editType.objects[i];
  390. removed.push(c);
  391. }
  392. for (var i in removed) {
  393. var c = removed[i];
  394. var node = c.node;
  395. c.remove();
  396. this.removeSerializable(removed[i]);
  397. var index = editType.nodes.indexOf(node);
  398. if (index != -1) {
  399. editType.nodes.splice(index, 1);
  400. }
  401. }
  402. if (removed.length) {
  403. this.sceneEditor.scene.sendEvent("SceneEditEnd");
  404. this.refresh();
  405. }
  406. }
  407. onComponentCopy(editType: SerializableEditType) {
  408. var copy: Atomic.Component[] = [];
  409. for (var i in editType.objects) {
  410. var c = <Atomic.Component>editType.objects[i];
  411. copy.push(c);
  412. }
  413. for (var i in copy) {
  414. var c = copy[i];
  415. this.component = c;
  416. this.sceneEditor.scene.sendEvent("SceneEditComponentCopy", { component: this.component });
  417. this.refresh();
  418. }
  419. }
  420. onComponentPaste(editType: SerializableEditType) {
  421. var paste: Atomic.Component[] = [];
  422. for (var i in editType.objects) {
  423. var c = <Atomic.Component>editType.objects[i];
  424. paste.push(c);
  425. }
  426. for (var i in paste) {
  427. var c = paste[i];
  428. this.component = c;
  429. this.sceneEditor.scene.sendEvent("SceneEditComponentPaste", { component: this.component });
  430. this.refresh();
  431. }
  432. }
  433. handleSelectionCreateComponent(ev) {
  434. var valid = true;
  435. if (ev.componentTypeName != "JSComponent" && ev.componentTypeName != "CSComponent") {
  436. for (var i in this.nodes) {
  437. var node = this.nodes[i];
  438. if (node.getComponent(ev.componentTypeName, false)) {
  439. valid = false;
  440. break;
  441. }
  442. }
  443. }
  444. if (!valid) {
  445. EditorUI.showModalError("Component Create", "Unable to create component, a node with an existing " + ev.componentTypeName + " component is selected");
  446. return;
  447. }
  448. for (var i in this.nodes) {
  449. var node = this.nodes[i];
  450. var c = node.createComponent(ev.componentTypeName);
  451. if (!c) {
  452. console.log("ERROR: unable to create component ", ev.componentTypeName);
  453. return;
  454. }
  455. var editType = this.addSerializable(c);
  456. editType.addNode(node);
  457. for (var i in this.sections) {
  458. if (this.sections[i].editType == editType) {
  459. this.sections[i].value = 1;
  460. break;
  461. }
  462. }
  463. }
  464. this.refresh();
  465. }
  466. handleSceneEditStateChangeEvent(ev: Editor.SceneEditStateChangeEvent) {
  467. if (!this.stateChangesInProgress)
  468. return;
  469. if (this.stateChanges.indexOf(ev.serializable) == -1) {
  470. this.stateChanges.push(ev.serializable);
  471. }
  472. }
  473. getPrefabComponent(node: Atomic.Node): Atomic.PrefabComponent {
  474. if (node.getComponent("PrefabComponent"))
  475. return <Atomic.PrefabComponent>node.getComponent("PrefabComponent");
  476. if (node.parent)
  477. return this.getPrefabComponent(node.parent);
  478. return null;
  479. }
  480. filterComponent(component: Atomic.Component): boolean {
  481. if (component.typeName == "PrefabComponent") {
  482. return true;
  483. }
  484. return false;
  485. }
  486. handleSceneEditStateChangesEndEvent() {
  487. Atomic.ui.blockChangedEvents = true;
  488. var sections: SelectionSection[] = [];
  489. for (var i in this.stateChanges) {
  490. var serial = this.stateChanges[i];
  491. for (var j in this.sections) {
  492. var section = this.sections[j];
  493. if (sections.indexOf(section) != -1)
  494. continue;
  495. if (section.editType.objects.indexOf(serial) != -1) {
  496. sections.push(section);
  497. if (section.hasDynamicAttr) {
  498. var object = section.editType.getFirstObject();
  499. if (object) {
  500. var attrInfos = object.getAttributes();
  501. section.updateDynamicAttrInfos(attrInfos);
  502. }
  503. }
  504. section.refresh();
  505. }
  506. }
  507. }
  508. Atomic.ui.blockChangedEvents = false;
  509. this.stateChanges = [];
  510. this.stateChangesInProgress = false;
  511. }
  512. mainLayout: Atomic.UILayout;
  513. multipleSelectNotice: Atomic.UILayout;
  514. sceneEditor: Editor.SceneEditor3D;
  515. nodes: Atomic.Node[] = [];
  516. sections: SelectionSection[] = [];
  517. createComponentButton: CreateComponentButton;
  518. nodeSection: NodeSection;
  519. stateChangesInProgress: boolean = false;
  520. stateChanges: Atomic.Serializable[] = [];
  521. // ------------------------------------
  522. static registerEditType(typeName: string, type: typeof SerializableEditType) {
  523. SelectionInspector._editTypes[typeName] = type;
  524. }
  525. private static sectionStates: { [typeName: string]: boolean } = {};
  526. private static _editTypes: { [typeName: string]: typeof SerializableEditType } = {};
  527. private static Ctor = (() => {
  528. SelectionInspector.sectionStates["Node"] = true;
  529. })();
  530. }
  531. export = SelectionInspector;