ComponentInspector.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. import ScriptWidget = require("../ScriptWidget");
  2. import DataBinding = require("./DataBinding");
  3. import InspectorUtils = require("./InspectorUtils");
  4. import EditorUI = require("../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 == "StaticModel" || component.typeName == "AnimatedModel" || component.typeName == "Skybox") {
  82. this.addModelUI(attrsVerticalLayout, component.typeName);
  83. }
  84. if (component.typeName == "SoundSource" || component.typeName == "SoundSource3D") {
  85. this.addSoundSourceUI(attrsVerticalLayout, component.typeName);
  86. }
  87. var deleteButton = new Atomic.UIButton();
  88. deleteButton.text = "Delete Component";
  89. deleteButton.fontDescription = fd;
  90. deleteButton.onClick = () => {
  91. var node = this.component.node;
  92. this.component.remove();
  93. // refresh entire inspector, fix this...
  94. this.sendEvent("EditorActiveNodeChange", { node: node });
  95. return true;
  96. }
  97. attrsVerticalLayout.addChild(deleteButton);
  98. for (var i in this.bindings) {
  99. this.bindings[i].setWidgetValueFromObject();
  100. this.bindings[i].objectLocked = false;
  101. }
  102. }
  103. // Move these to a mixing class
  104. addPrefabUI(layout: Atomic.UILayout) {
  105. // expand prefab
  106. this.value = 1;
  107. var fd = new Atomic.UIFontDescription();
  108. fd.id = "Vera";
  109. fd.size = 11;
  110. var selectButton = new Atomic.UIButton();
  111. selectButton.text = "Select Prefab";
  112. selectButton.fontDescription = fd;
  113. selectButton.onClick = () => {
  114. var node = (<Atomic.PrefabComponent> this.component).getPrefabNode();
  115. this.sendEvent("EditorActiveNodeChange", { node: node });
  116. return true;
  117. }
  118. layout.addChild(selectButton);
  119. }
  120. acceptAssetDrag(importerTypeName: string, ev: Atomic.DragEndedEvent): ToolCore.AssetImporter {
  121. var dragObject = ev.dragObject;
  122. if (dragObject.object && dragObject.object.typeName == "Asset") {
  123. var asset = <ToolCore.Asset> dragObject.object;
  124. if (asset.importerTypeName == importerTypeName) {
  125. return asset.importer;
  126. }
  127. }
  128. return null;
  129. }
  130. addModelUI(layout: Atomic.UILayout, typeName: string) {
  131. var staticModel = <Atomic.StaticModel> this.component;
  132. var cacheModel = staticModel.model;
  133. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Model", layout);
  134. var field = o.editField;
  135. field.readOnly = true;
  136. var select = o.selectButton;
  137. select.onClick = () => {
  138. EditorUI.getModelOps().showResourceSelection("Select Model", "ModelImporter", function(asset: ToolCore.Asset) {
  139. staticModel.model = <Atomic.Model> Atomic.cache.getResource("Model", asset.cachePath + ".mdl");
  140. field.text = asset.name;
  141. });
  142. }
  143. if (cacheModel) {
  144. var asset = ToolCore.assetDatabase.getAssetByCachePath(cacheModel.name);
  145. if (asset) {
  146. field.text = asset.name;
  147. }
  148. }
  149. // handle dropping of model on field
  150. field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  151. if (ev.target == field) {
  152. var importer = this.acceptAssetDrag("ModelImporter", ev);
  153. if (importer) {
  154. var modelImporter = <ToolCore.ModelImporter> importer;
  155. var asset = modelImporter.asset;
  156. // the model itself, not the node XML
  157. var model = <Atomic.Model> Atomic.cache.getResource("Model", asset.cachePath + ".mdl");
  158. if (model) {
  159. staticModel.model = model;
  160. ev.target.text = asset.name;
  161. }
  162. }
  163. }
  164. });
  165. // MATERIAL FIELD (single material, not multimaterial for now)
  166. o = InspectorUtils.createAttrEditFieldWithSelectButton("Material", layout);
  167. var materialField = o.editField;
  168. materialField.readOnly = true;
  169. select = o.selectButton;
  170. select.onClick = () => {
  171. EditorUI.getModelOps().showResourceSelection("Select Material", "MaterialImporter", function(asset: ToolCore.Asset) {
  172. staticModel.setMaterial(<Atomic.Material> Atomic.cache.getResource("Material", asset.path));
  173. if (staticModel.getMaterial())
  174. materialField.text = staticModel.getMaterial().name;
  175. else
  176. materialField.text = "";
  177. });
  178. }
  179. var material = staticModel.getMaterial();
  180. if (material) {
  181. materialField.text = material.name;
  182. }
  183. // handle dropping of material on field
  184. materialField.subscribeToEvent(materialField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  185. if (ev.target == materialField) {
  186. var importer = this.acceptAssetDrag("MaterialImporter", ev);
  187. if (importer) {
  188. var materialImporter = <ToolCore.MaterialImporter> importer;
  189. var asset = materialImporter.asset;
  190. var material = <Atomic.Material> Atomic.cache.getResource("Material", asset.path);
  191. if (material) {
  192. staticModel.material = material;
  193. ev.target.text = material.name;
  194. }
  195. }
  196. }
  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. addLightCascadeParametersUI(layout: Atomic.UILayout) {
  257. var light = <Atomic.Light> this.component;
  258. var cascadeInfo = light.getShadowCascade();
  259. var container = InspectorUtils.createContainer();
  260. container.gravity = Atomic.UI_GRAVITY_ALL;
  261. layout.addChild(container);
  262. var panel = new Atomic.UILayout();
  263. panel.axis = Atomic.UI_AXIS_Y;
  264. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  265. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  266. container.addChild(panel);
  267. var label = InspectorUtils.createAttrName("CSM Splits:");
  268. panel.addChild(label);
  269. function createHandler(index, light, field) {
  270. return function(data: Atomic.UIWidgetEvent) {
  271. if (data.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  272. this.light.setShadowCascadeParameter(this.index, Number(this.field.text));
  273. }
  274. }.bind({ index: index, light: light, field: field });
  275. }
  276. var field = InspectorUtils.createAttrEditField("Split 0", panel);
  277. field.text = cascadeInfo[0].toString();
  278. field.subscribeToEvent(field, "WidgetEvent", createHandler(0, light, field));
  279. field = InspectorUtils.createAttrEditField("Split 1", panel);
  280. field.text = cascadeInfo[1].toString();
  281. field.subscribeToEvent(field, "WidgetEvent", createHandler(1, light, field));
  282. field = InspectorUtils.createAttrEditField("Split 2", panel);
  283. field.text = cascadeInfo[2].toString();
  284. field.subscribeToEvent(field, "WidgetEvent", createHandler(2, light, field));
  285. field = InspectorUtils.createAttrEditField("Split 3", panel);
  286. field.text = cascadeInfo[3].toString();
  287. field.subscribeToEvent(field, "WidgetEvent", createHandler(3, light, field));
  288. }
  289. addCollisionShapeUI(layout: Atomic.UILayout) {
  290. var shape = <Atomic.CollisionShape> this.component;
  291. var button = new Atomic.UIButton();
  292. button.fontDescription = InspectorUtils.attrFontDesc;
  293. button.gravity = Atomic.UI_GRAVITY_RIGHT;
  294. button.text = "Set from Model";
  295. button.onClick = () => {
  296. var model = <Atomic.Drawable> shape.node.getComponent("StaticModel");
  297. if (model) {
  298. var box = model.boundingBox;
  299. shape.setBox([box[3] - box[0], box[4] - box[1], box[5] - box[2]]);
  300. }
  301. };
  302. layout.addChild(button);
  303. }
  304. component: Atomic.Component;
  305. bindings: Array<DataBinding> = new Array();
  306. }
  307. export = ComponentInspector;