container.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. import { Control } from "./control";
  2. import { Measure } from "../measure";
  3. import { Nullable } from "babylonjs";
  4. import { AdvancedDynamicTexture } from "../advancedDynamicTexture";
  5. /**
  6. * Root class for 2D containers
  7. * @see http://doc.babylonjs.com/how_to/gui#containers
  8. */
  9. export class Container extends Control {
  10. /** @hidden */
  11. protected _children = new Array<Control>();
  12. /** @hidden */
  13. protected _measureForChildren = Measure.Empty();
  14. /** @hidden */
  15. protected _background: string;
  16. /** @hidden */
  17. protected _adaptWidthToChildren = false;
  18. /** @hidden */
  19. protected _adaptHeightToChildren = false;
  20. /** Gets or sets a boolean indicating if the container should try to adapt to its children height */
  21. public get adaptHeightToChildren(): boolean {
  22. return this._adaptHeightToChildren;
  23. }
  24. public set adaptHeightToChildren(value: boolean) {
  25. if (this._adaptHeightToChildren === value) {
  26. return;
  27. }
  28. this._adaptHeightToChildren = value;
  29. if (value) {
  30. this.height = "100%";
  31. }
  32. this._markAsDirty();
  33. }
  34. /** Gets or sets a boolean indicating if the container should try to adapt to its children width */
  35. public get adaptWidthToChildren(): boolean {
  36. return this._adaptWidthToChildren;
  37. }
  38. public set adaptWidthToChildren(value: boolean) {
  39. if (this._adaptWidthToChildren === value) {
  40. return;
  41. }
  42. this._adaptWidthToChildren = value;
  43. if (value) {
  44. this.width = "100%";
  45. }
  46. this._markAsDirty();
  47. }
  48. /** Gets or sets background color */
  49. public get background(): string {
  50. return this._background;
  51. }
  52. public set background(value: string) {
  53. if (this._background === value) {
  54. return;
  55. }
  56. this._background = value;
  57. this._markAsDirty();
  58. }
  59. /** Gets the list of children */
  60. public get children(): Control[] {
  61. return this._children;
  62. }
  63. /**
  64. * Creates a new Container
  65. * @param name defines the name of the container
  66. */
  67. constructor(public name?: string) {
  68. super(name);
  69. }
  70. protected _getTypeName(): string {
  71. return "Container";
  72. }
  73. /**
  74. * Gets a child using its name
  75. * @param name defines the child name to look for
  76. * @returns the child control if found
  77. */
  78. public getChildByName(name: string): Nullable<Control> {
  79. for (var child of this.children) {
  80. if (child.name === name) {
  81. return child;
  82. }
  83. }
  84. return null;
  85. }
  86. /**
  87. * Gets a child using its type and its name
  88. * @param name defines the child name to look for
  89. * @param type defines the child type to look for
  90. * @returns the child control if found
  91. */
  92. public getChildByType(name: string, type: string): Nullable<Control> {
  93. for (var child of this.children) {
  94. if (child.typeName === type) {
  95. return child;
  96. }
  97. }
  98. return null;
  99. }
  100. /**
  101. * Search for a specific control in children
  102. * @param control defines the control to look for
  103. * @returns true if the control is in child list
  104. */
  105. public containsControl(control: Control): boolean {
  106. return this.children.indexOf(control) !== -1;
  107. }
  108. /**
  109. * Adds a new control to the current container
  110. * @param control defines the control to add
  111. * @returns the current container
  112. */
  113. public addControl(control: Nullable<Control>): Container {
  114. if (!control) {
  115. return this;
  116. }
  117. var index = this._children.indexOf(control);
  118. if (index !== -1) {
  119. return this;
  120. }
  121. control._link(this, this._host);
  122. control._markAllAsDirty();
  123. this._reOrderControl(control);
  124. this._markAsDirty();
  125. return this;
  126. }
  127. /**
  128. * Removes all controls from the current container
  129. * @returns the current container
  130. */
  131. public clearControls(): Container {
  132. let children = this._children.slice();
  133. for (var child of children) {
  134. this.removeControl(child);
  135. }
  136. return this;
  137. }
  138. /**
  139. * Removes a control from the current container
  140. * @param control defines the control to remove
  141. * @returns the current container
  142. */
  143. public removeControl(control: Control): Container {
  144. var index = this._children.indexOf(control);
  145. if (index !== -1) {
  146. this._children.splice(index, 1);
  147. control.parent = null;
  148. }
  149. control.linkWithMesh(null);
  150. if (this._host) {
  151. this._host._cleanControlAfterRemoval(control);
  152. }
  153. this._markAsDirty();
  154. return this;
  155. }
  156. /** @hidden */
  157. public _reOrderControl(control: Control): void {
  158. this.removeControl(control);
  159. for (var index = 0; index < this._children.length; index++) {
  160. if (this._children[index].zIndex > control.zIndex) {
  161. this._children.splice(index, 0, control);
  162. return;
  163. }
  164. }
  165. this._children.push(control);
  166. control.parent = this;
  167. this._markAsDirty();
  168. }
  169. /** @hidden */
  170. public _markMatrixAsDirty(): void {
  171. super._markMatrixAsDirty();
  172. for (var index = 0; index < this._children.length; index++) {
  173. this._children[index]._markMatrixAsDirty();
  174. }
  175. }
  176. /** @hidden */
  177. public _markAllAsDirty(): void {
  178. super._markAllAsDirty();
  179. for (var index = 0; index < this._children.length; index++) {
  180. this._children[index]._markAllAsDirty();
  181. }
  182. }
  183. /** @hidden */
  184. protected _localDraw(context: CanvasRenderingContext2D): void {
  185. if (this._background) {
  186. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  187. context.shadowColor = this.shadowColor;
  188. context.shadowBlur = this.shadowBlur;
  189. context.shadowOffsetX = this.shadowOffsetX;
  190. context.shadowOffsetY = this.shadowOffsetY;
  191. }
  192. context.fillStyle = this._background;
  193. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  194. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  195. context.shadowBlur = 0;
  196. context.shadowOffsetX = 0;
  197. context.shadowOffsetY = 0;
  198. }
  199. }
  200. }
  201. /** @hidden */
  202. public _link(root: Nullable<Container>, host: AdvancedDynamicTexture): void {
  203. super._link(root, host);
  204. for (var child of this._children) {
  205. child._link(root, host);
  206. }
  207. }
  208. /** @hidden */
  209. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  210. if (!this.isVisible || this.notRenderable) {
  211. return;
  212. }
  213. context.save();
  214. this._applyStates(context);
  215. if (this._processMeasures(parentMeasure, context)) {
  216. this._localDraw(context);
  217. this._clipForChildren(context);
  218. let computedWidth = -1;
  219. let computedHeight = -1;
  220. for (var child of this._children) {
  221. if (child.isVisible && !child.notRenderable) {
  222. child._tempParentMeasure.copyFrom(this._measureForChildren);
  223. child._draw(this._measureForChildren, context);
  224. if (child.onAfterDrawObservable.hasObservers()) {
  225. child.onAfterDrawObservable.notifyObservers(child);
  226. }
  227. if (this.adaptWidthToChildren && child._width.isPixel) {
  228. computedWidth = Math.max(computedWidth, child._currentMeasure.width);
  229. }
  230. if (this.adaptHeightToChildren && child._height.isPixel) {
  231. computedHeight = Math.max(computedHeight, child._currentMeasure.height);
  232. }
  233. }
  234. }
  235. if (this.adaptWidthToChildren && computedWidth >= 0) {
  236. this.width = computedWidth + "px";
  237. }
  238. if (this.adaptHeightToChildren && computedHeight >= 0) {
  239. this.height = computedHeight + "px";
  240. }
  241. }
  242. context.restore();
  243. if (this.onAfterDrawObservable.hasObservers()) {
  244. this.onAfterDrawObservable.notifyObservers(this);
  245. }
  246. }
  247. /** @hidden */
  248. public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
  249. if (!this.isVisible || this.notRenderable) {
  250. return false;
  251. }
  252. if (!super.contains(x, y)) {
  253. return false;
  254. }
  255. // Checking backwards to pick closest first
  256. for (var index = this._children.length - 1; index >= 0; index--) {
  257. var child = this._children[index];
  258. if (child._processPicking(x, y, type, pointerId, buttonIndex)) {
  259. if (child.hoverCursor) {
  260. this._host._changeCursor(child.hoverCursor);
  261. }
  262. return true;
  263. }
  264. }
  265. if (!this.isHitTestVisible) {
  266. return false;
  267. }
  268. return this._processObservables(type, x, y, pointerId, buttonIndex);
  269. }
  270. /** @hidden */
  271. protected _clipForChildren(context: CanvasRenderingContext2D): void {
  272. // DO nothing
  273. }
  274. /** @hidden */
  275. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  276. super._additionalProcessing(parentMeasure, context);
  277. this._measureForChildren.copyFrom(this._currentMeasure);
  278. }
  279. /** Releases associated resources */
  280. public dispose() {
  281. super.dispose();
  282. for (var control of this._children) {
  283. control.dispose();
  284. }
  285. }
  286. }