Added manual input for Dice Resources

This commit is contained in:
WBHarry 2025-07-13 21:05:52 +02:00
parent 07e39a38e2
commit 4262bf7a97
5 changed files with 83 additions and 16 deletions

View file

@ -390,9 +390,7 @@
},
"ResourceDice": {
"title": "{name} Resource",
"rerollDice": "Reroll Dice",
"rerollRecoveryInfo": "{name} refresh on {recovery}.",
"rerollConfirmation": "Are you sure you want to reroll your {name} dice?"
"rerollDice": "Reroll Dice"
}
},

View file

@ -1,13 +1,14 @@
const { ApplicationV2, HandlebarsApplicationMixin } = foundry.applications.api;
export default class ResourceDiceDialog extends HandlebarsApplicationMixin(ApplicationV2) {
constructor(name, recovery, actorName, resource, options = {}) {
constructor(name, recovery, actor, resource, options = {}) {
super(options);
this.name = name;
this.recovery = recovery;
this.actorName = actorName;
this.actor = actor;
this.resource = resource;
this.diceStates = foundry.utils.deepClone(resource.diceStates);
}
static DEFAULT_OPTIONS = {
@ -17,7 +18,8 @@ export default class ResourceDiceDialog extends HandlebarsApplicationMixin(Appli
icon: 'fa-solid fa-dice'
},
actions: {
rerollDice: this.rerollDice
rerollDice: this.rerollDice,
save: this.save
},
form: {
handler: this.updateResourceDice,
@ -42,15 +44,35 @@ export default class ResourceDiceDialog extends HandlebarsApplicationMixin(Appli
const context = await super._prepareContext(_options);
context.name = this.name;
context.recovery = game.i18n.localize(CONFIG.DH.GENERAL.refreshTypes[this.recovery].label);
context.resource = this.resource;
context.diceStates = this.diceStates;
context.actor = this.actor;
return context;
}
static async updateResourceDice(event, _, formData) {
const { diceStates } = foundry.utils.expandObject(formData.object);
this.diceStates = Object.keys(diceStates).reduce((acc, key) => {
const resourceState = this.resource.diceStates[key];
acc[key] = { ...diceStates[key], used: Boolean(resourceState?.used) };
return acc;
}, {});
this.render();
}
static async save() {
this.rollValues = Object.values(this.diceStates);
this.close();
}
static async rerollDice() {
const diceFormula = `${this.resource.max}d${this.resource.dieFaces}`;
const roll = await new Roll(diceFormula).evaluate();
if (game.modules.get('dice-so-nice')?.active) await game.dice3d.showForRoll(roll, game.user, true);
this.rollValues = roll.terms[0].results.map(x => x.result);
this.rollValues = roll.terms[0].results.map(x => ({ value: x.result, used: false }));
this.resetUsed = true;
const cls = getDocumentClass('ChatMessage');
const msg = new cls({
@ -58,7 +80,7 @@ export default class ResourceDiceDialog extends HandlebarsApplicationMixin(Appli
content: await foundry.applications.handlebars.renderTemplate(
'systems/daggerheart/templates/ui/chat/resource-roll.hbs',
{
user: this.actorName,
user: this.actor.name,
name: this.name
}
)
@ -68,9 +90,9 @@ export default class ResourceDiceDialog extends HandlebarsApplicationMixin(Appli
this.close();
}
static async create(name, recovery, actorName, resource, options = {}) {
static async create(name, recovery, actor, resource, options = {}) {
return new Promise(resolve => {
const app = new this(name, recovery, actorName, resource, options);
const app = new this(name, recovery, actor, resource, options);
app.addEventListener('close', () => resolve(app.rollValues), { once: true });
app.render({ force: true });
});

View file

@ -700,14 +700,14 @@ export default class CharacterSheet extends DHBaseActorSheet {
const rollValues = await game.system.api.applications.dialogs.ResourceDiceDialog.create(
item.name,
item.system.resource.recovery,
this.document.name,
this.document,
item.system.resource
);
if (!rollValues) return;
await item.update({
'system.resource.diceStates': rollValues.reduce((acc, value, index) => {
acc[index] = { value, used: false };
'system.resource.diceStates': rollValues.reduce((acc, state, index) => {
acc[index] = { value: state.value, used: state.used };
return acc;
}, {})
});

View file

@ -1,8 +1,44 @@
.theme-light .daggerheart.dialog.dh-style.views.resource-dice {
.resource-items input {
background-image: url('../assets/parchments/dh-parchment-light.png');
}
}
.daggerheart.dialog.dh-style.views.resource-dice {
.reroll-confirmation {
margin-bottom: 8px;
}
.resource-items {
display: flex;
gap: 8px;
.resource-item {
position: relative;
display: flex;
align-items: center;
justify-content: center;
input {
position: absolute;
border-color: light-dark(@dark-blue, @golden);
color: light-dark(black, white);
background-image: url('../assets/parchments/dh-parchment-dark.png');
z-index: 2;
line-height: 22px;
height: unset;
text-align: center;
}
img {
width: 48px;
height: 48px;
filter: brightness(0) saturate(100%) invert(97%) sepia(7%) saturate(580%) hue-rotate(332deg)
brightness(96%) contrast(95%);
}
}
}
footer {
display: flex;
gap: 8px;

View file

@ -1,10 +1,21 @@
<div>
<div class="reroll-confirmation">
<div class="resource-items">
{{#times (rollParsed resource.max actor)}}
{{#with (ifThen (lookup ../diceStates this) (lookup ../diceStates this) this) as | state |}}
<div class="resource-item" data-dice="{{#if ../../this}}{{../this}}{{else}}{{state}}{{/if}}">
<input type="number" data-dtype="Number" name={{concat "diceStates." (ifThen ../../this ../this state) ".value" }} value="{{state.value}}" />
{{!-- <label>{{ifThen state.value state.value '?'}}</label> --}}
<img src="{{concat "systems/daggerheart/assets/icons/dice/hope/d" (ifThen ../../resource.dieFaces ../../resource.dieFaces ../resource.dieFaces) ".svg"}}" />
</div>
{{/with}}
{{/times}}
</div>
{{!-- <div class="reroll-confirmation">
<div>{{localize 'DAGGERHEART.APPLICATIONS.ResourceDice.rerollRecoveryInfo' name=name recovery=recovery }}</div>
<div>{{localize 'DAGGERHEART.APPLICATIONS.ResourceDice.rerollConfirmation' name=name }}</div>
</div>
</div> --}}
<footer>
<button data-action="close">{{localize 'No'}}</button>
<button data-action="save">{{localize 'Save'}}</button>
<button data-action="rerollDice">{{localize "DAGGERHEART.APPLICATIONS.ResourceDice.rerollDice"}}</button>
</footer>
</div>