123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- import { Control } from "./control";
- import { Measure } from "../measure";
- import { Nullable } from "babylonjs";
- import { AdvancedDynamicTexture } from "../advancedDynamicTexture";
- /**
- * Root class for 2D containers
- * @see http://doc.babylonjs.com/how_to/gui#containers
- */
- export class Container extends Control {
- /** @hidden */
- protected _children = new Array<Control>();
- /** @hidden */
- protected _measureForChildren = Measure.Empty();
- /** @hidden */
- protected _background = "";
- /** @hidden */
- protected _adaptWidthToChildren = false;
- /** @hidden */
- protected _adaptHeightToChildren = false;
- /** Gets or sets a boolean indicating if the container should try to adapt to its children height */
- public get adaptHeightToChildren(): boolean {
- return this._adaptHeightToChildren;
- }
- public set adaptHeightToChildren(value: boolean) {
- if (this._adaptHeightToChildren === value) {
- return;
- }
- this._adaptHeightToChildren = value;
- if (value) {
- this.height = "100%";
- }
- this._markAsDirty();
- }
- /** Gets or sets a boolean indicating if the container should try to adapt to its children width */
- public get adaptWidthToChildren(): boolean {
- return this._adaptWidthToChildren;
- }
- public set adaptWidthToChildren(value: boolean) {
- if (this._adaptWidthToChildren === value) {
- return;
- }
- this._adaptWidthToChildren = value;
- if (value) {
- this.width = "100%";
- }
- this._markAsDirty();
- }
- /** Gets or sets background color */
- public get background(): string {
- return this._background;
- }
- public set background(value: string) {
- if (this._background === value) {
- return;
- }
- this._background = value;
- this._markAsDirty();
- }
- /** Gets the list of children */
- public get children(): Control[] {
- return this._children;
- }
- /**
- * Creates a new Container
- * @param name defines the name of the container
- */
- constructor(public name?: string) {
- super(name);
- }
- protected _getTypeName(): string {
- return "Container";
- }
- public _flagDescendantsAsMatrixDirty(): void {
- for (var child of this.children) {
- child._markMatrixAsDirty();
- }
- }
- /**
- * Gets a child using its name
- * @param name defines the child name to look for
- * @returns the child control if found
- */
- public getChildByName(name: string): Nullable<Control> {
- for (var child of this.children) {
- if (child.name === name) {
- return child;
- }
- }
- return null;
- }
- /**
- * Gets a child using its type and its name
- * @param name defines the child name to look for
- * @param type defines the child type to look for
- * @returns the child control if found
- */
- public getChildByType(name: string, type: string): Nullable<Control> {
- for (var child of this.children) {
- if (child.typeName === type) {
- return child;
- }
- }
- return null;
- }
- /**
- * Search for a specific control in children
- * @param control defines the control to look for
- * @returns true if the control is in child list
- */
- public containsControl(control: Control): boolean {
- return this.children.indexOf(control) !== -1;
- }
- /**
- * Adds a new control to the current container
- * @param control defines the control to add
- * @returns the current container
- */
- public addControl(control: Nullable<Control>): Container {
- if (!control) {
- return this;
- }
- var index = this._children.indexOf(control);
- if (index !== -1) {
- return this;
- }
- control._link(this._host);
- control._markAllAsDirty();
- this._reOrderControl(control);
- this._markAsDirty();
- return this;
- }
- /**
- * Removes all controls from the current container
- * @returns the current container
- */
- public clearControls(): Container {
- let children = this.children.slice();
- for (var child of children) {
- this.removeControl(child);
- }
- return this;
- }
- /**
- * Removes a control from the current container
- * @param control defines the control to remove
- * @returns the current container
- */
- public removeControl(control: Control): Container {
- var index = this._children.indexOf(control);
- if (index !== -1) {
- this._children.splice(index, 1);
- control.parent = null;
- }
- control.linkWithMesh(null);
- if (this._host) {
- this._host._cleanControlAfterRemoval(control);
- }
- this._markAsDirty();
- return this;
- }
- /** @hidden */
- public _reOrderControl(control: Control): void {
- this.removeControl(control);
- for (var index = 0; index < this._children.length; index++) {
- if (this._children[index].zIndex > control.zIndex) {
- this._children.splice(index, 0, control);
- return;
- }
- }
- this._children.push(control);
- control.parent = this;
- this._markAsDirty();
- }
- /** @hidden */
- public _offsetLeft(offset: number) {
- super._offsetLeft(offset);
- for (var child of this._children) {
- child._offsetLeft(offset);
- }
- }
- /** @hidden */
- public _offsetTop(offset: number) {
- super._offsetTop(offset);
- for (var child of this._children) {
- child._offsetTop(offset);
- }
- }
- /** @hidden */
- public _markAllAsDirty(): void {
- super._markAllAsDirty();
- for (var index = 0; index < this._children.length; index++) {
- this._children[index]._markAllAsDirty();
- }
- }
- /** @hidden */
- protected _localDraw(context: CanvasRenderingContext2D): void {
- if (this._background) {
- context.save();
- if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
- context.shadowColor = this.shadowColor;
- context.shadowBlur = this.shadowBlur;
- context.shadowOffsetX = this.shadowOffsetX;
- context.shadowOffsetY = this.shadowOffsetY;
- }
- context.fillStyle = this._background;
- context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
- context.restore();
- }
- }
- /** @hidden */
- public _link(host: AdvancedDynamicTexture): void {
- super._link(host);
- for (var child of this._children) {
- child._link(host);
- }
- }
- /** @hidden */
- protected _beforeLayout() {
- // Do nothing
- }
- /** @hidden */
- public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
- if (!this.isVisible || this.notRenderable) {
- return false;
- }
- let rebuildCount = 0;
- context.save();
- this._applyStates(context);
- this._beforeLayout();
- do {
- let computedWidth = -1;
- let computedHeight = -1;
- this._rebuildLayout = false;
- this._processMeasures(parentMeasure, context);
- if (!this._isClipped) {
- for (var child of this._children) {
- child._tempParentMeasure.copyFrom(this._measureForChildren);
- if (child._layout(this._measureForChildren, context)) {
- if (this.adaptWidthToChildren && child._width.isPixel) {
- computedWidth = Math.max(computedWidth, child._currentMeasure.width);
- }
- if (this.adaptHeightToChildren && child._height.isPixel) {
- computedHeight = Math.max(computedHeight, child._currentMeasure.height);
- }
- }
- }
- if (this.adaptWidthToChildren && computedWidth >= 0) {
- if (this.width !== computedWidth + "px") {
- this.width = computedWidth + "px";
- this._rebuildLayout = true;
- }
- }
- if (this.adaptHeightToChildren && computedHeight >= 0) {
- if (this.height !== computedHeight + "px") {
- this.height = computedHeight + "px";
- this._rebuildLayout = true;
- }
- }
- this._postMeasure();
- }
- rebuildCount++;
- }
- while (this._rebuildLayout && rebuildCount < 3);
- if (rebuildCount >= 3) {
- BABYLON.Tools.Error(`Layout cycle detected in GUI (Container uniqueId=${this.uniqueId})`);
- }
- context.restore();
- this._isDirty = false;
- return true;
- }
- protected _postMeasure() {
- // Do nothing by default
- }
- /** @hidden */
- public _draw(context: CanvasRenderingContext2D): void {
- this._localDraw(context);
- if (this.clipChildren) {
- this._clipForChildren(context);
- }
- for (var child of this._children) {
- child._render(context);
- }
- }
- /** @hidden */
- public _getDescendants(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
- if (!this.children) {
- return;
- }
- for (var index = 0; index < this.children.length; index++) {
- var item = this.children[index];
- if (!predicate || predicate(item)) {
- results.push(item);
- }
- if (!directDescendantsOnly) {
- item._getDescendants(results, false, predicate);
- }
- }
- }
- /** @hidden */
- public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
- if (!this.isVisible || this.notRenderable) {
- return false;
- }
- if (!super.contains(x, y)) {
- return false;
- }
- // Checking backwards to pick closest first
- for (var index = this._children.length - 1; index >= 0; index--) {
- var child = this._children[index];
- if (child._processPicking(x, y, type, pointerId, buttonIndex)) {
- if (child.hoverCursor) {
- this._host._changeCursor(child.hoverCursor);
- }
- return true;
- }
- }
- if (!this.isHitTestVisible) {
- return false;
- }
- return this._processObservables(type, x, y, pointerId, buttonIndex);
- }
- /** @hidden */
- protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
- super._additionalProcessing(parentMeasure, context);
- this._measureForChildren.copyFrom(this._currentMeasure);
- }
- /** Releases associated resources */
- public dispose() {
- super.dispose();
- for (var control of this._children) {
- control.dispose();
- }
- }
- }
|