|
| 1 | +import { ActorPF2e } from "@actor/base.ts"; |
| 2 | +import { DENOMINATIONS } from "@item/physical/values.ts"; |
| 3 | +import { ErrorPF2e } from "@util"; |
| 4 | +import fields = foundry.data.fields; |
| 5 | + |
| 6 | +/** Simple dialog to add coins of various denominations to an actor */ |
| 7 | +export class AddCurrencyDialog extends fa.api.DialogV2<AddCurrencyConfiguration> { |
| 8 | + static override DEFAULT_OPTIONS: DeepPartial<AddCurrencyConfiguration> = { |
| 9 | + id: "add-coins", |
| 10 | + window: { |
| 11 | + icon: "fa-solid fa-coins", |
| 12 | + title: "PF2E.AddCoinsTitle", |
| 13 | + }, |
| 14 | + }; |
| 15 | + |
| 16 | + static override async wait(options: WaitParams): Promise<unknown> { |
| 17 | + const actor = options.actor; |
| 18 | + const content = document.createElement("div"); |
| 19 | + const templatePath = "systems/pf2e/templates/actors/add-currency-dialog.hbs"; |
| 20 | + |
| 21 | + const denominations = DENOMINATIONS.map((d) => ({ |
| 22 | + name: d, |
| 23 | + field: new fields.NumberField({ |
| 24 | + min: 0, |
| 25 | + integer: true, |
| 26 | + label: game.i18n.localize(CONFIG.PF2E.currencies[d]), |
| 27 | + }), |
| 28 | + })); |
| 29 | + |
| 30 | + content.innerHTML = await fa.handlebars.renderTemplate(templatePath, { actor, denominations }); |
| 31 | + const buttons: fa.api.DialogV2Button[] = [ |
| 32 | + { |
| 33 | + type: "button", |
| 34 | + icon: "fa-regular fa-floppy-disk", |
| 35 | + label: "PF2E.AddCoinsTitle", |
| 36 | + action: "save", |
| 37 | + callback: (_, button) => { |
| 38 | + const form = button.closest("form"); |
| 39 | + if (!form) throw ErrorPF2e("Unexpected missing form"); |
| 40 | + const formData = new fa.ux.FormDataExtended(form).object; |
| 41 | + const combineStacks = !!formData.combineStacks; |
| 42 | + const coins = { |
| 43 | + pp: Number(formData.pp) || 0, |
| 44 | + gp: Number(formData.gp) || 0, |
| 45 | + sp: Number(formData.sp) || 0, |
| 46 | + cp: Number(formData.cp) || 0, |
| 47 | + }; |
| 48 | + return actor.inventory.addCoins(coins, { combineStacks }); |
| 49 | + }, |
| 50 | + }, |
| 51 | + ]; |
| 52 | + |
| 53 | + return super.wait(Object.assign(options, { content, buttons })); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +interface AddCurrencyConfiguration extends fa.api.DialogV2Configuration { |
| 58 | + actor: ActorPF2e; |
| 59 | +} |
| 60 | + |
| 61 | +type WaitParams = DeepPartial<fa.api.DialogV2Configuration> & { actor: ActorPF2e }; |
0 commit comments