radioButton.ts 6.6 KB

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