grid.ts 15 KB

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