Skip to content

Commit

Permalink
General improvements
Browse files Browse the repository at this point in the history
- Added hold click behavior.
- Added localization for the buttons
- Improved buttons' CSS
  • Loading branch information
mclemente committed Aug 16, 2022
1 parent c125db9 commit 710d517
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 48 deletions.
123 changes: 78 additions & 45 deletions Combat.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { libWrapper } from "./shim.js";
let CombatantsInitiative = {};
let firstInitiative = 0;
let lastInitiative = 0;
let disableOpenSheet;

function getInitiatives(combatTracker) {
combatTracker = combatTracker[0].children;
Expand Down Expand Up @@ -30,21 +31,26 @@ class FurnaceCombatQoL {
let TokenInitiative = html.find(".token-initiative");

if (orderButtons) {
TokenInitiative.before(`<div class="button double-click-initiative-first" title="Move to Top"><a>
<i class="fas fa-arrow-up"></i>
</a></div>`);
TokenInitiative.before(`<div class="button double-click-initiative-first" title="${game.i18n.localize("initiative-double-click.buttons.top")}"><a>
<i class="fas fa-arrow-up"></i>
</a></div>`);
html.find(".double-click-initiative-first a").on("click", FurnaceCombatQoL._onClickFirst);
}

TokenInitiative.off("dblclick").on("dblclick", FurnaceCombatQoL._onInitiativeDblClick);
if (disableOpenSheet) {
TokenInitiative.off("dblclick").on("dblclick", FurnaceCombatQoL._onInitiativeDblClick);
} else {
TokenInitiative.off("pointerdown").on("pointerdown", FurnaceCombatQoL._onPointerDown);
TokenInitiative.off("pointerup").on("pointerup", FurnaceCombatQoL._onPointerUp);
}

if (revertButton) {
for (let combatant of html.find("#combat-tracker li.combatant")) {
const cid = combatant.dataset.combatantId;
let div = document.createElement("div");
div.classList.add("button", "double-click-initiative-revert");
if (CombatantsInitiative[cid]) {
div.title = "Previous Value: " + CombatantsInitiative[cid].previous;
div.title = game.i18n.format("initiative-double-click.buttons.previous", { previous: CombatantsInitiative[cid].previous });
} else {
div.classList.add("double-click-disabled");
}
Expand All @@ -58,21 +64,41 @@ class FurnaceCombatQoL {
}
}
if (orderButtons) {
TokenInitiative.before(`<div class="button double-click-initiative-last" title="Move to Bottom"><a>
<i class="fas fa-arrow-down"></i>
</a></div>`);
TokenInitiative.before(`<div class="button double-click-initiative-last" title="${game.i18n.localize("initiative-double-click.buttons.bottom")}"><a>
<i class="fas fa-arrow-down"></i>
</a></div>`);
html.find(".double-click-initiative-last a").on("click", FurnaceCombatQoL._onClickLast);
}
}
static _onPointerDown(event) {
event.stopPropagation();
event.preventDefault();
const now = Date.now();
const dt = now - this._clickTime;
this._clickTime = now;
}
static _onPointerUp(event) {
const now = Date.now();
const dt = now - this._clickTime;
if (dt >= 500) {
FurnaceCombatQoL._changeInitiative(event);
}
}
static _onInitiativeDblClick(event) {
event.stopPropagation();
event.preventDefault();
FurnaceCombatQoL._changeInitiative(event);
}
static _changeInitiative(event) {
let html = $(event.target).closest(".combatant");
const cid = html.data("combatant-id");
let initiative = html.find(".token-initiative");
let combatant = game.combat.combatants.get(cid);
if (!combatant.isOwner) return;
let input = $(`<input class="initiative" style="width: 90%" value="${combatant.initiative}"/>`);
const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
// Width = 50-95, increasing by 15 for every digit over 2
const width = clamp(20 + 15 * numDigits(combatant.initiative), 50, 95);
let input = $(`<input class="initiative" style="width: ${width}%" value="${combatant.initiative}"/>`);
initiative.off("dblclick");
initiative.empty().append(input);
input.focus().select();
Expand Down Expand Up @@ -111,6 +137,24 @@ class FurnaceCombatQoL {

Hooks.on("renderCombatTracker", FurnaceCombatQoL.renderCombatTracker);
Hooks.once("init", () => {
game.settings.register("initiative-double-click", "disableOpenSheet", {
name: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.name"),
hint: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.hint"),
scope: "world",
config: true,
type: Number,
default: true,
choices: {
0: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.choices.0"),
1: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.choices.1"),
},
onChange: (value) => {
disableOpenSheet = value;
if (value) libWrapper.register("initiative-double-click", "CombatTracker.prototype._onCombatantMouseDown", wrappedOnCombatantMouseDown, "OVERRIDE");
else libWrapper.unregister("initiative-double-click", "CombatTracker.prototype._onCombatantMouseDown");
game.combats.render();
},
});
game.settings.register("initiative-double-click", "player-access", {
name: game.i18n.localize("initiative-double-click.settings.player-access.name"),
hint: game.i18n.localize("initiative-double-click.settings.player-access.hint"),
Expand Down Expand Up @@ -142,43 +186,32 @@ Hooks.once("init", () => {
type: Boolean,
default: false,
});
game.settings.register("initiative-double-click", "disableOpenSheet", {
name: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.name"),
hint: game.i18n.localize("initiative-double-click.settings.disableOpenSheet.hint"),
scope: "world",
config: true,
type: Boolean,
default: true,
});
libWrapper.register(
"initiative-double-click",
"CombatTracker.prototype._onCombatantMouseDown",
function (wrapped, ...args) {
event.preventDefault();
});

const li = event.currentTarget;
const combatant = this.viewed.combatants.get(li.dataset.combatantId);
const token = combatant.token;
if (!combatant.actor?.testUserPermission(game.user, "OBSERVED")) return;
const now = Date.now();
function wrappedOnCombatantMouseDown(wrapped, ...args) {
event.preventDefault();

// Handle double-left click to open sheet
if (!game.settings.get("initiative-double-click", "disableOpenSheet")) {
const dt = now - this._clickTime;
this._clickTime = now;
if (dt <= 250) return combatant.actor?.sheet.render(true);
}
const li = event.currentTarget;
const combatant = this.viewed.combatants.get(li.dataset.combatantId);
const token = combatant.token;
if (!combatant.actor?.testUserPermission(game.user, "OBSERVED")) return;

// Control and pan to Token object
if (token?.object) {
token.object?.control({ releaseOthers: true });
if (game.version > 10) {
return canvas.animatePan(token.object.center);
} else {
return canvas.animatePan({ x: token.data.x, y: token.data.y });
}
}
},
"OVERRIDE"
);
// Control and pan to Token object
if (token?.object) {
token.object?.control({ releaseOthers: true });
if (game.version > 10) {
return canvas.animatePan(token.object.center);
} else {
return canvas.animatePan({ x: token.data.x, y: token.data.y });
}
}
}

function numDigits(x) {
return (Math.log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}

Hooks.once("ready", () => {
disableOpenSheet = game.settings.get("initiative-double-click", "disableOpenSheet");
if (disableOpenSheet) libWrapper.register("initiative-double-click", "CombatTracker.prototype._onCombatantMouseDown", wrappedOnCombatantMouseDown, "OVERRIDE");
});
3 changes: 2 additions & 1 deletion initiative.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#combat li.combatant .double-click-initiative-first,
#combat li.combatant .double-click-initiative-last,
#combat li.combatant .double-click-initiative-revert {
flex: 0 0 48px;
flex: 0 0 16px;
text-align: center;
padding: 0px 2px;
}
#combat li.combatant .double-click-disabled {
color: grey;
Expand Down
13 changes: 11 additions & 2 deletions langs/en.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
{
"initiative-double-click": {
"buttons": {
"top": "Move to Top",
"bottom": "Move to Bottom",
"previous": "Previous Value: {previous}"
},
"settings": {
"disableOpenSheet": {
"name": "Disable Double-click Opening Sheets",
"hint": "Disables the default Foundry behavior of opening a character's when double-clicking it on the Combat Tracker."
"name": "Click Behavior",
"hint": "Sets the module's behavior. \"Double Click\" also disables the default Foundry behavior of opening a character's sheet.",
"choices": {
"0": "Hold click",
"1": "Double click"
}
},
"player-access": {
"name": "Minimum Permission Level",
Expand Down

0 comments on commit 710d517

Please sign in to comment.