container.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. public _flagDescendantsAsMatrixDirty(): void {
  74. for (var child of this.children) {
  75. child._markMatrixAsDirty();
  76. }
  77. }
  78. /**
  79. * Gets a child using its name
  80. * @param name defines the child name to look for
  81. * @returns the child control if found
  82. */
  83. public getChildByName(name: string): Nullable<Control> {
  84. for (var child of this.children) {
  85. if (child.name === name) {
  86. return child;
  87. }
  88. }
  89. return null;
  90. }
  91. /**
  92. * Gets a child using its type and its name
  93. * @param name defines the child name to look for
  94. * @param type defines the child type to look for
  95. * @returns the child control if found
  96. */
  97. public getChildByType(name: string, type: string): Nullable<Control> {
  98. for (var child of this.children) {
  99. if (child.typeName === type) {
  100. return child;
  101. }
  102. }
  103. return null;
  104. }
  105. /**
  106. * Search for a specific control in children
  107. * @param control defines the control to look for
  108. * @returns true if the control is in child list
  109. */
  110. public containsControl(control: Control): boolean {
  111. return this.children.indexOf(control) !== -1;
  112. }
  113. /**
  114. * Adds a new control to the current container
  115. * @param control defines the control to add
  116. * @returns the current container
  117. */
  118. public addControl(control: Nullable<Control>): Container {
  119. if (!control) {
  120. return this;
  121. }
  122. var index = this._children.indexOf(control);
  123. if (index !== -1) {
  124. return this;
  125. }
  126. control._link(this, this._host);
  127. control._markAllAsDirty();
  128. this._reOrderControl(control);
  129. this._markAsDirty();
  130. return this;
  131. }
  132. /**
  133. * Removes all controls from the current container
  134. * @returns the current container
  135. */
  136. public clearControls(): Container {
  137. let children = this._children.slice();
  138. for (var child of children) {
  139. this.removeControl(child);
  140. }
  141. return this;
  142. }
  143. /**
  144. * Removes a control from the current container
  145. * @param control defines the control to remove
  146. * @returns the current container
  147. */
  148. public removeControl(control: Control): Container {
  149. var index = this._children.indexOf(control);
  150. if (index !== -1) {
  151. this._children.splice(index, 1);
  152. control.parent = null;
  153. }
  154. control.linkWithMesh(null);
  155. if (this._host) {
  156. this._host._cleanControlAfterRemoval(control);
  157. }
  158. this._markAsDirty();
  159. return this;
  160. }
  161. /** @hidden */
  162. public _reOrderControl(control: Control): void {
  163. this.removeControl(control);
  164. for (var index = 0; index < this._children.length; index++) {
  165. if (this._children[index].zIndex > control.zIndex) {
  166. this._children.splice(index, 0, control);
  167. return;
  168. }
  169. }
  170. this._children.push(control);
  171. control.parent = this;
  172. this._markAsDirty();
  173. }
  174. /** @hidden */
  175. public _markAllAsDirty(): void {
  176. super._markAllAsDirty();
  177. for (var index = 0; index < this._children.length; index++) {
  178. this._children[index]._markAllAsDirty();
  179. }
  180. }
  181. /** @hidden */
  182. protected _localDraw(context: CanvasRenderingContext2D): void {
  183. if (this._background) {
  184. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  185. context.shadowColor = this.shadowColor;
  186. context.shadowBlur = this.shadowBlur;
  187. context.shadowOffsetX = this.shadowOffsetX;
  188. context.shadowOffsetY = this.shadowOffsetY;
  189. }
  190. context.fillStyle = this._background;
  191. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  192. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  193. context.shadowBlur = 0;
  194. context.shadowOffsetX = 0;
  195. context.shadowOffsetY = 0;
  196. }
  197. }
  198. }
  199. /** @hidden */
  200. public _link(root: Nullable<Container>, host: AdvancedDynamicTexture): void {
  201. super._link(root, host);
  202. for (var child of this._children) {
  203. child._link(this, host);
  204. }
  205. }
  206. /** @hidden */
  207. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  208. if (!this.isVisible || this.notRenderable) {
  209. return;
  210. }
  211. context.save();
  212. this._applyStates(context);
  213. if (this._processMeasures(parentMeasure, context)) {
  214. this._localDraw(context);
  215. if (this.clipChildren) {
  216. this._clipForChildren(context);
  217. }
  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. }