container.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 = "";
  16. /** @hidden */
  17. protected _adaptWidthToChildren = false;
  18. /** @hidden */
  19. protected _adaptHeightToChildren = false;
  20. /** @hidden */
  21. protected _rebuildLayout = false;
  22. /** Gets or sets a boolean indicating if the container should try to adapt to its children height */
  23. public get adaptHeightToChildren(): boolean {
  24. return this._adaptHeightToChildren;
  25. }
  26. public set adaptHeightToChildren(value: boolean) {
  27. if (this._adaptHeightToChildren === value) {
  28. return;
  29. }
  30. this._adaptHeightToChildren = value;
  31. if (value) {
  32. this.height = "100%";
  33. }
  34. this._markAsDirty();
  35. }
  36. /** Gets or sets a boolean indicating if the container should try to adapt to its children width */
  37. public get adaptWidthToChildren(): boolean {
  38. return this._adaptWidthToChildren;
  39. }
  40. public set adaptWidthToChildren(value: boolean) {
  41. if (this._adaptWidthToChildren === value) {
  42. return;
  43. }
  44. this._adaptWidthToChildren = value;
  45. if (value) {
  46. this.width = "100%";
  47. }
  48. this._markAsDirty();
  49. }
  50. /** Gets or sets background color */
  51. public get background(): string {
  52. return this._background;
  53. }
  54. public set background(value: string) {
  55. if (this._background === value) {
  56. return;
  57. }
  58. this._background = value;
  59. this._markAsDirty();
  60. }
  61. /** Gets the list of children */
  62. public get children(): Control[] {
  63. return this._children;
  64. }
  65. /**
  66. * Creates a new Container
  67. * @param name defines the name of the container
  68. */
  69. constructor(public name?: string) {
  70. super(name);
  71. }
  72. protected _getTypeName(): string {
  73. return "Container";
  74. }
  75. public _flagDescendantsAsMatrixDirty(): void {
  76. for (var child of this.children) {
  77. child._markMatrixAsDirty();
  78. }
  79. }
  80. /**
  81. * Gets a child using its name
  82. * @param name defines the child name to look for
  83. * @returns the child control if found
  84. */
  85. public getChildByName(name: string): Nullable<Control> {
  86. for (var child of this.children) {
  87. if (child.name === name) {
  88. return child;
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * Gets a child using its type and its name
  95. * @param name defines the child name to look for
  96. * @param type defines the child type to look for
  97. * @returns the child control if found
  98. */
  99. public getChildByType(name: string, type: string): Nullable<Control> {
  100. for (var child of this.children) {
  101. if (child.typeName === type) {
  102. return child;
  103. }
  104. }
  105. return null;
  106. }
  107. /**
  108. * Search for a specific control in children
  109. * @param control defines the control to look for
  110. * @returns true if the control is in child list
  111. */
  112. public containsControl(control: Control): boolean {
  113. return this.children.indexOf(control) !== -1;
  114. }
  115. /**
  116. * Adds a new control to the current container
  117. * @param control defines the control to add
  118. * @returns the current container
  119. */
  120. public addControl(control: Nullable<Control>): Container {
  121. if (!control) {
  122. return this;
  123. }
  124. var index = this._children.indexOf(control);
  125. if (index !== -1) {
  126. return this;
  127. }
  128. control._link(this._host);
  129. control._markAllAsDirty();
  130. this._reOrderControl(control);
  131. this._markAsDirty();
  132. return this;
  133. }
  134. /**
  135. * Removes all controls from the current container
  136. * @returns the current container
  137. */
  138. public clearControls(): Container {
  139. let children = this.children.slice();
  140. for (var child of children) {
  141. this.removeControl(child);
  142. }
  143. return this;
  144. }
  145. /**
  146. * Removes a control from the current container
  147. * @param control defines the control to remove
  148. * @returns the current container
  149. */
  150. public removeControl(control: Control): Container {
  151. var index = this._children.indexOf(control);
  152. if (index !== -1) {
  153. this._children.splice(index, 1);
  154. control.parent = null;
  155. }
  156. control.linkWithMesh(null);
  157. if (this._host) {
  158. this._host._cleanControlAfterRemoval(control);
  159. }
  160. this._markAsDirty();
  161. return this;
  162. }
  163. /** @hidden */
  164. public _reOrderControl(control: Control): void {
  165. this.removeControl(control);
  166. for (var index = 0; index < this._children.length; index++) {
  167. if (this._children[index].zIndex > control.zIndex) {
  168. this._children.splice(index, 0, control);
  169. return;
  170. }
  171. }
  172. this._children.push(control);
  173. control.parent = this;
  174. this._markAsDirty();
  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(host: AdvancedDynamicTexture): void {
  203. super._link(host);
  204. for (var child of this._children) {
  205. child._link(host);
  206. }
  207. }
  208. /** @hidden */
  209. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  210. if (!this.isVisible || this.notRenderable) {
  211. return false;
  212. }
  213. let rebuildCount = 0;
  214. context.save();
  215. this._applyStates(context);
  216. do
  217. {
  218. let computedWidth = -1;
  219. let computedHeight = -1;
  220. this._rebuildLayout = false;
  221. this._processMeasures(parentMeasure, context);
  222. if (!this._isClipped) {
  223. for (var child of this._children) {
  224. child._tempParentMeasure.copyFrom(this._measureForChildren);
  225. if (child._layout(this._measureForChildren, context)) {
  226. if (this.adaptWidthToChildren && child._width.isPixel) {
  227. computedWidth = Math.max(computedWidth, child._currentMeasure.width);
  228. }
  229. if (this.adaptHeightToChildren && child._height.isPixel) {
  230. computedHeight = Math.max(computedHeight, child._currentMeasure.height);
  231. }
  232. }
  233. }
  234. if (this.adaptWidthToChildren && computedWidth >= 0) {
  235. if (this.width !== computedWidth + "px") {
  236. this.width = computedWidth + "px";
  237. this._rebuildLayout = true;
  238. }
  239. }
  240. if (this.adaptHeightToChildren && computedHeight >= 0) {
  241. if (this.height !== computedHeight + "px") {
  242. this.height = computedHeight + "px";
  243. this._rebuildLayout = true;
  244. }
  245. }
  246. this._postMeasure();
  247. }
  248. rebuildCount++;
  249. }
  250. while(this._rebuildLayout && rebuildCount < 3);
  251. if (rebuildCount > 3) {
  252. BABYLON.Tools.Error(`Layout cycle detected in GUI (Container uniqueId=${this.uniqueId})`);
  253. }
  254. context.restore();
  255. return true;
  256. }
  257. protected _postMeasure() {
  258. // Do nothing by default
  259. }
  260. /** @hidden */
  261. public _draw(context: CanvasRenderingContext2D): void {
  262. this._localDraw(context);
  263. for (var child of this._children) {
  264. child._render(context);
  265. }
  266. }
  267. /** @hidden */
  268. public _getDescendants(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  269. if (!this.children) {
  270. return;
  271. }
  272. for (var index = 0; index < this.children.length; index++) {
  273. var item = this.children[index];
  274. if (!predicate || predicate(item)) {
  275. results.push(item);
  276. }
  277. if (!directDescendantsOnly) {
  278. item._getDescendants(results, false, predicate);
  279. }
  280. }
  281. }
  282. /** @hidden */
  283. public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
  284. if (!this.isVisible || this.notRenderable) {
  285. return false;
  286. }
  287. if (!super.contains(x, y)) {
  288. return false;
  289. }
  290. // Checking backwards to pick closest first
  291. for (var index = this._children.length - 1; index >= 0; index--) {
  292. var child = this._children[index];
  293. if (child._processPicking(x, y, type, pointerId, buttonIndex)) {
  294. if (child.hoverCursor) {
  295. this._host._changeCursor(child.hoverCursor);
  296. }
  297. return true;
  298. }
  299. }
  300. if (!this.isHitTestVisible) {
  301. return false;
  302. }
  303. return this._processObservables(type, x, y, pointerId, buttonIndex);
  304. }
  305. /** @hidden */
  306. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  307. super._additionalProcessing(parentMeasure, context);
  308. this._measureForChildren.copyFrom(this._currentMeasure);
  309. }
  310. /** Releases associated resources */
  311. public dispose() {
  312. super.dispose();
  313. for (var control of this._children) {
  314. control.dispose();
  315. }
  316. }
  317. }