AttributeInfoEdit.ts 25 KB

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