displayGrid.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { Control } from ".";
  2. import { Measure } from "..";
  3. /** Class used to render a grid */
  4. export class DisplayGrid extends Control {
  5. private _cellWidth = 20;
  6. private _cellHeight = 20;
  7. private _minorLineTickness = 1;
  8. private _minorLineColor = "DarkGray";
  9. private _majorLineTickness = 2;
  10. private _majorLineColor = "White";
  11. private _majorLineFrequency = 5;
  12. private _background = "Black";
  13. private _displayMajorLines = true;
  14. private _displayMinorLines = true;
  15. /** Gets or sets a boolean indicating if minor lines must be rendered (true by default)) */
  16. public get displayMinorLines(): boolean {
  17. return this._displayMinorLines;
  18. }
  19. public set displayMinorLines(value: boolean) {
  20. if (this._displayMinorLines === value) {
  21. return;
  22. }
  23. this._displayMinorLines = value;
  24. this._markAsDirty();
  25. }
  26. /** Gets or sets a boolean indicating if major lines must be rendered (true by default)) */
  27. public get displayMajorLines(): boolean {
  28. return this._displayMajorLines;
  29. }
  30. public set displayMajorLines(value: boolean) {
  31. if (this._displayMajorLines === value) {
  32. return;
  33. }
  34. this._displayMajorLines = value;
  35. this._markAsDirty();
  36. }
  37. /** Gets or sets background color (Black by default) */
  38. public get background(): string {
  39. return this._background;
  40. }
  41. public set background(value: string) {
  42. if (this._background === value) {
  43. return;
  44. }
  45. this._background = value;
  46. this._markAsDirty();
  47. }
  48. /** Gets or sets the width of each cell (20 by default) */
  49. public get cellWidth(): number {
  50. return this._cellWidth;
  51. }
  52. public set cellWidth(value: number) {
  53. this._cellWidth = value;
  54. this._markAsDirty();
  55. }
  56. /** Gets or sets the height of each cell (20 by default) */
  57. public get cellHeight(): number {
  58. return this._cellHeight;
  59. }
  60. public set cellHeight(value: number) {
  61. this._cellHeight = value;
  62. this._markAsDirty();
  63. }
  64. /** Gets or sets the tickness of minor lines (1 by default) */
  65. public get minorLineTickness(): number {
  66. return this._minorLineTickness;
  67. }
  68. public set minorLineTickness(value: number) {
  69. this._minorLineTickness = value;
  70. this._markAsDirty();
  71. }
  72. /** Gets or sets the color of minor lines (DarkGray by default) */
  73. public get minorLineColor(): string {
  74. return this._minorLineColor;
  75. }
  76. public set minorLineColor(value: string) {
  77. this._minorLineColor = value;
  78. this._markAsDirty();
  79. }
  80. /** Gets or sets the tickness of major lines (2 by default) */
  81. public get majorLineTickness(): number {
  82. return this._majorLineTickness;
  83. }
  84. public set majorLineTickness(value: number) {
  85. this._majorLineTickness = value;
  86. this._markAsDirty();
  87. }
  88. /** Gets or sets the color of major lines (White by default) */
  89. public get majorLineColor(): string {
  90. return this._majorLineColor;
  91. }
  92. public set majorLineColor(value: string) {
  93. this._majorLineColor = value;
  94. this._markAsDirty();
  95. }
  96. /** Gets or sets the frequency of major lines (default is 1 every 5 minor lines)*/
  97. public get majorLineFrequency(): number {
  98. return this._majorLineFrequency;
  99. }
  100. public set majorLineFrequency(value: number) {
  101. this._majorLineFrequency = value;
  102. this._markAsDirty();
  103. }
  104. /**
  105. * Creates a new GridDisplayRectangle
  106. * @param name defines the control name
  107. */
  108. constructor(public name?: string) {
  109. super(name);
  110. }
  111. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  112. context.save();
  113. this._applyStates(context);
  114. if (this._processMeasures(parentMeasure, context)) {
  115. if (this._background) {
  116. context.fillStyle = this._background;
  117. context.fillRect(this._currentMeasure.left, this._currentMeasure.top, this._currentMeasure.width, this._currentMeasure.height);
  118. }
  119. let cellCountX = this._currentMeasure.width / this._cellWidth;
  120. let cellCountY = this._currentMeasure.height / this._cellHeight;
  121. // Minor lines
  122. const left = this._currentMeasure.left + this._currentMeasure.width / 2;
  123. const top = this._currentMeasure.top + this._currentMeasure.height / 2;
  124. if (this._displayMinorLines) {
  125. context.strokeStyle = this._minorLineColor;
  126. context.lineWidth = this._minorLineTickness;
  127. for (var x = -cellCountX / 2; x < cellCountX / 2; x++) {
  128. const cellX = left + x * this.cellWidth;
  129. context.beginPath();
  130. context.moveTo(cellX, this._currentMeasure.top);
  131. context.lineTo(cellX, this._currentMeasure.top + this._currentMeasure.height);
  132. context.stroke();
  133. }
  134. for (var y = -cellCountY / 2; y < cellCountY / 2; y++) {
  135. const cellY = top + y * this.cellHeight;
  136. context.beginPath();
  137. context.moveTo(this._currentMeasure.left, cellY);
  138. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, cellY);
  139. context.stroke();
  140. }
  141. }
  142. // Major lines
  143. if (this._displayMajorLines) {
  144. context.strokeStyle = this._majorLineColor;
  145. context.lineWidth = this._majorLineTickness;
  146. for (var x = -cellCountX / 2 + this._majorLineFrequency; x < cellCountX / 2; x += this._majorLineFrequency) {
  147. let cellX = left + x * this.cellWidth;
  148. context.beginPath();
  149. context.moveTo(cellX, this._currentMeasure.top);
  150. context.lineTo(cellX, this._currentMeasure.top + this._currentMeasure.height);
  151. context.stroke();
  152. }
  153. for (var y = -cellCountY / 2 + this._majorLineFrequency; y < cellCountY / 2; y += this._majorLineFrequency) {
  154. let cellY = top + y * this.cellHeight;
  155. context.moveTo(this._currentMeasure.left, cellY);
  156. context.lineTo(this._currentMeasure.left + this._currentMeasure.width, cellY);
  157. context.closePath();
  158. context.stroke();
  159. }
  160. }
  161. }
  162. context.restore();
  163. }
  164. protected _getTypeName(): string {
  165. return "DisplayGrid";
  166. }
  167. }