checkbox.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class Checkbox extends Control {
  4. private _isChecked = false;
  5. private _background = "black";
  6. private _checkSizeRatio = 0.8;
  7. private _thickness = 1;
  8. public get thickness(): number {
  9. return this._thickness;
  10. }
  11. public set thickness(value: number) {
  12. if (this._thickness === value) {
  13. return;
  14. }
  15. this._thickness = value;
  16. this._markAsDirty();
  17. }
  18. public onIsCheckedChangedObservable = new Observable<boolean>();
  19. public get checkSizeRatio(): number {
  20. return this._checkSizeRatio;
  21. }
  22. public set checkSizeRatio(value: number) {
  23. value = Math.max(Math.min(1, value), 0);
  24. if (this._checkSizeRatio === value) {
  25. return;
  26. }
  27. this._checkSizeRatio = value;
  28. this._markAsDirty();
  29. }
  30. public get background(): string {
  31. return this._background;
  32. }
  33. public set background(value: string) {
  34. if (this._background === value) {
  35. return;
  36. }
  37. this._background = value;
  38. this._markAsDirty();
  39. }
  40. public get isChecked(): boolean {
  41. return this._isChecked;
  42. }
  43. public set isChecked(value: boolean) {
  44. if (this._isChecked === value) {
  45. return;
  46. }
  47. this._isChecked = value;
  48. this._markAsDirty();
  49. this.onIsCheckedChangedObservable.notifyObservers(value);
  50. }
  51. constructor(public name?: string) {
  52. super(name);
  53. this.isPointerBlocker = true;
  54. }
  55. protected _getTypeName(): string {
  56. return "CheckBox";
  57. }
  58. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  59. context.save();
  60. this._applyStates(context);
  61. if (this._processMeasures(parentMeasure, context)) {
  62. let actualWidth = this._currentMeasure.width - this._thickness;
  63. let actualHeight = this._currentMeasure.height - this._thickness;
  64. context.fillStyle = this._background;
  65. context.fillRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  66. if (this._isChecked) {
  67. context.fillStyle = this.color;
  68. let offsetWidth = actualWidth * this._checkSizeRatio;
  69. let offseHeight = actualHeight * this._checkSizeRatio;
  70. context.fillRect(this._currentMeasure.left + this._thickness / 2 + (actualWidth - offsetWidth) / 2, this._currentMeasure.top + this._thickness / 2 + (actualHeight - offseHeight) / 2, offsetWidth, offseHeight);
  71. }
  72. context.strokeStyle = this.color;
  73. context.lineWidth = this._thickness;
  74. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  75. }
  76. context.restore();
  77. }
  78. // Events
  79. protected _onPointerDown(coordinates: Vector2, buttonIndex: number): boolean {
  80. if (!super._onPointerDown(coordinates, buttonIndex)) {
  81. return false;
  82. }
  83. this.isChecked = !this.isChecked;
  84. return true;
  85. }
  86. }
  87. }