-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCombat.js
188 lines (177 loc) · 6.8 KB
/
Combat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { libWrapper } from "./shim.js";
let CombatantsInitiative = {};
let firstInitiative = 0;
let lastInitiative = 0;
function getInitiatives(combatTracker) {
let initiatives = $(combatTracker).find(".initiative");
if (!initiatives.length > 0) {
return;
}
let firstPlace = Number(initiatives[0].innerText);
let lastPlace = Number(initiatives[initiatives.length - 1].innerText);
if (firstPlace > lastPlace) {
firstInitiative = firstPlace + 1;
lastInitiative = lastPlace - 1;
} else if (firstPlace < lastPlace) {
firstInitiative = firstPlace - 1;
lastInitiative = lastPlace + 1;
}
}
class FurnaceCombatQoL {
static renderCombatTracker(tracker, html, data) {
if (game.user.role < parseInt(game.settings.get("initiative-double-click", "player-access"))) return;
const revertButton = game.settings.get("initiative-double-click", "revertButton");
const orderButtons = game.settings.get("initiative-double-click", "orderButtons");
const combatTracker = html.find("#combat-tracker");
if (!combatTracker[0].children.length) return;
getInitiatives(combatTracker);
const TokenInitiative = html.find(".token-initiative");
TokenInitiative.find(".initiative").on("dblclick", FurnaceCombatQoL._onInitiativeDblClick);
TokenInitiative.find(".combatant-control.roll").on("contextmenu", (event) => {
const cid = event.target.closest(".combatant").dataset.combatantId;
game.combat.setInitiative(cid, 0);
});
if (orderButtons || revertButton) {
TokenInitiative.before("<div class='double-click-initiative flexrow'></div>");
const dblClick = html.find(".double-click-initiative");
if (orderButtons) {
dblClick.append(`
<div class="button double-click-initiative-first"
data-tooltip="${game.i18n.localize("initiative-double-click.buttons.top")}"
data-tooltip-direction="UP"
>
<a>
<i class="fas fa-arrow-up"></i>
</a>
</div>
<div class="button double-click-initiative-last"
data-tooltip="${game.i18n.localize("initiative-double-click.buttons.bottom")}"
data-tooltip-direction="UP"
>
<a>
<i class="fas fa-arrow-down"></i>
</a>
</div>
`);
html.find(".double-click-initiative-first a").on("click", FurnaceCombatQoL._onClickFirst);
html.find(".double-click-initiative-last a").on("click", FurnaceCombatQoL._onClickLast);
}
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 = game.i18n.format("initiative-double-click.buttons.previous", { previous: CombatantsInitiative[cid].previous });
} else {
div.classList.add("double-click-disabled");
}
let i = document.createElement("i");
i.classList.add("fas", "fa-undo");
let a = document.createElement("a");
a.appendChild(i);
div.appendChild(a);
if (orderButtons) {
combatant.querySelector(".double-click-initiative-last").before(div);
} else combatant.querySelector(".double-click-initiative").append(div);
html.find(".double-click-initiative-revert a").on("click", FurnaceCombatQoL._onClickRevert);
}
}
}
}
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;
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();
input.on("change", (ev) => {
CombatantsInitiative[cid] = { previous: combatant.initiative };
game.combat.setInitiative(cid, input.val());
});
input.on("focusout", (ev) => game.combats.render());
}
static _onClickRevert(event) {
let html = $(event.target).closest(".combatant");
const cid = html.data("combatant-id");
let combatant = game.combat.combatants.get(cid);
if (!combatant.isOwner) return;
const previous = CombatantsInitiative[cid].previous;
CombatantsInitiative[cid] = { previous: combatant.initiative };
game.combat.setInitiative(cid, previous);
}
static _onClickFirst(event) {
let html = $(event.target).closest(".combatant");
const cid = html.data("combatant-id");
let combatant = game.combat.combatants.get(cid);
if (!combatant.isOwner) return;
CombatantsInitiative[cid] = { previous: combatant.initiative };
game.combat.setInitiative(cid, firstInitiative);
}
static _onClickLast(event) {
let html = $(event.target).closest(".combatant");
const cid = html.data("combatant-id");
let combatant = game.combat.combatants.get(cid);
if (!combatant.isOwner) return;
CombatantsInitiative[cid] = { previous: combatant.initiative };
game.combat.setInitiative(cid, lastInitiative);
}
}
Hooks.on("renderCombatTracker", FurnaceCombatQoL.renderCombatTracker);
Hooks.once("init", () => {
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"),
scope: "world",
config: true,
type: String,
default: "4",
choices: {
1: "USER.RolePlayer",
2: "USER.RoleTrusted",
3: "USER.RoleAssistant",
4: "USER.RoleGamemaster",
},
});
game.settings.register("initiative-double-click", "revertButton", {
name: game.i18n.localize("initiative-double-click.settings.revertButton.name"),
hint: game.i18n.localize("initiative-double-click.settings.revertButton.hint"),
scope: "world",
config: true,
type: Boolean,
default: false,
requiresReload: true,
});
game.settings.register("initiative-double-click", "orderButtons", {
name: game.i18n.localize("initiative-double-click.settings.orderButtons.name"),
hint: game.i18n.localize("initiative-double-click.settings.orderButtons.hint"),
scope: "world",
config: true,
type: Boolean,
default: false,
requiresReload: true,
});
});
function numDigits(x) {
return (Math.log10((x ^ (x >> 31)) - (x >> 31)) | 0) + 1;
}
function wrappedOnCombatantMouseDown(wrapped, event) {
if (event.target.className !== "initiative") {
wrapped(event);
}
}
Hooks.once("ready", () => {
libWrapper.register("initiative-double-click", "CombatTracker.prototype._onCombatantMouseDown", wrappedOnCombatantMouseDown, "MIXED");
});