ComponentAttributeUI.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("ui/EditorUI");
  23. import InspectorUtils = require("./InspectorUtils");
  24. import AttributeInfoEdit = require("./AttributeInfoEdit");
  25. import SerializableEditType = require("./SerializableEditType");
  26. import EditorEvents = require("editor/EditorEvents");
  27. class LightCascadeAttributeEdit extends AttributeInfoEdit {
  28. splitFields: Atomic.UIEditField[] = [];
  29. createEditWidget() {
  30. var panel = new Atomic.UILayout();
  31. panel.axis = Atomic.UI_AXIS_Y;
  32. panel.layoutSize = Atomic.UI_LAYOUT_SIZE_PREFERRED;
  33. panel.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  34. var lp = new Atomic.UILayoutParams();
  35. lp.width = 180;
  36. panel.layoutParams = lp;
  37. this.editWidget = panel;
  38. var _this = this;
  39. function createHandler(index, field) {
  40. return function(ev: Atomic.UIWidgetEditCompleteEvent) {
  41. for (var i in _this.editType.objects) {
  42. var o = <Atomic.Light>_this.editType.objects[i];
  43. o.setShadowCascadeParameter(this.index, Number(this.field.text));
  44. }
  45. o.scene.sendEvent("SceneEditEnd");
  46. _this.refresh();
  47. }.bind({ index: index, field: field });
  48. }
  49. var flp = new Atomic.UILayoutParams();
  50. flp.width = 60;
  51. for (var i = 0; i < 4; i++) {
  52. var field = InspectorUtils.createAttrEditField("Split " + i, panel);
  53. field.layoutParams = flp;
  54. field.subscribeToEvent(field, "UIWidgetEditComplete", createHandler(i, field));
  55. this.splitFields.push(field);
  56. }
  57. }
  58. refresh() {
  59. // Vector 4 internally
  60. for (var i = 0; i < 4; i++) {
  61. var uniform = this.editType.getUniformValue(this.attrInfo, i);
  62. var field = this.splitFields[i];
  63. if (uniform) {
  64. var object = this.editType.getFirstObject();
  65. if (object) {
  66. var value = object.getAttribute(this.attrInfo.name);
  67. field.text = parseFloat(value[i].toFixed(5)).toString();
  68. }
  69. else {
  70. field.text = "???";
  71. }
  72. }
  73. else {
  74. field.text = "--";
  75. }
  76. }
  77. }
  78. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  79. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  80. return true;
  81. }
  82. return false;
  83. }
  84. }
  85. interface MaterialEdit {
  86. index: number;
  87. pathReference: string;
  88. editField: Atomic.UIEditField;
  89. selectButton: Atomic.UIButton;
  90. }
  91. class SubmeshAttributeEdit extends AttributeInfoEdit {
  92. materialIndexes: number[] = [];
  93. materialEdits: { [index: number]: MaterialEdit } = {};
  94. mainLayout: Atomic.UILayout;
  95. enabledCheckBox: Atomic.UICheckBox;
  96. nameField: Atomic.UITextField;
  97. name: string;
  98. matIndex: number;
  99. constructor(name: string) {
  100. super();
  101. this.name = name;
  102. this.hideName = true;
  103. this.subscribeToEvent(EditorEvents.RemoveCurrentAssetAssigned, (ev: EditorEvents.RemoveCurrentAssetAssignedEvent) => {
  104. this.editType.onAttributeInfoEdited(this.attrInfo, null, this.matIndex);
  105. this.refresh();
  106. });
  107. }
  108. openResourceSelectionBox(materialIndex: number, resourceTypeName: string, importerName: string) {
  109. this.matIndex = materialIndex;
  110. EditorUI.getModelOps().showResourceSelection("Select " + resourceTypeName + " Resource", importerName, resourceTypeName, function (retObject: any) {
  111. var resource: Atomic.Resource = null;
  112. if (retObject instanceof ToolCore.Asset) {
  113. resource = (<ToolCore.Asset>retObject).getResource(resourceTypeName);
  114. } else if (retObject instanceof Atomic.Resource) {
  115. resource = <Atomic.Resource>retObject;
  116. }
  117. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": resource.name });
  118. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  119. this.refresh();
  120. }.bind(this));
  121. }
  122. createMaterialEdit(materialIndex: number) {
  123. var o = InspectorUtils.createAttrEditFieldWithSelectButton("Material", this.mainLayout);
  124. var lp = new Atomic.UILayoutParams();
  125. lp.width = 140;
  126. lp.height = 24;
  127. o.editField.layoutParams = lp;
  128. o.editField.readOnly = true;
  129. var selectButton = o.selectButton;
  130. var materialEdit: MaterialEdit = { index: materialIndex, pathReference: "" , editField: o.editField, selectButton: selectButton };
  131. this.materialEdits[materialIndex] = materialEdit;
  132. var resourceTypeName = "Material";
  133. var importerName = ToolCore.assetDatabase.getResourceImporterName(resourceTypeName);
  134. selectButton.onClick = () => {
  135. this.openResourceSelectionBox(materialIndex, resourceTypeName, importerName);
  136. // this.sendEvent(EditorEvents.InspectorProjectReference, { "path": pathName });
  137. };
  138. // handle dropping of component on field
  139. o.editField.subscribeToEvent(o.editField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  140. if (ev.target == o.editField) {
  141. var dragObject = ev.dragObject;
  142. var importer;
  143. if (dragObject.object && dragObject.object.typeName == "Asset") {
  144. var asset = <ToolCore.Asset>dragObject.object;
  145. if (asset.importerTypeName == importerName) {
  146. importer = asset.importer;
  147. }
  148. }
  149. if (importer) {
  150. var resource = asset.getResource(resourceTypeName);
  151. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": resource.name });
  152. this.editType.onAttributeInfoEdited(this.attrInfo, resource, materialIndex);
  153. this.refresh();
  154. }
  155. }
  156. });
  157. o.editField.subscribeToEvent(o.editField, "WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
  158. if (ev.type == Atomic.UI_EVENT_TYPE_POINTER_DOWN && o.editField.text != "") {
  159. var pathName = materialEdit.pathReference;
  160. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": pathName });
  161. } else if (o.editField.text == "") {
  162. this.openResourceSelectionBox(materialIndex, resourceTypeName, importerName);
  163. }
  164. });
  165. }
  166. createEditWidget() {
  167. var mainLayout = this.mainLayout = new Atomic.UILayout();
  168. mainLayout.axis = Atomic.UI_AXIS_Y;
  169. mainLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  170. mainLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  171. mainLayout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  172. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  173. var cb = InspectorUtils.createAttrCheckBox(this.name, mainLayout);
  174. this.enabledCheckBox = cb.checkBox;
  175. cb.checkBox.subscribeToEvent(cb.checkBox, "WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
  176. if (ev.type == Atomic.UI_EVENT_TYPE_CHANGED) {
  177. var scene: Atomic.Scene;
  178. for (var i in this.editType.objects) {
  179. var staticModel = <Atomic.StaticModel>this.editType.objects[i];
  180. scene = staticModel.scene;
  181. if (cb.checkBox.value) {
  182. staticModel.showGeometry(this.name);
  183. } else {
  184. staticModel.hideGeometry(this.name);
  185. }
  186. }
  187. scene.sendEvent("SceneEditEnd");
  188. return true;
  189. }
  190. return false;
  191. });
  192. this.nameField = cb.textField;
  193. this.editWidget = mainLayout;
  194. }
  195. refresh() {
  196. var editType = this.editType;
  197. var object = this.editType.getFirstObject();
  198. if (!object) {
  199. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  200. return;
  201. }
  202. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  203. var enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  204. var mixed = false;
  205. for (var i in editType.objects) {
  206. object = editType.objects[i];
  207. var _enabled = (<Atomic.StaticModel>object).getGeometryVisible(this.name);
  208. if (_enabled != enabled) {
  209. mixed = true;
  210. break;
  211. }
  212. }
  213. if (mixed) {
  214. this.enabledCheckBox.skinBg = "TBGreyCheckBoxNonUniform";
  215. this.enabledCheckBox.value = 1;
  216. } else {
  217. this.enabledCheckBox.skinBg = "TBGreyCheckBox";
  218. this.enabledCheckBox.value = enabled ? 1 : 0;
  219. }
  220. for (var i in this.materialIndexes) {
  221. var idx = this.materialIndexes[i];
  222. if (!this.materialEdits[idx]) {
  223. this.createMaterialEdit(idx);
  224. }
  225. var matEdit = this.materialEdits[idx];
  226. var uniform = editType.getUniformValue(this.attrInfo, matEdit.index);
  227. if (uniform) {
  228. var object = editType.getFirstObject();
  229. if (object) {
  230. // for cached resources, use the asset name, otherwise use the resource path name
  231. var resource: Atomic.Resource;
  232. if (matEdit.index != -1) {
  233. resource = object.getAttribute(this.attrInfo.name).resources[matEdit.index];
  234. } else {
  235. resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
  236. }
  237. var text = "";
  238. if (resource) {
  239. if (resource instanceof Atomic.Material) {
  240. text = (<Atomic.Material>resource).name;
  241. } else {
  242. text = "???";
  243. }
  244. }
  245. var pathinfo = Atomic.splitPath(text);
  246. matEdit.editField.text = pathinfo.fileName;
  247. matEdit.pathReference = text;
  248. }
  249. } else {
  250. matEdit.editField.text = "--";
  251. }
  252. }
  253. }
  254. }
  255. class SubmeshListAttributeEdit extends AttributeInfoEdit {
  256. layout: Atomic.UILayout;
  257. submeshEdits: { [name: string]: SubmeshAttributeEdit } = {};
  258. constructor() {
  259. super();
  260. this.hideName = true;
  261. }
  262. createEditWidget() {
  263. this.spacing = 0;
  264. var layout = this.layout = new Atomic.UILayout();
  265. layout.axis = Atomic.UI_AXIS_Y;
  266. layout.spacing = 2;
  267. layout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  268. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  269. layout.gravity = Atomic.UI_GRAVITY_LEFT_RIGHT;
  270. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  271. var lp = new Atomic.UILayoutParams();
  272. lp.width = 304;
  273. layout.layoutParams = lp;
  274. var name = new Atomic.UITextField();
  275. name.textAlign = Atomic.UI_TEXT_ALIGN_LEFT;
  276. name.skinBg = "InspectorTextAttrName";
  277. name.layoutParams = AttributeInfoEdit.attrNameLP;
  278. name.text = "Submeshes";
  279. layout.addChild(name);
  280. InspectorUtils.createSeparator(layout);
  281. this.editWidget = layout;
  282. }
  283. refresh() {
  284. var editType = this.editType;
  285. var object = this.editType.getFirstObject();
  286. if (!object) {
  287. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  288. return;
  289. }
  290. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  291. var name: string;
  292. for (var i in editType.objects) {
  293. // TODO: check for multiple selection with different models and display multiple selection info box
  294. var staticModel = <Atomic.StaticModel>editType.objects[i];
  295. var model = staticModel.model;
  296. if (!model)
  297. continue;
  298. for (var j = 0; j < model.numGeometries; j++) {
  299. name = model.getGeometryName(j);
  300. if (!name)
  301. name = "Mesh";
  302. if (!this.submeshEdits.hasOwnProperty(name)) {
  303. var submeshEdit = new SubmeshAttributeEdit(name);
  304. submeshEdit.initialize(this.editType, this.attrInfo);
  305. this.layout.addChild(submeshEdit);
  306. this.submeshEdits[name] = submeshEdit;
  307. InspectorUtils.createSeparator(this.layout);
  308. }
  309. this.submeshEdits[name].materialIndexes.push(j);
  310. }
  311. }
  312. for (name in this.submeshEdits)
  313. this.submeshEdits[name].refresh();
  314. }
  315. }
  316. AttributeInfoEdit.registerCustomAttr("AnimatedModel", "Material", SubmeshListAttributeEdit);
  317. AttributeInfoEdit.registerCustomAttr("StaticModel", "Material", SubmeshListAttributeEdit);
  318. AttributeInfoEdit.registerCustomAttr("Light", "CSM Splits", LightCascadeAttributeEdit);