AttributeInfoEdit.ts 35 KB

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