checkbox.ts 3.8 KB

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