SelectionSection.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 SerializableEditType = require("./SerializableEditType");
  23. import AttributeInfoEdit = require("./AttributeInfoEdit");
  24. import SelectionSectionUI = require("./SelectionSectionUI");
  25. import "./ComponentAttributeUI";
  26. abstract class SelectionSection extends Atomic.UISection {
  27. hasDynamicAttr: boolean = false;
  28. editType: SerializableEditType;
  29. attrLayout: Atomic.UILayout;
  30. suppressed: boolean = false;
  31. customUI: SelectionSectionUI;
  32. attrEdits: { [name: string]: AttributeInfoEdit } = {};
  33. constructor(editType: SerializableEditType) {
  34. super();
  35. this.editType = editType;
  36. this.text = editType.typeName;
  37. this.createUI();
  38. }
  39. contains(serial:Atomic.Serializable):boolean {
  40. return this.editType.objects.indexOf(serial) == -1;
  41. }
  42. refresh() {
  43. for (var name in this.attrEdits) {
  44. this.attrEdits[name].refresh();
  45. }
  46. if (this.customUI)
  47. this.customUI.refresh();
  48. }
  49. suppress(value: boolean) {
  50. if (this.suppressed == value) {
  51. return;
  52. }
  53. this.suppressed = value;
  54. if (value) {
  55. this.visibility = Atomic.UI_WIDGET_VISIBILITY_GONE;
  56. } else {
  57. this.visibility = Atomic.UI_WIDGET_VISIBILITY_VISIBLE;
  58. }
  59. }
  60. updateDynamicAttrInfos(attrInfos: Atomic.AttributeInfo[]) {
  61. Atomic.ui.blockChangedEvents = true;
  62. this.editType.attrInfos = attrInfos;
  63. var attrEdit: AttributeInfoEdit;
  64. var remove: AttributeInfoEdit[] = [];
  65. var addWidget: Atomic.UIWidget;
  66. for (var name in this.attrEdits) {
  67. attrEdit = this.attrEdits[name];
  68. if (attrEdit.attrInfo.dynamic) {
  69. remove.push(attrEdit);
  70. } else {
  71. addWidget = attrEdit;
  72. }
  73. }
  74. for (var i in remove) {
  75. var attrEdit = remove[i];
  76. attrEdit.remove();
  77. delete this.attrEdits[attrEdit.attrInfo.name];
  78. }
  79. for (var i in attrInfos) {
  80. var attr = attrInfos[i];
  81. if (!attr.dynamic) {
  82. continue;
  83. }
  84. if (attr.mode & Atomic.AM_NOEDIT)
  85. continue;
  86. var attrEdit = AttributeInfoEdit.createAttrEdit(this.editType, attr);
  87. if (!attrEdit)
  88. continue;
  89. this.attrEdits[attr.name] = attrEdit;
  90. if (!addWidget) {
  91. this.attrLayout.addChild(attrEdit);
  92. addWidget = attrEdit;
  93. } else {
  94. this.attrLayout.addChildAfter(attrEdit, addWidget);
  95. addWidget = attrEdit;
  96. }
  97. }
  98. this.refresh();
  99. Atomic.ui.blockChangedEvents = false;
  100. }
  101. createUI() {
  102. var attrLayout = this.attrLayout = new Atomic.UILayout(Atomic.UI_AXIS_Y);
  103. attrLayout.spacing = 3;
  104. attrLayout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
  105. attrLayout.layoutSize = Atomic.UI_LAYOUT_SIZE_AVAILABLE;
  106. this.contentRoot.addChild(attrLayout);
  107. for (var i in this.editType.attrInfos) {
  108. var attr = this.editType.attrInfos[i];
  109. if (attr.mode & Atomic.AM_NOEDIT)
  110. continue;
  111. var attrEdit = AttributeInfoEdit.createAttrEdit(this.editType, attr);
  112. if (!attrEdit)
  113. continue;
  114. this.attrEdits[attr.name] = attrEdit;
  115. attrLayout.addChild(attrEdit);
  116. }
  117. if (SelectionSection.customSectionUI[this.editType.typeName]) {
  118. this.customUI = new SelectionSection.customSectionUI[this.editType.typeName]();
  119. this.customUI.createUI(this.editType);
  120. attrLayout.addChild(this.customUI);
  121. }
  122. }
  123. static fontDesc: Atomic.UIFontDescription;
  124. static customSectionUI: { [typeName: string]: typeof SelectionSectionUI } = {};
  125. static registerCustomSectionUI(typeName: string, ui: typeof SelectionSectionUI) {
  126. SelectionSection.customSectionUI[typeName] = ui;
  127. }
  128. private static Ctor = (() => {
  129. var fd = SelectionSection.fontDesc = new Atomic.UIFontDescription();
  130. fd.id = "Vera";
  131. fd.size = 11;
  132. })();
  133. }
  134. export = SelectionSection;