Skip to content

Commit

Permalink
Auto template support for PF2e
Browse files Browse the repository at this point in the history
  • Loading branch information
Aedif committed May 1, 2023
1 parent c5fba00 commit 7ffb592
Show file tree
Hide file tree
Showing 6 changed files with 425 additions and 8 deletions.
6 changes: 3 additions & 3 deletions tokenmagic/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"id": "tokenmagic",
"title": "Token Magic FX",
"description": "<p>Add special effects and animations on your tokens, tiles, drawings and templates.</p>",
"version": "0.6.2",
"version": "0.6.3",
"compatibility": {
"minimum": "10",
"verified": "10.285",
"verified": "10.291",
"maximum": "10"
},
"authors": [
Expand Down Expand Up @@ -167,5 +167,5 @@
"socket": true,
"url": "https://github.com/Feu-Secret/Tokenmagic",
"manifest": "https://raw.githubusercontent.com/Feu-Secret/Tokenmagic/master/tokenmagic/module.json",
"download": "https://github.com/Feu-Secret/Tokenmagic/releases/download/v0.6.2-beta/Tokenmagic.zip"
"download": "https://github.com/Feu-Secret/Tokenmagic/releases/download/v0.6.3-beta/Tokenmagic.zip"
}
199 changes: 199 additions & 0 deletions tokenmagic/module/autoTemplate/pf2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { defaultOpacity, emptyPreset } from '../constants.js';

export class AutoTemplatePF2E {
static get defaultConfiguration() {
const defaultConfig = {
categories: {},
overrides: {
0: {
target: "Stinking Cloud",
opacity: 0.5,
tint: "#00a80b",
preset: "Smoky Area",
texture: null,
},
1: {
target: "Sanguine Mist",
opacity: 0.6,
tint: "#c41212",
preset: "Smoky Area",
},
2: {
target: "Web",
opacity: 0.5,
tint: "#808080",
preset: "Spider Web 2",
texture: null,
},
3: {
target: "Incendiary Aura",
opacity: 0.2,
tint: "#b12910",
preset: "Smoke Filaments",
texture: null
},
},
};

Object.keys(CONFIG.PF2E.damageTraits).forEach((dmgType) => {
if (defaultConfig.categories[dmgType] == undefined) {
const config = { opacity: defaultOpacity, tint: null };
switch (dmgType.toLowerCase()) {
case 'acid':
config.tint = '#2d8000';
config.opacity = 0.6;
break;
case 'cold':
config.tint = '#47b3ff';
break;
case 'electricity':
break;
case 'fire':
break;
case 'force':
break;
case 'mental':
config.tint = '#8000ff';
break;
case 'negative':
config.tint = '#502673';
break;
case 'poison':
config.tint = '#00a80b';
break;
case 'positive':
break;
case 'sonic':
config.tint = '#0060ff';
break;
default:
break;
}
defaultConfig.categories[dmgType] = config;
}
Object.keys(CONFIG.MeasuredTemplate.types).forEach((tplType) => {
const config = { preset: emptyPreset, texture: null }
switch (dmgType.toLowerCase()) {
case 'acid':
config.preset = 'Watery Surface 2';
break;
case 'cold':
config.preset = 'Thick Fog';
break;
case 'electricity':
config.preset = 'Shock'
break;
case 'fire':
config.preset = 'Flames';
break;
case 'force':
config.preset = 'Waves 3';
break;
case 'mental':
config.preset = 'Classic Rays';
break;
case 'negative':
config.preset = 'Smoke Filaments';
break;
case 'poison':
config.preset = 'Smoky Area';
break;
case 'positive':
config.preset = 'Annihilating Rays';
break;
case 'sonic':
config.preset = 'Waves';
break;
default:
break;
}
defaultConfig.categories[dmgType][tplType] = config;
});
});

return defaultConfig;
}

constructor() {
this._enabled = false;
}

configure(enabled = false) {
if (game.system.id !== "pf2e") return;
this._enabled = enabled;
}

get enabled(){
return this._enabled;
}

set enabled(value) {}

preCreateMeasuredTemplate(template) {
let hasPreset = template.hasOwnProperty("tmfxPreset");
if (hasPreset) {
return template;
}

const origin = template.flags?.pf2e?.origin;
const settings = game.settings.get('tokenmagic', 'autoTemplateSettings');
let updated = (settings.overrides ? fromOverrides(Object.values(settings.overrides), origin, template) : false);
if (!updated) {
fromCategories(settings.categories, origin, template);
}
return template;
}
}

function fromConfig(config, template) {
const o = { tokenmagic: { options: {} } };
if (config.preset && config.preset !== '' && config.preset !== emptyPreset) {
o.tokenmagic.options.tmfxPreset = config.preset;
}
if (config.texture && config.texture !== '') {
o.tokenmagic.options.tmfxTexture = config.texture;
}
if (config.tint && config.tint !== '') {
o.tokenmagic.options.tmfxTint = config.tint;
}
o.tokenmagic.options.tmfxTextureAlpha = config.opacity;
template.updateSource({flags: {tokenmagic : o.tokenmagic}});
}

function fromOverrides(overrides = [], origin, template) {
const {name, slug} = origin;

let config = overrides.find((el) => el.target.toLowerCase() === name?.toLowerCase());
if (!config) {
return false;
}
fromConfig(config, template);
return true;
}

function fromCategories(categories = {}, origin, template) {
if (!origin.traits?.length) {
return false;
}

let config, dmgSettings;

// some templates may have multiple traits
// this loop looks over all of them until it finds one with a valid fx preset
for (const trait of origin.traits) {
dmgSettings = categories[trait.toLowerCase()] || {};
config = dmgSettings[template.t];

if (config && config.preset !== emptyPreset) {
break;
}
}
if (!config) {
return false;
}
fromConfig(mergeObject(config, { opacity: dmgSettings.opacity, tint: dmgSettings.tint }, true, true), template);
return true;
}

export const pf2eTemplates = new AutoTemplatePF2E();

35 changes: 30 additions & 5 deletions tokenmagic/module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {presets as defaultPresets, PresetsLibrary} from "../fx/presets/defaultpr
import {DataVersion} from "../migration/migration.js";
import {TokenMagic, isVideoDisabled, fixPath} from "./tokenmagic.js";
import {AutoTemplateDND5E, dnd5eTemplates} from "./autoTemplate/dnd5e.js";
import {AutoTemplatePF2E, pf2eTemplates} from "./autoTemplate/pf2e.js";
import {defaultOpacity, emptyPreset} from "./constants.js";

const Magic = TokenMagic();
Expand Down Expand Up @@ -68,6 +69,17 @@ export class TokenMagicSettings extends FormApplication {
}, true, true)
);
break;
case "pf2e":
hasAutoTemplates = true;
game.settings.registerMenu("tokenmagic", menuAutoTemplateSettings.key, menuAutoTemplateSettings.config);
game.settings.register(
"tokenmagic",
settingAutoTemplateSettings.key,
mergeObject(settingAutoTemplateSettings.config, {
default: AutoTemplatePF2E.defaultConfiguration
}, true, true)
);
break;
default:
break;
}
Expand Down Expand Up @@ -197,17 +209,24 @@ export class TokenMagicSettings extends FormApplication {
loadTemplates([
"modules/tokenmagic/templates/settings/settings.html",
"modules/tokenmagic/templates/settings/dnd5e/categories.html",
"modules/tokenmagic/templates/settings/dnd5e/overrides.html"
"modules/tokenmagic/templates/settings/dnd5e/overrides.html",
"modules/tokenmagic/templates/settings/pf2e/categories.html",
"modules/tokenmagic/templates/settings/pf2e/overrides.html"
]);
}

static configureAutoTemplate(enabled = false) {
switch ( game.system.id ) {
this.getSystemTemplates()?.configure(enabled);
}

static getSystemTemplates() {
switch (game.system.id) {
case "dnd5e":
dnd5eTemplates.configure(enabled);
break;
return dnd5eTemplates;
case "pf2e":
return pf2eTemplates;
default:
break;
return null;
}
}

Expand All @@ -217,6 +236,7 @@ export class TokenMagicSettings extends FormApplication {
};
switch ( game.system.id ) {
case "dnd5e":
case "pf2e":
settingsData["autoTemplateSettings"] = game.settings.get("tokenmagic", "autoTemplateSettings");
break;
default:
Expand All @@ -236,6 +256,11 @@ export class TokenMagicSettings extends FormApplication {
data.dmgTypes = CONFIG.DND5E.damageTypes;
data.templateTypes = CONFIG.MeasuredTemplate.types;
break;
case "pf2e":
data.hasAutoTemplates = true;
data.dmgTypes = CONFIG.PF2E.damageTraits;
data.templateTypes = CONFIG.MeasuredTemplate.types;
break;
default:
break;
}
Expand Down
7 changes: 7 additions & 0 deletions tokenmagic/module/tokenmagic.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {emptyPreset} from "./constants.js";
import "./proto/PlaceableObjectProto.js";
import { FilterCRT } from "../fx/filters/FilterCRT.js";
import { FilterRGBSplit } from "../fx/filters/FilterRGBSplit.js";
import { TokenMagicSettings } from "./settings.js";

/*
Expand Down Expand Up @@ -2004,6 +2005,12 @@ Hooks.on("preUpdateMeasuredTemplate", async (document, options) => {
/* -------------------------------------------- */

Hooks.on("preCreateMeasuredTemplate", (document) => {
// Apply auto-preset if needed
const templates = TokenMagicSettings.getSystemTemplates();
if (templates?.enabled) {
templates.preCreateMeasuredTemplate?.(document);
}

const hasFlags = document.flags;
let hasPreset = false;
let hasTint = false;
Expand Down
Loading

0 comments on commit 7ffb592

Please sign in to comment.