ComponentInspector.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 == "StaticSprite2D" || component.typeName == "AnimatedSprite2D") {
  88. this.addSpriteUI(attrsVerticalLayout, component.typeName);
  89. }
  90. var deleteButton = new Atomic.UIButton();
  91. deleteButton.text = "Delete Component";
  92. deleteButton.fontDescription = fd;
  93. deleteButton.onClick = () => {
  94. var node = this.component.node;
  95. this.component.remove();
  96. // refresh entire inspector, fix this...
  97. this.sendEvent("EditorActiveNodeChange", { node: node });
  98. return true;
  99. }
  100. attrsVerticalLayout.addChild(deleteButton);
  101. for (var i in this.bindings) {
  102. this.bindings[i].setWidgetValueFromObject();
  103. this.bindings[i].objectLocked = false;
  104. }
  105. }
  106. // Move these to a mixing class
  107. addPrefabUI(layout: Atomic.UILayout) {
  108. }
  109. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  110. var dragObject = ev.dragObject;
  111. if (dragObject.object && dragObject.object.typeName == "Asset") {
  112. var asset = <ToolCore.Asset> dragObject.object;
  113. if (asset.importerTypeName == importerTypeName) {
  114. return asset.importer;
  115. }
  116. }
  117. return null;
  118. }
  119. createMaterialClosure(layout: Atomic.UILayout, staticModel: Atomic.StaticModel, index: number) {
  120. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Material " + index, layout);
  121. var materialField = o.editField;
  122. materialField.readOnly = true;
  123. var select = o.selectButton;
  124. select.onClick = () => {
  125. EditorUI.getModelOps().showResourceSelection("Select Material", "MaterialImporter", function(asset: ToolCore.Asset) {
  126. staticModel.setMaterialIndex(index, <Atomic.Material> Atomic.cache.getResource("Material", asset.path));
  127. if (staticModel.getMaterial())
  128. materialField.text = staticModel.getMaterial().name;
  129. else
  130. materialField.text = "";
  131. });
  132. }
  133. var material = staticModel.getMaterial();
  134. if (material) {
  135. materialField.text = material.name;
  136. }
  137. // handle dropping of material on field
  138. materialField.subscribeToEvent(materialField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  139. if (ev.target == materialField) {
  140. var importer = this.acceptAssetDrag("MaterialImporter", ev);
  141. if (importer) {
  142. var materialImporter = <ToolCore.MaterialImporter> importer;
  143. var asset = materialImporter.asset;
  144. var material = <Atomic.Material> Atomic.cache.getResource("Material", asset.path);
  145. if (material) {
  146. staticModel.setMaterialIndex(index, material);
  147. ev.target.text = material.name;
  148. }
  149. }
  150. }
  151. });
  152. }
  153. addModelUI(layout: Atomic.UILayout, typeName: string) {
  154. var staticModel = <Atomic.StaticModel> this.component;
  155. var cacheModel = staticModel.model;
  156. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Model", layout);
  157. var field = o.editField;
  158. field.readOnly = true;
  159. var select = o.selectButton;
  160. select.onClick = () => {
  161. EditorUI.getModelOps().showResourceSelection("Select Model", "ModelImporter", function(asset: ToolCore.Asset) {
  162. staticModel.model = <Atomic.Model> Atomic.cache.getResource("Model", asset.cachePath + ".mdl");
  163. field.text = asset.name;
  164. });
  165. }
  166. if (cacheModel) {
  167. var asset = ToolCore.assetDatabase.getAssetByCachePath(cacheModel.name);
  168. if (asset) {
  169. field.text = asset.name;
  170. }
  171. }
  172. // handle dropping of model on field
  173. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  174. if (ev.target == field) {
  175. var importer = this.acceptAssetDrag("ModelImporter", ev);
  176. if (importer) {
  177. var modelImporter = <ToolCore.ModelImporter> importer;
  178. var asset = modelImporter.asset;
  179. // the model itself, not the node XML
  180. var model = <Atomic.Model> Atomic.cache.getResource("Model", asset.cachePath + ".mdl");
  181. if (model) {
  182. staticModel.model = model;
  183. ev.target.text = asset.name;
  184. }
  185. }
  186. }
  187. });
  188. var numGeometries = staticModel.numGeometries;
  189. if (typeName == "Skybox") {
  190. numGeometries = 1;
  191. }
  192. for (var x = 0; x < staticModel.numGeometries; x++) {
  193. this.createMaterialClosure(layout, staticModel, x);
  194. }
  195. }
  196. addJSComponentUI(layout: Atomic.UILayout) {
  197. var js = <Atomic.JSComponent> this.component;
  198. // expand prefab
  199. this.value = 1;
  200. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Script", layout);
  201. var field = o.editField;
  202. field.readOnly = true;
  203. field.text = js.componentFile ? js.componentFile.name : "";
  204. var select = o.selectButton;
  205. select.onClick = () => {
  206. EditorUI.getModelOps().showResourceSelection("Select JSComponent Script", "JavascriptImporter", function(asset: ToolCore.Asset) {
  207. js.componentFile = <Atomic.JSComponentFile> Atomic.cache.getResource("JSComponentFile", asset.path);
  208. if (js.componentFile)
  209. field.text = js.componentFile.name;
  210. });
  211. }
  212. // handle dropping of component on field
  213. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  214. if (ev.target == field) {
  215. var importer = this.acceptAssetDrag("JavascriptImporter", ev);
  216. if (importer) {
  217. var jsImporter = <ToolCore.JavascriptImporter> importer;
  218. if (jsImporter.isComponentFile()) {
  219. js.componentFile = <Atomic.JSComponentFile> Atomic.cache.getResource("JSComponentFile", importer.asset.path);
  220. if (js.componentFile)
  221. ev.target.text = js.componentFile.name;
  222. }
  223. }
  224. }
  225. });
  226. }
  227. addSpriteUI(layout: Atomic.UILayout, typeName: string) {
  228. var spriteComponent = <Atomic.StaticSprite2D> this.component;
  229. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Sprite", layout);
  230. var field = o.editField;
  231. field.readOnly = true;
  232. field.text = spriteComponent.sprite ? spriteComponent.sprite.name : "";
  233. var select = o.selectButton;
  234. select.onClick = () => {
  235. // this should allow selecting of sprite sheets as well
  236. EditorUI.getModelOps().showResourceSelection("Select Sprite", "TextureImporter", function(asset: ToolCore.Asset) {
  237. spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", asset.path);
  238. if (spriteComponent.sprite)
  239. field.text = spriteComponent.sprite.name;
  240. });
  241. }
  242. // handle dropping of component on field
  243. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  244. if (ev.target == field) {
  245. var importer = this.acceptAssetDrag("TextureImporter", ev);
  246. if (importer) {
  247. spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", importer.asset.path);
  248. if (spriteComponent.sprite)
  249. field.text = spriteComponent.sprite.name;
  250. }
  251. }
  252. });
  253. }
  254. addTilemap2DUI(layout: Atomic.UILayout) {
  255. var tilemap = <Atomic.TileMap2D> this.component;
  256. var o = InspectorUtils.createAttrEditFieldWithSelectButton("TMX File", layout);
  257. var field = o.editField;
  258. field.readOnly = true;
  259. field.text = tilemap.tmxFile ? tilemap.tmxFile.name : "";
  260. var select = o.selectButton;
  261. select.onClick = () => {
  262. // this should allow selecting of sprite sheets as well
  263. EditorUI.getModelOps().showResourceSelection("Select TMX File", "TMXImporter", function(asset: ToolCore.Asset) {
  264. tilemap.tmxFile = <Atomic.TmxFile2D> Atomic.cache.getResource("TmxFile2D", asset.path);
  265. if (tilemap.tmxFile)
  266. field.text = tilemap.tmxFile.name;
  267. });
  268. }
  269. // handle dropping of component on field
  270. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  271. if (ev.target == field) {
  272. var importer = this.acceptAssetDrag("TextureImporter", ev);
  273. if (importer) {
  274. tilemap.tmxFile = <Atomic.TmxFile2D> Atomic.cache.getResource("TmxFile2D", importer.asset.path);
  275. if (tilemap.tmxFile)
  276. field.text = tilemap.tmxFile.name;
  277. }
  278. }
  279. });
  280. }
  281. addLightCascadeParametersUI(layout: Atomic.UILayout) {
  282. var light = <Atomic.Light> this.component;
  283. var cascadeInfo = light.getShadowCascade();
  284. var container = InspectorUtils.createContainer();
  285. container.gravity = Atomic.UI_GRAVITY_ALL;
  286. layout.addChild(container);
  287. var panel = new Atomic.UILayout();
  288. panel.axis = Atomic.UI_AXIS_Y;
  289. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  290. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  291. container.addChild(panel);
  292. var label = InspectorUtils.createAttrName("CSM Splits:");
  293. panel.addChild(label);
  294. function createHandler(index, light, field) {
  295. return function(data: Atomic.UIWidgetEvent) {
  296. if (data.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  297. this.light.setShadowCascadeParameter(this.index, Number(this.field.text));
  298. }
  299. }.bind({ index: index, light: light, field: field });
  300. }
  301. var field = InspectorUtils.createAttrEditField("Split 0", panel);
  302. field.text = cascadeInfo[0].toString();
  303. field.subscribeToEvent(field, "WidgetEvent", createHandler(0, light, field));
  304. field = InspectorUtils.createAttrEditField("Split 1", panel);
  305. field.text = cascadeInfo[1].toString();
  306. field.subscribeToEvent(field, "WidgetEvent", createHandler(1, light, field));
  307. field = InspectorUtils.createAttrEditField("Split 2", panel);
  308. field.text = cascadeInfo[2].toString();
  309. field.subscribeToEvent(field, "WidgetEvent", createHandler(2, light, field));
  310. field = InspectorUtils.createAttrEditField("Split 3", panel);
  311. field.text = cascadeInfo[3].toString();
  312. field.subscribeToEvent(field, "WidgetEvent", createHandler(3, light, field));
  313. }
  314. addCollisionShapeUI(layout: Atomic.UILayout) {
  315. var shape = <Atomic.CollisionShape> this.component;
  316. var button = new Atomic.UIButton();
  317. button.fontDescription = InspectorUtils.attrFontDesc;
  318. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  319. button.text = "Set from Model";
  320. button.onClick = () => {
  321. var model = <Atomic.Drawable> shape.node.getComponent("StaticModel");
  322. if (model) {
  323. var box = model.boundingBox;
  324. shape.setBox([box[3] - box[0], box[4] - box[1], box[5] - box[2]]);
  325. }
  326. };
  327. layout.addChild(button);
  328. }
  329. component: Atomic.Component;
  330. bindings: Array<DataBinding> = new Array();
  331. }
  332. export = ComponentInspector;