AttributeInfoEdit.ts 35 KB

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