ComponentInspector.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.typeName;
  23. // For JSComponents append the filename
  24. if (component.typeName == "JSComponent") {
  25. var jsc = <Atomic.JSComponent> component;
  26. if (jsc.componentFile) {
  27. var pathInfo = Atomic.splitPath( jsc.componentFile.name);
  28. this.text = "JS - " + pathInfo.fileName;
  29. }
  30. }
  31. // don't expand by default
  32. this.value = 0;
  33. var fd = new Atomic.UIFontDescription();
  34. fd.id = "Vera";
  35. fd.size = 11;
  36. var nlp = new Atomic.UILayoutParams();
  37. nlp.width = 304;
  38. // atttribute name layout param
  39. var atlp = new Atomic.UILayoutParams();
  40. atlp.width = 100;
  41. var attrsVerticalLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  42. attrsVerticalLayout.spacing = 3;
  43. attrsVerticalLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  44. attrsVerticalLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  45. this.contentRoot.addChild(attrsVerticalLayout);
  46. var attrs = component.getAttributes();
  47. for (var i in attrs) {
  48. var attr = <Atomic.AttributeInfo> attrs[i];
  49. if (attr.mode & Atomic.AM_NOEDIT)
  50. continue;
  51. var binding = DataBinding.createBinding(component, attr);
  52. if (!binding)
  53. continue;
  54. var attrLayout = new Atomic.UILayout();
  55. attrLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  56. var name = new Atomic.UITextField();
  57. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  58. name.skinBg = "InspectorTextAttrName";
  59. name.layoutParams = atlp;
  60. if (attr.type == Atomic.VAR_VECTOR3 || attr.type == Atomic.VAR_COLOR ||
  61. attr.type == Atomic.VAR_QUATERNION) {
  62. attrLayout.axis = Atomic.UI_AXIS_Y;
  63. attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  64. attrLayout.skinBg = "InspectorVectorAttrLayout";
  65. }
  66. var bname = attr.name;
  67. if (bname == "Is Enabled")
  68. bname = "Enabled";
  69. name.text = bname;
  70. name.fontDescription = fd;
  71. attrLayout.addChild(name);
  72. attrLayout.addChild(binding.widget);
  73. attrsVerticalLayout.addChild(attrLayout);
  74. this.bindings.push(binding);
  75. }
  76. // custom component UI
  77. if (component.typeName == "PrefabComponent") {
  78. this.addPrefabUI(attrsVerticalLayout);
  79. }
  80. if (component.typeName == "Light") {
  81. this.addLightCascadeParametersUI(attrsVerticalLayout);
  82. }
  83. if (component.typeName == "CollisionShape") {
  84. this.addCollisionShapeUI(attrsVerticalLayout);
  85. }
  86. if (component.typeName == "JSComponent") {
  87. // auto expand JSComponents
  88. this.value = 1;
  89. }
  90. if (component.typeName == "TileMap2D") {
  91. this.addTilemap2DUI(attrsVerticalLayout);
  92. }
  93. if (component.typeName == "StaticModel" || component.typeName == "AnimatedModel" || component.typeName == "Skybox") {
  94. this.addModelUI(attrsVerticalLayout, component.typeName);
  95. }
  96. if (component.typeName == "StaticSprite2D" || component.typeName == "AnimatedSprite2D") {
  97. this.addSpriteUI(attrsVerticalLayout, component.typeName);
  98. }
  99. var deleteButton = new Atomic.UIButton();
  100. deleteButton.text = "Delete Component";
  101. deleteButton.fontDescription = fd;
  102. deleteButton.onClick = () => {
  103. var node = this.component.node;
  104. this.component.remove();
  105. // refresh entire inspector, fix this...
  106. this.sendEvent("EditorActiveNodeChange", { node: node });
  107. return true;
  108. }
  109. attrsVerticalLayout.addChild(deleteButton);
  110. for (var i in this.bindings) {
  111. this.bindings[i].setWidgetValueFromObject();
  112. this.bindings[i].objectLocked = false;
  113. }
  114. }
  115. // Move these to a mixing class
  116. addPrefabUI(layout: Atomic.UILayout) {
  117. }
  118. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  119. var dragObject = ev.dragObject;
  120. if (dragObject.object && dragObject.object.typeName == "Asset") {
  121. var asset = <ToolCore.Asset> dragObject.object;
  122. if (asset.importerTypeName == importerTypeName) {
  123. return asset.importer;
  124. }
  125. }
  126. return null;
  127. }
  128. createMaterialClosure(layout: Atomic.UILayout, staticModel: Atomic.StaticModel, index: number) {
  129. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Material " + index, layout);
  130. var materialField = o.editField;
  131. materialField.readOnly = true;
  132. var select = o.selectButton;
  133. select.onClick = () => {
  134. EditorUI.getModelOps().showResourceSelection("Select Material", "MaterialImporter", function(asset: ToolCore.Asset) {
  135. staticModel.setMaterialIndex(index, <Atomic.Material> Atomic.cache.getResource("Material", asset.path));
  136. if (staticModel.getMaterial())
  137. materialField.text = staticModel.getMaterial().name;
  138. else
  139. materialField.text = "";
  140. });
  141. }
  142. var material = staticModel.getMaterial();
  143. if (material) {
  144. materialField.text = material.name;
  145. }
  146. // handle dropping of material on field
  147. materialField.subscribeToEvent(materialField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  148. if (ev.target == materialField) {
  149. var importer = this.acceptAssetDrag("MaterialImporter", ev);
  150. if (importer) {
  151. var materialImporter = <ToolCore.MaterialImporter> importer;
  152. var asset = materialImporter.asset;
  153. var material = <Atomic.Material> Atomic.cache.getResource("Material", asset.path);
  154. if (material) {
  155. staticModel.setMaterialIndex(index, material);
  156. ev.target.text = material.name;
  157. }
  158. }
  159. }
  160. });
  161. }
  162. addModelUI(layout: Atomic.UILayout, typeName: string) {
  163. var staticModel = <Atomic.StaticModel> this.component;
  164. var numGeometries = staticModel.numGeometries;
  165. if (typeName == "Skybox") {
  166. numGeometries = 1;
  167. }
  168. for (var x = 0; x < numGeometries; x++) {
  169. this.createMaterialClosure(layout, staticModel, x);
  170. }
  171. }
  172. addSpriteUI(layout: Atomic.UILayout, typeName: string) {
  173. var spriteComponent = <Atomic.StaticSprite2D> this.component;
  174. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Sprite", layout);
  175. var field = o.editField;
  176. field.readOnly = true;
  177. field.text = spriteComponent.sprite ? spriteComponent.sprite.name : "";
  178. var select = o.selectButton;
  179. select.onClick = () => {
  180. // this should allow selecting of sprite sheets as well
  181. EditorUI.getModelOps().showResourceSelection("Select Sprite", "TextureImporter", function(asset: ToolCore.Asset) {
  182. spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", asset.path);
  183. if (spriteComponent.sprite)
  184. field.text = spriteComponent.sprite.name;
  185. });
  186. }
  187. // handle dropping of component on field
  188. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  189. if (ev.target == field) {
  190. var importer = this.acceptAssetDrag("TextureImporter", ev);
  191. if (importer) {
  192. spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", importer.asset.path);
  193. if (spriteComponent.sprite)
  194. field.text = spriteComponent.sprite.name;
  195. }
  196. }
  197. });
  198. }
  199. addTilemap2DUI(layout: Atomic.UILayout) {
  200. var tilemap = <Atomic.TileMap2D> this.component;
  201. var o = InspectorUtils.createAttrEditFieldWithSelectButton("TMX File", layout);
  202. var field = o.editField;
  203. field.readOnly = true;
  204. field.text = tilemap.tmxFile ? tilemap.tmxFile.name : "";
  205. var select = o.selectButton;
  206. select.onClick = () => {
  207. // this should allow selecting of sprite sheets as well
  208. EditorUI.getModelOps().showResourceSelection("Select TMX File", "TMXImporter", function(asset: ToolCore.Asset) {
  209. tilemap.tmxFile = <Atomic.TmxFile2D> Atomic.cache.getResource("TmxFile2D", asset.path);
  210. if (tilemap.tmxFile)
  211. field.text = tilemap.tmxFile.name;
  212. });
  213. }
  214. // handle dropping of component on field
  215. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  216. if (ev.target == field) {
  217. var importer = this.acceptAssetDrag("TextureImporter", ev);
  218. if (importer) {
  219. tilemap.tmxFile = <Atomic.TmxFile2D> Atomic.cache.getResource("TmxFile2D", importer.asset.path);
  220. if (tilemap.tmxFile)
  221. field.text = tilemap.tmxFile.name;
  222. }
  223. }
  224. });
  225. }
  226. addLightCascadeParametersUI(layout: Atomic.UILayout) {
  227. var light = <Atomic.Light> this.component;
  228. var cascadeInfo = light.getShadowCascade();
  229. var container = InspectorUtils.createContainer();
  230. container.gravity = Atomic.UI_GRAVITY_ALL;
  231. layout.addChild(container);
  232. var panel = new Atomic.UILayout();
  233. panel.axis = Atomic.UI_AXIS_Y;
  234. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  235. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  236. container.addChild(panel);
  237. var label = InspectorUtils.createAttrName("CSM Splits:");
  238. panel.addChild(label);
  239. function createHandler(index, light, field) {
  240. return function(data: Atomic.UIWidgetEvent) {
  241. if (data.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  242. this.light.setShadowCascadeParameter(this.index, Number(this.field.text));
  243. }
  244. }.bind({ index: index, light: light, field: field });
  245. }
  246. var field = InspectorUtils.createAttrEditField("Split 0", panel);
  247. field.text = cascadeInfo[0].toString();
  248. field.subscribeToEvent(field, "WidgetEvent", createHandler(0, light, field));
  249. field = InspectorUtils.createAttrEditField("Split 1", panel);
  250. field.text = cascadeInfo[1].toString();
  251. field.subscribeToEvent(field, "WidgetEvent", createHandler(1, light, field));
  252. field = InspectorUtils.createAttrEditField("Split 2", panel);
  253. field.text = cascadeInfo[2].toString();
  254. field.subscribeToEvent(field, "WidgetEvent", createHandler(2, light, field));
  255. field = InspectorUtils.createAttrEditField("Split 3", panel);
  256. field.text = cascadeInfo[3].toString();
  257. field.subscribeToEvent(field, "WidgetEvent", createHandler(3, light, field));
  258. }
  259. addCollisionShapeUI(layout: Atomic.UILayout) {
  260. var shape = <Atomic.CollisionShape> this.component;
  261. var button = new Atomic.UIButton();
  262. button.fontDescription = InspectorUtils.attrFontDesc;
  263. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  264. button.text = "Set from Model";
  265. button.onClick = () => {
  266. var model = <Atomic.Drawable> shape.node.getComponent("StaticModel");
  267. if (model) {
  268. var box = model.boundingBox;
  269. shape.setBox([box[3] - box[0], box[4] - box[1], box[5] - box[2]]);
  270. }
  271. };
  272. layout.addChild(button);
  273. }
  274. component: Atomic.Component;
  275. bindings: Array<DataBinding> = new Array();
  276. }
  277. export = ComponentInspector;