stackPanel.ts 5.1 KB

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