radioButton.ts 6.9 KB

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