checkbox.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { Control } from "./control";
  2. import { Measure } from "../measure";
  3. import { Observable, Vector2 } from "babylonjs";
  4. /**
  5. * Class used to represent a 2D checkbox
  6. */
  7. export class Checkbox extends Control {
  8. private _isChecked = false;
  9. private _background = "black";
  10. private _checkSizeRatio = 0.8;
  11. private _thickness = 1;
  12. /** Gets or sets border thickness */
  13. public get thickness(): number {
  14. return this._thickness;
  15. }
  16. public set thickness(value: number) {
  17. if (this._thickness === value) {
  18. return;
  19. }
  20. this._thickness = value;
  21. this._markAsDirty();
  22. }
  23. /**
  24. * Observable raised when isChecked property changes
  25. */
  26. public onIsCheckedChangedObservable = new Observable<boolean>();
  27. /** Gets or sets a value indicating the ratio between overall size and check size */
  28. public get checkSizeRatio(): number {
  29. return this._checkSizeRatio;
  30. }
  31. public set checkSizeRatio(value: number) {
  32. value = Math.max(Math.min(1, value), 0);
  33. if (this._checkSizeRatio === value) {
  34. return;
  35. }
  36. this._checkSizeRatio = value;
  37. this._markAsDirty();
  38. }
  39. /** Gets or sets background color */
  40. public get background(): string {
  41. return this._background;
  42. }
  43. public set background(value: string) {
  44. if (this._background === value) {
  45. return;
  46. }
  47. this._background = value;
  48. this._markAsDirty();
  49. }
  50. /** Gets or sets a boolean indicating if the checkbox is checked or not */
  51. public get isChecked(): boolean {
  52. return this._isChecked;
  53. }
  54. public set isChecked(value: boolean) {
  55. if (this._isChecked === value) {
  56. return;
  57. }
  58. this._isChecked = value;
  59. this._markAsDirty();
  60. this.onIsCheckedChangedObservable.notifyObservers(value);
  61. }
  62. /**
  63. * Creates a new CheckBox
  64. * @param name defines the control name
  65. */
  66. constructor(public name?: string) {
  67. super(name);
  68. this.isPointerBlocker = true;
  69. }
  70. protected _getTypeName(): string {
  71. return "CheckBox";
  72. }
  73. /** @hidden */
  74. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  75. context.save();
  76. this._applyStates(context);
  77. if (this._processMeasures(parentMeasure, context)) {
  78. let actualWidth = this._currentMeasure.width - this._thickness;
  79. let actualHeight = this._currentMeasure.height - this._thickness;
  80. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  81. context.shadowColor = this.shadowColor;
  82. context.shadowBlur = this.shadowBlur;
  83. context.shadowOffsetX = this.shadowOffsetX;
  84. context.shadowOffsetY = this.shadowOffsetY;
  85. }
  86. context.fillStyle = this._background;
  87. context.fillRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  88. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  89. context.shadowBlur = 0;
  90. context.shadowOffsetX = 0;
  91. context.shadowOffsetY = 0;
  92. }
  93. if (this._isChecked) {
  94. context.fillStyle = this.color;
  95. let offsetWidth = actualWidth * this._checkSizeRatio;
  96. let offseHeight = actualHeight * this._checkSizeRatio;
  97. context.fillRect(this._currentMeasure.left + this._thickness / 2 + (actualWidth - offsetWidth) / 2, this._currentMeasure.top + this._thickness / 2 + (actualHeight - offseHeight) / 2, offsetWidth, offseHeight);
  98. }
  99. context.strokeStyle = this.color;
  100. context.lineWidth = this._thickness;
  101. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  102. }
  103. context.restore();
  104. }
  105. // Events
  106. /** @hidden */
  107. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  108. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  109. return false;
  110. }
  111. this.isChecked = !this.isChecked;
  112. return true;
  113. }
  114. }