ComponentInspector.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 ScriptWidget = require("ui/ScriptWidget");
  8. import DataBinding = require("./DataBinding");
  9. import InspectorUtils = require("./InspectorUtils");
  10. import EditorUI = require("ui/EditorUI");
  11. import CSComponentClassSelector = require("./CSComponentClassSelector");
  12. class ComponentInspector extends Atomic.UISection {
  13. constructor() {
  14. super();
  15. this.subscribeToEvent("WidgetEvent", (data) => this.handleWidgetEvent(data));
  16. }
  17. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  18. var handled = false;
  19. for (var i = 0; i < this.bindings.length; i++) {
  20. if (this.bindings[i].handleWidgetEvent(ev)) {
  21. handled = true;
  22. }
  23. }
  24. // return if handled
  25. return handled;
  26. }
  27. addAttr(attr: Atomic.AttributeInfo, before: Atomic.UIWidget = null, after: Atomic.UIWidget = null): Atomic.UILayout {
  28. if (attr.mode & Atomic.AM_NOEDIT)
  29. return null;
  30. var binding = DataBinding.createBinding(this.component, attr);
  31. if (!binding)
  32. return null;
  33. var attrLayout = new Atomic.UILayout();
  34. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  35. var name = new Atomic.UITextField();
  36. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  37. name.skinBg = "InspectorTextAttrName";
  38. name.layoutParams = this.atlp;
  39. if (attr.type == Atomic.VAR_VECTOR3 || attr.type == Atomic.VAR_COLOR ||
  40. attr.type == Atomic.VAR_QUATERNION) {
  41. attrLayout.axis = Atomic.UI_AXIS_Y;
  42. attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  43. attrLayout.skinBg = "InspectorVectorAttrLayout";
  44. }
  45. var bname = attr.name;
  46. if (bname == "Is Enabled")
  47. bname = "Enabled";
  48. name.text = bname;
  49. name.fontDescription = this.fd;
  50. attrLayout.addChild(name);
  51. attrLayout.addChild(binding.widget);
  52. if (before)
  53. this.attrsVerticalLayout.addChildBefore(attrLayout, before);
  54. else if (after)
  55. this.attrsVerticalLayout.addChildAfter(attrLayout, after);
  56. else
  57. this.attrsVerticalLayout.addChild(attrLayout);
  58. this.bindings.push(binding);
  59. return attrLayout;
  60. }
  61. inspect(component: Atomic.Component) {
  62. this.component = component;
  63. this.text = component.typeName;
  64. this.subscribeToEvent(component, "SceneEditStateChange", (data) => this.handleSceneEditStateChangeEvent(data));
  65. // For JSComponents append the filename
  66. if (component.typeName == "JSComponent") {
  67. var jsc = <Atomic.JSComponent>component;
  68. if (jsc.componentFile) {
  69. var pathInfo = Atomic.splitPath(jsc.componentFile.name);
  70. this.text = "JS - " + pathInfo.fileName;
  71. }
  72. }
  73. // For CSComponents append the classname
  74. if (component.typeName == "CSComponent") {
  75. var csc = <AtomicNET.CSComponent>component;
  76. if (csc.componentClassName) {
  77. this.text = "CS - " + csc.componentClassName;
  78. }
  79. }
  80. // don't expand by default
  81. this.value = 0;
  82. var fd = this.fd = new Atomic.UIFontDescription();
  83. fd.id = "Vera";
  84. fd.size = 11;
  85. var nlp = this.nlp = new Atomic.UILayoutParams();
  86. nlp.width = 304;
  87. // atttribute name layout param
  88. var atlp = this.atlp = new Atomic.UILayoutParams();
  89. atlp.width = 100;
  90. var attrsVerticalLayout = this.attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  91. attrsVerticalLayout.spacing = 3;
  92. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  93. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  94. this.contentRoot.addChild(attrsVerticalLayout);
  95. var attrs = component.getAttributes();
  96. for (var i in attrs) {
  97. this.addAttr(attrs[i]);
  98. }
  99. // custom component UI
  100. if (component.typeName == "PrefabComponent") {
  101. this.addPrefabUI(attrsVerticalLayout);
  102. }
  103. if (component.typeName == "Light") {
  104. this.addLightCascadeParametersUI(attrsVerticalLayout);
  105. }
  106. if (component.typeName == "CollisionShape") {
  107. this.addCollisionShapeUI(attrsVerticalLayout);
  108. }
  109. if (component.typeName == "JSComponent") {
  110. // auto expand JSComponents
  111. this.value = 1;
  112. }
  113. if (component.typeName == "CSComponent") {
  114. // auto expand CSComponents
  115. this.value = 1;
  116. this.addCSComponentUI(attrsVerticalLayout);
  117. var csc = <AtomicNET.CSComponent>this.component;
  118. var currentClassName = csc.componentClassName;
  119. this.subscribeToEvent(component, "CSComponentClassChanged", (ev: AtomicNET.CSComponentClassChangedEvent) => {
  120. if (currentClassName != ev.classname) {
  121. //console.log("CSComponent Class Name Changed ", currentClassName, " ", ev.classname);
  122. this.text = "CS - " + ev.classname;
  123. currentClassName = ev.classname;
  124. this.updateDataBindings();
  125. }
  126. });
  127. }
  128. if (component.typeName == "TileMap2D") {
  129. this.addTilemap2DUI(attrsVerticalLayout);
  130. }
  131. if (component.typeName == "StaticModel" || component.typeName == "AnimatedModel" || component.typeName == "Skybox") {
  132. this.addModelUI(attrsVerticalLayout, component.typeName);
  133. }
  134. if (component.typeName == "StaticSprite2D" || component.typeName == "AnimatedSprite2D") {
  135. this.addSpriteUI(attrsVerticalLayout, component.typeName);
  136. }
  137. var deleteButton = this.deleteButton = new Atomic.UIButton();
  138. deleteButton.text = "Delete Component";
  139. deleteButton.fontDescription = fd;
  140. deleteButton.onClick = () => {
  141. var node = this.component.node;
  142. this.component.remove();
  143. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  144. return true;
  145. }
  146. attrsVerticalLayout.addChild(deleteButton);
  147. for (var i in this.bindings) {
  148. this.bindings[i].setWidgetValueFromObject();
  149. this.bindings[i].objectLocked = false;
  150. }
  151. }
  152. updateDataBindings() {
  153. var newBindings: Array<DataBinding> = new Array();
  154. var foundAttr: Array<Atomic.AttributeInfo> = new Array();
  155. var attrs = this.component.getAttributes();
  156. // run through current attr bindings looking for ones to preserve
  157. for (var i in this.bindings) {
  158. var binding = this.bindings[i];
  159. for (var j in attrs) {
  160. var attr = <Atomic.AttributeInfo>attrs[j];
  161. if (attr.name == binding.attrInfo.name && attr.type == binding.attrInfo.type) {
  162. newBindings.push(binding);
  163. foundAttr.push(attr);
  164. break;
  165. }
  166. }
  167. }
  168. // remove bindings that no longer exist
  169. for (var i in this.bindings) {
  170. var binding = this.bindings[i];
  171. if (newBindings.indexOf(binding) == -1) {
  172. binding.widget.parent.remove();
  173. }
  174. }
  175. // set new bindings, additional bindings may be added below
  176. this.bindings = newBindings;
  177. // add new attr
  178. var curAttrLayout:Atomic.UILayout = null;
  179. for (var i in attrs) {
  180. var attr = attrs[i];
  181. if (foundAttr.indexOf(attr) == -1) {
  182. if (!curAttrLayout)
  183. curAttrLayout = this.addAttr(attr, this.deleteButton);
  184. else
  185. curAttrLayout = this.addAttr(attr, null, curAttrLayout);
  186. }
  187. }
  188. for (var i in this.bindings) {
  189. this.bindings[i].setWidgetValueFromObject();
  190. this.bindings[i].objectLocked = false;
  191. }
  192. }
  193. updateWidgetValues() {
  194. for (var i in this.bindings) {
  195. this.bindings[i].objectLocked = true;
  196. this.bindings[i].setWidgetValueFromObject();
  197. this.bindings[i].objectLocked = false;
  198. }
  199. }
  200. handleSceneEditStateChangeEvent(ev) {
  201. this.updateWidgetValues();
  202. }
  203. // Move these to a mixing class
  204. addPrefabUI(layout: Atomic.UILayout) {
  205. }
  206. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  207. var dragObject = ev.dragObject;
  208. if (dragObject.object && dragObject.object.typeName == "Asset") {
  209. var asset = <ToolCore.Asset>dragObject.object;
  210. if (asset.importerTypeName == importerTypeName) {
  211. return asset.importer;
  212. }
  213. }
  214. return null;
  215. }
  216. createMaterialClosure(layout: Atomic.UILayout, staticModel: Atomic.StaticModel, index: number) {
  217. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Material " + index, layout);
  218. var materialField = o.editField;
  219. materialField.readOnly = true;
  220. var select = o.selectButton;
  221. select.onClick = () => {
  222. EditorUI.getModelOps().showResourceSelection("Select Material", "MaterialImporter", function(asset: ToolCore.Asset) {
  223. staticModel.setMaterialIndex(index, <Atomic.Material>Atomic.cache.getResource("Material", asset.path));
  224. if (staticModel.getMaterial())
  225. materialField.text = staticModel.getMaterial().name;
  226. else
  227. materialField.text = "";
  228. });
  229. }
  230. var material = staticModel.getMaterial();
  231. if (material) {
  232. materialField.text = material.name;
  233. }
  234. // handle dropping of material on field
  235. materialField.subscribeToEvent(materialField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  236. if (ev.target == materialField) {
  237. var importer = this.acceptAssetDrag("MaterialImporter", ev);
  238. if (importer) {
  239. var materialImporter = <ToolCore.MaterialImporter>importer;
  240. var asset = materialImporter.asset;
  241. var material = <Atomic.Material>Atomic.cache.getResource("Material", asset.path);
  242. if (material) {
  243. staticModel.setMaterialIndex(index, material);
  244. ev.target.text = material.name;
  245. }
  246. }
  247. }
  248. });
  249. }
  250. addModelUI(layout: Atomic.UILayout, typeName: string) {
  251. var staticModel = <Atomic.StaticModel>this.component;
  252. var numGeometries = staticModel.numGeometries;
  253. if (typeName == "Skybox") {
  254. numGeometries = 1;
  255. }
  256. for (var x = 0; x < numGeometries; x++) {
  257. this.createMaterialClosure(layout, staticModel, x);
  258. }
  259. }
  260. addSpriteUI(layout: Atomic.UILayout, typeName: string) {
  261. var spriteComponent = <Atomic.StaticSprite2D>this.component;
  262. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Sprite", layout);
  263. var field = o.editField;
  264. field.readOnly = true;
  265. field.text = spriteComponent.sprite ? spriteComponent.sprite.name : "";
  266. var select = o.selectButton;
  267. select.onClick = () => {
  268. // this should allow selecting of sprite sheets as well
  269. EditorUI.getModelOps().showResourceSelection("Select Sprite", "TextureImporter", function(asset: ToolCore.Asset) {
  270. spriteComponent.sprite = <Atomic.Sprite2D>Atomic.cache.getResource("Sprite2D", asset.path);
  271. if (spriteComponent.sprite)
  272. field.text = spriteComponent.sprite.name;
  273. });
  274. }
  275. // handle dropping of component on field
  276. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  277. if (ev.target == field) {
  278. var importer = this.acceptAssetDrag("TextureImporter", ev);
  279. if (importer) {
  280. spriteComponent.sprite = <Atomic.Sprite2D>Atomic.cache.getResource("Sprite2D", importer.asset.path);
  281. if (spriteComponent.sprite)
  282. field.text = spriteComponent.sprite.name;
  283. }
  284. }
  285. });
  286. }
  287. addTilemap2DUI(layout: Atomic.UILayout) {
  288. var tilemap = <Atomic.TileMap2D>this.component;
  289. var o = InspectorUtils.createAttrEditFieldWithSelectButton("TMX File", layout);
  290. var field = o.editField;
  291. field.readOnly = true;
  292. field.text = tilemap.tmxFile ? tilemap.tmxFile.name : "";
  293. var select = o.selectButton;
  294. select.onClick = () => {
  295. // this should allow selecting of sprite sheets as well
  296. EditorUI.getModelOps().showResourceSelection("Select TMX File", "TMXImporter", function(asset: ToolCore.Asset) {
  297. tilemap.tmxFile = <Atomic.TmxFile2D>Atomic.cache.getResource("TmxFile2D", asset.path);
  298. if (tilemap.tmxFile)
  299. field.text = tilemap.tmxFile.name;
  300. });
  301. }
  302. // handle dropping of component on field
  303. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  304. if (ev.target == field) {
  305. var importer = this.acceptAssetDrag("TextureImporter", ev);
  306. if (importer) {
  307. tilemap.tmxFile = <Atomic.TmxFile2D>Atomic.cache.getResource("TmxFile2D", importer.asset.path);
  308. if (tilemap.tmxFile)
  309. field.text = tilemap.tmxFile.name;
  310. }
  311. }
  312. });
  313. }
  314. addLightCascadeParametersUI(layout: Atomic.UILayout) {
  315. var light = <Atomic.Light>this.component;
  316. var cascadeInfo = light.getShadowCascade();
  317. var container = InspectorUtils.createContainer();
  318. container.gravity = Atomic.UI_GRAVITY_ALL;
  319. layout.addChild(container);
  320. var panel = new Atomic.UILayout();
  321. panel.axis = Atomic.UI_AXIS_Y;
  322. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  323. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  324. container.addChild(panel);
  325. var label = InspectorUtils.createAttrName("CSM Splits:");
  326. panel.addChild(label);
  327. function createHandler(index, light, field) {
  328. return function(data: Atomic.UIWidgetEvent) {
  329. if (data.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  330. this.light.setShadowCascadeParameter(this.index, Number(this.field.text));
  331. }
  332. }.bind({ index: index, light: light, field: field });
  333. }
  334. var field = InspectorUtils.createAttrEditField("Split 0", panel);
  335. field.text = cascadeInfo[0].toString();
  336. field.subscribeToEvent(field, "WidgetEvent", createHandler(0, light, field));
  337. field = InspectorUtils.createAttrEditField("Split 1", panel);
  338. field.text = cascadeInfo[1].toString();
  339. field.subscribeToEvent(field, "WidgetEvent", createHandler(1, light, field));
  340. field = InspectorUtils.createAttrEditField("Split 2", panel);
  341. field.text = cascadeInfo[2].toString();
  342. field.subscribeToEvent(field, "WidgetEvent", createHandler(2, light, field));
  343. field = InspectorUtils.createAttrEditField("Split 3", panel);
  344. field.text = cascadeInfo[3].toString();
  345. field.subscribeToEvent(field, "WidgetEvent", createHandler(3, light, field));
  346. }
  347. addCollisionShapeUI(layout: Atomic.UILayout) {
  348. var shape = <Atomic.CollisionShape>this.component;
  349. var button = new Atomic.UIButton();
  350. button.fontDescription = InspectorUtils.attrFontDesc;
  351. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  352. button.text = "Set from Model";
  353. button.onClick = () => {
  354. var model = <Atomic.Drawable>shape.node.getComponent("StaticModel");
  355. if (model) {
  356. var box = model.boundingBox;
  357. shape.setBox([box[3] - box[0], box[4] - box[1], box[5] - box[2]]);
  358. }
  359. };
  360. layout.addChild(button);
  361. }
  362. addCSComponentUI(layout: Atomic.UILayout) {
  363. for (var i in this.bindings) {
  364. var binding = this.bindings[i];
  365. // replace the Class widget with a editfield + select button
  366. if (binding.attrInfo.name == "Class") {
  367. var parent = binding.widget.parent;
  368. var text = binding.widget.text;
  369. binding.widget.parent.removeChild(binding.widget);
  370. var o = InspectorUtils.createAttrEditFieldWithSelectButton("", parent);
  371. o.editField.text = text;
  372. binding.widget = o.editField;
  373. o.selectButton.onClick = () => {
  374. var cscomponent = <AtomicNET.CSComponent>this.component;
  375. if (cscomponent.assemblyFile) {
  376. var selector = new CSComponentClassSelector(o.editField, cscomponent);
  377. }
  378. }
  379. break;
  380. }
  381. }
  382. }
  383. component: Atomic.Component;
  384. bindings: Array<DataBinding> = new Array();
  385. fd: Atomic.UIFontDescription;
  386. nlp: Atomic.UILayoutParams;
  387. // atttribute name layout param
  388. atlp: Atomic.UILayoutParams;
  389. attrsVerticalLayout: Atomic.UILayout;
  390. deleteButton: Atomic.UIButton;
  391. }
  392. export = ComponentInspector;