ComponentAttributeUI.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 EditorUI = require("ui/EditorUI");
  8. import InspectorUtils = require("./InspectorUtils");
  9. import AttributeInfoEdit = require("./AttributeInfoEdit");
  10. import SerializableEditType = require("./SerializableEditType");
  11. class LightCascadeAttributeEdit extends AttributeInfoEdit {
  12. splitFields: Atomic.UIEditField[] = [];
  13. createEditWidget() {
  14. var panel = new Atomic.UILayout();
  15. panel.axis = Atomic.UI_AXIS_Y;
  16. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  17. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  18. var lp = new Atomic.UILayoutParams();
  19. lp.width = 180;
  20. panel.layoutParams = lp;
  21. this.editWidget = panel;
  22. var _this = this;
  23. function createHandler(index, field) {
  24. return function(ev: Atomic.UIWidgetEditCompleteEvent) {
  25. for (var i in _this.editType.objects) {
  26. var o = <Atomic.Light>_this.editType.objects[i];
  27. o.setShadowCascadeParameter(this.index, Number(this.field.text));
  28. }
  29. o.scene.sendEvent("SceneEditEnd");
  30. _this.refresh();
  31. }.bind({ index: index, field: field });
  32. }
  33. var flp = new Atomic.UILayoutParams();
  34. flp.width = 60;
  35. for (var i = 0; i < 4; i++) {
  36. var field = InspectorUtils.createAttrEditField("Split " + i, panel);
  37. field.layoutParams = flp;
  38. field.subscribeToEvent(field, "UIWidgetEditComplete", createHandler(i, field));
  39. this.splitFields.push(field);
  40. }
  41. }
  42. refresh() {
  43. // Vector 4 internally
  44. for (var i = 0; i < 4; i++) {
  45. var uniform = this.editType.getUniformValue(this.attrInfo, i);
  46. var field = this.splitFields[i];
  47. if (uniform) {
  48. var object = this.editType.getFirstObject();
  49. if (object) {
  50. var value = object.getAttribute(this.attrInfo.name);
  51. field.text = parseFloat(value[i].toFixed(5)).toString();
  52. }
  53. else {
  54. field.text = "???";
  55. }
  56. }
  57. else {
  58. field.text = "--";
  59. }
  60. }
  61. }
  62. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  63. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. }
  69. interface MaterialEdit {
  70. index: number;
  71. editField: Atomic.UIEditField;
  72. selectButton: Atomic.UIButton;
  73. }
  74. class SubmeshAttributeEdit extends AttributeInfoEdit {
  75. materialIndexes: number[] = [];
  76. materialEdits: { [index: number]: MaterialEdit } = {};
  77. mainLayout: Atomic.UILayout;
  78. enabledCheckBox: Atomic.UICheckBox;
  79. nameField: Atomic.UITextField;
  80. name: string;
  81. constructor(name: string) {
  82. super();
  83. this.name = name;
  84. this.hideName = true;
  85. }
  86. createMaterialEdit(materialIndex: number) {
  87. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Material", this.mainLayout);
  88. var lp = new Atomic.UILayoutParams();
  89. lp.width = 140;
  90. lp.height = 24;
  91. o.editField.layoutParams = lp;
  92. o.editField.readOnly = true;
  93. var selectButton = o.selectButton;
  94. var materialEdit: MaterialEdit = { index: materialIndex, editField: o.editField, selectButton: selectButton };
  95. this.materialEdits[materialIndex] = materialEdit;
  96. var resourceTypeName = "Material";
  97. var importerName = ToolCore.assetDatabase.getResourceImporterName(resourceTypeName);
  98. selectButton.onClick = () => {
  99. EditorUI.getModelOps().showResourceSelection("Select " + resourceTypeName + " Resource", importerName, resourceTypeName, function(retObject: any) {
  100. var resource: Atomic.Resource = null;
  101. if (retObject instanceof ToolCore.Asset) {
  102. resource = (<ToolCore.Asset>retObject).getResource(resourceTypeName);
  103. } else if (retObject instanceof Atomic.Resource) {
  104. resource = <Atomic.Resource>retObject;
  105. }
  106. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  107. this.refresh();
  108. }.bind(this));
  109. };
  110. // handle dropping of component on field
  111. o.editField.subscribeToEvent(o.editField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  112. if (ev.target == o.editField) {
  113. var dragObject = ev.dragObject;
  114. var importer;
  115. if (dragObject.object && dragObject.object.typeName == "Asset") {
  116. var asset = <ToolCore.Asset>dragObject.object;
  117. if (asset.importerTypeName == importerName) {
  118. importer = asset.importer;
  119. }
  120. }
  121. if (importer) {
  122. var resource = asset.getResource(resourceTypeName);
  123. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  124. this.refresh();
  125. }
  126. }
  127. });
  128. }
  129. createEditWidget() {
  130. var mainLayout = this.mainLayout = new Atomic.UILayout();
  131. mainLayout.axis = Atomic.UI_AXIS_Y;
  132. mainLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  133. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  134. mainLayout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  135. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  136. var cb = InspectorUtils.createAttrCheckBox(this.name, mainLayout);
  137. this.enabledCheckBox = cb.checkBox;
  138. cb.checkBox.subscribeToEvent(cb.checkBox, "WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
  139. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  140. var scene: Atomic.Scene;
  141. for (var i in this.editType.objects) {
  142. var staticModel = <Atomic.StaticModel>this.editType.objects[i];
  143. scene = staticModel.scene;
  144. if (cb.checkBox.value) {
  145. staticModel.showGeometry(this.name);
  146. } else {
  147. staticModel.hideGeometry(this.name);
  148. }
  149. }
  150. scene.sendEvent("SceneEditEnd");
  151. return true;
  152. }
  153. return false;
  154. });
  155. this.nameField = cb.textField;
  156. this.editWidget = mainLayout;
  157. }
  158. refresh() {
  159. var editType = this.editType;
  160. var object = this.editType.getFirstObject();
  161. if (!object) {
  162. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  163. return;
  164. }
  165. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  166. var enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  167. var mixed = false;
  168. for (var i in editType.objects) {
  169. object = editType.objects[i];
  170. var _enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  171. if (_enabled != enabled) {
  172. mixed = true;
  173. break;
  174. }
  175. }
  176. if (mixed) {
  177. this.enabledCheckBox.skinBg = "TBGreyCheckBoxNonUniform";
  178. this.enabledCheckBox.value = 1;
  179. } else {
  180. this.enabledCheckBox.skinBg = "TBGreyCheckBox";
  181. this.enabledCheckBox.value = enabled ? 1 : 0;
  182. }
  183. for (var i in this.materialIndexes) {
  184. var idx = this.materialIndexes[i];
  185. if (!this.materialEdits[idx]) {
  186. this.createMaterialEdit(idx);
  187. }
  188. var matEdit = this.materialEdits[idx];
  189. var uniform = editType.getUniformValue(this.attrInfo, matEdit.index);
  190. if (uniform) {
  191. var object = editType.getFirstObject();
  192. if (object) {
  193. // for cached resources, use the asset name, otherwise use the resource path name
  194. var resource: Atomic.Resource;
  195. if (matEdit.index != -1) {
  196. resource = object.getAttribute(this.attrInfo.name).resources[matEdit.index];
  197. } else {
  198. resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
  199. }
  200. var text = "";
  201. if (resource) {
  202. if (resource instanceof Atomic.Material) {
  203. text = (<Atomic.Material>resource).name;
  204. } else {
  205. text = "???";
  206. }
  207. }
  208. var pathinfo = Atomic.splitPath(text);
  209. matEdit.editField.text = pathinfo.fileName;
  210. }
  211. } else {
  212. matEdit.editField.text = "--";
  213. }
  214. }
  215. }
  216. }
  217. class SubmeshListAttributeEdit extends AttributeInfoEdit {
  218. layout: Atomic.UILayout;
  219. submeshEdits: { [name: string]: SubmeshAttributeEdit } = {};
  220. constructor() {
  221. super();
  222. this.hideName = true;
  223. }
  224. createEditWidget() {
  225. this.spacing = 0;
  226. var layout = this.layout = new Atomic.UILayout();
  227. layout.axis = Atomic.UI_AXIS_Y;
  228. layout.spacing = 2;
  229. layout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  230. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  231. layout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  232. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  233. var lp = new Atomic.UILayoutParams();
  234. lp.width = 304;
  235. layout.layoutParams = lp;
  236. var name = new Atomic.UITextField();
  237. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  238. name.skinBg = "InspectorTextAttrName";
  239. name.layoutParams = AttributeInfoEdit.attrNameLP;
  240. name.text = "Submeshes";
  241. layout.addChild(name);
  242. InspectorUtils.createSeparator(layout);
  243. this.editWidget = layout;
  244. }
  245. refresh() {
  246. var editType = this.editType;
  247. var object = this.editType.getFirstObject();
  248. if (!object) {
  249. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  250. return;
  251. }
  252. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  253. var name: string;
  254. for (var i in editType.objects) {
  255. // TODO: check for multiple selection with different models and display multiple selection info box
  256. var staticModel = <Atomic.StaticModel>editType.objects[i];
  257. var model = staticModel.model;
  258. if (!model)
  259. continue;
  260. for (var j = 0; j < model.numGeometries; j++) {
  261. name = model.getGeometryName(j);
  262. if (!name)
  263. name = "Mesh";
  264. if (!this.submeshEdits.hasOwnProperty(name)) {
  265. var submeshEdit = new SubmeshAttributeEdit(name);
  266. submeshEdit.initialize(this.editType, this.attrInfo);
  267. this.layout.addChild(submeshEdit);
  268. this.submeshEdits[name] = submeshEdit;
  269. InspectorUtils.createSeparator(this.layout);
  270. }
  271. this.submeshEdits[name].materialIndexes.push(j);
  272. }
  273. }
  274. for (name in this.submeshEdits)
  275. this.submeshEdits[name].refresh();
  276. }
  277. }
  278. AttributeInfoEdit.registerCustomAttr("AnimatedModel", "Material", SubmeshListAttributeEdit);
  279. AttributeInfoEdit.registerCustomAttr("StaticModel", "Material", SubmeshListAttributeEdit);
  280. AttributeInfoEdit.registerCustomAttr("Light", "CSM Splits", LightCascadeAttributeEdit);