AttributeInfoEdit.ts 29 KB

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