radioButton.ts 6.7 KB

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