stackPanel3D.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create a stack panel in 3D on XY plane
  5. */
  6. export class StackPanel3D extends Container3D {
  7. private _isVertical = false;
  8. /**
  9. * Gets or sets a boolean indicating if the stack panel is vertical or horizontal (horizontal by default)
  10. */
  11. public get isVertical(): boolean {
  12. return this._isVertical;
  13. }
  14. public set isVertical(value: boolean) {
  15. if (this._isVertical === value) {
  16. return;
  17. }
  18. this._isVertical = value;
  19. Tools.SetImmediate(() => {
  20. this._arrangeChildren();
  21. });
  22. }
  23. /**
  24. * Gets or sets the distance between elements
  25. */
  26. public margin = 0.1;
  27. /**
  28. * Creates new StackPanel
  29. * @param isVertical
  30. */
  31. public constructor(isVertical = false) {
  32. super();
  33. this._isVertical = isVertical;
  34. }
  35. protected _arrangeChildren() {
  36. let width = 0;
  37. let height = 0;
  38. let controlCount = 0;
  39. let extendSizes = [];
  40. let currentInverseWorld = Matrix.Invert(this.node!.computeWorldMatrix(true));
  41. // Measure
  42. for (var child of this._children) {
  43. if (!child.mesh) {
  44. continue;
  45. }
  46. controlCount++;
  47. child.mesh.computeWorldMatrix(true);
  48. child.mesh.getWorldMatrix().multiplyToRef(currentInverseWorld, Tmp.Matrix[0]);
  49. let boundingBox = child.mesh.getBoundingInfo().boundingBox;
  50. let extendSize = Vector3.TransformNormal(boundingBox.extendSize, Tmp.Matrix[0]);
  51. extendSizes.push(extendSize);
  52. if (this._isVertical) {
  53. height += extendSize.y;
  54. } else {
  55. width += extendSize.x;
  56. }
  57. }
  58. if (this._isVertical) {
  59. height += (controlCount - 1) * this.margin / 2;
  60. } else {
  61. width += (controlCount - 1) * this.margin / 2;
  62. }
  63. // Arrange
  64. let offset: number;
  65. if (this._isVertical) {
  66. offset = -height;
  67. } else {
  68. offset = -width;
  69. }
  70. let index = 0;
  71. for (var child of this._children) {
  72. if (!child.mesh) {
  73. continue;
  74. }
  75. controlCount--;
  76. let extendSize = extendSizes[index++];
  77. if (this._isVertical) {
  78. child.position.y = offset + extendSize.y;
  79. child.position.x = 0;
  80. offset += extendSize.y * 2;
  81. } else {
  82. child.position.x = offset + extendSize.x;
  83. child.position.y = 0;
  84. offset += extendSize.x * 2;
  85. }
  86. offset += (controlCount > 0 ? this.margin : 0)
  87. }
  88. }
  89. }
  90. }