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.

99 lines
3.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { getSetting } from './settings.js';
  2. const rad = Math.PI * 2, baseRotation = Math.PI / 4;
  3. function repositionToken(token, rotation, offset, pos = 0) {
  4. const size = token.scene.dimensions.size, x = Math.sin(rotation * pos + baseRotation) * offset * token.document.width * size, y = Math.cos(rotation * pos + baseRotation) * offset * token.document.height * size;
  5. token.border.x = token.document.x - x;
  6. token.border.y = token.document.y - y;
  7. token.hitArea.x = token.effects.x = token.bars.x = token.target.x = -x;
  8. token.hitArea.y = token.effects.y = token.bars.y = token.target.y = -y;
  9. token.nameplate.x = token.w / 2 - x;
  10. token.nameplate.y = token.h + 2 - y;
  11. token.tooltip.x = token.w / 2 - x;
  12. token.tooltip.y = -y - 2;
  13. const gridOffset = size / 2;
  14. token.mesh.x = token.border.x + gridOffset * token.document.width;
  15. token.mesh.y = token.border.y + gridOffset * token.document.height;
  16. }
  17. let SNAPPED_TOKENS = [];
  18. function findGroup(token) {
  19. for (const group of SNAPPED_TOKENS) {
  20. for (const t of group) {
  21. if (token === t)
  22. return group;
  23. }
  24. }
  25. }
  26. function sameGroup(oldGroup, newGroup) {
  27. if (oldGroup.length !== newGroup.length)
  28. return false;
  29. for (const t of oldGroup) {
  30. if (!newGroup.includes(t))
  31. return false;
  32. }
  33. return true;
  34. }
  35. export function refreshAll(groups = SNAPPED_TOKENS) {
  36. for (const t of SNAPPED_TOKENS.flat()) {
  37. t.object?.refresh();
  38. }
  39. }
  40. function snapToken(token, options) {
  41. if (token.isAnimating)
  42. return;
  43. if (!getSetting('snapTokens')) {
  44. token.hitArea.x = token.effects.x = token.bars.x = token.target.x = 0;
  45. token.hitArea.y = token.effects.y = token.bars.y = token.target.y = 0;
  46. return;
  47. }
  48. const oldGroup = findGroup(token.document);
  49. const x = token.document.x, y = token.document.y, height = token.document.height, width = token.document.width;
  50. const ignoreDead = getSetting('ignoreDead');
  51. const tokens = token.scene.tokens.contents.filter((token) => !token.object?.destroyed &&
  52. token.x === x &&
  53. token.y === y &&
  54. token.height === height &&
  55. token.width === width &&
  56. !(ignoreDead && checkStatus(token, ['dead', 'dying', 'unconscious'])) &&
  57. token.object.visible);
  58. if (tokens.length < 2) {
  59. token.hitArea.x = token.effects.x = token.bars.x = token.target.x = 0;
  60. token.hitArea.y = token.effects.y = token.bars.y = token.target.y = 0;
  61. if (oldGroup) {
  62. if (oldGroup.length > 1) {
  63. const idx = oldGroup.indexOf(token.document);
  64. oldGroup.splice(idx, 1);
  65. refreshAll(oldGroup);
  66. }
  67. else {
  68. const idx = SNAPPED_TOKENS.indexOf(oldGroup);
  69. SNAPPED_TOKENS.splice(idx, 1);
  70. }
  71. }
  72. return;
  73. }
  74. if (oldGroup && !sameGroup(oldGroup, tokens)) {
  75. const idx = oldGroup.indexOf(token.document);
  76. oldGroup.splice(idx, 1);
  77. if (oldGroup.length)
  78. refreshAll(oldGroup);
  79. else {
  80. const idx = SNAPPED_TOKENS.indexOf(oldGroup);
  81. SNAPPED_TOKENS.splice(idx, 1);
  82. }
  83. }
  84. const newGroup = findGroup(tokens.find((t) => t !== token.document));
  85. if (newGroup) {
  86. const idx = SNAPPED_TOKENS.indexOf(newGroup);
  87. SNAPPED_TOKENS.splice(idx, 1);
  88. }
  89. SNAPPED_TOKENS.push(tokens);
  90. const angle = rad / tokens.length;
  91. const offset = getSetting('scatter');
  92. for (let i = 0; i < tokens.length; i++)
  93. repositionToken(tokens[i].object, angle, offset, i);
  94. }
  95. function checkStatus(token, status) {
  96. return status.some((s) => token.hasStatusEffect(s));
  97. }
  98. Hooks.on('refreshToken', snapToken);
  99. Hooks.on('canvasTearDown', () => (SNAPPED_TOKENS = []));