mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-03-07 14:36:13 +01:00
.
This commit is contained in:
parent
91d22292e1
commit
08c9c86d48
5 changed files with 95 additions and 52 deletions
|
|
@ -2,7 +2,9 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
|
||||||
static DEFAULT_OPTIONS = {
|
static DEFAULT_OPTIONS = {
|
||||||
...super.DEFAULT_OPTIONS,
|
...super.DEFAULT_OPTIONS,
|
||||||
actions: {
|
actions: {
|
||||||
|
changeMode: DhRollTableSheet.#onChangeMode,
|
||||||
drawResult: DhRollTableSheet.#onDrawResult,
|
drawResult: DhRollTableSheet.#onDrawResult,
|
||||||
|
resetResults: DhRollTableSheet.#onResetResults,
|
||||||
addFormula: DhRollTableSheet.#addFormula,
|
addFormula: DhRollTableSheet.#addFormula,
|
||||||
removeFormula: DhRollTableSheet.#removeFormula
|
removeFormula: DhRollTableSheet.#removeFormula
|
||||||
}
|
}
|
||||||
|
|
@ -44,16 +46,18 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
|
||||||
|
|
||||||
switch (partId) {
|
switch (partId) {
|
||||||
case 'sheet':
|
case 'sheet':
|
||||||
|
context.altFormula = this.daggerheartFlag.altFormula;
|
||||||
|
context.usesAltFormula = Object.keys(this.daggerheartFlag.altFormula).length > 0;
|
||||||
context.altFormulaOptions = {
|
context.altFormulaOptions = {
|
||||||
'': { name: this.daggerheartFlag.formulaName },
|
'': { name: this.daggerheartFlag.formulaName },
|
||||||
...this.daggerheartFlag.altFormula
|
...this.daggerheartFlag.altFormula
|
||||||
};
|
};
|
||||||
context.activeAltFormula = this.daggerheartFlag.activeAltFormula;
|
context.activeAltFormula = this.daggerheartFlag.activeAltFormula;
|
||||||
context.selectedFormula = context.activeAltFormula
|
context.selectedFormula = this.daggerheartFlag.getActiveFormula(this.document.formula);
|
||||||
? this.daggerheartFlag.altFormula[context.activeAltFormula].formula
|
|
||||||
: this.document.formula;
|
|
||||||
break;
|
break;
|
||||||
case 'header':
|
case 'header':
|
||||||
|
context.altFormula = this.daggerheartFlag.altFormula;
|
||||||
|
context.usesAltFormula = Object.keys(this.daggerheartFlag.altFormula).length > 0;
|
||||||
context.altFormulaOptions = {
|
context.altFormulaOptions = {
|
||||||
'': { name: this.daggerheartFlag.formulaName },
|
'': { name: this.daggerheartFlag.formulaName },
|
||||||
...this.daggerheartFlag.altFormula
|
...this.daggerheartFlag.altFormula
|
||||||
|
|
@ -70,48 +74,27 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* -------------------------------------------- */
|
||||||
|
/* Flag SystemData update methods */
|
||||||
|
/* -------------------------------------------- */
|
||||||
|
|
||||||
async updateSystemField(event) {
|
async updateSystemField(event) {
|
||||||
const { dataset, value } = event.target;
|
const { dataset, value } = event.target;
|
||||||
await this.daggerheartFlag.updateSource({ [dataset.path]: value });
|
await this.daggerheartFlag.updateSource({ [dataset.path]: value });
|
||||||
this.render({ internalRefresh: true });
|
this.render({ internalRefresh: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @override */
|
getSystemFlagUpdate() {
|
||||||
async _processSubmitData(event, form, submitData, options) {
|
const deleteUpdate = Object.keys(this.document._source.flags.daggerheart?.altFormula ?? {}).reduce(
|
||||||
/* RollTable sends an empty dummy event when swapping from view/edit first time */
|
(acc, formulaKey) => {
|
||||||
if (Object.keys(submitData).length) {
|
if (!this.daggerheartFlag.altFormula[formulaKey]) acc.altFormula[`-=${formulaKey}`] = null;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.daggerheartFlag.updateSource(submitData.flags.daggerheart);
|
return acc;
|
||||||
}
|
},
|
||||||
|
{ altFormula: {} }
|
||||||
|
);
|
||||||
|
|
||||||
super._processSubmitData(event, form, submitData, options);
|
return foundry.utils.mergeObject(this.daggerheartFlag.toObject(), deleteUpdate);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async #addFormula() {
|
static async #addFormula() {
|
||||||
|
|
@ -127,4 +110,61 @@ export default class DhRollTableSheet extends foundry.applications.sheets.RollTa
|
||||||
});
|
});
|
||||||
this.render({ internalRefresh: true });
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ export default class DhRollTable extends foundry.abstract.TypeDataModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
getActiveFormula(baseFormula) {
|
getActiveFormula(baseFormula) {
|
||||||
return this.activeAltFormula ? (this.altFormula[this.activeAltFormula].formula ?? baseFormula) : baseFormula;
|
return this.activeAltFormula ? (this.altFormula[this.activeAltFormula]?.formula ?? baseFormula) : baseFormula;
|
||||||
}
|
}
|
||||||
|
|
||||||
static getDefaultFormula = () => ({
|
static getDefaultFormula = () => ({
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<img src="{{source.img}}" data-action="editImage" data-edit="img" alt="{{localize "DOCUMENT.FIELDS.img.label"}}">
|
<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"}}">
|
<input type="text" name="name" value="{{source.name}}" placeholder="{{localize "DOCUMENT.FIELDS.name.label"}}" aria-label="{{localize "DOCUMENT.FIELDS.name.label"}}">
|
||||||
|
{{#if usesAltFormula}}
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>{{localize "Formula"}}</label>
|
<label>{{localize "Formula"}}</label>
|
||||||
<div class="form-fields">
|
<div class="form-fields">
|
||||||
|
|
@ -11,6 +11,7 @@
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
<button data-action="changeMode">
|
<button data-action="changeMode">
|
||||||
<i class="fa-solid fa-eye" inert></i>
|
<i class="fa-solid fa-eye" inert></i>
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,11 @@
|
||||||
<img src="{{document.img}}" alt="{{localize "DOCUMENT.FIELDS.img.label"}}">
|
<img src="{{document.img}}" alt="{{localize "DOCUMENT.FIELDS.img.label"}}">
|
||||||
<h1>{{document.name}}</h1>
|
<h1>{{document.name}}</h1>
|
||||||
<div class="roll-table-view-formula-container">
|
<div class="roll-table-view-formula-container">
|
||||||
|
{{#if usesAltFormula}}
|
||||||
<select class="system-update-field" data-path="activeAltFormula">
|
<select class="system-update-field" data-path="activeAltFormula">
|
||||||
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
|
{{selectOptions this.altFormulaOptions selected=this.activeAltFormula labelAttr="name"}}
|
||||||
</select>
|
</select>
|
||||||
|
{{/if}}
|
||||||
<h4>{{selectedFormula}}</h4>
|
<h4>{{selectedFormula}}</h4>
|
||||||
</div>
|
</div>
|
||||||
<button data-action="changeMode">
|
<button data-action="changeMode">
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,12 @@
|
||||||
<span>{{localize "DAGGERHEART.ROLLTABLES.FIELDS.formulaName.label"}}</span>
|
<span>{{localize "DAGGERHEART.ROLLTABLES.FIELDS.formulaName.label"}}</span>
|
||||||
<span>{{localize "Formula Roll"}}</span>
|
<span>{{localize "Formula Roll"}}</span>
|
||||||
<span></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}}
|
{{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>
|
<button class="formula-button" data-action="addFormula"><i class="fa-solid fa-plus"></i></button>
|
||||||
{{#each @root.altFormula as | formula key |}}
|
{{#each @root.altFormula as | formula key |}}
|
||||||
{{formInput @root.systemFields.altFormula.element.fields.name value=formula.name name=(concat "flags.daggerheart.altFormula." key ".name")}}
|
<input type="text" value="{{formula.name}}" class="system-update-field" data-path="{{concat "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.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>
|
<a class="formula-button" data-action="removeFormula" data-key="{{key}}"><i class="fa-solid fa-trash"></i></a>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue