radioButton.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON.GUI {
  3. /**
  4. * Class used to create radio button controls
  5. */
  6. export class RadioButton extends Control {
  7. private _isChecked = false;
  8. private _background = "black";
  9. private _checkSizeRatio = 0.8;
  10. private _thickness = 1;
  11. /** Gets or sets border thickness */
  12. public get thickness(): number {
  13. return this._thickness;
  14. }
  15. public set thickness(value: number) {
  16. if (this._thickness === value) {
  17. return;
  18. }
  19. this._thickness = value;
  20. this._markAsDirty();
  21. }
  22. /** Gets or sets group name */
  23. public group = "";
  24. /** Observable raised when isChecked is changed */
  25. public onIsCheckedChangedObservable = new Observable<boolean>();
  26. /** Gets or sets a value indicating the ratio between overall size and check size */
  27. public get checkSizeRatio(): number {
  28. return this._checkSizeRatio;
  29. }
  30. public set checkSizeRatio(value: number) {
  31. value = Math.max(Math.min(1, value), 0);
  32. if (this._checkSizeRatio === value) {
  33. return;
  34. }
  35. this._checkSizeRatio = value;
  36. this._markAsDirty();
  37. }
  38. /** Gets or sets background color */
  39. public get background(): string {
  40. return this._background;
  41. }
  42. public set background(value: string) {
  43. if (this._background === value) {
  44. return;
  45. }
  46. this._background = value;
  47. this._markAsDirty();
  48. }
  49. /** Gets or sets a boolean indicating if the checkbox is checked or not */
  50. public get isChecked(): boolean {
  51. return this._isChecked;
  52. }
  53. public set isChecked(value: boolean) {
  54. if (this._isChecked === value) {
  55. return;
  56. }
  57. this._isChecked = value;
  58. this._markAsDirty();
  59. this.onIsCheckedChangedObservable.notifyObservers(value);
  60. if (this._isChecked) {
  61. // Update all controls from same group
  62. this._host.executeOnAllControls((control) => {
  63. if (control === this) {
  64. return;
  65. }
  66. if ((<any>control).group === undefined) {
  67. return;
  68. }
  69. var childRadio = (<RadioButton>control);
  70. if (childRadio.group === this.group) {
  71. childRadio.isChecked = false;
  72. }
  73. });
  74. }
  75. }
  76. /**
  77. * Creates a new RadioButton
  78. * @param name defines the control name
  79. */
  80. constructor(public name?: string) {
  81. super(name);
  82. this.isPointerBlocker = true;
  83. }
  84. protected _getTypeName(): string {
  85. return "RadioButton";
  86. }
  87. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  88. context.save();
  89. this._applyStates(context);
  90. if (this._processMeasures(parentMeasure, context)) {
  91. let actualWidth = this._currentMeasure.width - this._thickness;
  92. let actualHeight = this._currentMeasure.height - this._thickness;
  93. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  94. context.shadowColor = this.shadowColor;
  95. context.shadowBlur = this.shadowBlur;
  96. context.shadowOffsetX = this.shadowOffsetX;
  97. context.shadowOffsetY = this.shadowOffsetY;
  98. }
  99. // Outer
  100. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  101. this._currentMeasure.width / 2 - this._thickness / 2, this._currentMeasure.height / 2 - this._thickness / 2, context);
  102. context.fillStyle = this._background;
  103. context.fill();
  104. if(this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY){
  105. context.shadowBlur = 0;
  106. context.shadowOffsetX = 0;
  107. context.shadowOffsetY = 0;
  108. }
  109. context.strokeStyle = this.color;
  110. context.lineWidth = this._thickness;
  111. context.stroke();
  112. // Inner
  113. if (this._isChecked) {
  114. context.fillStyle = this.color;
  115. let offsetWidth = actualWidth * this._checkSizeRatio;
  116. let offseHeight = actualHeight * this._checkSizeRatio;
  117. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  118. offsetWidth / 2 - this._thickness / 2, offseHeight / 2 - this._thickness / 2, context);
  119. context.fill();
  120. }
  121. }
  122. context.restore();
  123. }
  124. // Events
  125. public _onPointerDown(target: Control, coordinates: Vector2, pointerId:number, buttonIndex: number): boolean {
  126. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  127. return false;
  128. }
  129. if (!this.isChecked) {
  130. this.isChecked = true;
  131. }
  132. return true;
  133. }
  134. }
  135. }