SelectionSection.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import SerializableEditType = require("./SerializableEditType");
  8. import AttributeInfoEdit = require("./AttributeInfoEdit");
  9. import SelectionSectionUI = require("./SelectionSectionUI");
  10. import "./ComponentAttributeUI";
  11. abstract class SelectionSection extends Atomic.UISection {
  12. hasDynamicAttr: boolean = false;
  13. editType: SerializableEditType;
  14. attrLayout: Atomic.UILayout;
  15. suppressed: boolean = false;
  16. customUI: SelectionSectionUI;
  17. attrEdits: { [name: string]: AttributeInfoEdit } = {};
  18. constructor(editType: SerializableEditType) {
  19. super();
  20. this.editType = editType;
  21. this.text = editType.typeName;
  22. this.createUI();
  23. }
  24. contains(serial:Atomic.Serializable):boolean {
  25. return this.editType.objects.indexOf(serial) == -1;
  26. }
  27. refresh() {
  28. for (var name in this.attrEdits) {
  29. this.attrEdits[name].refresh();
  30. }
  31. if (this.customUI)
  32. this.customUI.refresh();
  33. }
  34. suppress(value: boolean) {
  35. if (this.suppressed == value) {
  36. return;
  37. }
  38. this.suppressed = value;
  39. if (value) {
  40. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  41. } else {
  42. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  43. }
  44. }
  45. updateDynamicAttrInfos(attrInfos: Atomic.AttributeInfo[]) {
  46. Atomic.ui.blockChangedEvents = true;
  47. this.editType.attrInfos = attrInfos;
  48. var attrEdit: AttributeInfoEdit;
  49. var remove: AttributeInfoEdit[] = [];
  50. var addWidget: Atomic.UIWidget;
  51. for (var name in this.attrEdits) {
  52. attrEdit = this.attrEdits[name];
  53. if (attrEdit.attrInfo.dynamic) {
  54. remove.push(attrEdit);
  55. } else {
  56. addWidget = attrEdit;
  57. }
  58. }
  59. for (var i in remove) {
  60. var attrEdit = remove[i];
  61. attrEdit.remove();
  62. delete this.attrEdits[attrEdit.attrInfo.name];
  63. }
  64. for (var i in attrInfos) {
  65. var attr = attrInfos[i];
  66. if (!attr.dynamic) {
  67. continue;
  68. }
  69. if (attr.mode & Atomic.AM_NOEDIT)
  70. continue;
  71. var attrEdit = AttributeInfoEdit.createAttrEdit(this.editType, attr);
  72. if (!attrEdit)
  73. continue;
  74. this.attrEdits[attr.name] = attrEdit;
  75. if (!addWidget) {
  76. this.attrLayout.addChild(attrEdit);
  77. addWidget = attrEdit;
  78. } else {
  79. this.attrLayout.addChildAfter(attrEdit, addWidget);
  80. addWidget = attrEdit;
  81. }
  82. }
  83. this.refresh();
  84. Atomic.ui.blockChangedEvents = false;
  85. }
  86. createUI() {
  87. var attrLayout = this.attrLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  88. attrLayout.spacing = 3;
  89. attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  90. attrLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  91. this.contentRoot.addChild(attrLayout);
  92. for (var i in this.editType.attrInfos) {
  93. var attr = this.editType.attrInfos[i];
  94. if (attr.mode & Atomic.AM_NOEDIT)
  95. continue;
  96. var attrEdit = AttributeInfoEdit.createAttrEdit(this.editType, attr);
  97. if (!attrEdit)
  98. continue;
  99. this.attrEdits[attr.name] = attrEdit;
  100. attrLayout.addChild(attrEdit);
  101. }
  102. if (SelectionSection.customSectionUI[this.editType.typeName]) {
  103. this.customUI = new SelectionSection.customSectionUI[this.editType.typeName]();
  104. this.customUI.createUI(this.editType);
  105. attrLayout.addChild(this.customUI);
  106. }
  107. }
  108. static fontDesc: Atomic.UIFontDescription;
  109. static customSectionUI: { [typeName: string]: typeof SelectionSectionUI } = {};
  110. static registerCustomSectionUI(typeName: string, ui: typeof SelectionSectionUI) {
  111. SelectionSection.customSectionUI[typeName] = ui;
  112. }
  113. private static Ctor = (() => {
  114. var fd = SelectionSection.fontDesc = new Atomic.UIFontDescription();
  115. fd.id = "Vera";
  116. fd.size = 11;
  117. })();
  118. }
  119. export = SelectionSection;