container.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. /** 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._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 _offsetLeft(offset: number) {
  176. super._offsetLeft(offset);
  177. for (var child of this._children) {
  178. child._offsetLeft(offset);
  179. }
  180. }
  181. /** @hidden */
  182. public _offsetTop(offset: number) {
  183. super._offsetTop(offset);
  184. for (var child of this._children) {
  185. child._offsetTop(offset);
  186. }
  187. }
  188. /** @hidden */
  189. public _markAllAsDirty(): void {
  190. super._markAllAsDirty();
  191. for (var index = 0; index < this._children.length; index++) {
  192. this._children[index]._markAllAsDirty();
  193. }
  194. }
  195. /** @hidden */
  196. protected _localDraw(context: CanvasRenderingContext2D): void {
  197. if (this._background) {
  198. context.save();
  199. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  200. context.shadowColor = this.shadowColor;
  201. context.shadowBlur = this.shadowBlur;
  202. context.shadowOffsetX = this.shadowOffsetX;
  203. context.shadowOffsetY = this.shadowOffsetY;
  204. }
  205. context.fillStyle = this._background;
  206. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  207. context.restore();
  208. }
  209. }
  210. /** @hidden */
  211. public _link(host: AdvancedDynamicTexture): void {
  212. super._link(host);
  213. for (var child of this._children) {
  214. child._link(host);
  215. }
  216. }
  217. /** @hidden */
  218. protected _beforeLayout() {
  219. // Do nothing
  220. }
  221. /** @hidden */
  222. public _layout(parentMeasure: Measure, context: CanvasRenderingContext2D): boolean {
  223. if (!this.isVisible || this.notRenderable) {
  224. return false;
  225. }
  226. let rebuildCount = 0;
  227. context.save();
  228. this._applyStates(context);
  229. this._beforeLayout();
  230. do {
  231. let computedWidth = -1;
  232. let computedHeight = -1;
  233. this._rebuildLayout = false;
  234. this._processMeasures(parentMeasure, context);
  235. if (!this._isClipped) {
  236. for (var child of this._children) {
  237. child._tempParentMeasure.copyFrom(this._measureForChildren);
  238. if (child._layout(this._measureForChildren, context)) {
  239. if (this.adaptWidthToChildren && child._width.isPixel) {
  240. computedWidth = Math.max(computedWidth, child._currentMeasure.width);
  241. }
  242. if (this.adaptHeightToChildren && child._height.isPixel) {
  243. computedHeight = Math.max(computedHeight, child._currentMeasure.height);
  244. }
  245. }
  246. }
  247. if (this.adaptWidthToChildren && computedWidth >= 0) {
  248. if (this.width !== computedWidth + "px") {
  249. this.width = computedWidth + "px";
  250. this._rebuildLayout = true;
  251. }
  252. }
  253. if (this.adaptHeightToChildren && computedHeight >= 0) {
  254. if (this.height !== computedHeight + "px") {
  255. this.height = computedHeight + "px";
  256. this._rebuildLayout = true;
  257. }
  258. }
  259. this._postMeasure();
  260. }
  261. rebuildCount++;
  262. }
  263. while (this._rebuildLayout && rebuildCount < 3);
  264. if (rebuildCount >= 3) {
  265. BABYLON.Tools.Error(`Layout cycle detected in GUI (Container uniqueId=${this.uniqueId})`);
  266. }
  267. context.restore();
  268. this._isDirty = false;
  269. return true;
  270. }
  271. protected _postMeasure() {
  272. // Do nothing by default
  273. }
  274. /** @hidden */
  275. public _draw(context: CanvasRenderingContext2D): void {
  276. this._localDraw(context);
  277. if (this.clipChildren) {
  278. this._clipForChildren(context);
  279. }
  280. for (var child of this._children) {
  281. child._render(context);
  282. }
  283. }
  284. /** @hidden */
  285. public _getDescendants(results: Control[], directDescendantsOnly: boolean = false, predicate?: (control: Control) => boolean): void {
  286. if (!this.children) {
  287. return;
  288. }
  289. for (var index = 0; index < this.children.length; index++) {
  290. var item = this.children[index];
  291. if (!predicate || predicate(item)) {
  292. results.push(item);
  293. }
  294. if (!directDescendantsOnly) {
  295. item._getDescendants(results, false, predicate);
  296. }
  297. }
  298. }
  299. /** @hidden */
  300. public _processPicking(x: number, y: number, type: number, pointerId: number, buttonIndex: number): boolean {
  301. if (!this.isVisible || this.notRenderable) {
  302. return false;
  303. }
  304. if (!super.contains(x, y)) {
  305. return false;
  306. }
  307. // Checking backwards to pick closest first
  308. for (var index = this._children.length - 1; index >= 0; index--) {
  309. var child = this._children[index];
  310. if (child._processPicking(x, y, type, pointerId, buttonIndex)) {
  311. if (child.hoverCursor) {
  312. this._host._changeCursor(child.hoverCursor);
  313. }
  314. return true;
  315. }
  316. }
  317. if (!this.isHitTestVisible) {
  318. return false;
  319. }
  320. return this._processObservables(type, x, y, pointerId, buttonIndex);
  321. }
  322. /** @hidden */
  323. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  324. super._additionalProcessing(parentMeasure, context);
  325. this._measureForChildren.copyFrom(this._currentMeasure);
  326. }
  327. /** Releases associated resources */
  328. public dispose() {
  329. super.dispose();
  330. for (var control of this._children) {
  331. control.dispose();
  332. }
  333. }
  334. }