AttributeInfoEdit.ts 35 KB

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