ComponentAttributeUI.ts 14 KB

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