AttributeInfoEdit.ts 29 KB

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