ComponentInspector.ts 17 KB

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