Skip to content

Commit

Permalink
🎸 feat |CombatMove|Add button to reset combat move history
Browse files Browse the repository at this point in the history
  • Loading branch information
caewok committed Jul 11, 2024
1 parent ccf948e commit df1db4a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 3 deletions.
4 changes: 3 additions & 1 deletion languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,7 @@
"elevationruler.controls.pathfinding-control.name": "Use Pathfinding",

"elevationruler.drawingconfig.movementPenalty.name": "Movement Bonus/Penalty",
"elevationruler.drawingconfig.movementPenalty.hint": "Set to 1 for no penalty. Values greater than one penalize movement by that percent; values less than one effectively grant a bonus to movement. For example, set to 2 to double movement through this area."
"elevationruler.drawingconfig.movementPenalty.hint": "Set to 1 for no penalty. Values greater than one penalize movement by that percent; values less than one effectively grant a bonus to movement. For example, set to 2 to double movement through this area.",

"elevationruler.clearMovement": "Clear Combatant Movement"
}
56 changes: 56 additions & 0 deletions scripts/CombatTracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* globals
game
*/
/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }] */

import { TEMPLATES, MODULE_ID, FLAGS } from "./const.js";

// Patches for the Combat tracker

export const PATCHES = {};
PATCHES.BASIC = {};

import { injectConfiguration, renderTemplateSync } from "./util.js";

// ----- NOTE: Hooks ----- //

/**
* Hook renderCombatTracker
* Add a button at the top left to clear the current token's movement.
* @param {Application} application The Application instance being rendered
* @param {jQuery} html The inner HTML of the document that will be displayed and may be modified
* @param {object} data The object of data used when rendering the application
*/
function renderCombatTracker(app, html, data) {
if ( !game.user.isGM ) return;
const encounterControlsDiv = html.find(".encounter-controls")[0];
if ( !encounterControlsDiv ) return;
const combatButtons = encounterControlsDiv.getElementsByClassName("combat-button");
if ( !combatButtons.length ) return;
const dividers = encounterControlsDiv.getElementsByTagName("h3");
if ( !dividers.length ) return;

const myHtml = renderTemplateSync(TEMPLATES.COMBAT_TRACKER, data);
// const aElem = document.createElement("a");
// aElem.innerHTML = myHtml;
dividers[0].insertAdjacentHTML("beforebegin", myHtml);

// const npcButton = Object.values(combatButtons).findIndex(b => b.dataset.control === "rollNPC");
// const findString = ".combat-button[data-control='rollNPC']";
// await injectConfiguration(app, html, data, template, findString);

html.find(`.${MODULE_ID}`).click(ev => clearMovement.call(app, ev));
}

PATCHES.BASIC.HOOKS = { renderCombatTracker };

async function clearMovement(event) {
event.preventDefault();
event.stopPropagation();
const combat = this.viewed;
const tokenD = combat?.combatant?.token;
if ( !tokenD ) return;
await tokenD.unsetFlag(MODULE_ID, FLAGS.MOVEMENT_HISTORY);
ui.notifications.notify(`Combat movement history for ${tokenD.name} reset.`);

}
3 changes: 2 additions & 1 deletion scripts/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const MODULE_ID = "elevationruler";
export const EPSILON = 1e-08;

export const TEMPLATES = {
DRAWING_CONFIG: `modules/${MODULE_ID}/templates/drawing-config.html`
DRAWING_CONFIG: `modules/${MODULE_ID}/templates/drawing-config.html`,
COMBAT_TRACKER: `modules/${MODULE_ID}/templates/combat-tracker.html`
};

export const FLAGS = {
Expand Down
4 changes: 3 additions & 1 deletion scripts/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ui

import { Settings } from "./settings.js";
import { initializePatching, PATCHER } from "./patching.js";
import { MODULE_ID, MOVEMENT_TYPES, MOVEMENT_BUTTONS, SPEED } from "./const.js";
import { MODULE_ID, MOVEMENT_TYPES, MOVEMENT_BUTTONS, SPEED, TEMPLATES } from "./const.js";
import { defaultHPAttribute } from "./system_attributes.js";
import { registerGeometry } from "./geometry/registration.js";

Expand Down Expand Up @@ -185,6 +185,8 @@ Hooks.once("init", function() {

Settings
};

loadTemplates(Object.values(TEMPLATES)).then(_value => log(`Templates loaded.`)); // eslint-disable-line no-unused-vars
});

// Setup is after init; before ready.
Expand Down
2 changes: 2 additions & 0 deletions scripts/patching.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { PATCHES as PATCHES_TokenPF } from "./pathfinding/Token.js";

// Movement tracking
import { PATCHES as PATCHES_TokenHUD } from "./token_hud.js";
import { PATCHES as PATCHES_CombatTracker } from "./CombatTracker.js";

// Settings
import { PATCHES as PATCHES_ClientSettings } from "./ModuleSettingsAbstract.js";
Expand All @@ -27,6 +28,7 @@ const mergeObject = foundry.utils.mergeObject;
const PATCHES = {
ClientKeybindings: PATCHES_ClientKeybindings,
ClientSettings: PATCHES_ClientSettings,
CombatTracker: PATCHES_CombatTracker,
["foundry.canvas.edges.CanvasEdges"]: PATCHES_CanvasEdges,
DrawingConfig: PATCHES_DrawingConfig,
Ruler: PATCHES_Ruler,
Expand Down
17 changes: 17 additions & 0 deletions scripts/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,20 @@ export function * iterateGridUnderLine(origin, destination, { reverse = false }
tPrior = t;
}
}

/**
* Synchronous version of renderTemplate.
* Requires the template to be already loaded.
* @param {string} path The file path to the target HTML template
* @param {Object} data A data object against which to compile the template
* @returns {string|undefined} Returns the compiled and rendered template as a string
*/
export function renderTemplateSync(path, data) {
if ( !Object.hasOwn(Handlebars.partials, path) ) return;
const template = Handlebars.partials[path];
return template(data || {}, {
allowProtoMethodsByDefault: true,
allowProtoPropertiesByDefault: true
});
}

3 changes: 3 additions & 0 deletions templates/combat-tracker.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<a class="combat-button elevationruler" aria-label="{{localize 'elevationruler.clearMovement'}}" role="button" data-tooltip="elevationruler.clearMovement" data-control="clearMovement" {{#unless turns}}disabled{{/unless}}>
<i class="fas fa-person-walking-arrow-loop-left"></i>
</a>

0 comments on commit df1db4a

Please sign in to comment.