radioButton.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. export class RadioButton 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 group = "";
  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. if (this._isChecked) {
  52. // Update all controls from same group
  53. this._host.executeOnAllControls((control) => {
  54. if (control === this) {
  55. return;
  56. }
  57. if ((<any>control).group === undefined) {
  58. return;
  59. }
  60. var childRadio = (<RadioButton>control);
  61. if (childRadio.group === this.group) {
  62. childRadio.isChecked = false;
  63. }
  64. });
  65. }
  66. }
  67. constructor(public name?: string) {
  68. super(name);
  69. this.isPointerBlocker = true;
  70. }
  71. protected _getTypeName(): string {
  72. return "RadioButton";
  73. }
  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. // Outer
  87. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  88. this._currentMeasure.width / 2 - this._thickness / 2, this._currentMeasure.height / 2 - this._thickness / 2, context);
  89. context.fillStyle = this._background;
  90. context.fill();
  91. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  92. context.shadowBlur = 0;
  93. context.shadowOffsetX = 0;
  94. context.shadowOffsetY = 0;
  95. }
  96. context.strokeStyle = this.color;
  97. context.lineWidth = this._thickness;
  98. context.stroke();
  99. // Inner
  100. if (this._isChecked) {
  101. context.fillStyle = this.color;
  102. let offsetWidth = actualWidth * this._checkSizeRatio;
  103. let offseHeight = actualHeight * this._checkSizeRatio;
  104. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  105. offsetWidth / 2 - this._thickness / 2, offseHeight / 2 - this._thickness / 2, context);
  106. context.fill();
  107. }
  108. }
  109. context.restore();
  110. }
  111. // Events
  112. public _onPointerDown(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number): boolean {
  113. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  114. return false;
  115. }
  116. this.isChecked = !this.isChecked;
  117. return true;
  118. }
  119. }
  120. }