ComponentAttributeUI.ts 13 KB

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