container.ts 5.2 KB

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