All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB

  1. import { FEATURE_CONTROL } from '../settings.js';
  2. import { HTMLOverlay } from '../sprite/HTMLOverlay.js';
  3. import { TVAOverlay } from '../sprite/TVAOverlay.js';
  4. import { drawOverlays } from '../token/overlay.js';
  5. import { registerHook, unregisterHook } from './hooks.js';
  6. const feature_id = 'Overlays';
  7. export function registerOverlayHooks() {
  8. if (!FEATURE_CONTROL[feature_id]) {
  9. [
  10. 'refreshToken',
  11. 'destroyToken',
  12. 'updateActor',
  13. 'renderCombatTracker',
  14. 'updateToken',
  15. 'createToken',
  16. 'renderHeadsUpDisplay',
  17. ].forEach((id) => unregisterHook(feature_id, id));
  18. return;
  19. }
  20. registerHook(feature_id, 'createToken', async function (token) {
  21. if (token.object) drawOverlays(token.object);
  22. });
  23. registerHook(feature_id, 'updateToken', async function (token) {
  24. if (token.object) drawOverlays(token.object);
  25. });
  26. registerHook(feature_id, 'refreshToken', (token) => {
  27. if (token.tvaOverlays)
  28. for (const child of token.tvaOverlays) {
  29. if (child instanceof TVAOverlay) {
  30. child.refresh(null, { preview: false, fullRefresh: false });
  31. }
  32. }
  33. });
  34. registerHook(feature_id, 'destroyToken', (token) => {
  35. if (token.tvaOverlays)
  36. for (const child of token.tvaOverlays) {
  37. child.parent?.removeChild(child)?.destroy();
  38. }
  39. });
  40. registerHook(feature_id, 'updateActor', async function (actor) {
  41. if (actor.getActiveTokens)
  42. actor.getActiveTokens(true).forEach((token) => {
  43. drawOverlays(token);
  44. });
  45. });
  46. registerHook(feature_id, 'renderCombatTracker', function () {
  47. for (const tkn of canvas.tokens.placeables) {
  48. drawOverlays(tkn);
  49. }
  50. });
  51. registerHook(feature_id, 'renderHeadsUpDisplay', () => {
  52. HTMLOverlay.hudRendered();
  53. });
  54. }
  55. const REFRESH_HOOKS = {};
  56. export function registerOverlayRefreshHook(tvaOverlay, hookName) {
  57. if (!(hookName in REFRESH_HOOKS)) {
  58. registerHook('TVAOverlayRefresh', hookName, () => {
  59. REFRESH_HOOKS[hookName]?.forEach((s) => s.refresh());
  60. });
  61. REFRESH_HOOKS[hookName] = [tvaOverlay];
  62. } else if (!REFRESH_HOOKS[hookName].find((s) => s == tvaOverlay)) {
  63. REFRESH_HOOKS[hookName].push(tvaOverlay);
  64. }
  65. }
  66. export function unregisterOverlayRefreshHooks(tvaOverlay, hookName = null) {
  67. const unregister = function (hook) {
  68. if (REFRESH_HOOKS[hook]) {
  69. let index = REFRESH_HOOKS[hook].findIndex((s) => s == tvaOverlay);
  70. if (index > -1) {
  71. REFRESH_HOOKS[hook].splice(index, 1);
  72. if (!REFRESH_HOOKS[hook].length) {
  73. unregisterHook('TVAOverlayRefresh', hook);
  74. delete REFRESH_HOOKS[hook];
  75. }
  76. }
  77. }
  78. };
  79. if (hookName) unregister(hookName);
  80. else {
  81. Object.keys(REFRESH_HOOKS).forEach((k) => unregister(k));
  82. }
  83. }