ds4/src/module/ds4.ts

124 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-12-23 16:52:20 +01:00
// Import Modules
2020-12-23 17:05:12 +01:00
import { DS4Actor } from "./actor/actor";
import { DS4Item } from "./item/item";
import { DS4ItemSheet } from "./item/item-sheet";
import { DS4 } from "./config";
2021-01-08 23:18:01 +01:00
import { DS4Check } from "./rolls/check";
import { DS4CharacterActorSheet } from "./actor/sheets/character-sheet";
import { DS4CreatureActorSheet } from "./actor/sheets/creature-sheet";
import { createCheckRoll } from "./rolls/check-factory";
import { registerSystemSettings } from "./settings";
2021-01-20 22:11:53 +01:00
import { migration } from "./migrations";
2020-12-23 16:52:20 +01:00
Hooks.once("init", async function () {
console.log(`DS4 | Initializing the DS4 Game System\n${DS4.ASCII}`);
2021-02-04 12:32:07 +01:00
game["DS4"] = {
2020-12-23 16:52:20 +01:00
DS4Actor,
DS4Item,
DS4,
createCheckRoll,
2021-01-20 22:11:53 +01:00
migration,
2020-12-23 16:52:20 +01:00
};
// Record configuration
CONFIG.DS4 = DS4;
// Define custom Entity classes
CONFIG.Actor.entityClass = DS4Actor as typeof Actor;
CONFIG.Item.entityClass = DS4Item as typeof Item;
2020-12-23 16:52:20 +01:00
// Define localized type labels
2021-01-26 20:47:28 +01:00
CONFIG.Actor.typeLabels = DS4.i18n.actorTypes;
CONFIG.Item.typeLabels = DS4.i18n.itemTypes;
// Configure Dice
2021-01-31 19:57:14 +01:00
CONFIG.Dice.types = [Die, DS4Check]; // TODO(types) fix upstream
CONFIG.Dice.terms = {
c: Coin,
d: Die,
2021-01-08 23:18:01 +01:00
s: DS4Check,
};
// Register system settings
registerSystemSettings();
2020-12-23 16:52:20 +01:00
// Register sheet application classes
Actors.unregisterSheet("core", ActorSheet);
Actors.registerSheet("ds4", DS4CharacterActorSheet, { types: ["character"], makeDefault: true });
Actors.registerSheet("ds4", DS4CreatureActorSheet, { types: ["creature"], makeDefault: true });
2020-12-23 16:52:20 +01:00
Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("ds4", DS4ItemSheet, { makeDefault: true });
registerHandlebarsPartials();
});
async function registerHandlebarsPartials() {
const templatePaths = [
"systems/ds4/templates/item/partials/sheet-header.hbs",
"systems/ds4/templates/item/partials/description.hbs",
"systems/ds4/templates/item/partials/details.hbs",
"systems/ds4/templates/item/partials/effects.hbs",
"systems/ds4/templates/item/partials/body.hbs",
"systems/ds4/templates/actor/partials/items-overview.hbs",
"systems/ds4/templates/actor/partials/talents-abilities-overview.hbs",
2021-01-09 23:39:35 +01:00
"systems/ds4/templates/actor/partials/spells-overview.hbs",
"systems/ds4/templates/actor/partials/overview-add-button.hbs",
"systems/ds4/templates/actor/partials/overview-control-buttons.hbs",
"systems/ds4/templates/actor/partials/attributes-traits.hbs",
"systems/ds4/templates/actor/partials/combat-values.hbs",
"systems/ds4/templates/actor/partials/profile.hbs",
"systems/ds4/templates/actor/partials/character-progression.hbs",
"systems/ds4/templates/actor/partials/special-creature-abilites-overview.hbs",
2021-01-18 19:59:25 +01:00
"systems/ds4/templates/actor/partials/character-inventory.hbs",
"systems/ds4/templates/actor/partials/creature-inventory.hbs",
];
2020-12-23 16:52:20 +01:00
return loadTemplates(templatePaths);
}
/* -------------------------------------------- */
/* Foundry VTT Setup */
/* -------------------------------------------- */
/**
* This function runs after game data has been requested and loaded from the servers, so entities exist
*/
Hooks.once("setup", function () {
// Exclude some from sorting where the default order matters
const noSort = ["attributes", "traits", "combatValues", "creatureSizeCategories"];
2020-12-23 16:52:20 +01:00
// Localize and sort CONFIG objects
2021-01-26 20:47:28 +01:00
for (const o of Object.keys(CONFIG.DS4.i18n)) {
const localized = Object.entries(CONFIG.DS4.i18n[o]).map((e) => {
2020-12-23 16:52:20 +01:00
return [e[0], game.i18n.localize(e[1] as string)];
});
if (!noSort.includes(o)) localized.sort((a, b) => a[1].localeCompare(b[1]));
CONFIG.DS4.i18n[o] = localized.reduce((obj, e) => {
2020-12-23 16:52:20 +01:00
obj[e[0]] = e[1];
return obj;
}, {});
}
});
Hooks.once("ready", function () {
2021-01-20 22:11:53 +01:00
migration.migrate();
});
/**
* Select the text of input elements in given sheets via onfocus listener.
* The hook names are of the form "render"+sheet_superclassname and are called within
* the render() method of the foundry Application class.
* Note: The render hooks of all classes in the class hierarchy are called,
* so e.g. for a Dialog, both "renderDialog" and "renderApplication" are called
* (in this order).
*/
["renderApplication", "renderActorSheet", "renderItemSheet"].forEach((hookName: string) => {
2021-01-27 01:38:47 +01:00
Hooks.on(hookName, (app: Dialog, html: JQueryStatic) => {
$(html)
.find("input")
.on("focus", (ev: JQuery.FocusEvent<HTMLInputElement>) => {
ev.currentTarget.select();
});
});
});