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.

59 lines
2.9 KiB

1 year ago
  1. export class UIHandler {
  2. static UIVisible(placeable) {
  3. const isPreview = placeable.document?.id == null;
  4. if (isPreview) return true;
  5. if (!game.user.isGM || !CONFIG.Levels.UI?.rangeEnabled) return;
  6. const isTokenSelected = canvas?.tokens?.controlled[0] || CONFIG.Levels.currentToken;
  7. const isVision = canvas.effects.visionSources.size;
  8. if (isTokenSelected && isVision) return;
  9. if (isTokenSelected && !isVision && !(placeable instanceof Token)) return;
  10. if ((canvas?.tokens?.controlled[0] || CONFIG.Levels.currentToken) && canvas.effects.visionSources.size) return;
  11. let { rangeBottom, rangeTop } = CONFIG.Levels.helpers.getRangeForDocument(placeable.document);
  12. rangeBottom = placeable.document.elevation ?? rangeBottom;
  13. if (rangeBottom == -Infinity && rangeTop == Infinity) return;
  14. if (placeable instanceof Token) {
  15. rangeBottom = placeable.losHeight;
  16. rangeTop = placeable.losHeight;
  17. }
  18. placeable.visible = placeable instanceof Tile ? CONFIG.Levels.handlers.UIHandler.inUIRangeTile(rangeBottom, rangeTop, placeable) : CONFIG.Levels.handlers.UIHandler.inUIRange(rangeBottom, rangeTop);
  19. }
  20. static tokenUIWrapperIsVisible(wrapped, ...args) {
  21. if (!game.user.isGM || !CONFIG.Levels.UI?.rangeEnabled) return wrapped(...args);
  22. const isTokenSelected = canvas?.tokens?.controlled[0] || CONFIG.Levels.currentToken;
  23. const isVision = canvas.effects.visionSources.size;
  24. if ((isTokenSelected && !isVision) || !isTokenSelected) {
  25. const wrappedResult = wrapped(...args);
  26. return wrappedResult && CONFIG.Levels.handlers.UIHandler.inUIRange(this.losHeight, this.losHeight);
  27. } else {
  28. return wrapped(...args);
  29. }
  30. }
  31. static emitsLightUI(light) {
  32. if (!game.user.isGM || !CONFIG.Levels.UI?.rangeEnabled || CONFIG.Levels.currentToken) return true;
  33. const { rangeBottom, rangeTop } = CONFIG.Levels.helpers.getRangeForDocument(light.document);
  34. const UITop = parseFloat(CONFIG.Levels.UI.range[1]);
  35. return rangeTop <= UITop;
  36. }
  37. static inUIRange(bottom, top) {
  38. const UIBottom = parseFloat(CONFIG.Levels.UI.range[0]);
  39. const UITop = parseFloat(CONFIG.Levels.UI.range[1]);
  40. return bottom >= UIBottom && top <= UITop;
  41. }
  42. static inUIRangeTile(bottom, top, tile) {
  43. const overhead = tile.document.overhead;
  44. const UIBottom = parseFloat(CONFIG.Levels.UI.range[0]);
  45. const UITop = parseFloat(CONFIG.Levels.UI.range[1]);
  46. if (!overhead) {
  47. return UIBottom >= (canvas?.scene?.flags?.levels?.backgroundElevation ?? 0);
  48. }
  49. if (CONFIG.Levels.UI.roofEnabled) {
  50. if (top == Infinity && bottom <= UITop + 1 && bottom >= UIBottom) {
  51. return true;
  52. }
  53. }
  54. return top <= UITop; //bottom >= UIBottom && top <= UITop;
  55. }
  56. }