radioButton.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var DOMImage = Image;
  3. module BABYLON.GUI {
  4. export class RadioButton 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 group = "";
  20. public onIsCheckedChangedObservable = new Observable<boolean>();
  21. public get checkSizeRatio(): number {
  22. return this._checkSizeRatio;
  23. }
  24. public set checkSizeRatio(value: number) {
  25. value = Math.max(Math.min(1, value), 0);
  26. if (this._checkSizeRatio === value) {
  27. return;
  28. }
  29. this._checkSizeRatio = value;
  30. this._markAsDirty();
  31. }
  32. public get background(): string {
  33. return this._background;
  34. }
  35. public set background(value: string) {
  36. if (this._background === value) {
  37. return;
  38. }
  39. this._background = value;
  40. this._markAsDirty();
  41. }
  42. public get isChecked(): boolean {
  43. return this._isChecked;
  44. }
  45. public set isChecked(value: boolean) {
  46. if (this._isChecked === value) {
  47. return;
  48. }
  49. this._isChecked = value;
  50. this._markAsDirty();
  51. this.onIsCheckedChangedObservable.notifyObservers(value);
  52. if (this._isChecked) {
  53. // Update all controls from same group
  54. this._host.executeOnAllControls((control) => {
  55. if (control === this) {
  56. return;
  57. }
  58. if ((<any>control).group === undefined) {
  59. return;
  60. }
  61. var childRadio = (<RadioButton>control);
  62. if (childRadio.group === this.group) {
  63. childRadio.isChecked = false;
  64. }
  65. });
  66. }
  67. }
  68. constructor(public name?: string) {
  69. super(name);
  70. this.isPointerBlocker = true;
  71. }
  72. protected _getTypeName(): string {
  73. return "RadioButton";
  74. }
  75. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  76. context.save();
  77. this._applyStates(context);
  78. if (this._processMeasures(parentMeasure, context)) {
  79. let actualWidth = this._currentMeasure.width - this._thickness;
  80. let actualHeight = this._currentMeasure.height - this._thickness;
  81. // Outer
  82. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  83. this._currentMeasure.width / 2 - this._thickness / 2, this._currentMeasure.height / 2 - this._thickness / 2, context);
  84. context.fillStyle = this._background;
  85. context.fill();
  86. context.strokeStyle = this.color;
  87. context.lineWidth = this._thickness;
  88. context.stroke();
  89. // Inner
  90. if (this._isChecked) {
  91. context.fillStyle = this.color;
  92. let offsetWidth = actualWidth * this._checkSizeRatio;
  93. let offseHeight = actualHeight * this._checkSizeRatio;
  94. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  95. offsetWidth / 2 - this._thickness / 2, offseHeight / 2 - this._thickness / 2, context);
  96. context.fill();
  97. }
  98. }
  99. context.restore();
  100. }
  101. // Events
  102. protected _onPointerDown(coordinates: Vector2): boolean {
  103. if (!super._onPointerDown(coordinates)) {
  104. return false;
  105. }
  106. this.isChecked = !this.isChecked;
  107. return true;
  108. }
  109. }
  110. }