Skip to content

Commit

Permalink
new settings
Browse files Browse the repository at this point in the history
  • Loading branch information
elizeuangelo committed Mar 7, 2023
1 parent 1b046f7 commit d0911d1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "z-scatter",
"title": "Z Scatter",
"description": "Automatically force snap small and medium-sized tokens in the same square, for better token management in the grid.",
"version": "1.0",
"version": "1.1",
"authors": [
{
"name": "Elizeu Angelo",
Expand Down
21 changes: 21 additions & 0 deletions module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ const settings = {
default: true,
onChange: (value) => canvas.tokens.placeables.forEach((t) => t.refresh()),
},
scatter: {
name: 'Scattering',
hint: 'How much the tokens will scatter around?',
scope: 'world',
config: true,
type: Number,
default: 0.4,
range: {
min: 0.01,
max: 1,
step: 0.01,
},
},
ignoreDead: {
name: 'Ignore Special Cases',
hint: 'Dead or incapacitated tokens will not be snapped.',
scope: 'world',
config: true,
type: Boolean,
default: true,
},
};
export function getSetting(name) {
return game.settings.get(SYSTEM_ID, name);
Expand Down
21 changes: 21 additions & 0 deletions module/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@ const settings = {
default: true,
onChange: (value) => canvas.tokens!.placeables.forEach((t) => t.refresh()),
},
scatter: {
name: 'Scattering',
hint: 'How much the tokens will scatter around?',
scope: 'world',
config: true,
type: Number,
default: 0.4,
range: {
min: 0.01,
max: 1,
step: 0.01,
},
},
ignoreDead: {
name: 'Ignore Special Cases',
hint: 'Dead or incapacitated tokens will not be snapped.',
scope: 'world',
config: true,
type: Boolean,
default: true,
},
} as const;

export type Settings = typeof settings;
Expand Down
21 changes: 16 additions & 5 deletions module/snap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getSetting } from './settings.js';
const offset = 0.4, rad = Math.PI * 2, baseRotation = Math.PI / 4;
function repositionToken(token, rotation, pos = 0) {
const rad = Math.PI * 2, baseRotation = Math.PI / 4;
function repositionToken(token, rotation, offset, pos = 0) {
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;
token.border.x = token.document.x - x;
token.border.y = token.document.y - y;
Expand Down Expand Up @@ -43,8 +43,15 @@ function snapToken(token, options) {
}
const oldGroup = findGroup(token.document);
const x = token.document.x, y = token.document.y, height = token.document.height, width = token.document.width;
const tokens = token.scene.tokens.contents.filter((token) => !token.object?.destroyed && token.object.x === x && token.object.y === y && token.height === height && token.width === width);
if (tokens.length === 1) {
const ignoreDead = getSetting('ignoreDead');
const tokens = token.scene.tokens.contents.filter((token) => !token.object?.destroyed &&
token.object.x === x &&
token.object.y === y &&
token.height === height &&
token.width === width &&
!(ignoreDead && checkStatus(token, ['dead', 'dying', 'unconscious'])) &&
token.object.visible);
if (tokens.length < 2) {
token.hitArea.x = 0;
token.hitArea.y = 0;
if (oldGroup) {
Expand Down Expand Up @@ -77,8 +84,12 @@ function snapToken(token, options) {
}
SNAPPED_TOKENS.push(tokens);
const angle = rad / tokens.length;
const offset = getSetting('scatter');
for (let i = 0; i < tokens.length; i++)
repositionToken(tokens[i].object, angle, i);
repositionToken(tokens[i].object, angle, offset, i);
}
function checkStatus(token, status) {
return status.some((s) => token.hasStatusEffect(s));
}
Hooks.on('refreshToken', snapToken);
Hooks.on('canvasTearDown', () => (SNAPPED_TOKENS = []));
24 changes: 18 additions & 6 deletions module/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { getSetting } from './settings.js';

type TokenExpanded = Token & { mesh: any; destroyed: boolean; isAnimating: boolean };

const offset = 0.4,
rad = Math.PI * 2,
const rad = Math.PI * 2,
baseRotation = Math.PI / 4;

function repositionToken(token: TokenExpanded, rotation: number, pos = 0) {
function repositionToken(token: TokenExpanded, rotation: number, offset: number, pos = 0) {
const size = (token.scene.dimensions as Canvas.Dimensions).size,
x = Math.sin(rotation * pos + baseRotation) * offset * token.document.width * size,
y = Math.cos(rotation * pos + baseRotation) * offset * token.document.height * size;
Expand Down Expand Up @@ -70,11 +69,19 @@ function snapToken(
height = token.document.height,
width = token.document.width;

const ignoreDead = getSetting('ignoreDead');

const tokens = token.scene.tokens.contents.filter(
(token: TokenDocument & { object: any }) =>
!token.object?.destroyed && token.object.x === x && token.object.y === y && token.height === height && token.width === width
!token.object?.destroyed &&
token.object.x === x &&
token.object.y === y &&
token.height === height &&
token.width === width &&
!(ignoreDead && checkStatus(token, ['dead', 'dying', 'unconscious'])) &&
token.object.visible
);
if (tokens.length === 1) {
if (tokens.length < 2) {
(token.hitArea as any).x = 0;
(token.hitArea as any).y = 0;
if (oldGroup) {
Expand Down Expand Up @@ -107,7 +114,12 @@ function snapToken(
SNAPPED_TOKENS.push(tokens);

const angle = rad / tokens.length;
for (let i = 0; i < tokens.length; i++) repositionToken(tokens[i].object as TokenExpanded, angle, i);
const offset = getSetting('scatter');
for (let i = 0; i < tokens.length; i++) repositionToken(tokens[i].object as TokenExpanded, angle, offset, i);
}

function checkStatus(token: TokenDocument, status: string[]) {
return status.some((s) => token.hasStatusEffect(s));
}

Hooks.on('refreshToken', snapToken);
Expand Down

0 comments on commit d0911d1

Please sign in to comment.