From 24c81dbcf86c8eae65c7784807667c0f889e7bf8 Mon Sep 17 00:00:00 2001 From: Byroks Date: Thu, 15 Oct 2026 18:10:45 +0200 Subject: [PATCH 01/21] a new begining --- M6.css | 128 ++++++ README.md | 2 +- lang/de.json | 63 +++ lang/en.json | 2 +- lang/fr.json | 20 - package-lock.json | 52 +-- package.json | 8 +- src/constants.mjs | 8 + src/data/character-data.mjs | 228 +++++++++++ src/data/character-data.ts | 33 -- src/data/{item-data.ts => item-data.mjs} | 5 +- src/helpers/utils.mjs | 57 +++ src/{i18n.ts => i18n.mjs} | 0 src/sheets/character-sheet.mjs | 378 ++++++++++++++++++ src/sheets/character-sheet.ts | 32 -- .../CharacterSheetApp.logic.test.ts | 23 -- .../components/CharacterSheetApp.logic.ts | 6 - .../components/CharacterSheetApp.module.scss | 38 -- src/sheets/components/CharacterSheetApp.tsx | 75 ---- .../components/ItemSheetApp.module.scss | 13 - src/sheets/components/ItemSheetApp.tsx | 37 -- src/sheets/item-sheet.ts | 32 -- src/system.mjs | 27 ++ src/system.ts | 28 -- src/templates/actor.hbs | 40 ++ src/templates/actor/header.hbs | 23 ++ src/templates/actor/items.hbs | 47 +++ src/templates/actor/stats.hbs | 65 +++ src/templates/combat/create-dialog.hbs | 9 + src/templates/item/header.hbs | 14 + src/templates/shared/effects.hbs | 59 +++ src/templates/shared/properties.hbs | 31 ++ .../{data-config.d.ts => data-config.d.mjs} | 0 src/types/{foundry.d.ts => foundry.d.mjs} | 0 style.css | 103 +++++ system.json | 32 +- tsconfig.json | 17 - vite.config.ts | 2 +- 38 files changed, 1313 insertions(+), 424 deletions(-) create mode 100644 M6.css create mode 100644 lang/de.json delete mode 100644 lang/fr.json create mode 100644 src/constants.mjs create mode 100644 src/data/character-data.mjs delete mode 100644 src/data/character-data.ts rename src/data/{item-data.ts => item-data.mjs} (72%) create mode 100644 src/helpers/utils.mjs rename src/{i18n.ts => i18n.mjs} (100%) create mode 100644 src/sheets/character-sheet.mjs delete mode 100644 src/sheets/character-sheet.ts delete mode 100644 src/sheets/components/CharacterSheetApp.logic.test.ts delete mode 100644 src/sheets/components/CharacterSheetApp.logic.ts delete mode 100644 src/sheets/components/CharacterSheetApp.module.scss delete mode 100644 src/sheets/components/CharacterSheetApp.tsx delete mode 100644 src/sheets/components/ItemSheetApp.module.scss delete mode 100644 src/sheets/components/ItemSheetApp.tsx delete mode 100644 src/sheets/item-sheet.ts create mode 100644 src/system.mjs delete mode 100644 src/system.ts create mode 100644 src/templates/actor.hbs create mode 100644 src/templates/actor/header.hbs create mode 100644 src/templates/actor/items.hbs create mode 100644 src/templates/actor/stats.hbs create mode 100644 src/templates/combat/create-dialog.hbs create mode 100644 src/templates/item/header.hbs create mode 100644 src/templates/shared/effects.hbs create mode 100644 src/templates/shared/properties.hbs rename src/types/{data-config.d.ts => data-config.d.mjs} (100%) rename src/types/{foundry.d.ts => foundry.d.mjs} (100%) create mode 100644 style.css delete mode 100644 tsconfig.json diff --git a/M6.css b/M6.css new file mode 100644 index 0000000..dac8eaa --- /dev/null +++ b/M6.css @@ -0,0 +1,128 @@ +.application.sheet.M6 { + background-color: #d3d7d7; + color: #595d5f; + + section { + .sheet-header { + margin-top: 8px; + img { + position: absolute; + top: 8px; + left: 44%; + background-color: #d3d7d7; + &:hover { + cursor: pointer; + } + } + input { + margin-bottom: 4px; + &:focus { + border: none; + } + } + } + } + input { + background-color: #fdfeff; + color: black; + border-radius: 0; + &.skill { + max-width: 2rem; + text-align: center; + margin-left: 0.5rem; + } + &.attribut { + max-width: 2rem; + text-align: center; + margin: 0 0.5rem; + } + &.text { + &.right { + text-align: end; + } + } + } + p { + &.center { + text-align: center; + } + } + fieldset { + background-color: #eedecd; + } + + .sheet-header { + img { + border-radius: 0 0 50% 50%; + border: solid 16px #d3d7d7; + border-top: none; + flex: 0 0 100px; + object-fit: fill; + } + } + .tab { + height: 100%; + padding-top: 10px; + } + .tab[data-tab="items"] { + ul { + list-style: none; + padding-left: 5px; + } + .itemHeader { + border-bottom: 1px solid var(--color-border-light-primary); + .type-label { + font-size: 20px; + } + .document-controls { + flex: 0 0 100px; + } + } + .item { + img { + border: none; + flex: 0 0 30px; + padding-right: 5px; + } + .document-controls { + flex: 0 0 50px; + } + } + } + .tab[data-tab="effects"] { + ul { + list-style: none; + padding-left: 5px; + margin-top: 0; + margin-bottom: 0; + } + .effects-header { + font-size: 16px; + border-bottom: 1px solid var(--color-border-light-primary); + .document-controls { + flex: 0 0 100px; + } + } + .effect { + .effect-label { + img { + border: none; + flex: 0 0 30px; + padding-right: 5px; + } + .effect-name { + line-height: 24px; + } + } + .effect-duration { + flex: 0 0 130px; + } + .document-controls { + flex: 0 0 80px; + } + } + .effect-label { + flex: 2; + } + } +} diff --git a/README.md b/README.md index 92f80b0..00bfd7e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# fvtt-ts-react-boilerplate +# midgard6-lvd A TypeScript + React + Vite boilerplate for building Foundry VTT (V14) game systems on top of **ApplicationV2**, with full type safety via [`fvtt-types`](https://github.com/League-of-Foundry-Developers/foundry-vtt-types). diff --git a/lang/de.json b/lang/de.json new file mode 100644 index 0000000..b8dd41b --- /dev/null +++ b/lang/de.json @@ -0,0 +1,63 @@ +{ + "Midgard6": { + "Biography": { + "name": "Name", + "spezies": "Spezies", + "kultur": "Kultur", + "abenteuertyp": "Abenteuertyp", + "glaube": "Glaube", + "mondgestalt": "Mondgestalt" + }, + "Skills": { + "gruppen": { + "kampf": "Kampf", + "geist": "Geist", + "körper": "Körper", + "ausdruck": "Ausdruck", + "magie": "Magie" + }, + "athletik": "Athletik", + "anführen": "Anführen", + "beweglichkeit": "Beweglichkeit", + "bildung": "Bildung", + "darbietung": "Darbietung", + "einschüchterung": "Einschüchterung", + "empathie": "Empathie", + "fernkampf": "Fernkampf", + "fingerfertigkeit": "Fingerfertigkeit", + "gassenwissen": "Gassenwissen", + "handwerk": "Handwerk", + "heilkunst": "Heilkunst", + "heimlichkeit": "Heimlichkeit", + "kulturwissen": "Kulturwissen", + "nahkampf": "Nahkampf", + "manipulation": "Manipulation", + "thaumaturgie": "Thaumaturgie", + "tierführung": "Tierführung", + "überlebenskunst": "Überlebenskunst", + "überzeugen": "Überzeugen", + "wahrnehmung": "Wahrnehmung", + "zaubern": "Zaubern", + "zauberwissen": "Zauberwissen" + }, + "Sheets": { + "Tabs": { + "properties": "Fertigkeiten", + "items": "Gegenstände", + "effects": "Effekte" + } + }, + "Item": { + "Quantity": "Anzahl", + "Description": "Beschreibung" + } + }, + "TYPES": { + "Actor": { + "character": "Charakter" + }, + "Item": { + "item": "Gegenstand" + } + } +} diff --git a/lang/en.json b/lang/en.json index 675acb8..0ff8db5 100644 --- a/lang/en.json +++ b/lang/en.json @@ -1,5 +1,5 @@ { - "BOILERPLATE": { + "Midgard6": { "Attributes": { "strength": "Strength", "agility": "Agility", diff --git a/lang/fr.json b/lang/fr.json deleted file mode 100644 index 0085782..0000000 --- a/lang/fr.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "BOILERPLATE": { - "Attributes": { - "strength": "Force", - "agility": "Agilité", - "spirit": "Esprit" - }, - "Sheet": { - "Biography": "Biographie", - "Total": "Total", - "TotalMod": "Modificateur total", - "ChatPlaceholder": "Dites quelque chose...", - "SendToChat": "Envoyer dans le chat" - }, - "Item": { - "Quantity": "Quantité", - "Description": "Description" - } - } -} diff --git a/package-lock.json b/package-lock.json index a568541..5a7ea47 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,12 @@ { - "name": "fvtt-ts-react-boilerplate", + "name": "midgard6-lvd", "version": "0.8.2", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "fvtt-ts-react-boilerplate", + "name": "midgard6-lvd", "version": "0.8.2", - "dependencies": { - "react": "^18.3.0", - "react-dom": "^18.3.0" - }, "devDependencies": { "@eslint/js": "^9.39.4", "@types/react": "^18.3.0", @@ -19,7 +15,7 @@ "eslint": "^9.39.4", "eslint-config-prettier": "^10.1.8", "eslint-plugin-react": "^7.37.5", - "fvtt-types": "^13.346.0-beta.20251209192131", + "fvtt-types": "^13.346.0-beta.20250812191140", "globals": "^17.7.0", "prettier": "^3.8.4", "sass": "^1.101.0", @@ -4994,9 +4990,9 @@ } }, "node_modules/fvtt-types": { - "version": "13.346.0-beta.20251209192131", - "resolved": "https://registry.npmjs.org/fvtt-types/-/fvtt-types-13.346.0-beta.20251209192131.tgz", - "integrity": "sha512-4HhASmSpoibRAo1eeEtkZCSqy68yGFuT6Mkh2nNjM35cot43yo5JBB64owGQ9PK30hY8Ko1OucdjSDf87zdYRg==", + "version": "13.346.0-beta.20250812191140", + "resolved": "https://registry.npmjs.org/fvtt-types/-/fvtt-types-13.346.0-beta.20250812191140.tgz", + "integrity": "sha512-Xa+89ZzxvaGrY507LWb0DY3s6HIOak7pXZGjRuxT+6rLRjluQduiylqAOm4ZTQCRfh7+HdbrUsmrZOKoE/xadQ==", "dev": true, "license": "MIT", "dependencies": { @@ -5922,6 +5918,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "dev": true, "license": "MIT" }, "node_modules/js-yaml": { @@ -6332,6 +6329,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "dev": true, "license": "MIT", "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" @@ -7136,31 +7134,6 @@ "safe-buffer": "^5.1.0" } }, - "node_modules/react": { - "version": "18.3.1", - "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", - "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", - "license": "MIT", - "dependencies": { - "loose-envify": "^1.1.0" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/react-dom": { - "version": "18.3.1", - "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.3.1.tgz", - "integrity": "sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==", - "license": "MIT", - "dependencies": { - "loose-envify": "^1.1.0", - "scheduler": "^0.23.2" - }, - "peerDependencies": { - "react": "^18.3.1" - } - }, "node_modules/react-is": { "version": "16.13.1", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", @@ -7491,15 +7464,6 @@ "@parcel/watcher": "^2.4.1" } }, - "node_modules/scheduler": { - "version": "0.23.2", - "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.2.tgz", - "integrity": "sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==", - "license": "MIT", - "dependencies": { - "loose-envify": "^1.1.0" - } - }, "node_modules/semver": { "version": "6.3.1", "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.1.tgz", diff --git a/package.json b/package.json index c24774d..88da6b1 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "fvtt-ts-react-boilerplate", + "name": "midgard6-lvd", "version": "0.8.2", "private": true, "type": "module", @@ -21,7 +21,7 @@ "eslint": "^9.39.4", "eslint-config-prettier": "^10.1.8", "eslint-plugin-react": "^7.37.5", - "fvtt-types": "^13.346.0-beta.20251209192131", + "fvtt-types": "^13.346.0-beta.20250812191140", "globals": "^17.7.0", "prettier": "^3.8.4", "sass": "^1.101.0", @@ -29,9 +29,5 @@ "typescript-eslint": "^8.62.0", "vite": "^5.4.0", "vitest": "^4.1.9" - }, - "dependencies": { - "react": "^18.3.0", - "react-dom": "^18.3.0" } } diff --git a/src/constants.mjs b/src/constants.mjs new file mode 100644 index 0000000..dfdf2d9 --- /dev/null +++ b/src/constants.mjs @@ -0,0 +1,8 @@ +export const systemID = "midgard6-lvd"; + +/** + * Translates repository paths to Foundry Data paths. + * @param {string} path - A path relative to the root of this repository. + * @returns {string} The path relative to the Foundry data folder. + */ +export const systemPath = (path) => `systems/${systemID}/src/${path}`; diff --git a/src/data/character-data.mjs b/src/data/character-data.mjs new file mode 100644 index 0000000..ebea12a --- /dev/null +++ b/src/data/character-data.mjs @@ -0,0 +1,228 @@ +function defineSchema() { + const fields = foundry.data.fields; + return { + biography: new fields.SchemaField({ + mondgestalt: new fields.StringField({ initial: "" }), + abenteuertyp: new fields.StringField({ initial: "" }), + glaube: new fields.StringField({ initial: "" }), + spezies: new fields.StringField({ initial: "" }), + kultur: new fields.StringField({ initial: "" }) + }), + resources: new fields.SchemaField({ + leben: new fields.SchemaField({ + value: new fields.NumberField({ + required: true, + min: 0, + initial: 4 + }), + max: new fields.NumberField({ + required: true, + initial: 8 + }) + }), + ausdauer: new fields.SchemaField({ + value: new fields.NumberField({ + required: true, + min: 0, + initial: 4 + }), + max: new fields.NumberField({ + required: true, + initial: 44 + }) + }) + }), + skills: new fields.SchemaField({ + kampf: new fields.SchemaField( + { + anführen: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + fernkampf: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + nahkampf: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }) + }, + { label: "kampf" } + ), + körper: new fields.SchemaField( + { + athletik: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + beweglichkeit: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + fingerfertigkeit: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + heimlichkeit: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + wahrnehmung: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }) + }, + { label: "körper" } + ), + ausdruck: new fields.SchemaField( + { + darbietung: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + einschüchterung: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + empathie: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + gassenwissen: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + manipulation: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + überzeugen: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }) + }, + { label: "ausdruck" } + ), + geist: new fields.SchemaField( + { + bildung: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + handwerk: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + heilkunst: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + kulturwissen: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + tierführung: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + überlebenskunst: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }) + }, + { label: "geist" } + ), + magie: new fields.SchemaField( + { + thaumaturgie: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + zaubern: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }), + zauberwissen: new fields.NumberField({ + required: true, + nullable: false, + max: 15, + min: 5, + initial: 5 + }) + }, + { label: "magie" } + ) + }) + }; +} + +export class CharacterData extends foundry.abstract.TypeDataModel { + static defineSchema = defineSchema; +} diff --git a/src/data/character-data.ts b/src/data/character-data.ts deleted file mode 100644 index 506ecb7..0000000 --- a/src/data/character-data.ts +++ /dev/null @@ -1,33 +0,0 @@ -function defineSchema() { - const fields = foundry.data.fields; - return { - biography: new fields.HTMLField({ initial: "" }), - attributes: new fields.SchemaField({ - strength: new fields.NumberField({ - required: true, - nullable: false, - integer: true, - initial: 10 - }), - agility: new fields.NumberField({ - required: true, - nullable: false, - integer: true, - initial: 10 - }), - spirit: new fields.NumberField({ - required: true, - nullable: false, - integer: true, - initial: 10 - }) - }) - }; -} - -export class CharacterData extends foundry.abstract.TypeDataModel< - ReturnType, - Actor.Implementation -> { - static defineSchema = defineSchema; -} diff --git a/src/data/item-data.ts b/src/data/item-data.mjs similarity index 72% rename from src/data/item-data.ts rename to src/data/item-data.mjs index 332263d..6658e41 100644 --- a/src/data/item-data.ts +++ b/src/data/item-data.mjs @@ -12,9 +12,6 @@ function defineSchema() { }; } -export class ItemData extends foundry.abstract.TypeDataModel< - ReturnType, - Item.Implementation -> { +export class ItemData extends foundry.abstract.TypeDataModel { static defineSchema = defineSchema; } diff --git a/src/helpers/utils.mjs b/src/helpers/utils.mjs new file mode 100644 index 0000000..f671895 --- /dev/null +++ b/src/helpers/utils.mjs @@ -0,0 +1,57 @@ +/** + * Searches through an object recursively and localizes strings. + * @param {Record} object + */ +export function localizeHelper(object) { + for (const [key, value] of Object.entries(object)) { + // const type = foundry.utils.getType(value) + switch (typeof value) { + case "object": + if (value) localizeHelper(value); + break; + case "string": + if (key === "label") object[key] = game.i18n.localize(value); + break; + } + } +} + +/* -------------------------------------------------- */ + +/** + * Prepare the data structure for Active Effects which are currently embedded in an Actor or Item. + * @param {ActiveEffect[]} effects A collection or generator of Active Effect documents to prepare sheet data for. + * @return {object} Data for rendering. + */ +export function prepareActiveEffectCategories(effects) { + const categories = { + temporary: { + type: "temporary", + label: game.i18n.localize("M6.Effect.Temporary"), + effects: [] + }, + passive: { + type: "passive", + label: game.i18n.localize("M6.Effect.Passive"), + effects: [] + }, + inactive: { + type: "inactive", + label: game.i18n.localize("M6.Effect.Inactive"), + effects: [] + } + }; + + // Iterate over active effects, classifying them into categories + for (const e of effects) { + if (!e.active) categories.inactive.effects.push(e); + else if (e.isTemporary) categories.temporary.effects.push(e); + else categories.passive.effects.push(e); + } + + // Sort each category + for (const c of Object.values(categories)) { + c.effects.sort((a, b) => (a.sort || 0) - (b.sort || 0)); + } + return categories; +} diff --git a/src/i18n.ts b/src/i18n.mjs similarity index 100% rename from src/i18n.ts rename to src/i18n.mjs diff --git a/src/sheets/character-sheet.mjs b/src/sheets/character-sheet.mjs new file mode 100644 index 0000000..2cce425 --- /dev/null +++ b/src/sheets/character-sheet.mjs @@ -0,0 +1,378 @@ +import { systemPath } from "../constants.mjs"; +import { prepareActiveEffectCategories } from "../helpers/utils.mjs"; + +const { api, sheets } = foundry.applications; + +/** + * Extend the basic ActorSheet with some very simple modifications. + */ +export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorSheetV2) { + /** @inheritdoc */ + static DEFAULT_OPTIONS = { + classes: ["M6", "actor"], + window: { + icon: "fa-solid fa-dice-d10", + resizable: true + }, + position: { + width: 800, + height: 800 + }, + actions: { + viewDoc: this.#viewDoc, + createDoc: this.#createDoc, + deleteDoc: this.#deleteDoc, + toggleEffect: this.#toggleEffect, + rollSkill: this.#rollSkill + }, + form: { + submitOnChange: true + } + }; + + /* -------------------------------------------------- */ + + static TABS = { + primary: { + tabs: [ + { + id: "properties" + }, + { + id: "items" + }, + { + id: "effects" + } + ], + initial: "properties", + labelPrefix: "Midgard6.Sheets.Tabs" + } + }; + + /* -------------------------------------------------- */ + + /** @inheritdoc */ + static PARTS = { + header: { + template: systemPath("templates/actor/header.hbs") + }, + stats: { + template: systemPath("templates/actor/stats.hbs") + }, + tabs: { + template: "templates/generic/tab-navigation.hbs" + }, + properties: { + template: systemPath("templates/shared/properties.hbs"), + scrollable: [""] + }, + items: { + template: systemPath("templates/actor/items.hbs"), + scrollable: [""] + }, + effects: { + template: systemPath("templates/shared/effects.hbs"), + scrollable: [""] + } + }; + + /* -------------------------------------------------- */ + + /** @inheritdoc */ + _initializeApplicationOptions(options) { + const initialized = super._initializeApplicationOptions(options); + + initialized.classes.push(initialized.document.type); + + return initialized; + } + + /* -------------------------------------------------- */ + + /** @inheritdoc */ + async _prepareContext(options) { + const context = await super._prepareContext(options); + + Object.assign(context, { + owner: this.document.isOwner, + limited: this.document.limited, + actor: this.actor, + system: this.actor.system, + skills: this.actor.system.skills, + flags: this.actor.flags, + config: CONFIG + }); + + return context; + } + + /* -------------------------------------------------- */ + + /** @inheritdoc */ + async _preparePartContext(partId, context) { + switch (partId) { + case "effects": + context.effects = prepareActiveEffectCategories(this.actor.allApplicableEffects()); + context.tab = context.tabs[partId]; + break; + case "properties": + context.fields = await this._getSkills(); + context.tab = context.tabs[partId]; + break; + case "items": + context.itemTypes = this._getItems(); + context.tab = context.tabs[partId]; + break; + } + return context; + } + + /** + * Handles the system fields for the form-fields generic. + * @returns {object[]} + */ + async _getFields() { + const doc = this.actor; + const source = doc._source; + const systemFields = CONFIG.Actor.dataModels[doc.type]?.schema.fields; + const fieldSets = []; + // TODO: Find a clever way to handle enrichment + for (const field of Object.values(systemFields ?? {})) { + const path = `system.${field.name}`; + if (field instanceof foundry.data.fields.SchemaField) { + const fieldset = { fieldset: true, legend: field.label, fields: [] }; + await this.#addSystemFields(fieldset, field.fields, source, path); + fieldSets.push(fieldset); + } else { + fieldSets.push({ outer: { field, value: foundry.utils.getProperty(source, path) } }); + } + } + return fieldSets; + } + + async _getSkills() { + const doc = this.actor; + const source = doc._source; + const systemFields = CONFIG.Actor.dataModels[doc.type]?.schema.fields.skills.fields; + const fieldSets = []; + // TODO: Find a clever way to handle enrichment + for (const field of Object.values(systemFields ?? {})) { + const path = `system.skills.${field.name}`; + if (field instanceof foundry.data.fields.SchemaField) { + const fieldset = { + fieldset: true, + legend: `Midgard6.Skills.gruppen.${field.label}`, + fields: [] + }; + await this.#addSystemFields(fieldset, field.fields, source, path); + fieldSets.push(fieldset); + } else { + fieldSets.push({ outer: { field, value: foundry.utils.getProperty(source, path) } }); + } + } + return fieldSets; + } + + /* -------------------------------------------------- */ + + /** + * Recursively add system model fields to the fieldset. + */ + async #addSystemFields(fieldset, schema, source, _path = "system") { + for (const field of Object.values(schema)) { + const path = `${_path}.${field.name}`; + if (field instanceof foundry.data.fields.SchemaField) { + this.#addSystemFields(fieldset, field.fields, source, path); + } else if (field.constructor.hasFormSupport) { + fieldset.fields.push({ + field, + value: foundry.utils.getProperty(source, path), + localizationName: "Midgard6.Skills." + field.name + }); + } + } + } + + /* -------------------------------------------------- */ + + /** + * Adapted from Actor#itemTypes. + */ + _getItems() { + const types = Object.fromEntries( + game.documentTypes.Item.map((t) => { + return [t, { label: game.i18n.localize(CONFIG.Item.typeLabels[t]), items: [] }]; + }) + ); + for (const item of this.actor.items) { + types[item.type].items.push(item); + } + // Only show Base if it's actually being used + if (types.base.items.length === 0) delete types.base; + return types; + } + + /* -------------------------------------------------- */ + + /** + * Actions performed after any render of the Application. + * Post-render steps are not awaited by the render process. + * @param {ApplicationRenderContext} context Prepared context data. + * @param {RenderOptions} options Provided render options. + * @protected + * @inheritdoc + */ + async _onRender(context, options) { + await super._onRender(context, options); + this.#disableOverrides(); + } + + /* -------------------------------------------------- */ + /* Event handlers */ + /* -------------------------------------------------- */ + + /** + * Renders an embedded document's sheet. + * + * @this M6ActorSheet + * @param {PointerEvent} event The originating click event. + * @param {HTMLElement} target The capturing HTML element which defined a [data-action]. + * @protected + */ + static async #viewDoc(event, target) { + const doc = this._getEmbeddedDocument(target); + doc.sheet.render(true); + } + + /* -------------------------------------------------- */ + + /** + * Handles item deletion. + * + * @this M6ActorSheet + * @param {PointerEvent} event The originating click event. + * @param {HTMLElement} target The capturing HTML element which defined a [data-action]. + * @protected + */ + static async #deleteDoc(event, target) { + const doc = this._getEmbeddedDocument(target); + doc.delete(); + } + + /* -------------------------------------------------- */ + + /** + * Handle creating a new Owned Item or ActiveEffect for the actor using initial data defined in the HTML dataset. + * + * @this M6ActorSheet + * @param {PointerEvent} event The originating click event. + * @param {HTMLElement} target The capturing HTML element which defined a [data-action]. + * @private + */ + static async #createDoc(event, target) { + const docCls = getDocumentClass(target.dataset.documentClass); + const docData = { + name: docCls.defaultName({ + type: target.dataset.type, + parent: this.actor + }) + }; + for (const [dataKey, value] of Object.entries(target.dataset)) { + if (["action", "documentClass"].includes(dataKey)) continue; + foundry.utils.setProperty(docData, dataKey, value); + } + docCls.create(docData, { parent: this.actor }); + } + + /* -------------------------------------------------- */ + + /** + * Determines effect parent to pass to helper. + * + * @this M6ActorSheet + * @param {PointerEvent} event The originating click event. + * @param {HTMLElement} target The capturing HTML element which defined a [data-action]. + * @private + */ + static async #toggleEffect(event, target) { + const effect = this._getEmbeddedDocument(target); + effect.update({ disabled: !effect.disabled }); + } + + static async #rollSkill(event, target) { + console.warn(event, target.dataset); + // Construct the Roll instance + const skillValue = target.dataset.value; + let roll = new Roll("2d20kh + @skill", { skill: skillValue }, { flavor: target.dataset.name }); + + // The parsed terms of the roll formula + console.log("Roll:", roll); // [Die, OperatorTerm, NumericTerm, OperatorTerm, NumericTerm] + + // Execute the roll + await roll.evaluate(); + + roll.toMessage(); + + roll.toAnchor(); + // let message = new Message(roll); + // console.warn(message); + } + + /* -------------------------------------------------- */ + /* Helper functions */ + /* -------------------------------------------------- */ + + /** + * Fetches the embedded document representing the containing HTML element. + * + * @param {HTMLElement} target The element subject to search. + * @returns {Item|ActiveEffect} The embedded Item or ActiveEffect. + */ + _getEmbeddedDocument(target) { + const docRow = target.closest("li[data-document-class]"); + if (docRow.dataset.documentClass === "Item") { + return this.actor.items.get(docRow.dataset.itemId); + } else if (docRow.dataset.documentClass === "ActiveEffect") { + const parent = + docRow.dataset.parentId === this.actor.id + ? this.actor + : this.actor.items.get(docRow?.dataset.parentId); + return parent.effects.get(docRow.dataset.effectId); + } else { + console.warn("Could not find document class"); + } + } + + /* -------------------------------------------------- */ + /* Actor override handling */ + /* -------------------------------------------------- */ + + /** + * Submit a document update based on the processed form data. + * @param {SubmitEvent} event The originating form submission event. + * @param {HTMLFormElement} form The form element that was submitted. + * @param {object} submitData Processed and validated form data to be used for a document update. + * @returns {Promise} + * @protected + * @inheritdoc + */ + async _processSubmitData(event, form, submitData) { + const overrides = foundry.utils.flattenObject(this.actor.overrides); + for (const k of Object.keys(overrides)) delete submitData[k]; + this.document.update(submitData); + } + + /* -------------------------------------------------- */ + + /** + * Disables inpM6 subject to active effects. + */ + #disableOverrides() { + const flatOverrides = foundry.utils.flattenObject(this.actor.overrides); + for (const override of Object.keys(flatOverrides)) { + const input = this.element.querySelector(`[name="${override}"]`); + if (input) input.disabled = true; + } + } +} diff --git a/src/sheets/character-sheet.ts b/src/sheets/character-sheet.ts deleted file mode 100644 index 1bb7ebf..0000000 --- a/src/sheets/character-sheet.ts +++ /dev/null @@ -1,32 +0,0 @@ -import React from "react"; -import { createRoot, type Root } from "react-dom/client"; -import { CharacterSheetApp } from "./components/CharacterSheetApp"; - -export class CharacterSheet extends foundry.applications.sheets.ActorSheetV2 { - static DEFAULT_OPTIONS = { - classes: ["fvtt-ts-react-boilerplate", "sheet", "actor"], - position: { width: 600, height: 480 } - }; - - #root: Root | null = null; - - override async _renderHTML( - ...args: Parameters - ) { - return args[0]; - } - - override _replaceHTML( - ...args: Parameters - ) { - const content = args[1]; - if (!this.#root) this.#root = createRoot(content); - this.#root.render(React.createElement(CharacterSheetApp, { actor: this.document })); - } - - override async close(...args: Parameters) { - this.#root?.unmount(); - this.#root = null; - return super.close(...args); - } -} diff --git a/src/sheets/components/CharacterSheetApp.logic.test.ts b/src/sheets/components/CharacterSheetApp.logic.test.ts deleted file mode 100644 index d0931eb..0000000 --- a/src/sheets/components/CharacterSheetApp.logic.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { fmtMod, modifier } from "./CharacterSheetApp.logic"; - -describe("modifier", () => { - it("matches the d20-style table around the 10 baseline", () => { - expect(modifier(10)).toBe(0); - expect(modifier(11)).toBe(0); - expect(modifier(12)).toBe(1); - expect(modifier(9)).toBe(-1); - expect(modifier(20)).toBe(5); - }); -}); - -describe("fmtMod", () => { - it("prefixes non-negative values with +", () => { - expect(fmtMod(0)).toBe("+0"); - expect(fmtMod(3)).toBe("+3"); - }); - - it("leaves negative values as-is", () => { - expect(fmtMod(-2)).toBe("-2"); - }); -}); diff --git a/src/sheets/components/CharacterSheetApp.logic.ts b/src/sheets/components/CharacterSheetApp.logic.ts deleted file mode 100644 index f19cda1..0000000 --- a/src/sheets/components/CharacterSheetApp.logic.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Pure game-logic helpers, deliberately kept free of any Foundry global. -// Code that touches `actor`/`ChatMessage`/etc. needs a running Foundry instance -// and isn't realistically unit-testable — this is the boundary to keep that out. -export const modifier = (score: number) => Math.floor((score - 10) / 2); - -export const fmtMod = (mod: number) => (mod >= 0 ? `+${mod}` : `${mod}`); diff --git a/src/sheets/components/CharacterSheetApp.module.scss b/src/sheets/components/CharacterSheetApp.module.scss deleted file mode 100644 index 4d0a7e2..0000000 --- a/src/sheets/components/CharacterSheetApp.module.scss +++ /dev/null @@ -1,38 +0,0 @@ -.sheet { - display: flex; - flex-direction: column; - gap: 0.5rem; - padding: 0.5rem; -} - -.attributes { - display: flex; - gap: 1rem; -} - -.attributeLabel { - display: flex; - flex-direction: column; - font-size: 0.8rem; - text-transform: capitalize; - - .modifier { - font-weight: bold; - text-align: center; - } -} - -.totals { - display: flex; - gap: 1rem; - font-weight: bold; -} - -.chatRow { - display: flex; - gap: 0.5rem; - - input { - flex: 1; - } -} diff --git a/src/sheets/components/CharacterSheetApp.tsx b/src/sheets/components/CharacterSheetApp.tsx deleted file mode 100644 index 6110091..0000000 --- a/src/sheets/components/CharacterSheetApp.tsx +++ /dev/null @@ -1,75 +0,0 @@ -import React, { useState } from "react"; -import styles from "./CharacterSheetApp.module.scss"; -import { localize } from "../../i18n"; -import { modifier, fmtMod } from "./CharacterSheetApp.logic"; - -export function CharacterSheetApp({ actor }: { actor: Actor.Implementation }) { - const system = actor.system; - const attrs = system.attributes; - const [chatMessage, setChatMessage] = useState(""); - - const updateAttribute = (key: "strength" | "agility" | "spirit", value: number) => { - actor.update({ system: { attributes: { [key]: value } } }); - }; - - const updateBiography = (value: string) => { - actor.update({ system: { biography: value } }); - }; - - const sendChatMessage = () => { - if (!chatMessage.trim()) return; - ChatMessage.create({ - speaker: ChatMessage.getSpeaker({ actor }), - content: chatMessage - }); - setChatMessage(""); - }; - - // derived purely from current doc state — recomputed every render, no local state needed - const total = attrs.strength + attrs.agility + attrs.spirit; - const totalMod = modifier(attrs.strength) + modifier(attrs.agility) + modifier(attrs.spirit); - - return ( -
-

{actor.name}

-
- {(["strength", "agility", "spirit"] as const).map((key) => ( - - ))} -
-
- - {localize("BOILERPLATE.Sheet.Total")}: {total} - - - {localize("BOILERPLATE.Sheet.TotalMod")}: {fmtMod(totalMod)} - -
-