radioButton.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { Control } from "./control";
  2. import { Observable, Vector2 } from "babylonjs";
  3. import { Measure } from "../measure";
  4. import { StackPanel, TextBlock } from ".";
  5. /**
  6. * Class used to create radio button controls
  7. */
  8. export class RadioButton extends Control {
  9. private _isChecked = false;
  10. private _background = "black";
  11. private _checkSizeRatio = 0.8;
  12. private _thickness = 1;
  13. /** Gets or sets border thickness */
  14. public get thickness(): number {
  15. return this._thickness;
  16. }
  17. public set thickness(value: number) {
  18. if (this._thickness === value) {
  19. return;
  20. }
  21. this._thickness = value;
  22. this._markAsDirty();
  23. }
  24. /** Gets or sets group name */
  25. public group = "";
  26. /** Observable raised when isChecked is changed */
  27. public onIsCheckedChangedObservable = new Observable<boolean>();
  28. /** Gets or sets a value indicating the ratio between overall size and check size */
  29. public get checkSizeRatio(): number {
  30. return this._checkSizeRatio;
  31. }
  32. public set checkSizeRatio(value: number) {
  33. value = Math.max(Math.min(1, value), 0);
  34. if (this._checkSizeRatio === value) {
  35. return;
  36. }
  37. this._checkSizeRatio = value;
  38. this._markAsDirty();
  39. }
  40. /** Gets or sets background color */
  41. public get background(): string {
  42. return this._background;
  43. }
  44. public set background(value: string) {
  45. if (this._background === value) {
  46. return;
  47. }
  48. this._background = value;
  49. this._markAsDirty();
  50. }
  51. /** Gets or sets a boolean indicating if the checkbox is checked or not */
  52. public get isChecked(): boolean {
  53. return this._isChecked;
  54. }
  55. public set isChecked(value: boolean) {
  56. if (this._isChecked === value) {
  57. return;
  58. }
  59. this._isChecked = value;
  60. this._markAsDirty();
  61. this.onIsCheckedChangedObservable.notifyObservers(value);
  62. if (this._isChecked && this._host) {
  63. // Update all controls from same group
  64. this._host.executeOnAllControls((control) => {
  65. if (control === this) {
  66. return;
  67. }
  68. if ((<any>control).group === undefined) {
  69. return;
  70. }
  71. var childRadio = (<RadioButton>control);
  72. if (childRadio.group === this.group) {
  73. childRadio.isChecked = false;
  74. }
  75. });
  76. }
  77. }
  78. /**
  79. * Creates a new RadioButton
  80. * @param name defines the control name
  81. */
  82. constructor(public name?: string) {
  83. super(name);
  84. this.isPointerBlocker = true;
  85. }
  86. protected _getTypeName(): string {
  87. return "RadioButton";
  88. }
  89. public _draw(parentMeasure: Measure, context: CanvasRenderingContext2D): void {
  90. context.save();
  91. this._applyStates(context);
  92. if (this._processMeasures(parentMeasure, context)) {
  93. let actualWidth = this._currentMeasure.width - this._thickness;
  94. let actualHeight = this._currentMeasure.height - this._thickness;
  95. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  96. context.shadowColor = this.shadowColor;
  97. context.shadowBlur = this.shadowBlur;
  98. context.shadowOffsetX = this.shadowOffsetX;
  99. context.shadowOffsetY = this.shadowOffsetY;
  100. }
  101. // Outer
  102. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  103. this._currentMeasure.width / 2 - this._thickness / 2, this._currentMeasure.height / 2 - this._thickness / 2, context);
  104. context.fillStyle = this._background;
  105. context.fill();
  106. if (this.shadowBlur || this.shadowOffsetX || this.shadowOffsetY) {
  107. context.shadowBlur = 0;
  108. context.shadowOffsetX = 0;
  109. context.shadowOffsetY = 0;
  110. }
  111. context.strokeStyle = this.color;
  112. context.lineWidth = this._thickness;
  113. context.stroke();
  114. // Inner
  115. if (this._isChecked) {
  116. context.fillStyle = this.color;
  117. let offsetWidth = actualWidth * this._checkSizeRatio;
  118. let offseHeight = actualHeight * this._checkSizeRatio;
  119. Control.drawEllipse(this._currentMeasure.left + this._currentMeasure.width / 2, this._currentMeasure.top + this._currentMeasure.height / 2,
  120. offsetWidth / 2 - this._thickness / 2, offseHeight / 2 - this._thickness / 2, context);
  121. context.fill();
  122. }
  123. }
  124. context.restore();
  125. }
  126. // Events
  127. public _onPointerDown(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number): boolean {
  128. if (!super._onPointerDown(target, coordinates, pointerId, buttonIndex)) {
  129. return false;
  130. }
  131. if (!this.isChecked) {
  132. this.isChecked = true;
  133. }
  134. return true;
  135. }
  136. /**
  137. * Utility function to easily create a radio button with a header
  138. * @param title defines the label to use for the header
  139. * @param group defines the group to use for the radio button
  140. * @param isChecked defines the initial state of the radio button
  141. * @param onValueChanged defines the callback to call when value changes
  142. * @returns a StackPanel containing the radio button and a textBlock
  143. */
  144. public static AddRadioButtonWithHeader(title: string, group: string, isChecked: boolean, onValueChanged: (button: RadioButton, value: boolean) => void): StackPanel {
  145. var panel = new StackPanel();
  146. panel.isVertical = false;
  147. panel.height = "30px";
  148. var radio = new RadioButton();
  149. radio.width = "20px";
  150. radio.height = "20px";
  151. radio.isChecked = isChecked;
  152. radio.color = "green";
  153. radio.group = group;
  154. radio.onIsCheckedChangedObservable.add((value) => onValueChanged(radio, value));
  155. panel.addControl(radio);
  156. var header = new TextBlock();
  157. header.text = title;
  158. header.width = "180px";
  159. header.paddingLeft = "5px";
  160. header.textHorizontalAlignment = Control.HORIZONTAL_ALIGNMENT_LEFT;
  161. header.color = "white";
  162. panel.addControl(header);
  163. return panel;
  164. }
  165. }