ComponentAttributeUI.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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("", this.mainLayout);
  88. var lp = new Atomic.UILayoutParams();
  89. lp.width = 250;
  90. o.editField.layoutParams = lp;
  91. o.editField.readOnly = true;
  92. var selectButton = o.selectButton;
  93. var materialEdit: MaterialEdit = { index: materialIndex, editField: o.editField, selectButton: selectButton };
  94. this.materialEdits[materialIndex] = materialEdit;
  95. var resourceTypeName = "Material";
  96. var importerName = ToolCore.assetDatabase.getResourceImporterName(resourceTypeName);
  97. selectButton.onClick = () => {
  98. EditorUI.getModelOps().showResourceSelection("Select " + resourceTypeName + " Resource", importerName, resourceTypeName, function(retObject: any) {
  99. var resource: Atomic.Resource = null;
  100. if (retObject instanceof ToolCore.Asset) {
  101. resource = (<ToolCore.Asset>retObject).getResource(resourceTypeName);
  102. } else if (retObject instanceof Atomic.Resource) {
  103. resource = <Atomic.Resource>retObject;
  104. }
  105. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  106. this.refresh();
  107. }.bind(this));
  108. }
  109. // handle dropping of component on field
  110. o.editField.subscribeToEvent(o.editField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  111. if (ev.target == o.editField) {
  112. var dragObject = ev.dragObject;
  113. var importer;
  114. if (dragObject.object && dragObject.object.typeName == "Asset") {
  115. var asset = <ToolCore.Asset>dragObject.object;
  116. if (asset.importerTypeName == importerName) {
  117. importer = asset.importer;
  118. }
  119. }
  120. if (importer) {
  121. var resource = asset.getResource(resourceTypeName);
  122. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  123. this.refresh();
  124. }
  125. }
  126. });
  127. }
  128. createEditWidget() {
  129. var mainLayout = this.mainLayout = new Atomic.UILayout();
  130. mainLayout.axis = Atomic.UI_AXIS_Y;
  131. mainLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  132. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  133. mainLayout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  134. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  135. var cb = InspectorUtils.createAttrCheckBox(this.name, mainLayout);
  136. this.enabledCheckBox = cb.checkBox;
  137. cb.checkBox.subscribeToEvent(cb.checkBox, "WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
  138. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  139. var scene: Atomic.Scene;
  140. for (var i in this.editType.objects) {
  141. var staticModel = <Atomic.StaticModel>this.editType.objects[i];
  142. scene = staticModel.scene;
  143. if (cb.checkBox.value) {
  144. staticModel.showGeometry(this.name);
  145. } else {
  146. staticModel.hideGeometry(this.name);
  147. }
  148. }
  149. scene.sendEvent("SceneEditEnd");
  150. return true;
  151. }
  152. return false;
  153. });
  154. this.nameField = cb.textField;
  155. this.editWidget = mainLayout;
  156. }
  157. refresh() {
  158. var editType = this.editType;
  159. var object = this.editType.getFirstObject();
  160. if (!object) {
  161. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  162. return;
  163. }
  164. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  165. var enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  166. var mixed = false;
  167. for (var i in editType.objects) {
  168. object = editType.objects[i];
  169. var _enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  170. if (_enabled != enabled) {
  171. mixed = true;
  172. break;
  173. }
  174. }
  175. if (mixed) {
  176. this.enabledCheckBox.skinBg = "TBGreyCheckBoxNonUniform";
  177. this.enabledCheckBox.value = 1;
  178. } else {
  179. this.enabledCheckBox.skinBg = "TBGreyCheckBox";
  180. this.enabledCheckBox.value = enabled ? 1 : 0;
  181. }
  182. for (var i in this.materialIndexes) {
  183. var idx = this.materialIndexes[i];
  184. if (!this.materialEdits[idx]) {
  185. this.createMaterialEdit(idx);
  186. }
  187. var matEdit = this.materialEdits[idx];
  188. var uniform = editType.getUniformValue(this.attrInfo, matEdit.index);
  189. if (uniform) {
  190. var object = editType.getFirstObject();
  191. if (object) {
  192. // for cached resources, use the asset name, otherwise use the resource path name
  193. var resource: Atomic.Resource;
  194. if (matEdit.index != -1) {
  195. resource = object.getAttribute(this.attrInfo.name).resources[matEdit.index];
  196. } else {
  197. resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
  198. }
  199. var text = "";
  200. if (resource) {
  201. if (resource instanceof Atomic.Material) {
  202. text = (<Atomic.Material>resource).name;
  203. } else {
  204. text = "???";
  205. }
  206. }
  207. var pathinfo = Atomic.splitPath(text);
  208. matEdit.editField.text = pathinfo.fileName;
  209. }
  210. } else {
  211. matEdit.editField.text = "--";
  212. }
  213. }
  214. }
  215. }
  216. class SubmeshListAttributeEdit extends AttributeInfoEdit {
  217. layout: Atomic.UILayout;
  218. submeshEdits: { [name: string]: SubmeshAttributeEdit } = {};
  219. constructor() {
  220. super();
  221. this.hideName = true;
  222. }
  223. createEditWidget() {
  224. this.spacing = 0;
  225. var layout = this.layout = new Atomic.UILayout();
  226. layout.axis = Atomic.UI_AXIS_Y;
  227. layout.spacing = 2;
  228. layout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  229. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  230. layout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  231. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  232. var lp = new Atomic.UILayoutParams();
  233. lp.width = 304;
  234. layout.layoutParams = lp;
  235. var name = new Atomic.UITextField();
  236. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  237. name.skinBg = "InspectorTextAttrName";
  238. name.layoutParams = AttributeInfoEdit.attrNameLP;
  239. name.text = "Submeshes";
  240. layout.addChild(name);
  241. InspectorUtils.createSeparator(layout);
  242. this.editWidget = layout;
  243. }
  244. refresh() {
  245. var editType = this.editType;
  246. var object = this.editType.getFirstObject();
  247. if (!object) {
  248. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  249. return;
  250. }
  251. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  252. var name: string;
  253. for (var i in editType.objects) {
  254. // TODO: check for multiple selection with different models and display multiple selection info box
  255. var staticModel = <Atomic.StaticModel>editType.objects[i];
  256. var model = staticModel.model;
  257. if (!model)
  258. continue;
  259. for (var j = 0; j < model.numGeometries; j++) {
  260. name = model.getGeometryName(j);
  261. if (!name)
  262. name = "Mesh";
  263. if (!this.submeshEdits.hasOwnProperty(name)) {
  264. var submeshEdit = new SubmeshAttributeEdit(name);
  265. submeshEdit.initialize(this.editType, this.attrInfo);
  266. this.layout.addChild(submeshEdit);
  267. this.submeshEdits[name] = submeshEdit;
  268. }
  269. this.submeshEdits[name].materialIndexes.push(j);
  270. }
  271. }
  272. for (name in this.submeshEdits)
  273. this.submeshEdits[name].refresh();
  274. }
  275. }
  276. AttributeInfoEdit.registerCustomAttr("AnimatedModel", "Material", SubmeshListAttributeEdit);
  277. AttributeInfoEdit.registerCustomAttr("StaticModel", "Material", SubmeshListAttributeEdit);
  278. AttributeInfoEdit.registerCustomAttr("Light", "CSM Splits", LightCascadeAttributeEdit);