grid.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import { Nullable } from "babylonjs/types";
  2. import { Container } from "./container";
  3. import { ValueAndUnit } from "../valueAndUnit";
  4. import { Control } from "./control";
  5. import { Measure } from "../measure";
  6. import { Tools } from 'babylonjs/Misc/tools';
  7. /**
  8. * Class used to create a 2D grid container
  9. */
  10. export class Grid extends Container {
  11. private _rowDefinitions = new Array<ValueAndUnit>();
  12. private _columnDefinitions = new Array<ValueAndUnit>();
  13. private _cells: { [key: string]: Container } = {};
  14. private _childControls = new Array<Control>();
  15. /**
  16. * Gets the number of columns
  17. */
  18. public get columnCount(): number {
  19. return this._columnDefinitions.length;
  20. }
  21. /**
  22. * Gets the number of rows
  23. */
  24. public get rowCount(): number {
  25. return this._rowDefinitions.length;
  26. }
  27. /** Gets the list of children */
  28. public get children(): Control[] {
  29. return this._childControls;
  30. }
  31. /** Gets the list of cells (e.g. the containers) */
  32. public get cells(): { [key: string]: Container } {
  33. return this._cells;
  34. }
  35. /**
  36. * Gets the definition of a specific row
  37. * @param index defines the index of the row
  38. * @returns the row definition
  39. */
  40. public getRowDefinition(index: number): Nullable<ValueAndUnit> {
  41. if (index < 0 || index >= this._rowDefinitions.length) {
  42. return null;
  43. }
  44. return this._rowDefinitions[index];
  45. }
  46. /**
  47. * Gets the definition of a specific column
  48. * @param index defines the index of the column
  49. * @returns the column definition
  50. */
  51. public getColumnDefinition(index: number): Nullable<ValueAndUnit> {
  52. if (index < 0 || index >= this._columnDefinitions.length) {
  53. return null;
  54. }
  55. return this._columnDefinitions[index];
  56. }
  57. /**
  58. * Adds a new row to the grid
  59. * @param height defines the height of the row (either in pixel or a value between 0 and 1)
  60. * @param isPixel defines if the height is expressed in pixel (or in percentage)
  61. * @returns the current grid
  62. */
  63. public addRowDefinition(height: number, isPixel = false): Grid {
  64. this._rowDefinitions.push(new ValueAndUnit(height, isPixel ? ValueAndUnit.UNITMODE_PIXEL : ValueAndUnit.UNITMODE_PERCENTAGE));
  65. this._markAsDirty();
  66. return this;
  67. }
  68. /**
  69. * Adds a new column to the grid
  70. * @param width defines the width of the column (either in pixel or a value between 0 and 1)
  71. * @param isPixel defines if the width is expressed in pixel (or in percentage)
  72. * @returns the current grid
  73. */
  74. public addColumnDefinition(width: number, isPixel = false): Grid {
  75. this._columnDefinitions.push(new ValueAndUnit(width, isPixel ? ValueAndUnit.UNITMODE_PIXEL : ValueAndUnit.UNITMODE_PERCENTAGE));
  76. this._markAsDirty();
  77. return this;
  78. }
  79. /**
  80. * Update a row definition
  81. * @param index defines the index of the row to update
  82. * @param height defines the height of the row (either in pixel or a value between 0 and 1)
  83. * @param isPixel defines if the weight is expressed in pixel (or in percentage)
  84. * @returns the current grid
  85. */
  86. public setRowDefinition(index: number, height: number, isPixel = false): Grid {
  87. if (index < 0 || index >= this._rowDefinitions.length) {
  88. return this;
  89. }
  90. let current = this._rowDefinitions[index];
  91. if (current && current.isPixel === isPixel && current.internalValue === height) {
  92. return this;
  93. }
  94. this._rowDefinitions[index] = new ValueAndUnit(height, isPixel ? ValueAndUnit.UNITMODE_PIXEL : ValueAndUnit.UNITMODE_PERCENTAGE);
  95. this._markAsDirty();
  96. return this;
  97. }
  98. /**
  99. * Update a column definition
  100. * @param index defines the index of the column to update
  101. * @param width defines the width of the column (either in pixel or a value between 0 and 1)
  102. * @param isPixel defines if the width is expressed in pixel (or in percentage)
  103. * @returns the current grid
  104. */
  105. public setColumnDefinition(index: number, width: number, isPixel = false): Grid {
  106. if (index < 0 || index >= this._columnDefinitions.length) {
  107. return this;
  108. }
  109. let current = this._columnDefinitions[index];
  110. if (current && current.isPixel === isPixel && current.internalValue === width) {
  111. return this;
  112. }
  113. this._columnDefinitions[index] = new ValueAndUnit(width, isPixel ? ValueAndUnit.UNITMODE_PIXEL : ValueAndUnit.UNITMODE_PERCENTAGE);
  114. this._markAsDirty();
  115. return this;
  116. }
  117. /**
  118. * Gets the list of children stored in a specific cell
  119. * @param row defines the row to check
  120. * @param column defines the column to check
  121. * @returns the list of controls
  122. */
  123. public getChildrenAt(row: number, column: number): Nullable<Array<Control>> {
  124. const cell = this._cells[`${row}:${column}`];
  125. if (!cell) {
  126. return null;
  127. }
  128. return cell.children;
  129. }
  130. /**
  131. * Gets a string representing the child cell info (row x column)
  132. * @param child defines the control to get info from
  133. * @returns a string containing the child cell info (row x column)
  134. */
  135. public getChildCellInfo(child: Control): string {
  136. return child._tag;
  137. }
  138. private _removeCell(cell: Container, key: string) {
  139. if (!cell) {
  140. return;
  141. }
  142. super.removeControl(cell);
  143. for (var control of cell.children) {
  144. let childIndex = this._childControls.indexOf(control);
  145. if (childIndex !== -1) {
  146. this._childControls.splice(childIndex, 1);
  147. }
  148. }
  149. delete this._cells[key];
  150. }
  151. private _offsetCell(previousKey: string, key: string) {
  152. if (!this._cells[key]) {
  153. return;
  154. }
  155. this._cells[previousKey] = this._cells[key];
  156. for (var control of this._cells[previousKey].children) {
  157. control._tag = previousKey;
  158. }
  159. delete this._cells[key];
  160. }
  161. /**
  162. * Remove a column definition at specified index
  163. * @param index defines the index of the column to remove
  164. * @returns the current grid
  165. */
  166. public removeColumnDefinition(index: number): Grid {
  167. if (index < 0 || index >= this._columnDefinitions.length) {
  168. return this;
  169. }
  170. for (var x = 0; x < this._rowDefinitions.length; x++) {
  171. let key = `${x}:${index}`;
  172. let cell = this._cells[key];
  173. this._removeCell(cell, key);
  174. }
  175. for (var x = 0; x < this._rowDefinitions.length; x++) {
  176. for (var y = index + 1; y < this._columnDefinitions.length; y++) {
  177. let previousKey = `${x}:${y - 1}`;
  178. let key = `${x}:${y}`;
  179. this._offsetCell(previousKey, key);
  180. }
  181. }
  182. this._columnDefinitions.splice(index, 1);
  183. this._markAsDirty();
  184. return this;
  185. }
  186. /**
  187. * Remove a row definition at specified index
  188. * @param index defines the index of the row to remove
  189. * @returns the current grid
  190. */
  191. public removeRowDefinition(index: number): Grid {
  192. if (index < 0 || index >= this._rowDefinitions.length) {
  193. return this;
  194. }
  195. for (var y = 0; y < this._columnDefinitions.length; y++) {
  196. let key = `${index}:${y}`;
  197. let cell = this._cells[key];
  198. this._removeCell(cell, key);
  199. }
  200. for (var y = 0; y < this._columnDefinitions.length; y++) {
  201. for (var x = index + 1; x < this._rowDefinitions.length; x++) {
  202. let previousKey = `${x - 1}:${y}`;
  203. let key = `${x}:${y}`;
  204. this._offsetCell(previousKey, key);
  205. }
  206. }
  207. this._rowDefinitions.splice(index, 1);
  208. this._markAsDirty();
  209. return this;
  210. }
  211. /**
  212. * Adds a new control to the current grid
  213. * @param control defines the control to add
  214. * @param row defines the row where to add the control (0 by default)
  215. * @param column defines the column where to add the control (0 by default)
  216. * @returns the current grid
  217. */
  218. public addControl(control: Control, row: number = 0, column: number = 0): Grid {
  219. if (this._rowDefinitions.length === 0) {
  220. // Add default row definition
  221. this.addRowDefinition(1, false);
  222. }
  223. if (this._columnDefinitions.length === 0) {
  224. // Add default column definition
  225. this.addColumnDefinition(1, false);
  226. }
  227. if (this._childControls.indexOf(control) !== -1) {
  228. Tools.Warn(`Control (Name:${control.name}, UniqueId:${control.uniqueId}) is already associated with this grid. You must remove it before reattaching it`);
  229. return this;
  230. }
  231. let x = Math.min(row, this._rowDefinitions.length - 1);
  232. let y = Math.min(column, this._columnDefinitions.length - 1);
  233. let key = `${x}:${y}`;
  234. let goodContainer = this._cells[key];
  235. if (!goodContainer) {
  236. goodContainer = new Container(key);
  237. this._cells[key] = goodContainer;
  238. goodContainer.horizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  239. goodContainer.verticalAlignment = Control.VERTICAL_ALIGNMENT_TOP;
  240. super.addControl(goodContainer);
  241. }
  242. goodContainer.addControl(control);
  243. this._childControls.push(control);
  244. control._tag = key;
  245. control.parent = this;
  246. this._markAsDirty();
  247. return this;
  248. }
  249. /**
  250. * Removes a control from the current container
  251. * @param control defines the control to remove
  252. * @returns the current container
  253. */
  254. public removeControl(control: Control): Container {
  255. var index = this._childControls.indexOf(control);
  256. if (index !== -1) {
  257. this._childControls.splice(index, 1);
  258. }
  259. let cell = this._cells[control._tag];
  260. if (cell) {
  261. cell.removeControl(control);
  262. control._tag = null;
  263. }
  264. this._markAsDirty();
  265. return this;
  266. }
  267. /**
  268. * Creates a new Grid
  269. * @param name defines control name
  270. */
  271. constructor(public name?: string) {
  272. super(name);
  273. }
  274. protected _getTypeName(): string {
  275. return "Grid";
  276. }
  277. protected _getGridDefinitions(definitionCallback: (lefts: number[], tops: number[], widths: number[], heights: number[]) => void) {
  278. let widths = [];
  279. let heights = [];
  280. let lefts = [];
  281. let tops = [];
  282. let availableWidth = this._currentMeasure.width;
  283. let globalWidthPercentage = 0;
  284. let availableHeight = this._currentMeasure.height;
  285. let globalHeightPercentage = 0;
  286. // Heights
  287. let index = 0;
  288. for (var value of this._rowDefinitions) {
  289. if (value.isPixel) {
  290. let height = value.getValue(this._host);
  291. availableHeight -= height;
  292. heights[index] = height;
  293. } else {
  294. globalHeightPercentage += value.internalValue;
  295. }
  296. index++;
  297. }
  298. let top = 0;
  299. index = 0;
  300. for (var value of this._rowDefinitions) {
  301. tops.push(top);
  302. if (!value.isPixel) {
  303. let height = (value.internalValue / globalHeightPercentage) * availableHeight;
  304. top += height;
  305. heights[index] = height;
  306. } else {
  307. top += value.getValue(this._host);
  308. }
  309. index++;
  310. }
  311. // Widths
  312. index = 0;
  313. for (var value of this._columnDefinitions) {
  314. if (value.isPixel) {
  315. let width = value.getValue(this._host);
  316. availableWidth -= width;
  317. widths[index] = width;
  318. } else {
  319. globalWidthPercentage += value.internalValue;
  320. }
  321. index++;
  322. }
  323. let left = 0;
  324. index = 0;
  325. for (var value of this._columnDefinitions) {
  326. lefts.push(left);
  327. if (!value.isPixel) {
  328. let width = (value.internalValue / globalWidthPercentage) * availableWidth;
  329. left += width;
  330. widths[index] = width;
  331. } else {
  332. left += value.getValue(this._host);
  333. }
  334. index++;
  335. }
  336. definitionCallback(lefts, tops, widths, heights);
  337. }
  338. protected _additionalProcessing(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  339. this._getGridDefinitions((lefts: number[], tops: number[], widths: number[], heights: number[]) => {
  340. // Setting child sizes
  341. for (var key in this._cells) {
  342. if (!this._cells.hasOwnProperty(key)) {
  343. continue;
  344. }
  345. let split = key.split(":");
  346. let x = parseInt(split[0]);
  347. let y = parseInt(split[1]);
  348. let cell = this._cells[key];
  349. cell.left = lefts[y] + "px";
  350. cell.top = tops[x] + "px";
  351. cell.width = widths[y] + "px";
  352. cell.height = heights[x] + "px";
  353. cell._left.ignoreAdaptiveScaling = true;
  354. cell._top.ignoreAdaptiveScaling = true;
  355. cell._width.ignoreAdaptiveScaling = true;
  356. cell._height.ignoreAdaptiveScaling = true;
  357. }
  358. });
  359. super._additionalProcessing(parentMeasure, context);
  360. }
  361. public _flagDescendantsAsMatrixDirty(): void {
  362. for (var key in this._cells) {
  363. if (!this._cells.hasOwnProperty(key)) {
  364. continue;
  365. }
  366. let child = this._cells[key];
  367. child._markMatrixAsDirty();
  368. }
  369. }
  370. public _renderHighlightSpecific(context: CanvasRenderingContext2D): void {
  371. super._renderHighlightSpecific(context);
  372. this._getGridDefinitions((lefts: number[], tops: number[], widths: number[], heights: number[]) => {
  373. // Columns
  374. for (var index = 0; index < lefts.length; index++) {
  375. const left = this._currentMeasure.left + lefts[index] + widths[index];
  376. context.beginPath();
  377. context.moveTo(left, this._currentMeasure.top);
  378. context.lineTo(left, this._currentMeasure.top + this._currentMeasure.height);
  379. context.stroke();
  380. }
  381. // Rows
  382. for (var index = 0; index < tops.length; index++) {
  383. const top = this._currentMeasure.top + tops[index] + heights[index];
  384. context.beginPath();
  385. context.moveTo(this._currentMeasure.left, top);
  386. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, top);
  387. context.stroke();
  388. }
  389. });
  390. context.restore();
  391. }
  392. /** Releases associated resources */
  393. public dispose() {
  394. super.dispose();
  395. for (var control of this._childControls) {
  396. control.dispose();
  397. }
  398. this._childControls = [];
  399. }
  400. }