focusableButton.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /** Highlight color when button is focused */
  14. public focusedColor: Nullable<string> = null;
  15. private _isFocused = false;
  16. private _unfocusedColor: Nullable<string> = null;
  17. /** Observable raised when the control gets the focus */
  18. public onFocusObservable = new Observable<Button>();
  19. /** Observable raised when the control loses the focus */
  20. public onBlurObservable = new Observable<Button>();
  21. /** Observable raised when a key event was processed */
  22. public onKeyboardEventProcessedObservable = new Observable<KeyboardEvent>();
  23. constructor(public name?: string) {
  24. super(name);
  25. this._unfocusedColor = this.color;
  26. }
  27. /** @hidden */
  28. public onBlur(): void {
  29. if (this._isFocused) {
  30. this._isFocused = false;
  31. if (this.focusedColor && this._unfocusedColor != null) {
  32. // Set color back to saved unfocused color
  33. this.color = this._unfocusedColor;
  34. }
  35. this.onBlurObservable.notifyObservers(this);
  36. }
  37. }
  38. /** @hidden */
  39. public onFocus(): void {
  40. this._isFocused = true;
  41. if (this.focusedColor) {
  42. // Save the unfocused color
  43. this._unfocusedColor = this.color;
  44. this.color = this.focusedColor;
  45. }
  46. this.onFocusObservable.notifyObservers(this);
  47. }
  48. /**
  49. * Function called to get the list of controls that should not steal the focus from this control
  50. * @returns an array of controls
  51. */
  52. public keepsFocusWith(): Nullable<Control[]> {
  53. return null;
  54. }
  55. /**
  56. * Function to focus a button programmatically
  57. */
  58. public focus() {
  59. this._host.moveFocusToControl(this);
  60. }
  61. /**
  62. * Function to unfocus a button programmatically
  63. */
  64. public blur() {
  65. this._host.focusedControl = null;
  66. }
  67. /**
  68. * Handles the keyboard event
  69. * @param evt Defines the KeyboardEvent
  70. */
  71. public processKeyboard(evt: KeyboardEvent): void {
  72. this.onKeyboardEventProcessedObservable.notifyObservers(evt);
  73. }
  74. /** @hidden */
  75. public _onPointerUp(target: Control, coordinates: Vector2, pointerId: number, buttonIndex: number, notifyClick: boolean, pi: PointerInfoBase): void {
  76. // Clicking on button should focus
  77. this.focus();
  78. super._onPointerUp(target, coordinates, pointerId, buttonIndex, notifyClick, pi);
  79. }
  80. /** @hidden */
  81. public displose() {
  82. super.dispose();
  83. this.onBlurObservable.clear();
  84. this.onFocusObservable.clear();
  85. this.onKeyboardEventProcessedObservable.clear();
  86. }
  87. }
  88. _TypeStore.RegisteredTypes["BABYLON.GUI.FocusableButton"] = FocusableButton;