stackPanel.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create a 2D stack panel container
  5. */
  6. export class StackPanel extends Container {
  7. private _isVertical = true;
  8. private _manualWidth = false;
  9. private _manualHeight = false;
  10. private _doNotTrackManualChanges = false;
  11. private _tempMeasureStore = Measure.Empty();
  12. /** Gets or sets a boolean indicating if the stack panel is vertical or horizontal*/
  13. public get isVertical(): boolean {
  14. return this._isVertical;
  15. }
  16. public set isVertical(value: boolean) {
  17. if (this._isVertical === value) {
  18. return;
  19. }
  20. this._isVertical = value;
  21. this._markAsDirty();
  22. }
  23. /** Gets or sets panel width */
  24. public set width(value: string | number ) {
  25. if (!this._doNotTrackManualChanges) {
  26. this._manualWidth = true;
  27. }
  28. if (this._width.toString(this._host) === value) {
  29. return;
  30. }
  31. if (this._width.fromString(value)) {
  32. this._markAsDirty();
  33. }
  34. }
  35. public get width(): string | number {
  36. return this._width.toString(this._host);
  37. }
  38. /** Gets or sets panel height */
  39. public set height(value: string | number ) {
  40. if (!this._doNotTrackManualChanges) {
  41. this._manualHeight = true;
  42. }
  43. if (this._height.toString(this._host) === value) {
  44. return;
  45. }
  46. if (this._height.fromString(value)) {
  47. this._markAsDirty();
  48. }
  49. }
  50. public get height(): string | number {
  51. return this._height.toString(this._host);
  52. }
  53. /**
  54. * Creates a new StackPanel
  55. * @param name defines control name
  56. */
  57. constructor(public name?: string) {
  58. super(name);
  59. }
  60. protected _getTypeName(): string {
  61. return "StackPanel";
  62. }
  63. protected _preMeasure(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  64. var stackWidth = 0;
  65. var stackHeight = 0;
  66. for (var child of this._children) {
  67. this._tempMeasureStore.copyFrom(child._currentMeasure);
  68. child._currentMeasure.copyFrom(parentMeasure);
  69. child._measure();
  70. if (this._isVertical) {
  71. child.top = stackHeight + "px";
  72. if (!child._top.ignoreAdaptiveScaling) {
  73. child._markAsDirty();
  74. }
  75. child._top.ignoreAdaptiveScaling = true;
  76. stackHeight += child._currentMeasure.height;
  77. if(child._currentMeasure.width > stackWidth) {
  78. stackWidth = child._currentMeasure.width;
  79. }
  80. child.verticalAlignment = BABYLON.GUI.Control.VERTICAL_ALIGNMENT_TOP;
  81. } else {
  82. child.left = stackWidth + "px";
  83. if (!child._left.ignoreAdaptiveScaling) {
  84. child._markAsDirty();
  85. }
  86. child._left.ignoreAdaptiveScaling = true;
  87. stackWidth += child._currentMeasure.width;
  88. if(child._currentMeasure.height > stackHeight) {
  89. stackHeight = child._currentMeasure.height;
  90. }
  91. child.horizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
  92. }
  93. child._currentMeasure.copyFrom(this._tempMeasureStore);
  94. }
  95. this._doNotTrackManualChanges = true;
  96. // Let stack panel width and height default to stackHeight and stackWidth if dimensions are not specified.
  97. // User can now define their own height and width for stack panel.
  98. let panelWidthChanged = false;
  99. let panelHeightChanged = false;
  100. let previousHeight = this.height;
  101. let previousWidth = this.width;
  102. if (!this._manualHeight) {
  103. // do not specify height if strictly defined by user
  104. this.height = stackHeight + "px";
  105. }
  106. if (!this._manualWidth) {
  107. // do not specify width if strictly defined by user
  108. this.width = stackWidth + "px";
  109. }
  110. panelWidthChanged = previousWidth !== this.width || !this._width.ignoreAdaptiveScaling;
  111. panelHeightChanged = previousHeight !== this.height || !this._height.ignoreAdaptiveScaling;
  112. if (panelHeightChanged) {
  113. this._height.ignoreAdaptiveScaling = true;
  114. }
  115. if (panelWidthChanged) {
  116. this._width.ignoreAdaptiveScaling = true;
  117. }
  118. this._doNotTrackManualChanges = false;
  119. if (panelWidthChanged || panelHeightChanged) {
  120. this._markAllAsDirty();
  121. }
  122. super._preMeasure(parentMeasure, context);
  123. }
  124. }
  125. }