checkbox.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  65. context.shadowColor = this.shadowColor;
  66. context.shadowBlur = this.shadowBlur;
  67. context.shadowOffsetX = this.shadowOffsetX;
  68. context.shadowOffsetY = this.shadowOffsetY;
  69. }
  70. context.fillStyle = this._background;
  71. context.fillRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  72. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  73. context.shadowBlur = 0;
  74. context.shadowOffsetX = 0;
  75. context.shadowOffsetY = 0;
  76. }
  77. if (this._isChecked) {
  78. context.fillStyle = this.color;
  79. let offsetWidth = actualWidth * this._checkSizeRatio;
  80. let offseHeight = actualHeight * this._checkSizeRatio;
  81. context.fillRect(this._currentMeasure.left + this._thickness / 2 + (actualWidth - offsetWidth) / 2, this._currentMeasure.top + this._thickness / 2 + (actualHeight - offseHeight) / 2, offsetWidth, offseHeight);
  82. }
  83. context.strokeStyle = this.color;
  84. context.lineWidth = this._thickness;
  85. context.strokeRect(this._currentMeasure.left + this._thickness / 2, this._currentMeasure.top + this._thickness / 2, actualWidth, actualHeight);
  86. }
  87. context.restore();
  88. }
  89. // Events
  90. public _onPointerDown(target: Control, coordinates: Vector2, buttonIndex: number): boolean {
  91. if (!super._onPointerDown(target, coordinates, buttonIndex)) {
  92. return false;
  93. }
  94. this.isChecked = !this.isChecked;
  95. return true;
  96. }
  97. }
  98. }