Skip to content

Commit

Permalink
Merge pull request #90 from p4535992/master
Browse files Browse the repository at this point in the history
version 0.8.35
  • Loading branch information
p4535992 authored Jan 25, 2022
2 parents e819892 + 1b46af1 commit 494785d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 62 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.8.35

- Add checker for avoid the reload of trigger when update/delete/create journal if they aren't under the trigger happy folder [himmelweiss comment](https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/issues/88)
- Managed the use case [[Feature Request] NAVBAR TWEAKS breaking TRIGGER HAPPY's triggers](https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/issues/89)

## 0.8.34

- Little bug fix from [this discussion](https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/commit/baeb6c385eb142b61da38f667314c3ca7edfcd8a#commitcomment-64167717)
Expand Down
8 changes: 4 additions & 4 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trigger-happy",
"title": "Trigger Happy",
"description": "Automate everything in your world by creating triggers for your players to spring traps or anything you can think of!",
"version": "0.8.34",
"version": "0.8.35",
"author": "KaKaRoTo, tposney, p4535992",
"type": "module",
"socket": true,
Expand Down Expand Up @@ -61,9 +61,9 @@
],
"url": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy",
"manifest": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/releases/latest/download/module.json",
"download": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/releases/download/v0.8.34/module.zip",
"readme": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/blob/v0.8.34/README.md",
"changelog": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/blob/v0.8.34/changelog.md",
"download": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/releases/download/v0.8.35/module.zip",
"readme": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/blob/v0.8.35/README.md",
"changelog": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/blob/v0.8.35/changelog.md",
"bugs": "https://github.com/League-of-Foundry-Developers/fvtt-module-trigger-happy/issues",
"minimumCoreVersion": "0.8.9",
"compatibleCoreVersion": "9",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "trigger-happy",
"title": "Trigger Happy",
"description": "Automate everything in your world by creating triggers for your players to spring traps or anything you can think of!",
"version": "0.8.34",
"version": "0.8.35",
"scripts": {
"package": "gulp package",
"build": "gulp clean && gulp build && gulp link",
Expand Down
151 changes: 94 additions & 57 deletions trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Hooks.once('setup', function () {
Hooks.once('ready', () => {
Hooks.on('renderJournalSheet', (app, html, options) => {
if (game.settings.get(TRIGGER_HAPPY_MODULE_NAME, 'enableEnrichHtml')) {
if (game.triggers.journals.filter((e) => e.id === options.document.id).length > 0) {
if (game.triggers?.journals?.filter((e) => e.id === options.document.id).length > 0) {
const htmlString = HTMLEnricherTriggers.enrichAll(html.find('.editor-content').html());
html.find('.editor-content').html(htmlString);
//HTMLEnricherTriggers.bindRichTextLinks(html);
Expand Down Expand Up @@ -339,17 +339,44 @@ export const EVENT_TRIGGER_ENTITY_TYPES = {

export class TriggerHappy {
constructor() {
Hooks.on('ready', this._parseJournals.bind(this));
Hooks.on('canvasReady', this._onCanvasReady.bind(this));
Hooks.on('ready', (...args) => {
this._parseJournals();
});
Hooks.on('canvasReady', (...args) => {
this._onCanvasReady(canvas);
});
Hooks.on('controlToken', this._onControlToken.bind(this));
Hooks.on('createJournalEntry', this._parseJournals.bind(this));
Hooks.on('updateJournalEntry', this._parseJournals.bind(this));
Hooks.on('deleteJournalEntry', this._parseJournals.bind(this));
Hooks.on('createJournalEntry', (entityData, data) => {
const folders = game.folders.contents.filter((f) => {
return f.type === 'JournalEntry' && f.name === this.folderJournalName;
});
if (
folders.some((folder) => {
return folder.name == entityData.folder?.name;
})
) {
this._parseJournals();
}
});
Hooks.on('updateJournalEntry', (entityData, data) => {
if (game.triggers?.journals?.filter((e) => e.id === entityData.id).length > 0) {
this._parseJournals();
}
});
Hooks.on('deleteJournalEntry', (entityData, data) => {
if (game.triggers?.journals?.filter((e) => e.id === entityData.id).length > 0) {
this._parseJournals();
}
});
Hooks.on('preUpdateToken', this._onPreUpdateToken.bind(this));
Hooks.on('preUpdateWall', this._onPreUpdateWall.bind(this));
Hooks.on('renderSettingsConfig', this._parseJournals.bind(this)); // TODO maybe we don't need this anymore ???
Hooks.on('renderSettingsConfig', (...args) => {
this._parseJournals();
}); // TODO maybe we don't need this anymore ???
Hooks.on('preUpdateNote', this._onPreUpdateNote.bind(this));
Hooks.on('getSceneNavigationContext', this._parseJournals.bind(this)); // parse again the journal when change scene
Hooks.on('getSceneNavigationContext', (...args) => {
this._parseJournals();
}); // parse again the journal when change scene

this.registeredEffects = [];
this.triggers = [];
Expand Down Expand Up @@ -1216,73 +1243,83 @@ export class TriggerHappy {
// TokenDocument.object.w and .h report the size of the token object according to its boundaries on the grid (orange rectangle on hover)
// while .width and .height refer to the token image, which takes into account the image scale as well. For the purpose of clicks and token
// collisions, we will consider scaling and rotation of the token image to be purely aesthetic, and work off of the token's grid dimensions
let x = placeable.object.x
let y = placeable.object.y
let width = placeable.object.w
let height = placeable.object.h
let x = placeable.object.x;
let y = placeable.object.y;
let width = placeable.object.w;
let height = placeable.object.h;

// For both Tokens and Drawings, we will start by determining if 'position' is inside the placeable's bounding box
// For both Tokens and Drawings, we will start by determining if 'position' is inside the placeable's bounding box
// Since tokens have rectangular boundaries, we don't need to perform any other calculations
return (Number.between(position.x, x, x + width) && Number.between(position.y, y, y + height))
}

else if (placeable instanceof DrawingDocument) {
return Number.between(position.x, x, x + width) && Number.between(position.y, y, y + height);
} else if (placeable instanceof DrawingDocument) {
// For a DrawingDocument, the width and height of the contents are stored in DrawingDocument.data
// the .data member also includes .points, which contains the coordinates of the drawing's vertices relative to the drawing's origin
let x = placeable.data.x
let y = placeable.data.y
let width = placeable.data.width
let height = placeable.data.height
// Possible drawing types: (r)ectangle, circl(e), (p)olygon, (f)reehand
let type = placeable.data.type

if (placeable.data.rotation != 0) {
// It looks like Foundry applies the rotation to the image drawn on the canvas, but not to the position or size of the DrawingData
let x = placeable.data.x;
let y = placeable.data.y;
let width = placeable.data.width;
let height = placeable.data.height;
// Possible drawing types: (r)ectangle, circl(e), (p)olygon, (f)reehand
let type = placeable.data.type;

if (placeable.data.rotation != 0) {
// It looks like Foundry applies the rotation to the image drawn on the canvas, but not to the position or size of the DrawingData
// Instead of rotating the entire DrawingData to match what is rendered on the canvas, we can just inverse-rotate position
let drawing_center = [x + 0.5*width, y + 0.5*height]
let drawing_center = [x + 0.5 * width, y + 0.5 * height];
position = {
x: Math.cos(-placeable.data.rotation*Math.PI/180) * (position.x - drawing_center[0]) - Math.sin(-placeable.data.rotation*Math.PI/180) * (position.y - drawing_center[1]) + drawing_center[0],
y: Math.sin(-placeable.data.rotation*Math.PI/180) * (position.x - drawing_center[0]) + Math.cos(-placeable.data.rotation*Math.PI/180) * (position.y - drawing_center[1]) + drawing_center[1]
}
}

// For both Tokens and Drawings, we will start by determining if 'position' is inside the placeable's bounding box
x:
Math.cos((-placeable.data.rotation * Math.PI) / 180) * (position.x - drawing_center[0]) -
Math.sin((-placeable.data.rotation * Math.PI) / 180) * (position.y - drawing_center[1]) +
drawing_center[0],
y:
Math.sin((-placeable.data.rotation * Math.PI) / 180) * (position.x - drawing_center[0]) +
Math.cos((-placeable.data.rotation * Math.PI) / 180) * (position.y - drawing_center[1]) +
drawing_center[1],
};
}

// For both Tokens and Drawings, we will start by determining if 'position' is inside the placeable's bounding box
if (Number.between(position.x, x, x + width) && Number.between(position.y, y, y + height)) {
// If the position is within the bounding box, then we can perform additional tests to see if it is within the drawing's geometry
if (type == "r") {
if (type == 'r') {
// A rectangle is its own bounding box, so we've already determined that it contains the position
return true
}
else if (type == "e") {
return true;
} else if (type == 'e') {
// All points inside an ellipse satisfy the following inequality
return ((position.x - x - 0.5*width)**2 * (0.5*height)**2 + (position.y - y - 0.5*height)**2 * (0.5*width)**2) <= ((0.5*width)**2 * (0.5*height)**2)
}
else if (type == "p" || type == "f") {
return (
(position.x - x - 0.5 * width) ** 2 * (0.5 * height) ** 2 +
(position.y - y - 0.5 * height) ** 2 * (0.5 * width) ** 2 <=
(0.5 * width) ** 2 * (0.5 * height) ** 2
);
} else if (type == 'p' || type == 'f') {
// Point and freehand drawings have point data that we can use to perform a point inclusion in polygon test as described in https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
// To get the pixel coordinates of the vertices, we have to add the drawing origin
let vertices = placeable.data.points.map(point => [point[0]+x, point[1]+y])
let isInside = false
let i = 0, j = vertices.length - 1;
let vertices = placeable.data.points.map((point) => [point[0] + x, point[1] + y]);
let isInside = false;
let i = 0,
j = vertices.length - 1;
for (i, j; i < vertices.length; j = i++) {
if ( (vertices[i][1] > position.y) != (vertices[j][1] > position.y) && (position.x < (vertices[j][0] - vertices[i][0]) * (position.y - vertices[i][1]) / (vertices[j][1] - vertices[i][1]) + vertices[i][0])) {
isInside = !isInside;
}
if (
vertices[i][1] > position.y != vertices[j][1] > position.y &&
position.x <
((vertices[j][0] - vertices[i][0]) * (position.y - vertices[i][1])) /
(vertices[j][1] - vertices[i][1]) +
vertices[i][0]
) {
isInside = !isInside;
}
}
return isInside
}
else {
return isInside;
} else {
// If this runs, then foundry added a new drawing type. Guess we'll default to just using the bounding box
return true
return true;
}
}
else {
} else {
// If the position is outside the bounding box, then we know immediately that the drawing does not contain it
return false
return false;
}
}

// TODO other specific placeable case NoteDocument, WallDocument

else {
// Other types of placeables don't have an area that could contain the position
let width = placeable.w || placeable.data?.width || placeable.width;
Expand All @@ -1303,7 +1340,6 @@ export class TriggerHappy {
}
return Number.between(position.x, x, x + width) && Number.between(position.y, y, y + height);
}

}

_getPlaceablesAt(placeables, position) {
Expand Down Expand Up @@ -1360,10 +1396,11 @@ export class TriggerHappy {
return triggers.filter((trigger) => drawings.some((drawing) => this._isDrawingTrigger(drawing, trigger, type)));
}

_onCanvasReady(canvas) {
async _onCanvasReady(canvas) {
const triggers = this.triggers.filter((trigger) => this._isSceneTrigger(canvas.scene, trigger));
this._executeTriggers(triggers);
await this._executeTriggers(triggers);
canvas.stage.on('mousedown', (ev) => this._onMouseDown(ev));
this._parseJournals();
}

_getMousePosition(event) {
Expand Down

0 comments on commit 494785d

Please sign in to comment.