stackPanel.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Container } from "./container";
  2. import { Measure } from "../measure";
  3. import { Control } from "./control";
  4. import { Tools } from "babylonjs";
  5. /**
  6. * Class used to create a 2D stack panel container
  7. */
  8. export class StackPanel extends Container {
  9. private _isVertical = true;
  10. private _manualWidth = false;
  11. private _manualHeight = false;
  12. private _doNotTrackManualChanges = false;
  13. /** Gets or sets a boolean indicating if the stack panel is vertical or horizontal*/
  14. public get isVertical(): boolean {
  15. return this._isVertical;
  16. }
  17. public set isVertical(value: boolean) {
  18. if (this._isVertical === value) {
  19. return;
  20. }
  21. this._isVertical = value;
  22. this._markAsDirty();
  23. }
  24. /**
  25. * Gets or sets panel width.
  26. * This value should not be set when in horizontal mode as it will be computed automatically
  27. */
  28. public set width(value: string | number) {
  29. if (!this._doNotTrackManualChanges) {
  30. this._manualWidth = true;
  31. }
  32. if (this._width.toString(this._host) === value) {
  33. return;
  34. }
  35. if (this._width.fromString(value)) {
  36. this._markAsDirty();
  37. }
  38. }
  39. public get width(): string | number {
  40. return this._width.toString(this._host);
  41. }
  42. /**
  43. * Gets or sets panel height.
  44. * This value should not be set when in vertical mode as it will be computed automatically
  45. */
  46. public set height(value: string | number) {
  47. if (!this._doNotTrackManualChanges) {
  48. this._manualHeight = true;
  49. }
  50. if (this._height.toString(this._host) === value) {
  51. return;
  52. }
  53. if (this._height.fromString(value)) {
  54. this._markAsDirty();
  55. }
  56. }
  57. public get height(): string | number {
  58. return this._height.toString(this._host);
  59. }
  60. /**
  61. * Creates a new StackPanel
  62. * @param name defines control name
  63. */
  64. constructor(public name?: string) {
  65. super(name);
  66. }
  67. protected _getTypeName(): string {
  68. return "StackPanel";
  69. }
  70. /** @hidden */
  71. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  72. for (var child of this._children) {
  73. if (this._isVertical) {
  74. child.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  75. } else {
  76. child.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  77. }
  78. }
  79. super._preMeasure(parentMeasure, context);
  80. }
  81. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  82. super._additionalProcessing(parentMeasure, context);
  83. this._measureForChildren.copyFrom(parentMeasure);
  84. this._measureForChildren.left = this._currentMeasure.left;
  85. this._measureForChildren.top = this._currentMeasure.top;
  86. if (!this.isVertical || this._manualWidth) {
  87. this._measureForChildren.width = this._currentMeasure.width;
  88. }
  89. if (this.isVertical || this._manualHeight) {
  90. this._measureForChildren.height = this._currentMeasure.height;
  91. }
  92. }
  93. protected _postMeasure(): void {
  94. var stackWidth = 0;
  95. var stackHeight = 0;
  96. for (var child of this._children) {
  97. if (!child.isVisible || child.notRenderable) {
  98. continue;
  99. }
  100. if (this._isVertical) {
  101. if (child.top !== stackHeight + "px") {
  102. child.top = stackHeight + "px";
  103. this._rebuildLayout = true;
  104. child._top.ignoreAdaptiveScaling = true;
  105. }
  106. if (child._height.isPercentage) {
  107. Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using height in percentage mode inside a vertical StackPanel`);
  108. } else {
  109. stackHeight += child._currentMeasure.height + child.paddingTopInPixels + child.paddingBottomInPixels;
  110. }
  111. } else {
  112. if (child.left !== stackWidth + "px") {
  113. child.left = stackWidth + "px";
  114. this._rebuildLayout = true;
  115. child._left.ignoreAdaptiveScaling = true;
  116. }
  117. if (child._width.isPercentage) {
  118. Tools.Warn(`Control (Name:${child.name}, UniqueId:${child.uniqueId}) is using width in percentage mode inside a horizontal StackPanel`);
  119. } else {
  120. stackWidth += child._currentMeasure.width + child.paddingLeftInPixels + child.paddingRightInPixels;
  121. }
  122. }
  123. }
  124. this._doNotTrackManualChanges = true;
  125. // Let stack panel width or height default to stackHeight and stackWidth if dimensions are not specified.
  126. // User can now define their own height and width for stack panel.
  127. let panelWidthChanged = false;
  128. let panelHeightChanged = false;
  129. if (!this._manualHeight && this._isVertical) { // do not specify height if strictly defined by user
  130. let previousHeight = this.height;
  131. this.height = stackHeight + "px";
  132. panelHeightChanged = previousHeight !== this.height || !this._height.ignoreAdaptiveScaling;
  133. }
  134. if (!this._manualWidth && !this._isVertical) { // do not specify width if strictly defined by user
  135. let previousWidth = this.width;
  136. this.width = stackWidth + "px";
  137. panelWidthChanged = previousWidth !== this.width || !this._width.ignoreAdaptiveScaling;
  138. }
  139. if (panelHeightChanged) {
  140. this._height.ignoreAdaptiveScaling = true;
  141. }
  142. if (panelWidthChanged) {
  143. this._width.ignoreAdaptiveScaling = true;
  144. }
  145. this._doNotTrackManualChanges = false;
  146. if (panelWidthChanged || panelHeightChanged) {
  147. this._rebuildLayout = true;
  148. }
  149. super._postMeasure();
  150. }
  151. }