babylon.gui.stackPanel.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. module BABYLON {
  2. @className("StackPanel", "BABYLON")
  3. export class StackPanel extends UIElement {
  4. static STACKPANEL_PROPCOUNT = UIElement.UIELEMENT_PROPCOUNT + 3;
  5. static orientationHorizontalProperty: Prim2DPropInfo;
  6. constructor(settings?: {
  7. id ?: string,
  8. parent ?: UIElement,
  9. children ?: Array<UIElement>,
  10. templateName ?: string,
  11. styleName ?: string,
  12. isOrientationHorizontal ?: any,
  13. marginTop ?: number | string,
  14. marginLeft ?: number | string,
  15. marginRight ?: number | string,
  16. marginBottom ?: number | string,
  17. margin ?: number | string,
  18. marginHAlignment ?: number,
  19. marginVAlignment ?: number,
  20. marginAlignment ?: string,
  21. paddingTop ?: number | string,
  22. paddingLeft ?: number | string,
  23. paddingRight ?: number | string,
  24. paddingBottom ?: number | string,
  25. padding ?: string,
  26. paddingHAlignment ?: number,
  27. paddingVAlignment ?: number,
  28. paddingAlignment ?: string,
  29. }) {
  30. if (!settings) {
  31. settings = {};
  32. }
  33. super(settings);
  34. this.isOrientationHorizontal = (settings.isOrientationHorizontal == null) ? true : settings.isOrientationHorizontal;
  35. this._children = new Array<UIElement>();
  36. if (settings.children != null) {
  37. for (let child of settings.children) {
  38. this._children.push(child);
  39. }
  40. }
  41. }
  42. @dependencyProperty(StackPanel.STACKPANEL_PROPCOUNT + 0, pi => StackPanel.orientationHorizontalProperty = pi)
  43. public get isOrientationHorizontal(): boolean {
  44. return this._isOrientationHorizontal;
  45. }
  46. public set isOrientationHorizontal(value: boolean) {
  47. this._isOrientationHorizontal = value;
  48. }
  49. protected createVisualTree() {
  50. super.createVisualTree();
  51. // A StackPanel Control has a Group2D, child of the visualPlaceHolder, which is the Children placeholder.
  52. // The Children UIElement Tree will be create inside this placeholder.
  53. this._childrenPlaceholder = new Group2D({ parent: this._visualPlaceholder, id: `StackPanel Children Placeholder of ${this.id}` });
  54. let p = this._childrenPlaceholder;
  55. p.layoutEngine = this.isOrientationHorizontal ? StackPanelLayoutEngine.Horizontal : StackPanelLayoutEngine.Vertical;
  56. // The UIElement padding properties (padding and paddingAlignment) are bound to the Group2D Children placeholder, we bound to the Margin properties as the Group2D acts as an inner element already, so margin of inner is padding.
  57. p.dataSource = this;
  58. p.createSimpleDataBinding(Prim2DBase.marginProperty, "padding", DataBinding.MODE_ONEWAY);
  59. p.createSimpleDataBinding(Prim2DBase.marginAlignmentProperty, "paddingAlignment", DataBinding.MODE_ONEWAY);
  60. // The UIElement set the childrenPlaceholder with the visual returned by the renderingTemplate.
  61. // But it's not the case for a StackPanel, the placeholder of UIElement Children (the content)
  62. this._visualChildrenPlaceholder = this._childrenPlaceholder;
  63. }
  64. public get children(): Array<UIElement> {
  65. return this._children;
  66. }
  67. protected _getChildren(): Array<UIElement> {
  68. return this.children;
  69. }
  70. private _childrenPlaceholder: Group2D;
  71. private _children;
  72. private _isOrientationHorizontal: boolean;
  73. }
  74. @registerWindowRenderingTemplate("BABYLON.StackPanel", "Default", () => new DefaultStackPanelRenderingTemplate())
  75. export class DefaultStackPanelRenderingTemplate extends UIElementRenderingTemplateBase {
  76. createVisualTree(owner: UIElement, visualPlaceholder: Group2D): { root: Prim2DBase; contentPlaceholder: Prim2DBase } {
  77. return { root: visualPlaceholder, contentPlaceholder: visualPlaceholder };
  78. }
  79. attach(owner: UIElement): void {
  80. super.attach(owner);
  81. }
  82. }
  83. }