ComponentInspector.ts 13 KB

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