This commit is contained in:
WBHarry 2026-01-17 20:30:32 +01:00
parent 91d22292e1
commit 08c9c86d48
5 changed files with 95 additions and 52 deletions

View file

@ -2,7 +2,9 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
static DEFAULT_OPTIONS = {
...super.DEFAULT_OPTIONS,
actions: {
changeMode: DhRollTableSheet.#onChangeMode,
drawResult: DhRollTableSheet.#onDrawResult,
resetResults: DhRollTableSheet.#onResetResults,
addFormula: DhRollTableSheet.#addFormula,
removeFormula: DhRollTableSheet.#removeFormula
}
@ -44,16 +46,18 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
switch (partId) {
case 'sheet':
context.altFormula = this.daggerheartFlag.altFormula;
context.usesAltFormula = Object.keys(this.daggerheartFlag.altFormula).length > 0;
context.altFormulaOptions = {
'': { name: this.daggerheartFlag.formulaName },
...this.daggerheartFlag.altFormula
};
context.activeAltFormula = this.daggerheartFlag.activeAltFormula;
context.selectedFormula = context.activeAltFormula
? this.daggerheartFlag.altFormula[context.activeAltFormula].formula
: this.document.formula;
context.selectedFormula = this.daggerheartFlag.getActiveFormula(this.document.formula);
break;
case 'header':
context.altFormula = this.daggerheartFlag.altFormula;
context.usesAltFormula = Object.keys(this.daggerheartFlag.altFormula).length > 0;
context.altFormulaOptions = {
'': { name: this.daggerheartFlag.formulaName },
...this.daggerheartFlag.altFormula
@ -70,48 +74,27 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
return context;
}
/* -------------------------------------------- */
/* Flag SystemData update methods */
/* -------------------------------------------- */
async updateSystemField(event) {
const { dataset, value } = event.target;
await this.daggerheartFlag.updateSource({ [dataset.path]: value });
this.render({ internalRefresh: true });
}
/** @override */
async _processSubmitData(event, form, submitData, options) {
/* RollTable sends an empty dummy event when swapping from view/edit first time */
if (Object.keys(submitData).length) {
if (!submitData.flags.daggerheart.altFormula) submitData.flags.daggerheart.altFormula = {};
for (const formulaKey of Object.keys(this.document._source.flags.daggerheart?.altFormula ?? {})) {
if (!submitData.flags.daggerheart.altFormula[formulaKey]) {
submitData.flags.daggerheart.altFormula[`-=${formulaKey}`] = null;
}
}
getSystemFlagUpdate() {
const deleteUpdate = Object.keys(this.document._source.flags.daggerheart?.altFormula ?? {}).reduce(
(acc, formulaKey) => {
if (!this.daggerheartFlag.altFormula[formulaKey]) acc.altFormula[`-=${formulaKey}`] = null;
await this.daggerheartFlag.updateSource(submitData.flags.daggerheart);
}
return acc;
},
{ altFormula: {} }
);
super._processSubmitData(event, form, submitData, options);
}
/**
* Roll and draw a TableResult.
* @this {RollTableSheet}
* @type {ApplicationClickAction}
*/
static async #onDrawResult(_event, button) {
if (this.form) await this.submit({ operation: { render: false } });
button.disabled = true;
const table = this.document;
const selectedFormula = this.daggerheartFlag.getActiveFormula(this.document.formula);
const tableRoll = await table.roll({ selectedFormula });
const draws = table.getResultsForRoll(tableRoll.roll.total);
if (draws.length > 0) {
if (game.settings.get('core', 'animateRollTable')) await this._animateRoll(draws);
await table.draw(tableRoll);
}
// Reenable the button if drawing with replacement since the draw won't trigger a sheet re-render
if (table.replacement) button.disabled = false;
return foundry.utils.mergeObject(this.daggerheartFlag.toObject(), deleteUpdate);
}
static async #addFormula() {
@ -127,4 +110,61 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
});
this.render({ internalRefresh: true });
}
/* -------------------------------------------- */
/* Extended RollTable methods */
/* -------------------------------------------- */
/**
* Alternate between view and edit modes.
* @this {RollTableSheet}
* @type {ApplicationClickAction}
*/
static async #onChangeMode() {
this.mode = this.isEditMode ? 'view' : 'edit';
await this.document.update(this.getSystemFlagUpdate());
await this.render({ internalRefresh: true });
}
/** @inheritdoc */
async _processSubmitData(event, form, submitData, options) {
/* RollTable sends an empty dummy event when swapping from view/edit first time */
if (Object.keys(submitData).length) {
if (!submitData.flags) submitData.flags = { daggerheart: {} };
submitData.flags.daggerheart = this.getSystemFlagUpdate();
}
super._processSubmitData(event, form, submitData, options);
}
/** @inheritdoc */
static async #onResetResults() {
await this.document.update(this.getSystemFlagUpdate());
await this.document.resetResults();
}
/**
* Roll and draw a TableResult.
* @this {RollTableSheet}
* @type {ApplicationClickAction}
*/
static async #onDrawResult(_event, button) {
if (this.form) await this.submit({ operation: { render: false } });
button.disabled = true;
const table = this.document;
await this.document.update(this.getSystemFlagUpdate());
/* Sending in the currently selectd activeFormula to table.roll to use as the formula */
const selectedFormula = this.daggerheartFlag.getActiveFormula(this.document.formula);
const tableRoll = await table.roll({ selectedFormula });
const draws = table.getResultsForRoll(tableRoll.roll.total);
if (draws.length > 0) {
if (game.settings.get('core', 'animateRollTable')) await this._animateRoll(draws);
await table.draw(tableRoll);
}
// Reenable the button if drawing with replacement since the draw won't trigger a sheet re-render
if (table.replacement) button.disabled = false;
}
}

View file

@ -28,7 +28,7 @@ export default class DhRollTable extends foundry.abstract.TypeDataModel {
}
getActiveFormula(baseFormula) {
return this.activeAltFormula ? (this.altFormula[this.activeAltFormula].formula ?? baseFormula) : baseFormula;
return this.activeAltFormula ? (this.altFormula[this.activeAltFormula]?.formula ?? baseFormula) : baseFormula;
}
static getDefaultFormula = () => ({

View file

@ -2,15 +2,16 @@
<img src="{{source.img}}" data-action="editImage" data-edit="img" alt="{{localize "DOCUMENT.FIELDS.img.label"}}">
<input type="text" name="name" value="{{source.name}}" placeholder="{{localize "DOCUMENT.FIELDS.name.label"}}" aria-label="{{localize "DOCUMENT.FIELDS.name.label"}}">
<div class="form-group">
<label>{{localize "Formula"}}</label>
<div class="form-fields">
<select class="system-update-field" data-path="activeAltFormula">
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
</select>
{{#if usesAltFormula}}
<div class="form-group">
<label>{{localize "Formula"}}</label>
<div class="form-fields">
<select class="system-update-field" data-path="activeAltFormula">
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
</select>
</div>
</div>
</div>
{{/if}}
<button data-action="changeMode">
<i class="fa-solid fa-eye" inert></i>

View file

@ -2,9 +2,11 @@
<img src="{{document.img}}" alt="{{localize "DOCUMENT.FIELDS.img.label"}}">
<h1>{{document.name}}</h1>
<div class="roll-table-view-formula-container">
<select class="system-update-field" data-path="activeAltFormula">
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
</select>
{{#if usesAltFormula}}
<select class="system-update-field" data-path="activeAltFormula">
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
</select>
{{/if}}
<h4>{{selectedFormula}}</h4>
</div>
<button data-action="changeMode">

View file

@ -7,12 +7,12 @@
<span>{{localize "DAGGERHEART.ROLLTABLES.FIELDS.formulaName.label"}}</span>
<span>{{localize "Formula Roll"}}</span>
<span></span>
{{formInput systemFields.formulaName value=@root.formulaName name="flags.daggerheart.formulaName"}}
<input type="text" value="{{@root.formulaName}}" class="system-update-field" data-path="formulaName" />
{{formInput fields.formula value=source.formula placeholder=formulaPlaceholder rootId=rootId}}
<button class="formula-button" data-action="addFormula"><i class="fa-solid fa-plus"></i></button>
{{#each @root.altFormula as | formula key |}}
{{formInput @root.systemFields.altFormula.element.fields.name value=formula.name name=(concat "flags.daggerheart.altFormula." key ".name")}}
{{formInput @root.systemFields.altFormula.element.fields.formula value=formula.formula name=(concat "flags.daggerheart.altFormula." key ".formula")}}
<input type="text" value="{{formula.name}}" class="system-update-field" data-path="{{concat "altFormula." key ".name"}}" />
<input type="text" value="{{formula.formula}}" class="system-update-field" data-path="{{concat "altFormula." key ".formula"}}" />
<a class="formula-button" data-action="removeFormula" data-key="{{key}}"><i class="fa-solid fa-trash"></i></a>
{{/each}}
</div>