123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- /// <reference path="../../../dist/preview release/babylon.d.ts"/>
- module BABYLON.GUI {
- export class Container extends Control {
- protected _children = new Array<Control>();
- protected _measureForChildren = Measure.Empty();
- protected _background: string;
- public get background(): string {
- return this._background;
- }
- public set background(value: string) {
- if (this._background === value) {
- return;
- }
- this._background = value;
- this._markAsDirty();
- }
- public get children(): Control[] {
- return this._children;
- }
- constructor(public name?: string) {
- super(name);
- }
- protected _getTypeName(): string {
- return "Container";
- }
- public getChildByName(name: string): Control {
- for (var child of this._children) {
- if (child.name === name) {
- return child;
- }
- }
- return null;
- }
- public getChildByType(name: string, type: string): Control {
- for (var child of this._children) {
- if (child.typeName === type) {
- return child;
- }
- }
- return null;
- }
- public containsControl(control: Control): boolean {
- return this._children.indexOf(control) !== -1;
- }
- public addControl(control: Control): Container {
- var index = this._children.indexOf(control);
- if (index !== -1) {
- return this;
- }
- control._link(this, this._host);
- control._markAllAsDirty();
- this._reOrderControl(control);
- this._markAsDirty();
- return this;
- }
- public removeControl(control: Control): Container {
- var index = this._children.indexOf(control);
- if (index !== -1) {
- this._children.splice(index, 1);
- }
- this._markAsDirty();
- return this;
- }
- 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);
- this._markAsDirty();
- }
- public _markMatrixAsDirty(): void {
- super._markMatrixAsDirty();
- for (var index = 0; index < this._children.length; index++) {
- this._children[index]._markMatrixAsDirty();
- }
- }
- public _markAllAsDirty(): void {
- super._markAllAsDirty();
- for (var index = 0; index < this._children.length; index++) {
- this._children[index]._markAllAsDirty();
- }
- }
- protected _localDraw(context: CanvasRenderingContext2D): void {
- if (this._background) {
- context.fillStyle = this._background;
- context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
- }
- }
- public _link(root: Container, host: AdvancedDynamicTexture): void {
- super._link(root, host);
- for (var child of this._children) {
- child._link(root, host);
- }
- }
- public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
- if (!this.isVisible || this.notRenderable) {
- return;
- }
- context.save();
-
- this._applyStates(context);
- if (this._processMeasures(parentMeasure, context)) {
- this._localDraw(context);
- this._clipForChildren(context);
- for (var child of this._children) {
- if (child.isVisible && !child.notRenderable) {
- child._draw(this._measureForChildren, context);
- }
- }
- }
- context.restore();
- }
- public _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean {
- if (!this.isHitTestVisible || !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, buttonIndex)) {
- return true;
- }
- }
- return this._processObservables(type, x, y, buttonIndex);
- }
- protected _clipForChildren(context: CanvasRenderingContext2D): void {
- // DO nothing
- }
- protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
- super._additionalProcessing(parentMeasure, context);
- this._measureForChildren.copyFrom(this._currentMeasure);
- }
- public dispose() {
- super.dispose();
- for (var control of this._children) {
- control.dispose();
- }
- }
- }
- }
|