Skip to content

Commit

Permalink
Merge branch 'release/0.8.2' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Nov 5, 2024
2 parents e212606 + 2a4854f commit 367bc9d
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.2
Add module config and template config settings to modify how snap-to-grid works. Also added to dnd5e spell config, but this will not affect template previews in dnd5e until [PR](https://github.com/foundryvtt/dnd5e/pull/4649) is accepted. #116.
Add module config to use a rotating square instead of a circle template. #118.

## 0.8.1
Dnd5e v4 compatibility. #128.
Tidy sheets compatibility. #129. Thanks @morepurplemorebetter!
Expand Down
4 changes: 3 additions & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@
"walledtemplates.settings.hideHighlighting.Name": "Hide template highlighting",
"walledtemplates.settings.hideHighlighting.Hint": "Unless hovering over the template control icon, do not show the highlighted grid for the template.",

"walledtemplates.settings.showOnHover.Name": "Show on token hover",
"walledtemplates.settings.showOnHover.Name": "Show on token hover",
"walledtemplates.settings.showOnHover.Hint": "When hovering over a token, show grid highlighting for all hidden templates that overlap that token's border.",

"walledtemplates.settings.circleSquare.Name": "Replace Circles with Squares",

"walledtemplates.controls.autotarget.Title": "Autotarget tokens with template",

"walledtemplates.notifications.attach-last-selected-token": "Please select a token to attach.",
Expand Down
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"library": false,
"compatibility": {
"minimum": "12",
"verified": "12.327"
"verified": "12.331"
},
"authors": [
{
Expand Down
14 changes: 14 additions & 0 deletions scripts/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,20 @@ Hooks.once("ready", () => {
(Sorry, you may need to update the template elevation manually if you had relied on the walled template elevation previously.)`
})

.addEntry({
version: "0.8.2",
title: "Snap-to-grid and Square Circles",
body: `\
In the default template settings, you can now set how snap-to-grid works for each template shape.
As with other defaults, you can modify this on a per-template basis in the template configuration.
If you are using dnd5e, you can also modify the snap settings on a per-spell basis. Unfortunately,
the preview template will not respect this setting until this PR is accepted.
https://github.com/foundryvtt/dnd5e/pull/4649
I also added an option, in the module configuration, to use rotating squares instead of circles.`
})

.build()
?.render(true);
});
Expand Down
2 changes: 2 additions & 0 deletions scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { WalledTemplateCone } from "./template_shapes/WalledTemplateCone.js";
import { WalledTemplateRay } from "./template_shapes/WalledTemplateRay.js";
import { WalledTemplateRoundedCone } from "./template_shapes/WalledTemplateRoundedCone.js";
import { WalledTemplateSquare } from "./template_shapes/WalledTemplateSquare.js";
import { WalledTemplateRotatedSquare } from "./template_shapes/WalledTemplateRotatedSquare.js";

// Self-executing scripts for hooks
import "./changelog.js";
Expand Down Expand Up @@ -102,6 +103,7 @@ Hooks.once("init", function() {
WalledTemplateRay,
WalledTemplateRoundedCone,
WalledTemplateSquare,
WalledTemplateRotatedSquare,

PATCHER,

Expand Down
19 changes: 17 additions & 2 deletions scripts/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals
canvas,
CONFIG,
CONST,
game,
ui
Expand All @@ -12,6 +11,9 @@ import { MODULE_ID, SHAPE_KEYS } from "./const.js";
import { registerAutotargeting } from "./patching.js";
import { WalledTemplateShapeSettings } from "./WalledTemplateShapeSettings.js";
import { ModuleSettingsAbstract } from "./ModuleSettingsAbstract.js";
import { WalledTemplateShape } from "./template_shapes/WalledTemplateShape.js";
import { WalledTemplateCircle } from "./template_shapes/WalledTemplateCircle.js";
import { WalledTemplateRotatedSquare } from "./template_shapes/WalledTemplateRotatedSquare.js";

const KEYBINDINGS = {
AUTOTARGET: "autoTarget",
Expand All @@ -28,7 +30,8 @@ export const SETTINGS = {
HIGHLIGHTING: "hideHighlighting",
SHOW_ON_HOVER: "showOnHover"
},
CHANGELOG: "changelog"
CHANGELOG: "changelog",
CIRCLE_SQUARE: "circleSquare"
};

SETTINGS.AUTOTARGET = {
Expand Down Expand Up @@ -201,6 +204,18 @@ export class Settings extends ModuleSettingsAbstract {
config: true
});

register(KEYS.CIRCLE_SQUARE, {
name: localize(`${KEYS.CIRCLE_SQUARE}.Name`),
type: Boolean,
default: false,
scope: "world",
config: true,
onChange: value => {
const shapeCl = value ? WalledTemplateRotatedSquare : WalledTemplateCircle;
WalledTemplateShape.shapeCodeRegister.set("circle", shapeCl);
}
});

// ----- NOTE: Submenu ---- //

for ( const shape of SHAPE_KEYS ) {
Expand Down
2 changes: 1 addition & 1 deletion scripts/template_shapes/WalledTemplateCircle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class WalledTemplateCircle extends WalledTemplateShape {
calculateOriginalShape({ distance } = {}) {
// Convert to degrees and grid units for Foundry method.
distance ??= this.distance;
distance = CONFIG.GeometryLib.utils.pixelsToGridUnits(distance);
distance = CONFIG.GeometryLib.utils.pixelsToGridUnits(distance);
return CONFIG.MeasuredTemplate.objectClass.getCircleShape(distance);
// Pad the circle by one pixel so it better covers expected grid spaces?
// (Rounding tends to drop spaces on the edge.)
Expand Down
74 changes: 74 additions & 0 deletions scripts/template_shapes/WalledTemplateRotatedSquare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* globals
CONFIG,
PIXI
*/
/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */
"use strict";

import { WalledTemplateCircle } from "./WalledTemplateCircle.js";

export class WalledTemplateRotatedSquare extends WalledTemplateCircle {

/**
* Square centered on an origin point.
* See dndHelpers for original:
* https://github.com/trioderegion/dnd5e-helpers/blob/342548530088f929d5c243ad2c9381477ba072de/scripts/modules/TemplateScaling.js#L91
* Conforms with 5-5-5 diagonal rule.
* @param {object} [opts] Optional values to temporarily override the ones in this instance.
* @returns {PIXI.Rectangle}
*/
calculateOriginalShape({ distance, direction } = {}) {
// distance ??= this.distance;

// Convert to degrees and grid units for Foundry method.
// distance = CONFIG.GeometryLib.utils.pixelsToGridUnits(distance);

// TODO: Redo for v12's grid settings.
// Based on 5-5-5, the square's width should equate to the circle's diameter.
// (Consider the diameter of the circle in the X-Y directions.)
distance ??= this.distance;
const dist2 = distance * 2;

direction ??= this.direction;
const rect = new PIXI.Rectangle(-distance, -distance, dist2, dist2);
const poly = rotatePolygon(rect.toPolygon(), direction, new PIXI.Point(0, 0));
return poly;
}
}


/**
* Rotate a polygon a given amount clockwise, in radians.
* @param {PIXI.Polygon} poly The polygon
* @param {number} rotation The amount to rotate clockwise in radians
* @param {number} [centroid] Center of the polygon
*/
function rotatePolygon(poly, rotation = 0, centroid) {
if ( !rotation ) return poly;
centroid ??= poly.center;

// Translate to 0,0, rotate, translate back based on centroid.
const Matrix = CONFIG.GeometryLib.Matrix;
const rot = Matrix.rotationZ(rotation, false);
const trans = Matrix.translation(-centroid.x, -centroid.y);
const revTrans = Matrix.translation(centroid.x, centroid.y);
const M = trans.multiply3x3(rot).multiply3x3(revTrans);

// Multiply by the points of the polygon.
const nPoints = poly.points.length * 0.5;
const arr = new Array(nPoints);
for ( let i = 0; i < nPoints; i += 1 ) {
const j = i * 2;
arr[i] = [poly.points[j], poly.points[j+1], 1];
}
const polyM = new Matrix(arr);
const rotatedM = polyM.multiply(M);

const rotatedPoints = new Array(poly.points.length);
for ( let i = 0; i < nPoints; i += 1 ) {
const j = i * 2;
rotatedPoints[j] = rotatedM.arr[i][0];
rotatedPoints[j+1] = rotatedM.arr[i][1];
}
return new PIXI.Polygon(rotatedPoints);
}
4 changes: 2 additions & 2 deletions scripts/template_shapes/WalledTemplateSquare.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export class WalledTemplateSquare extends WalledTemplateCircle {
* @returns {PIXI.Rectangle}
*/
calculateOriginalShape({ distance } = {}) {
distance ??= this.distance;
// distance ??= this.distance;

// Convert to degrees and grid units for Foundry method.
distance = CONFIG.GeometryLib.utils.pixelsToGridUnits(distance);
// distance = CONFIG.GeometryLib.utils.pixelsToGridUnits(distance);

// TODO: Redo for v12's grid settings.
// Based on 5-5-5, the square's width should equate to the circle's diameter.
Expand Down

0 comments on commit 367bc9d

Please sign in to comment.