displayGrid.ts 6.7 KB

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