Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ar-events.ts 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2025 Alexandre Martins <alemartf(at)gmail.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * ar-events.ts
  20. * AR-related Events
  21. */
  22. /**
  23. * AR Event Type
  24. */
  25. type AREventType = string;
  26. /**
  27. * AR Event Listener (callback)
  28. */
  29. export type AREventListener = EventListener;
  30. /**
  31. * AR Event
  32. */
  33. export class AREvent<T extends AREventType> extends Event
  34. {
  35. /**
  36. * Constructor
  37. * @param type event type
  38. */
  39. constructor(type: T)
  40. {
  41. super(type);
  42. }
  43. /**
  44. * Event type
  45. */
  46. get type(): T
  47. {
  48. return super.type as T;
  49. }
  50. }
  51. /**
  52. * AR Event Target
  53. */
  54. export class AREventTarget<T extends AREventType> implements EventTarget
  55. {
  56. /** event target delegate */
  57. private readonly _delegate: EventTarget;
  58. /**
  59. * Constructor
  60. */
  61. constructor()
  62. {
  63. this._delegate = new EventTarget();
  64. }
  65. /**
  66. * Add event listener
  67. * @param type event type
  68. * @param callback
  69. */
  70. addEventListener(type: T, callback: AREventListener): void
  71. {
  72. this._delegate.addEventListener(type, callback);
  73. }
  74. /**
  75. * Remove event listener
  76. * @param type event type
  77. * @param callback
  78. */
  79. removeEventListener(type: T, callback: AREventListener): void
  80. {
  81. this._delegate.removeEventListener(type, callback);
  82. }
  83. /**
  84. * Synchronously trigger an event
  85. * @param event
  86. * @returns same value as a standard event target
  87. * @internal
  88. */
  89. dispatchEvent(event: AREvent<T>): boolean
  90. {
  91. return this._delegate.dispatchEvent(event);
  92. }
  93. }