TheMillsFabula/scripts/fortune_integration.mjs
trotFunky 6d4c5b74dc Fortune: Fix indenting in character sheet code
When moving this part of the code to a dedicated function, the IDE
"helpfully"  "fixed" the indentation, making the organization
of this part of the code less clear.

Re-introduce the indentation to make the structure clearer.
2025-07-06 21:48:18 +01:00

118 lines
5 KiB
JavaScript

import * as MillsFabula from "./mills_fabula.mjs";
const fortune_flag = "char_fortune"
const FortuneSettings = Object.freeze({
EnableFortuneDisplay: "enableFortuneDisplay",
})
/**
* Find all the characters in the group sheet and insert their Fortune amount
* within the resources, copying the system's style.
* @param {DocumentSheet|foundry.applications.api.DocumentSheetV2} app The source Application
* @param {HTMLElement} html The rendered HTML
* @returns {Promise<void>} Does not return
*/
async function party_sheet_fortune(app, html) {
if (!(app instanceof foundry.applications.api.DocumentSheetV2))
html = html[0] /* Retrieve the internal DOM element */
else
console.warn("AppV2 is not properly tested yet")
let character_divs = html.querySelectorAll(".section-container.plate.character")
for (let char_div of character_divs) {
/** @type{FUActor} */
let actor = await fromUuid(char_div.getAttribute("data-uuid"))
let resources = char_div.getElementsByClassName("resources")[0]
let icon = document.createElement("i");
icon.classList.add("fuk", "icon-aff", "mf-fortune")
icon.setAttribute("data-tooltip", game.i18n.localize('MF.Fortune.Name'))
/* Insert the Fortune elements before Fabula points, so find it */
let fp_icon = resources.getElementsByClassName("fu-fp")[0]
resources.insertBefore(icon, fp_icon)
/* The system also has whitespace for spacing */
resources.insertBefore(
document.createTextNode(` ${actor.getFlag(MillsFabula.id, fortune_flag) ?? 0} `),
fp_icon)
}
}
/**
* Insert a Fortune input at the top of the character sheet,
* following the same styling as the system for Fabula points.
* @param {DocumentSheet|foundry.applications.api.DocumentSheetV2} app The source Application
* @param {HTMLElement} html The rendered HTML
*/
function character_sheet_fortune(app, html) {
/* Only player characters have Fortune. */
if (app.actor.type !== "character")
return
if (!(app instanceof foundry.applications.api.DocumentSheetV2))
html = html[0] /* Retrieve the internal DOM element */
else
console.warn("AppV2 is not properly tested yet")
let resources_div = html.getElementsByClassName("header-center")[0].lastElementChild
resources_div.classList.replace("grid-3col", "grid-4col")
/*
* We want the Fortune to have the same importance/weight as Fabula points,
* so we'll copy the style and bundle them in a 2 column div.
*/
/* TODO: This all should probably be some form of template ? */
let fortune_fabula_div = document.createElement("div")
fortune_fabula_div.classList.add("grid", "grid-2col")
let fortune_div = document.createElement("div")
fortune_div.classList.add("flex-group-center", "resource-content")
let fortune_label = document.createElement("span")
fortune_label.classList.add("resource-label-l")
let fortune_icon = document.createElement("i")
fortune_icon.classList.add("fas", "fa-leaf", "icon")
fortune_label.appendChild(fortune_icon)
fortune_label.appendChild(document.createTextNode(game.i18n.localize('MF.Fortune.Name')))
fortune_div.appendChild(fortune_label)
let fortune_data_div = document.createElement("div")
fortune_data_div.classList.add("buttons-inc")
let fortune_input = foundry.applications.fields.createNumberInput({
/*
* By setting the name of the field using this format, the backing
* flag will automatically be picked up by Foundry's code and updated
* when the field is changed on the sheet.
* This works even if it wasn't set previously !
*/
name: `flags.${MillsFabula.id}.${fortune_flag}`,
value: app.document.getFlag(MillsFabula.id, fortune_flag) ?? 0,
step: 1,
min: 0,
type: "number"
})
fortune_input.classList.add("fp-resource-inputs")
fortune_data_div.appendChild(fortune_input)
fortune_div.appendChild(fortune_data_div)
fortune_fabula_div.appendChild(fortune_div)
/* The last element should always be the Fabula Points div. */
fortune_fabula_div.appendChild(resources_div.lastElementChild)
resources_div.appendChild(fortune_fabula_div)
}
Hooks.once("init", () => {
game.settings.register(MillsFabula.id, FortuneSettings.EnableFortuneDisplay, {
name: game.i18n.localize('MF.Fortune.Settings.DisplayEnabled.Name'),
hint: game.i18n.localize('MF.Fortune.Settings.DisplayEnabled.Hint'),
type: Boolean,
config: true,
scope: 'world',
requiresReload: true,
default: true
});
if (game.settings.get(MillsFabula.id, FortuneSettings.EnableFortuneDisplay)) {
Hooks.on("renderFUPartySheet", party_sheet_fortune)
Hooks.on("renderFUStandardActorSheet", character_sheet_fortune)
}
})