Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * encantar.js
  3. * GPU-accelerated Augmented Reality for the web
  4. * Copyright (C) 2022-2024 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. * hud.ts
  20. * Heads Up Display
  21. */
  22. import { Nullable, Utils } from "../utils/utils";
  23. /** HUD container */
  24. export type HUDContainer = HTMLDivElement;
  25. /**
  26. * Heads Up Display: an overlay displayed in front of the augmented scene
  27. */
  28. export class HUD
  29. {
  30. /** Container */
  31. private _container: HUDContainer;
  32. /** Whether or not we have created our own container */
  33. private _isOwnContainer: boolean;
  34. /**
  35. * Constructor
  36. * @param parent parent of the hud container
  37. * @param hudContainer an existing hud container (optional)
  38. */
  39. constructor(parent: HTMLElement, hudContainer: Nullable<HUDContainer>)
  40. {
  41. this._container = hudContainer || this._createContainer(parent);
  42. this._isOwnContainer = (hudContainer == null);
  43. // move the HUD container to the parent node
  44. if(this._container.parentElement !== parent) {
  45. this._container.remove();
  46. parent.insertAdjacentElement('afterbegin', this._container);
  47. }
  48. // the HUD should be hidden initially
  49. if(!this._container.hidden) {
  50. Utils.warning(`The container of the HUD should have the hidden attribute`);
  51. this._container.hidden = true;
  52. }
  53. }
  54. /**
  55. * The container of the HUD
  56. */
  57. get container(): HUDContainer
  58. {
  59. return this._container;
  60. }
  61. /**
  62. * Whether or not the HUD is visible
  63. */
  64. get visible(): boolean
  65. {
  66. return !this._container.hidden;
  67. }
  68. /**
  69. * Whether or not the HUD is visible
  70. */
  71. set visible(visible: boolean)
  72. {
  73. this._container.hidden = !visible;
  74. }
  75. /**
  76. * Initialize the HUD
  77. * @param zIndex the z-index of the container
  78. * @internal
  79. */
  80. _init(zIndex: number): void
  81. {
  82. const container = this._container;
  83. container.style.position = 'absolute';
  84. container.style.left = container.style.top = '0px';
  85. container.style.right = container.style.bottom = '0px';
  86. container.style.padding = container.style.margin = '0px';
  87. container.style.zIndex = String(zIndex);
  88. container.style.userSelect = 'none';
  89. this.visible = true;
  90. }
  91. /**
  92. * Release the HUD
  93. * @internal
  94. */
  95. _release(): void
  96. {
  97. if(this._isOwnContainer) {
  98. this._isOwnContainer = false;
  99. this._container.remove();
  100. }
  101. }
  102. /**
  103. * Create a HUD container as an immediate child of the input node
  104. * @param parent parent container
  105. * @returns HUD container
  106. */
  107. private _createContainer(parent: HTMLElement): HUDContainer
  108. {
  109. const node = document.createElement('div') as HTMLDivElement;
  110. node.hidden = true;
  111. parent.insertAdjacentElement('afterbegin', node);
  112. return node;
  113. }
  114. }