AttributeInfoEdit.ts 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 SerializableEditType = require("./SerializableEditType");
  25. import EditorEvents = require("editor/EditorEvents");
  26. import ColorChooser = require("./ColorChooser");
  27. class AttributeInfoEdit extends Atomic.UILayout {
  28. attrInfo: Atomic.AttributeInfo;
  29. editType: SerializableEditType;
  30. editWidget: Atomic.UIWidget;
  31. nameOverride: string;
  32. hideName: boolean = false;
  33. constructor() {
  34. super();
  35. }
  36. initialize(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): boolean {
  37. this.editType = editType;
  38. this.attrInfo = attrInfo;
  39. this.createLayout();
  40. return true;
  41. }
  42. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  43. return false;
  44. }
  45. createLayout() {
  46. this.createEditWidget();
  47. this.editWidget.subscribeToEvent(this.editWidget, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  48. if (this.attrInfo.tooltip) {
  49. this.editWidget.tooltip = this.attrInfo.tooltip;
  50. }
  51. var attr = this.attrInfo;
  52. var attrNameLP = AttributeInfoEdit.attrNameLP;
  53. this.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  54. if (attr.type == Atomic.VariantType.VAR_VECTOR3 || attr.type == Atomic.VariantType.VAR_COLOR ||
  55. attr.type == Atomic.VariantType.VAR_QUATERNION) {
  56. this.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  57. this.layoutPosition = Atomic.UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_LEFT_TOP;
  58. this.skinBg = "InspectorVectorAttrLayout";
  59. }
  60. if (!this.hideName) {
  61. var name = new Atomic.UITextField();
  62. name.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_LEFT;
  63. name.skinBg = "InspectorTextAttrName";
  64. name.layoutParams = attrNameLP;
  65. var bname = attr.name;
  66. if (bname == "Is Enabled")
  67. bname = "Enabled";
  68. if (this.nameOverride)
  69. name.text = this.nameOverride;
  70. else
  71. name.text = bname;
  72. name.fontDescription = AttributeInfoEdit.fontDesc;
  73. if (this.attrInfo.tooltip) {
  74. name.tooltip = this.attrInfo.tooltip;
  75. }
  76. this.addChild(name);
  77. }
  78. this.addChild(this.editWidget);
  79. }
  80. createEditWidget() {
  81. }
  82. refresh() {
  83. }
  84. static createAttrEdit(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): AttributeInfoEdit {
  85. var type: typeof AttributeInfoEdit;
  86. var customTypes = AttributeInfoEdit.customAttrEditTypes[editType.typeName];
  87. if (customTypes) {
  88. type = customTypes[attrInfo.name];
  89. }
  90. if (!type) {
  91. type = AttributeInfoEdit.standardAttrEditTypes[attrInfo.type];
  92. }
  93. if (!type)
  94. return null;
  95. var attrEdit = new type();
  96. if (!attrEdit.initialize(editType, attrInfo))
  97. return null;
  98. return attrEdit;
  99. }
  100. // atttribute name layout param
  101. static attrNameLP: Atomic.UILayoutParams;
  102. static fontDesc: Atomic.UIFontDescription;
  103. static standardAttrEditTypes: { [variantType: number /*Atomic.VariantType*/]: typeof AttributeInfoEdit } = {};
  104. static customAttrEditTypes: { [typeName: string]: { [name: string]: typeof AttributeInfoEdit } } = {};
  105. static registerCustomAttr(typeName: string, attrName: string, edit: typeof AttributeInfoEdit) {
  106. if (!AttributeInfoEdit.customAttrEditTypes[typeName]) {
  107. AttributeInfoEdit.customAttrEditTypes[typeName] = {};
  108. }
  109. AttributeInfoEdit.customAttrEditTypes[typeName][attrName] = edit;
  110. }
  111. private static Ctor = (() => {
  112. var attrNameLP = AttributeInfoEdit.attrNameLP = new Atomic.UILayoutParams();
  113. attrNameLP.width = 120;
  114. var fd = AttributeInfoEdit.fontDesc = new Atomic.UIFontDescription();
  115. fd.id = "Vera";
  116. fd.size = 11;
  117. })();
  118. }
  119. class BoolAttributeEdit extends AttributeInfoEdit {
  120. createEditWidget() {
  121. var box = new Atomic.UICheckBox();
  122. this.editWidget = box;
  123. }
  124. refresh() {
  125. var uniform = this.editType.getUniformValue(this.attrInfo);
  126. if (uniform) {
  127. var object = this.editType.getFirstObject();
  128. this.editWidget.skinBg = "TBCheckBox";
  129. if (object) {
  130. var value = object.getAttribute(this.attrInfo.name);
  131. this.editWidget.value = (value ? 1 : 0);
  132. }
  133. } else {
  134. this.editWidget.skinBg = "TBCheckBoxNonUniform";
  135. this.editWidget.value = 1;
  136. }
  137. }
  138. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  139. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  140. this.editType.onAttributeInfoEdited(this.attrInfo, this.editWidget.value ? true : false);
  141. this.refresh();
  142. return true;
  143. }
  144. return false;
  145. }
  146. }
  147. class StringAttributeEdit extends AttributeInfoEdit {
  148. createEditWidget() {
  149. var field = new Atomic.UIEditField();
  150. field.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_LEFT;
  151. field.skinBg = "TBAttrEditorField";
  152. field.fontDescription = AttributeInfoEdit.fontDesc;
  153. var lp = new Atomic.UILayoutParams();
  154. lp.width = 160;
  155. field.layoutParams = lp;
  156. field.subscribeToEvent(field, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  157. this.editWidget = field;
  158. }
  159. refresh() {
  160. var uniform = this.editType.getUniformValue(this.attrInfo);
  161. if (uniform) {
  162. var object = this.editType.getFirstObject();
  163. if (object) {
  164. var value = object.getAttribute(this.attrInfo.name);
  165. this.editWidget.text = value;
  166. }
  167. } else {
  168. this.editWidget.text = "--";
  169. }
  170. }
  171. handleUIWidgetEditCompleteEvent(ev) {
  172. this.editType.onAttributeInfoEdited(this.attrInfo, this.editWidget.text);
  173. this.refresh();
  174. }
  175. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  176. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. }
  182. class IntAttributeEdit extends AttributeInfoEdit {
  183. enumSource: Atomic.UISelectItemSource;
  184. createEditWidget() {
  185. var attrInfo = this.attrInfo;
  186. if (attrInfo.enumNames.length) {
  187. var enumSource = this.enumSource = new Atomic.UISelectItemSource();
  188. for (var i in attrInfo.enumNames) {
  189. enumSource.addItem(new Atomic.UISelectItem(attrInfo.enumNames[i], (Number(i) + 1).toString()));
  190. }
  191. var button = new Atomic.UIButton();
  192. button.fontDescription = AttributeInfoEdit.fontDesc;
  193. button.text = "Enum Value!";
  194. var lp = new Atomic.UILayoutParams();
  195. lp.width = 140;
  196. button.layoutParams = lp;
  197. this.editWidget = button;
  198. } else {
  199. var field = new Atomic.UIEditField();
  200. field.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_CENTER;
  201. field.skinBg = "TBAttrEditorField";
  202. field.fontDescription = AttributeInfoEdit.fontDesc;
  203. var lp = new Atomic.UILayoutParams();
  204. lp.width = 140;
  205. field.layoutParams = lp;
  206. field.subscribeToEvent(field, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  207. this.editWidget = field;
  208. }
  209. }
  210. refresh() {
  211. var uniform = this.editType.getUniformValue(this.attrInfo);
  212. if (uniform) {
  213. var object = this.editType.getFirstObject();
  214. if (object) {
  215. var value = object.getAttribute(this.attrInfo.name);
  216. var widget = this.editWidget;
  217. var attrInfo = this.attrInfo;
  218. if (attrInfo.enumNames.length) {
  219. widget.text = attrInfo.enumNames[value];
  220. }
  221. else {
  222. widget.text = value.toString();
  223. }
  224. }
  225. } else {
  226. this.editWidget.text = "--";
  227. }
  228. }
  229. handleUIWidgetEditCompleteEvent(ev) {
  230. // non-enum
  231. this.editType.onAttributeInfoEdited(this.attrInfo, Number(this.editWidget.text));
  232. this.refresh();
  233. }
  234. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  235. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  236. return true;
  237. }
  238. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  239. var id = this.attrInfo.name + " enum popup";
  240. if (ev.target.id == id) {
  241. this.editType.onAttributeInfoEdited(this.attrInfo, Number(ev.refid) - 1);
  242. this.refresh();
  243. }
  244. else if (this.editWidget == ev.target && this.attrInfo.enumNames.length) {
  245. if (this.enumSource) {
  246. var menu = new Atomic.UIMenuWindow(ev.target, id);
  247. menu.show(this.enumSource);
  248. }
  249. return true;
  250. }
  251. }
  252. return false;
  253. }
  254. }
  255. class FloatAttributeEdit extends AttributeInfoEdit {
  256. createEditWidget() {
  257. var attrInfo = this.attrInfo;
  258. var field = new Atomic.UIEditField();
  259. field.textAlign = Atomic.UI_TEXT_ALIGN.UI_TEXT_ALIGN_CENTER;
  260. field.skinBg = "TBAttrEditorField";
  261. field.fontDescription = AttributeInfoEdit.fontDesc;
  262. var lp = new Atomic.UILayoutParams();
  263. lp.width = 140;
  264. field.layoutParams = lp;
  265. field.subscribeToEvent(field, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  266. this.editWidget = field;
  267. }
  268. refresh() {
  269. var uniform = this.editType.getUniformValue(this.attrInfo);
  270. if (uniform) {
  271. var object = this.editType.getFirstObject();
  272. if (object) {
  273. var widget = this.editWidget;
  274. var attrInfo = this.attrInfo;
  275. var value = object.getAttribute(attrInfo.name);
  276. if (value == undefined) {
  277. console.log("WARNING: Undefined value for object: ", this.editType.typeName + "." + attrInfo.name);
  278. widget.text = "???";
  279. } else {
  280. widget.text = parseFloat(value.toFixed(5)).toString();
  281. }
  282. }
  283. } else {
  284. this.editWidget.text = "--";
  285. }
  286. }
  287. handleUIWidgetEditCompleteEvent(ev) {
  288. this.editType.onAttributeInfoEdited(this.attrInfo, Number(this.editWidget.text));
  289. this.refresh();
  290. }
  291. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  292. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  293. return true;
  294. }
  295. return false;
  296. }
  297. }
  298. class NumberArrayAttributeEdit extends AttributeInfoEdit {
  299. selects: Atomic.UIInlineSelect[] = [];
  300. private numElements: number;
  301. constructor(numElements: number) {
  302. super();
  303. this.numElements = numElements;
  304. }
  305. createEditWidget() {
  306. var attrInfo = this.attrInfo;
  307. var layout = new Atomic.UILayout();
  308. layout.spacing = 0;
  309. var lp = new Atomic.UILayoutParams();
  310. lp.width = this.numElements != 4 ? 100 : 70;
  311. for (var i = 0; i < this.numElements; i++) {
  312. var select = new Atomic.UIInlineSelect();
  313. this.selects.push(select);
  314. select.id = String(i + 1);
  315. select.fontDescription = AttributeInfoEdit.fontDesc;
  316. select.skinBg = "InspectorVectorAttrName";
  317. select.setLimits(-10000000, 10000000);
  318. if (this.numElements != 4) {
  319. var editlp = new Atomic.UILayoutParams();
  320. editlp.minWidth = 60;
  321. select.editFieldLayoutParams = editlp;
  322. }
  323. select.layoutParams = lp;
  324. layout.addChild(select);
  325. select["_edit"] = select.getWidget("edit");
  326. select["_dec"] = select.getWidget("dec");
  327. select["_inc"] = select.getWidget("inc");
  328. select.subscribeToEvent(select, "WidgetEvent", (ev) => this.handleWidgetEvent(ev));
  329. select.subscribeToEvent(select, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  330. }
  331. this.editWidget = layout;
  332. }
  333. refresh() {
  334. for (var i = 0; i < this.selects.length; i++) {
  335. var select = this.selects[i];
  336. if (select["_edit"].focus || select["_dec"].captured || select["_inc"].captured)
  337. continue;
  338. var uniform = this.editType.getUniformValue(this.attrInfo, i);
  339. if (uniform) {
  340. var object = this.editType.getFirstObject();
  341. if (object) {
  342. var value = object.getAttribute(this.attrInfo.name);
  343. select.value = parseFloat(value[i].toFixed(5));
  344. }
  345. } else {
  346. select["_edit"].text = "--";
  347. }
  348. }
  349. }
  350. handleUIWidgetEditCompleteEvent(ev: Atomic.UIWidgetEditCompleteEvent) {
  351. var index = Number(ev.widget.id) - 1;
  352. this.editType.onAttributeInfoEdited(this.attrInfo, ev.widget.value, index);
  353. this.refresh();
  354. }
  355. handleWidgetEvent(ev: Atomic.UIWidgetEvent): boolean {
  356. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED) {
  357. var captured = false;
  358. for (var i in this.selects) {
  359. var select = this.selects[i];
  360. if (select["_dec"].captured || select["_inc"].captured) {
  361. captured = true;
  362. break;
  363. }
  364. }
  365. if (captured) {
  366. var index = Number(ev.target.id) - 1;
  367. this.editType.onAttributeInfoEdited(this.attrInfo, ev.target.value, index, false);
  368. }
  369. return true;
  370. }
  371. return false;
  372. }
  373. }
  374. class Vector2AttributeEdit extends NumberArrayAttributeEdit {
  375. constructor() {
  376. super(2);
  377. }
  378. }
  379. class Vector3AttributeEdit extends NumberArrayAttributeEdit {
  380. constructor() {
  381. super(3);
  382. }
  383. }
  384. class QuaternionAttributeEdit extends NumberArrayAttributeEdit {
  385. constructor() {
  386. super(3);
  387. }
  388. }
  389. class ColorAttributeEdit extends AttributeInfoEdit {
  390. createLayout() {
  391. var layout = new Atomic.UILayout();
  392. var o = InspectorUtils.createAttrColorFieldWithSelectButton(this.attrInfo.name, layout);
  393. var colorWidget = this.colorWidget = o.colorWidget;
  394. var selectButton = o.selectButton;
  395. layout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  396. layout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT_RIGHT;
  397. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  398. var lp = new Atomic.UILayoutParams();
  399. lp.width = 140;
  400. colorWidget.layoutParams = lp;
  401. this.editWidget = layout;
  402. this.editWidget.subscribeToEvent(this.editWidget, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  403. selectButton.onClick = () => {
  404. // store original color
  405. let color = [1, 1, 1, 1];
  406. let object = this.editType.getFirstObject();
  407. if (object) {
  408. color = object.getAttribute(this.attrInfo.name);
  409. }
  410. colorWidget.color = color;
  411. let restore = null;
  412. let chooser = new ColorChooser ( color );
  413. this.subscribeToEvent(chooser, "ColorChooserChanged", (ev) => {
  414. restore = color;
  415. this.updateColor(chooser.getRGBA());
  416. });
  417. this.subscribeToEvent(chooser, "UIWidgetEditCanceled", (ev) => {
  418. if (restore) {
  419. colorWidget.color = restore;
  420. this.updateColor(restore);
  421. }
  422. });
  423. this.subscribeToEvent(chooser, "UIWidgetEditComplete", (ev) => {
  424. let newColor = chooser.getRGBA();
  425. // check for new color edit
  426. let committed = false;
  427. for (let i = 0; i < 4; i++) {
  428. if (color[i] != newColor[i]) {
  429. this.editType.onAttributeInfoEdited(this.attrInfo, newColor);
  430. this.refresh();
  431. committed = true;
  432. break;
  433. }
  434. }
  435. if (restore && !committed) {
  436. for (let i = 0; i < 4; i++) {
  437. if (color[i] != restore[i]) {
  438. this.updateColor(color);
  439. break;
  440. }
  441. }
  442. }
  443. });
  444. };
  445. this.addChild(this.editWidget);
  446. }
  447. refresh() {
  448. let object = this.editType.getFirstObject();
  449. if (object) {
  450. this.colorWidget.color = object.getAttribute(this.attrInfo.name);
  451. }
  452. }
  453. // updates color on selection without committing to undo/redo for preview
  454. updateColor(rgba:number[]) {
  455. this.colorWidget.color = rgba;
  456. for (var i in this.editType.objects) {
  457. let object = this.editType.objects[i];
  458. object.setAttribute(this.attrInfo.name, rgba );
  459. }
  460. }
  461. colorWidget : Atomic.UIColorWidget;
  462. }
  463. class ResourceRefAttributeEdit extends AttributeInfoEdit {
  464. refListIndex: number;
  465. editField: Atomic.UIEditField;
  466. constructor(refListIndex: number = -1) {
  467. super();
  468. this.refListIndex = refListIndex;
  469. }
  470. onResourceChanged(resource: Atomic.Resource) {
  471. var parent = this.parent;
  472. while (parent) {
  473. if (parent.typeName == "UISection") {
  474. break;
  475. }
  476. parent = parent.parent;
  477. }
  478. if (parent) {
  479. parent.sendEvent("AttributeEditResourceChanged", { attrInfoEdit: this, resource: resource });
  480. }
  481. }
  482. initialize(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): boolean {
  483. if (!attrInfo.resourceTypeName)
  484. return false;
  485. if (this.refListIndex >= 0)
  486. this.nameOverride = attrInfo.resourceTypeName + " " + this.refListIndex;
  487. var importerName = ToolCore.assetDatabase.getResourceImporterName(attrInfo.resourceTypeName);
  488. if (!importerName)
  489. return false;
  490. return super.initialize(editType, attrInfo);
  491. }
  492. refresh() {
  493. var uniform = this.editType.getUniformValue(this.attrInfo, this.refListIndex);
  494. if (uniform) {
  495. var object = this.editType.getFirstObject();
  496. if (object) {
  497. // for cached resources, use the asset name, otherwise use the resource path name
  498. var resource: Atomic.Resource;
  499. if (this.refListIndex != -1) {
  500. resource = object.getAttribute(this.attrInfo.name).resources[this.refListIndex];
  501. } else {
  502. resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
  503. }
  504. var text = "";
  505. if (resource) {
  506. if (resource instanceof Atomic.Animation) {
  507. text = (<Atomic.Animation>resource).animationName;
  508. } else {
  509. text = resource.name;
  510. var asset = ToolCore.assetDatabase.getAssetByCachePath(resource.name);
  511. if (asset)
  512. text = asset.name;
  513. }
  514. }
  515. this.editField.text = text;
  516. this.editField.subscribeToEvent(this.editField, "WidgetEvent", (ev: Atomic.UIWidgetEvent) => {
  517. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_POINTER_DOWN) {
  518. resource = <Atomic.Resource>object.getAttribute(this.attrInfo.name);
  519. if (resource instanceof Atomic.JSComponentFile) {
  520. var pathName = resource.name;
  521. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": pathName });
  522. } else if (resource instanceof Atomic.Model) {
  523. var asset = ToolCore.assetDatabase.getAssetByCachePath(resource.name);
  524. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": asset.getRelativePath() });
  525. } else if (resource instanceof Atomic.Animation) {
  526. var animCacheReferenceName = resource.name.replace( "_" + (<Atomic.Animation>resource).animationName, "");
  527. var asset = ToolCore.assetDatabase.getAssetByCachePath(animCacheReferenceName);
  528. this.sendEvent(EditorEvents.InspectorProjectReference, { "path": asset.getRelativePath() });
  529. } else {
  530. //Unknown Resource
  531. }
  532. }
  533. });
  534. }
  535. } else {
  536. this.editField.text = "--";
  537. }
  538. }
  539. createEditWidget() {
  540. var layout = new Atomic.UILayout();
  541. var o = InspectorUtils.createAttrEditFieldWithSelectButton("", layout);
  542. this.editField = o.editField;
  543. layout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  544. layout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT_RIGHT;
  545. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  546. var lp = new Atomic.UILayoutParams();
  547. lp.width = 140;
  548. o.editField.layoutParams = lp;
  549. o.editField.readOnly = true;
  550. this.editWidget = layout;
  551. var selectButton = o.selectButton;
  552. var resourceTypeName = this.attrInfo.resourceTypeName;
  553. var importerName = ToolCore.assetDatabase.getResourceImporterName(resourceTypeName);
  554. selectButton.onClick = () => {
  555. EditorUI.getModelOps().showResourceSelection("Select " + resourceTypeName + " Resource", importerName, resourceTypeName, function(retObject: any) {
  556. var resource: Atomic.Resource = null;
  557. if (retObject instanceof ToolCore.Asset) {
  558. resource = (<ToolCore.Asset>retObject).getResource(resourceTypeName);
  559. } else if (retObject instanceof Atomic.Resource) {
  560. resource = <Atomic.Resource>retObject;
  561. }
  562. this.editType.onAttributeInfoEdited(this.attrInfo, resource, this.refListIndex);
  563. this.onResourceChanged(resource);
  564. this.refresh();
  565. }.bind(this));
  566. };
  567. // handle dropping of component on field
  568. this.editField.subscribeToEvent(this.editField, "DragEnded", (ev: Atomic.DragEndedEvent) => {
  569. if (ev.target == o.editField) {
  570. var dragObject = ev.dragObject;
  571. var importer;
  572. if (dragObject.object && dragObject.object.typeName == "Asset") {
  573. var asset = <ToolCore.Asset>dragObject.object;
  574. if (asset.importerTypeName == importerName) {
  575. importer = asset.importer;
  576. }
  577. }
  578. if (importer) {
  579. var resource = asset.getResource(resourceTypeName);
  580. this.editType.onAttributeInfoEdited(this.attrInfo, resource, this.refListIndex);
  581. this.onResourceChanged(resource);
  582. this.refresh();
  583. }
  584. }
  585. });
  586. }
  587. }
  588. class ResourceRefListAttributeEdit extends AttributeInfoEdit {
  589. layout: Atomic.UILayout;
  590. refEdits: ResourceRefAttributeEdit[] = [];
  591. sizeEdit: Atomic.UIEditField;
  592. initialize(editType: SerializableEditType, attrInfo: Atomic.AttributeInfo): boolean {
  593. return super.initialize(editType, attrInfo);
  594. }
  595. createRefEdit(index: number) {
  596. var refEdit = new ResourceRefAttributeEdit(index);
  597. refEdit.initialize(this.editType, this.attrInfo);
  598. this.layout.addChild(refEdit);
  599. this.refEdits.push(refEdit);
  600. }
  601. createEditWidget() {
  602. this.spacing = 0;
  603. var layout = this.layout = new Atomic.UILayout();
  604. layout.axis = Atomic.UI_AXIS.UI_AXIS_Y;
  605. layout.spacing = 2;
  606. layout.layoutSize = Atomic.UI_LAYOUT_SIZE.UI_LAYOUT_SIZE_AVAILABLE;
  607. layout.gravity = Atomic.UI_GRAVITY.UI_GRAVITY_LEFT_RIGHT;
  608. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_LEFT_TOP;
  609. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  610. var lp = new Atomic.UILayoutParams();
  611. lp.width = 304;
  612. layout.layoutParams = lp;
  613. var name = this.attrInfo.name + " Size";
  614. if (name == "AnimationResources Size")
  615. name = "Animations";
  616. var sizeEdit = this.sizeEdit = InspectorUtils.createAttrEditField(name, layout);
  617. lp = new Atomic.UILayoutParams();
  618. lp.width = 160;
  619. sizeEdit.layoutParams = lp;
  620. sizeEdit.subscribeToEvent(sizeEdit, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  621. this.editWidget = layout;
  622. }
  623. createLayout() {
  624. this.createEditWidget();
  625. this.editWidget.subscribeToEvent(this.editWidget, "WidgetEvent", (data) => this.handleWidgetEvent(data));
  626. this.addChild(this.editWidget);
  627. }
  628. handleUIWidgetEditCompleteEvent(ev) {
  629. var size = Number(this.sizeEdit.text);
  630. if (size > 64 || size < 0)
  631. return;
  632. var editType = this.editType;
  633. var refresh = false;
  634. for (var i in editType.objects) {
  635. var object = editType.objects[i];
  636. var value = object.getAttribute(this.attrInfo.name);
  637. if (value.resources.length > size) {
  638. value.resources.length = size;
  639. object.setAttribute(this.attrInfo.name, value);
  640. refresh = true;
  641. } else if (value.resources.length < size) {
  642. for (var j = value.resources.length; j < size; j++) {
  643. value.resources.push(null);
  644. }
  645. object.setAttribute(this.attrInfo.name, value);
  646. refresh = true;
  647. }
  648. }
  649. if (refresh)
  650. this.refresh();
  651. }
  652. refresh() {
  653. var editType = this.editType;
  654. var object = this.editType.getFirstObject();
  655. if (!object) {
  656. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  657. return;
  658. }
  659. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE;
  660. var maxLength = -1;
  661. var i;
  662. for (i in editType.objects) {
  663. object = editType.objects[i];
  664. var value = object.getAttribute(this.attrInfo.name);
  665. if (value.resources.length > maxLength) {
  666. maxLength = value.resources.length;
  667. }
  668. }
  669. this.sizeEdit.text = maxLength.toString();
  670. if (maxLength == -1) {
  671. this.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  672. return;
  673. }
  674. for (i = this.refEdits.length; i < maxLength; i++) {
  675. this.createRefEdit(i);
  676. }
  677. for (i = 0; i < this.refEdits.length; i++) {
  678. var refEdit = this.refEdits[i];
  679. if (i < maxLength) {
  680. refEdit.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_VISIBLE;
  681. refEdit.refresh();
  682. }
  683. else {
  684. refEdit.visibility = Atomic.UI_WIDGET_VISIBILITY.UI_WIDGET_VISIBILITY_GONE;
  685. }
  686. }
  687. }
  688. }
  689. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_BOOL] = BoolAttributeEdit;
  690. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_INT] = IntAttributeEdit;
  691. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_FLOAT] = FloatAttributeEdit;
  692. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_STRING] = StringAttributeEdit;
  693. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_VECTOR2] = Vector2AttributeEdit;
  694. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_VECTOR3] = Vector3AttributeEdit;
  695. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_QUATERNION] = QuaternionAttributeEdit;
  696. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_COLOR] = ColorAttributeEdit;
  697. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_RESOURCEREF] = ResourceRefAttributeEdit;
  698. AttributeInfoEdit.standardAttrEditTypes[Atomic.VariantType.VAR_RESOURCEREFLIST] = ResourceRefListAttributeEdit;
  699. export = AttributeInfoEdit;