container.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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): 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): 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. }
  61. this._markAsDirty();
  62. return this;
  63. }
  64. public _reOrderControl(control: Control): void {
  65. this.removeControl(control);
  66. for (var index = 0; index < this._children.length; index++) {
  67. if (this._children[index].zIndex > control.zIndex) {
  68. this._children.splice(index, 0, control);
  69. return;
  70. }
  71. }
  72. this._children.push(control);
  73. this._markAsDirty();
  74. }
  75. public _markMatrixAsDirty(): void {
  76. super._markMatrixAsDirty();
  77. for (var index = 0; index < this._children.length; index++) {
  78. this._children[index]._markMatrixAsDirty();
  79. }
  80. }
  81. public _markAllAsDirty(): void {
  82. super._markAllAsDirty();
  83. for (var index = 0; index < this._children.length; index++) {
  84. this._children[index]._markAllAsDirty();
  85. }
  86. }
  87. protected _localDraw(context: CanvasRenderingContext2D): void {
  88. if (this._background) {
  89. context.fillStyle = this._background;
  90. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  91. }
  92. }
  93. public _link(root: Container, host: AdvancedDynamicTexture): void {
  94. super._link(root, host);
  95. for (var child of this._children) {
  96. child._link(root, host);
  97. }
  98. }
  99. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  100. if (!this.isVisible || this.notRenderable) {
  101. return;
  102. }
  103. context.save();
  104. this._applyStates(context);
  105. if (this._processMeasures(parentMeasure, context)) {
  106. this._localDraw(context);
  107. this._clipForChildren(context);
  108. for (var child of this._children) {
  109. if (child.isVisible && !child.notRenderable) {
  110. child._draw(this._measureForChildren, context);
  111. }
  112. }
  113. }
  114. context.restore();
  115. }
  116. public _processPicking(x: number, y: number, type: number, buttonIndex: number): boolean {
  117. if (!this.isHitTestVisible || !this.isVisible || this.notRenderable) {
  118. return false;
  119. }
  120. if (!super.contains(x, y)) {
  121. return false;
  122. }
  123. // Checking backwards to pick closest first
  124. for (var index = this._children.length - 1; index >= 0; index--) {
  125. var child = this._children[index];
  126. if (child._processPicking(x, y, type, buttonIndex)) {
  127. return true;
  128. }
  129. }
  130. return this._processObservables(type, x, y, buttonIndex);
  131. }
  132. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  133. // DO nothing
  134. }
  135. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  136. super._additionalProcessing(parentMeasure, context);
  137. this._measureForChildren.copyFrom(this._currentMeasure);
  138. }
  139. public dispose() {
  140. super.dispose();
  141. for (var control of this._children) {
  142. control.dispose();
  143. }
  144. }
  145. }
  146. }