AttributeInfoEdit.ts 26 KB

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