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.

107 lines
3.0 KiB

  1. export class HTMLOverlay {
  2. static container = null;
  3. static renderQueue = [];
  4. static hudReady = false;
  5. static hudRendered() {
  6. HTMLOverlay.hudReady = true;
  7. HTMLOverlay.renderQueue.forEach((ov) => ov.render());
  8. HTMLOverlay.renderQueue = [];
  9. }
  10. constructor(overlayConfig, token) {
  11. this.overlayConfig = overlayConfig;
  12. this.token = token;
  13. this.render();
  14. }
  15. render(overlayConfig = null, force = false) {
  16. if (!HTMLOverlay.hudReady) {
  17. HTMLOverlay.renderQueue.push(this);
  18. return;
  19. }
  20. if (!HTMLOverlay.container) {
  21. HTMLOverlay.container = $('<div id="tva-html-overlays"></div>');
  22. $('#hud').append(HTMLOverlay.container);
  23. }
  24. if (this.element) this.remove();
  25. if (overlayConfig) this.overlayConfig = overlayConfig;
  26. this.element = $(renderTemplate(this.overlayConfig, this.getData(), force));
  27. HTMLOverlay.container.append(this.element);
  28. this.activateListeners(this.element);
  29. this.setPosition();
  30. }
  31. remove() {
  32. if (this.element) this.element.remove();
  33. this.element = null;
  34. }
  35. getData(options = {}) {
  36. const data = this.token.document.toObject();
  37. return foundry.utils.mergeObject(data, {
  38. isGM: game.user.isGM,
  39. });
  40. }
  41. setPosition({ left, top, width, height, scale, angle, origin } = {}) {
  42. if (!HTMLOverlay.hudReady) return;
  43. const ratio = canvas.dimensions.size / 100;
  44. const position = {
  45. width: width || this.token.document.width * 100,
  46. height: height || this.token.document.height * 100,
  47. left: left ?? this.token.document.x,
  48. top: top ?? this.token.document.y,
  49. };
  50. if (ratio !== 1) position.transform = `scale(${ratio})`;
  51. this.element.css(position);
  52. if (angle != null) {
  53. this.element.css({ transform: 'rotate(' + angle + 'deg)' });
  54. }
  55. if (origin != null) {
  56. this.element.css({ 'transform-origin': origin.x + 'px ' + origin.y + 'px' });
  57. }
  58. }
  59. /** @override */
  60. activateListeners(html) {
  61. try {
  62. eval(this.overlayConfig.html.listeners);
  63. } catch (e) {}
  64. }
  65. }
  66. const _templateCache = {};
  67. function _compile(html, id) {
  68. // <style scoped>${html.style}</style>
  69. return Handlebars.compile(
  70. `<div class="tva-html-overlay overlay-${id}">
  71. <section class="window-content">
  72. <form>${html.template}</form>
  73. </section>
  74. </div>`
  75. );
  76. }
  77. function constructTemplate(ovConfig, force = false) {
  78. if (!_templateCache.hasOwnProperty(ovConfig.id)) {
  79. const compiled = _compile(ovConfig.html, ovConfig.id);
  80. Handlebars.registerPartial(ovConfig.id, compiled);
  81. _templateCache[ovConfig.id] = compiled;
  82. } else if (force) {
  83. return _compile(ovConfig.html, ovConfig.id);
  84. }
  85. return _templateCache[ovConfig.id];
  86. }
  87. function renderTemplate(ovConfig, data, force = false) {
  88. const template = constructTemplate(ovConfig, force);
  89. return template(data || {}, {
  90. allowProtoMethodsByDefault: true,
  91. allowProtoPropertiesByDefault: true,
  92. });
  93. }