diff --git a/module/applications/dialogs/_module.mjs b/module/applications/dialogs/_module.mjs index 43faa68a..92038c41 100644 --- a/module/applications/dialogs/_module.mjs +++ b/module/applications/dialogs/_module.mjs @@ -6,6 +6,7 @@ export { default as DamageReductionDialog } from './damageReductionDialog.mjs'; export { default as DeathMove } from './deathMove.mjs'; export { default as Downtime } from './downtime.mjs'; export { default as ImageSelectDialog } from './imageSelectDialog.mjs'; +export { default as ItemTransferDialog } from './itemTransfer.mjs'; export { default as MulticlassChoiceDialog } from './multiclassChoiceDialog.mjs'; export { default as OwnershipSelection } from './ownershipSelection.mjs'; export { default as RerollDamageDialog } from './rerollDamageDialog.mjs'; diff --git a/module/applications/dialogs/itemTransfer.mjs b/module/applications/dialogs/itemTransfer.mjs new file mode 100644 index 00000000..ad3cf103 --- /dev/null +++ b/module/applications/dialogs/itemTransfer.mjs @@ -0,0 +1,62 @@ +const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api; + +export default class ItemTransferDialog extends HandlebarsApplicationMixin(ApplicationV2) { + constructor(data) { + super({}); + this.data = data; + } + + get title() { + return this.data.title; + } + + static DEFAULT_OPTIONS = { + tag: 'form', + classes: ['daggerheart', 'dh-style', 'dialog', 'item-transfer'], + position: { width: 400, height: 'auto' }, + window: { icon: 'fa-solid fa-hand-holding-hand' }, + actions: { + finish: ItemTransferDialog.#finish + } + }; + + static PARTS = { + main: { template: 'systems/daggerheart/templates/dialogs/item-transfer.hbs', root: true } + }; + + async _prepareContext(_options) { + const context = await super._prepareContext(_options); + return foundry.utils.mergeObject(context, this.data); + } + + static async #finish() { + this.selected = this.form.elements.quantity.valueAsNumber || null; + this.close(); + } + + static #determineTransferOptions({ originActor, targetActor, item, currency }) { + originActor ??= item?.actor; + const homebrewKey = CONFIG.DH.SETTINGS.gameSettings.Homebrew; + const currencySetting = game.settings.get(CONFIG.DH.id, homebrewKey).currency?.[currency] ?? null; + + return { + originActor, + targetActor, + itemImage: item?.img, + currencyIcon: currencySetting?.icon, + max: item?.system.quantity ?? originActor.system.gold[currency] ?? 0, + title: item?.name ?? currencySetting?.label + }; + } + + static async configure(options) { + return new Promise(resolve => { + const data = this.#determineTransferOptions(options); + if (data.max <= 1) return resolve(data.max); + + const app = new this(data); + app.addEventListener('close', () => resolve(app.selected), { once: true }); + app.render({ force: true }); + }); + } +} diff --git a/module/applications/sheets/api/base-actor.mjs b/module/applications/sheets/api/base-actor.mjs index f0ebe250..5d054949 100644 --- a/module/applications/sheets/api/base-actor.mjs +++ b/module/applications/sheets/api/base-actor.mjs @@ -257,42 +257,6 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) { /* Application Drag/Drop */ /* -------------------------------------------- */ - async #determineTransferQuantity({ originActor, item, currency }) { - originActor ??= item?.actor; - const max = item?.system.quantity ?? originActor.system.gold[currency] ?? null; - if (!max || max === 0) return null; - if (max === 1) return max; - - const homebrewKey = CONFIG.DH.SETTINGS.gameSettings.Homebrew; - const currencySetting = game.settings.get(CONFIG.DH.id, homebrewKey).currency?.[currency] ?? null; - const name = item?.name ?? currencySetting?.label; - - return foundry.applications.api.DialogV2.input({ - classes: ['daggerheart', 'dh-style', "item-transfer"], - content: await foundry.applications.handlebars.renderTemplate( - "systems/daggerheart/templates/dialogs/item-transfer.hbs", - { - originActor: originActor, - targetActor: this.document, - itemImage: item?.img, - currencyIcon: currencySetting?.icon, - max, - } - ), - window: { - title: name, - icon: 'fa-solid fa-hand-holding-hand' - }, - position: { - width: 400, - }, - ok: { - label: game.i18n.localize("DAGGERHEART.APPLICATIONS.ItemTransfer.transfer"), - callback: (_, button) => button.form.elements.quantity.value || null, - } - }); - } - async _onDrop(event) { event.stopPropagation(); const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event); @@ -300,7 +264,11 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) { const originActor = await foundry.utils.fromUuid(data.originActor); if (!originActor || originActor.uuid === this.document.uuid) return; const currency = data.currency; - const quantity = await this.#determineTransferQuantity({ originActor, currency }); + const quantity = await game.system.api.applications.dialogs.ItemTransferDialog.configure({ + originActor, + targetActor: this.document, + currency + }); if (quantity) { originActor.update({ [`system.gold.${currency}`]: Math.max(0, originActor.system.gold[currency] - quantity) }); this.document.update({ [`system.gold.${currency}`]: this.document.system.gold[currency] + quantity }); @@ -326,7 +294,10 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) { if (item.system.metadata.isInventoryItem) { if (item.system.metadata.isQuantifiable) { const actorItem = originActor.items.get(data.originId); - const quantityTransfered = await this.#determineTransferQuantity({ item }); + const quantityTransfered = await game.system.api.applications.dialogs.ItemTransferDialog.configure({ + item, + targetActor: this.document + }); if (quantityTransfered) { if (quantityTransfered === actorItem.system.quantity) { diff --git a/templates/dialogs/item-transfer.hbs b/templates/dialogs/item-transfer.hbs index c657a180..0e7df3dc 100644 --- a/templates/dialogs/item-transfer.hbs +++ b/templates/dialogs/item-transfer.hbs @@ -1,18 +1,26 @@ -