Changed to use a dialog to choose which parts are kept when reseting (#1557)

This commit is contained in:
WBHarry 2026-01-21 02:56:47 +01:00 committed by GitHub
parent 3725fc29ef
commit 2aba7cf921
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 179 additions and 22 deletions

View file

@ -330,6 +330,12 @@
"title": "{actor} - Character Setup", "title": "{actor} - Character Setup",
"traitIncreases": "Trait Increases" "traitIncreases": "Trait Increases"
}, },
"CharacterReset": {
"title": "Reset Character",
"alwaysDeleteSection": "Deleted Data",
"optionalDeleteSection": "Optional Data",
"headerTitle": "Select which data you'd like to keep"
},
"CombatTracker": { "CombatTracker": {
"combatStarted": "Active", "combatStarted": "Active",
"giveSpotlight": "Give The Spotlight", "giveSpotlight": "Give The Spotlight",
@ -2214,6 +2220,7 @@
"single": "Player", "single": "Player",
"plurial": "Players" "plurial": "Players"
}, },
"portrait": "Portrait",
"proficiency": "Proficiency", "proficiency": "Proficiency",
"quantity": "Quantity", "quantity": "Quantity",
"range": "Range", "range": "Range",

View file

@ -1,5 +1,6 @@
export { default as AttributionDialog } from './attributionDialog.mjs'; export { default as AttributionDialog } from './attributionDialog.mjs';
export { default as BeastformDialog } from './beastformDialog.mjs'; export { default as BeastformDialog } from './beastformDialog.mjs';
export { default as CharacterResetDialog } from './characterResetDialog.mjs';
export { default as d20RollDialog } from './d20RollDialog.mjs'; export { default as d20RollDialog } from './d20RollDialog.mjs';
export { default as DamageDialog } from './damageDialog.mjs'; export { default as DamageDialog } from './damageDialog.mjs';
export { default as DamageReductionDialog } from './damageReductionDialog.mjs'; export { default as DamageReductionDialog } from './damageReductionDialog.mjs';

View file

@ -0,0 +1,105 @@
const { ApplicationV2, HandlebarsApplicationMixin } = foundry.applications.api;
export default class CharacterResetDialog extends HandlebarsApplicationMixin(ApplicationV2) {
constructor(actor, options = {}) {
super(options);
this.actor = actor;
this.data = {
delete: {
class: { keep: false, label: 'TYPES.Item.class' },
subclass: { keep: false, label: 'TYPES.Item.subclass' },
ancestry: { keep: false, label: 'TYPES.Item.ancestry' },
community: { keep: false, label: 'TYPES.Item.community' }
},
optional: {
portrait: { keep: true, label: 'DAGGERHEART.GENERAL.portrait' },
name: { keep: true, label: 'Name' },
biography: { keep: true, label: 'DAGGERHEART.GENERAL.Tabs.biography' },
inventory: { keep: false, label: 'DAGGERHEART.GENERAL.inventory' }
}
};
}
static DEFAULT_OPTIONS = {
tag: 'form',
classes: ['daggerheart', 'dialog', 'dh-style', 'views', 'character-reset'],
window: {
icon: 'fa-solid fa-arrow-rotate-left',
title: 'DAGGERHEART.APPLICATIONS.CharacterReset.title'
},
actions: {
finishSelection: this.#finishSelection
},
form: {
handler: this.updateData,
submitOnChange: true,
submitOnClose: false
}
};
/** @override */
static PARTS = {
resourceDice: {
id: 'resourceDice',
template: 'systems/daggerheart/templates/dialogs/characterReset.hbs'
}
};
async _prepareContext(_options) {
const context = await super._prepareContext(_options);
context.data = this.data;
return context;
}
static async updateData(event, _, formData) {
const { data } = foundry.utils.expandObject(formData.object);
this.data = foundry.utils.mergeObject(this.data, data);
this.render();
}
static getUpdateData() {
const update = {};
if (!this.data.optional.portrait) update.if(!this.data.optional.biography);
if (!this.data.optional.inventory) return update;
}
static async #finishSelection() {
const update = {};
if (!this.data.optional.name.keep) {
const defaultName = game.system.api.documents.DhpActor.defaultName({ type: 'character' });
foundry.utils.setProperty(update, 'name', defaultName);
foundry.utils.setProperty(update, 'prototypeToken.name', defaultName);
}
if (!this.data.optional.portrait.keep) {
foundry.utils.setProperty(update, 'img', this.actor.schema.fields.img.initial(this.actor));
foundry.utils.setProperty(update, 'prototypeToken.==texture', {});
foundry.utils.setProperty(update, 'prototypeToken.==ring', {});
}
if (this.data.optional.biography.keep)
foundry.utils.setProperty(update, 'system.biography', this.actor.system.biography);
if (this.data.optional.inventory.keep) foundry.utils.setProperty(update, 'system.gold', this.actor.system.gold);
const { system, ...rest } = update;
await this.actor.update({
...rest,
'==system': system ?? {}
});
const inventoryItemTypes = ['weapon', 'armor', 'consumable', 'loot'];
await this.actor.deleteEmbeddedDocuments(
'Item',
this.actor.items
.filter(x => !inventoryItemTypes.includes(x.type) || !this.data.optional.inventory.keep)
.map(x => x.id)
);
this.close();
}
}

View file

@ -669,26 +669,7 @@ export default class CharacterSheet extends DHBaseActorSheet {
* Resets the character data and removes all embedded documents. * Resets the character data and removes all embedded documents.
*/ */
static async #resetCharacter() { static async #resetCharacter() {
const confirmed = await foundry.applications.api.DialogV2.confirm({ new game.system.api.applications.dialogs.CharacterResetDialog(this.document).render({ force: true });
window: {
title: game.i18n.localize('DAGGERHEART.ACTORS.Character.resetCharacterConfirmationTitle')
},
content: game.i18n.localize('DAGGERHEART.ACTORS.Character.resetCharacterConfirmationContent')
});
if (!confirmed) return;
await this.document.update({
'==system': {}
});
await this.document.deleteEmbeddedDocuments(
'Item',
this.document.items.map(x => x.id)
);
await this.document.deleteEmbeddedDocuments(
'ActiveEffect',
this.document.effects.map(x => x.id)
);
} }
/** /**
@ -753,8 +734,9 @@ export default class CharacterSheet extends DHBaseActorSheet {
if (!result) return; if (!result) return;
/* This could be avoided by baking config.costs into config.resourceUpdates. Didn't feel like messing with it at the time */ /* This could be avoided by baking config.costs into config.resourceUpdates. Didn't feel like messing with it at the time */
const costResources = result.costs?.filter(x => x.enabled) const costResources =
.map(cost => ({ ...cost, value: -cost.value, total: -cost.total })) || {}; result.costs?.filter(x => x.enabled).map(cost => ({ ...cost, value: -cost.value, total: -cost.total })) ||
{};
config.resourceUpdates.addResources(costResources); config.resourceUpdates.addResources(costResources);
await config.resourceUpdates.updateResources(); await config.resourceUpdates.updateResources();
} }

View file

@ -0,0 +1,27 @@
.daggerheart.dh-style.dialog.views.character-reset {
.character-reset-container {
display: flex;
flex-direction: column;
gap: 8px;
legend {
padding: 0 4px;
}
.character-reset-header {
font-size: var(--font-size-18);
text-align: center;
}
.reset-data-container {
display: grid;
grid-template-columns: 3fr 2fr;
align-items: center;
gap: 4px;
label {
font-weight: bold;
}
}
}
}

View file

@ -41,3 +41,5 @@
@import './settings/change-currency-icon.less'; @import './settings/change-currency-icon.less';
@import './risk-it-all/sheet.less'; @import './risk-it-all/sheet.less';
@import './character-reset/sheet.less';

View file

@ -0,0 +1,33 @@
<div>
<div class="character-reset-container">
<div class="character-reset-header">{{localize "DAGGERHEART.APPLICATIONS.CharacterReset.headerTitle"}}</div>
<fieldset>
<legend>{{localize "DAGGERHEART.APPLICATIONS.CharacterReset.alwaysDeleteSection"}} <i class="fa-solid fa-lock"></i></legend>
<div class="reset-data-wrapper two-columns even">
{{#each this.data.delete as | data key|}}
<div class="reset-data-container">
<label>{{localize data.label}}</label>
<input type="checkbox" {{checked data.keep}} disabled />
</div>
{{/each}}
</div>
</fieldset>
<fieldset>
<legend>{{localize "DAGGERHEART.APPLICATIONS.CharacterReset.optionalDeleteSection"}}</legend>
<div class="reset-data-wrapper two-columns even">
{{#each this.data.optional as | data key|}}
<div class="reset-data-container">
<label>{{localize data.label}}</label>
<input type="checkbox" name="{{concat "data.optional." key ".keep"}}" {{checked data.keep}} />
</div>
{{/each}}
</div>
</fieldset>
<button type="button" data-action="finishSelection">{{localize "Reset"}}</button>
</div>
</div>