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.

103 lines
2.9 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(stringHTML) {
  68. return Handlebars.compile(
  69. '<div class="tva-html-overlay"> <section class="window-content"><form>' + stringHTML + '</form></section></div>'
  70. );
  71. }
  72. function constructTemplate(ovConfig, force = false) {
  73. if (!_templateCache.hasOwnProperty(ovConfig.id)) {
  74. const compiled = _compile(ovConfig.html.template);
  75. Handlebars.registerPartial(ovConfig.id, compiled);
  76. _templateCache[ovConfig.id] = compiled;
  77. } else if (force) {
  78. return _compile(ovConfig.html.template);
  79. }
  80. return _templateCache[ovConfig.id];
  81. }
  82. function renderTemplate(ovConfig, data, force = false) {
  83. const template = constructTemplate(ovConfig, force);
  84. console.log(data.x);
  85. return template(data || {}, {
  86. allowProtoMethodsByDefault: true,
  87. allowProtoPropertiesByDefault: true,
  88. });
  89. }