diff --git a/module/applications/sheets/actors/adversary.mjs b/module/applications/sheets/actors/adversary.mjs index b0fc5fb4..cf009b4f 100644 --- a/module/applications/sheets/actors/adversary.mjs +++ b/module/applications/sheets/actors/adversary.mjs @@ -9,7 +9,9 @@ export default class AdversarySheet extends DHBaseActorSheet { position: { width: 660, height: 766 }, window: { resizable: true }, actions: { - reactionRoll: AdversarySheet.#reactionRoll + reactionRoll: AdversarySheet.#reactionRoll, + toggleResourceDice: AdversarySheet.#toggleResourceDice, + handleResourceDice: AdversarySheet.#handleResourceDice }, window: { resizable: true @@ -133,6 +135,40 @@ export default class AdversarySheet extends DHBaseActorSheet { this.actor.diceRoll(config); } + /** + * Toggle the used state of a resource dice. + * @type {ApplicationClickAction} + */ + static async #toggleResourceDice(event, target) { + const item = await getDocFromElement(target); + + const { dice } = event.target.closest('.item-resource').dataset; + const diceState = item.system.resource.diceStates[dice]; + + await item.update({ + [`system.resource.diceStates.${dice}.used`]: diceState ? !diceState.used : true + }); + } + + /** + * Handle the roll values of resource dice. + * @type {ApplicationClickAction} + */ + static async #handleResourceDice(_, target) { + const item = await getDocFromElement(target); + if (!item) return; + + const rollValues = await game.system.api.applications.dialogs.ResourceDiceDialog.create(item, this.document); + if (!rollValues) return; + + await item.update({ + 'system.resource.diceStates': rollValues.reduce((acc, state, index) => { + acc[index] = { value: state.value, used: state.used }; + return acc; + }, {}) + }); + } + /* -------------------------------------------- */ /* Application Listener Actions */ /* -------------------------------------------- */ diff --git a/module/applications/sheets/actors/environment.mjs b/module/applications/sheets/actors/environment.mjs index aa2759a2..560e5ed2 100644 --- a/module/applications/sheets/actors/environment.mjs +++ b/module/applications/sheets/actors/environment.mjs @@ -1,3 +1,4 @@ +import { getDocFromElement } from '../../../helpers/utils.mjs'; import DHBaseActorSheet from '../api/base-actor.mjs'; /**@typedef {import('@client/applications/_types.mjs').ApplicationClickAction} ApplicationClickAction */ @@ -13,7 +14,10 @@ export default class DhpEnvironment extends DHBaseActorSheet { window: { resizable: true }, - actions: {}, + actions: { + toggleResourceDice: DhpEnvironment.#toggleResourceDice, + handleResourceDice: DhpEnvironment.#handleResourceDice + }, dragDrop: [{ dragSelector: '.action-section .inventory-item', dropSelector: null }] }; @@ -106,4 +110,44 @@ export default class DhpEnvironment extends DHBaseActorSheet { event.dataTransfer.setDragImage(item, 60, 0); } } + + /* -------------------------------------------- */ + /* Application Clicks Actions */ + /* -------------------------------------------- */ + + + /** + * Toggle the used state of a resource dice. + * @type {ApplicationClickAction} + */ + static async #toggleResourceDice(event, target) { + const item = await getDocFromElement(target); + + const { dice } = event.target.closest('.item-resource').dataset; + const diceState = item.system.resource.diceStates[dice]; + + await item.update({ + [`system.resource.diceStates.${dice}.used`]: diceState ? !diceState.used : true + }); + } + + /** + * Handle the roll values of resource dice. + * @type {ApplicationClickAction} + */ + static async #handleResourceDice(_, target) { + const item = await getDocFromElement(target); + if (!item) return; + + const rollValues = await game.system.api.applications.dialogs.ResourceDiceDialog.create(item, this.document); + if (!rollValues) return; + + await item.update({ + 'system.resource.diceStates': rollValues.reduce((acc, state, index) => { + acc[index] = { value: state.value, used: state.used }; + return acc; + }, {}) + }); + } + }