container.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Container extends Control {
  4. protected _children = new Array<Control>();
  5. protected _measureForChildren = Measure.Empty();
  6. protected _background: string;
  7. public get background(): string {
  8. return this._background;
  9. }
  10. public set background(value: string) {
  11. if (this._background === value) {
  12. return;
  13. }
  14. this._background = value;
  15. this._markAsDirty();
  16. }
  17. public get children(): Control[] {
  18. return this._children;
  19. }
  20. constructor(public name?: string) {
  21. super(name);
  22. }
  23. protected _getTypeName(): string {
  24. return "Container";
  25. }
  26. public getChildByName(name: string): Nullable<Control> {
  27. for (var child of this._children) {
  28. if (child.name === name) {
  29. return child;
  30. }
  31. }
  32. return null;
  33. }
  34. public getChildByType(name: string, type: string): Nullable<Control> {
  35. for (var child of this._children) {
  36. if (child.typeName === type) {
  37. return child;
  38. }
  39. }
  40. return null;
  41. }
  42. public containsControl(control: Control): boolean {
  43. return this._children.indexOf(control) !== -1;
  44. }
  45. public addControl(control: Control): Container {
  46. var index = this._children.indexOf(control);
  47. if (index !== -1) {
  48. return this;
  49. }
  50. control._link(this, this._host);
  51. control._markAllAsDirty();
  52. this._reOrderControl(control);
  53. this._markAsDirty();
  54. return this;
  55. }
  56. public removeControl(control: Control): Container {
  57. var index = this._children.indexOf(control);
  58. if (index !== -1) {
  59. this._children.splice(index, 1);
  60. control.parent = null;
  61. }
  62. this._markAsDirty();
  63. return this;
  64. }
  65. public _reOrderControl(control: Control): void {
  66. this.removeControl(control);
  67. for (var index = 0; index < this._children.length; index++) {
  68. if (this._children[index].zIndex > control.zIndex) {
  69. this._children.splice(index, 0, control);
  70. return;
  71. }
  72. }
  73. this._children.push(control);
  74. control.parent = this;
  75. this._markAsDirty();
  76. }
  77. public _markMatrixAsDirty(): void {
  78. super._markMatrixAsDirty();
  79. for (var index = 0; index < this._children.length; index++) {
  80. this._children[index]._markMatrixAsDirty();
  81. }
  82. }
  83. public _markAllAsDirty(): void {
  84. super._markAllAsDirty();
  85. for (var index = 0; index < this._children.length; index++) {
  86. this._children[index]._markAllAsDirty();
  87. }
  88. }
  89. protected _localDraw(context: CanvasRenderingContext2D): void {
  90. if (this._background) {
  91. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  92. context.shadowColor = this.shadowColor;
  93. context.shadowBlur = this.shadowBlur;
  94. context.shadowOffsetX = this.shadowOffsetX;
  95. context.shadowOffsetY = this.shadowOffsetY;
  96. }
  97. context.fillStyle = this._background;
  98. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  99. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  100. context.shadowBlur = 0;
  101. context.shadowOffsetX = 0;
  102. context.shadowOffsetY = 0;
  103. }
  104. }
  105. }
  106. public _link(root: Nullable<Container>, host: AdvancedDynamicTexture): void {
  107. super._link(root, host);
  108. for (var child of this._children) {
  109. child._link(root, host);
  110. }
  111. }
  112. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  113. if (!this.isVisible || this.notRenderable) {
  114. return;
  115. }
  116. context.save();
  117. this._applyStates(context);
  118. if (this._processMeasures(parentMeasure, context)) {
  119. this._localDraw(context);
  120. this._clipForChildren(context);
  121. for (var child of this._children) {
  122. if (child.isVisible && !child.notRenderable) {
  123. child._draw(this._measureForChildren, context);
  124. if (child.onAfterDrawObservable.hasObservers()) {
  125. child.onAfterDrawObservable.notifyObservers(child);
  126. }
  127. }
  128. }
  129. }
  130. context.restore();
  131. if (this.onAfterDrawObservable.hasObservers()) {
  132. this.onAfterDrawObservable.notifyObservers(this);
  133. }
  134. }
  135. public _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean {
  136. if (!this.isVisible || this.notRenderable) {
  137. return false;
  138. }
  139. if (!super.contains(x, y)) {
  140. return false;
  141. }
  142. // Checking backwards to pick closest first
  143. for (var index = this._children.length - 1; index >= 0; index--) {
  144. var child = this._children[index];
  145. if (child._processPicking(x, y, type, buttonIndex)) {
  146. return true;
  147. }
  148. }
  149. if (!this.isHitTestVisible) {
  150. return false;
  151. }
  152. return this._processObservables(type, x, y, buttonIndex);
  153. }
  154. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  155. // DO nothing
  156. }
  157. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  158. super._additionalProcessing(parentMeasure, context);
  159. this._measureForChildren.copyFrom(this._currentMeasure);
  160. }
  161. public dispose() {
  162. super.dispose();
  163. for (var control of this._children) {
  164. control.dispose();
  165. }
  166. }
  167. }
  168. }