focusableButton.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { Nullable } from "babylonjs/types";
  2. import { Vector2 } from "babylonjs/Maths/math.vector";
  3. import { Button } from "./button";
  4. import { Control } from "./control";
  5. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  6. import { PointerInfoBase } from 'babylonjs/Events/pointerEvents';
  7. import { IFocusableControl } from '../advancedDynamicTexture';
  8. import { Observable } from 'babylonjs/Misc/observable';
  9. /**
  10. * Class used to create a focusable button that can easily handle keyboard events
  11. */
  12. export class FocusableButton extends Button implements IFocusableControl {
  13. private _isFocused = false;
  14. public focusedColor: Nullable<string> = null;
  15. private _unfocusedColor: Nullable<string> = null;
  16. /** Observable raised when the control gets the focus */
  17. public onFocusObservable = new Observable<Button>();
  18. /** Observable raised when the control loses the focus */
  19. public onBlurObservable = new Observable<Button>();
  20. /** Observable raised when a key event was processed */
  21. public onKeyboardEventProcessedObservable = new Observable<KeyboardEvent>();
  22. constructor(public name?: string) {
  23. super(name);
  24. this._unfocusedColor = this.color;
  25. }
  26. /** @hidden */
  27. public onBlur(): void {
  28. if (this._isFocused) {
  29. this._isFocused = false;
  30. if (this.focusedColor && this._unfocusedColor != null) {
  31. // Set color back to saved unfocused color
  32. this.color = this._unfocusedColor;
  33. }
  34. this.onBlurObservable.notifyObservers(this);
  35. }
  36. }
  37. /** @hidden */
  38. public onFocus(): void {
  39. this._isFocused = true;
  40. if (this.focusedColor) {
  41. // Save the unfocused color
  42. this._unfocusedColor = this.color;
  43. this.color = this.focusedColor;
  44. }
  45. this.onFocusObservable.notifyObservers(this);
  46. }
  47. /**
  48. * Function called to get the list of controls that should not steal the focus from this control
  49. * @returns an array of controls
  50. */
  51. public keepsFocusWith(): Nullable<Control[]> {
  52. return null;
  53. }
  54. /**
  55. * Function to focus a button programmatically
  56. */
  57. public focus() {
  58. this._host.moveFocusToControl(this);
  59. }
  60. /**
  61. * Function to unfocus a button programmatically
  62. */
  63. public blur() {
  64. this._host.focusedControl = null;
  65. }
  66. /**
  67. * Handles the keyboard event
  68. * @param evt Defines the KeyboardEvent
  69. */
  70. public processKeyboard(evt: KeyboardEvent): void {
  71. this.onKeyboardEventProcessedObservable.notifyObservers(evt);
  72. }
  73. /** @hidden */
  74. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean, pi: PointerInfoBase): void {
  75. // Clicking on button should focus
  76. this.focus();
  77. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick, pi);
  78. }
  79. public displose() {
  80. super.dispose();
  81. this.onBlurObservable.clear();
  82. this.onFocusObservable.clear();
  83. this.onKeyboardEventProcessedObservable.clear();
  84. }
  85. }
  86. _TypeStore.RegisteredTypes["BABYLON.GUI.FocusableButton"] = FocusableButton;