Skip to content

Commit

Permalink
Merge pull request #1 from kaelad02/v10-compat
Browse files Browse the repository at this point in the history
V10 compat
  • Loading branch information
martinchaves authored Oct 22, 2022
2 parents d6e9d0f + 04242a1 commit 972c06c
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 428 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
download: https://github.com/${{github.repository}}/releases/download/${{github.event.release.tag_name}}/module.zip

# Create a zip file with all files required by the module to add to the release
- run: zip -r ./module.zip module.json src/ README.md packs/
- run: zip -r ./module.zip module.json src/ README.md packs/ templates/

# Create a release for this specific version
- name: Update Release with Files
Expand Down
16 changes: 9 additions & 7 deletions module.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "ATL",
"id": "ATL",
"title": "Active Token Effects",
"description": "Active Token Effects",
"author": "Kandashi",
"authors": [{
"name": "Kandashi"
}],
"version": "0.2.14",
"minimumCoreVersion": "9",
"compatibleCoreVersion": "9",

"compatibility": {
"minimum": 10,
"verified": 10
},
"esmodules": [
"src/activeLighting.js",
"src/updateManager.js"
Expand All @@ -17,8 +20,7 @@
"label": "Token Effects premade",
"path": "packs/premade",
"system" : "dnd5e",
"entity": "Item",
"module": "ATL"
"type": "Item"
}
],
"styles": [
Expand Down
44 changes: 22 additions & 22 deletions packs/premade

Large diffs are not rendered by default.

458 changes: 63 additions & 395 deletions src/activeLighting.js

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions src/preset-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* The Application used for defining a preset configuration that can be used by the `ATL.preset`
* active effect key. It can handle updating an existing preset as well as creating a new one.
*/
export class PresetConfig extends FormApplication {
/**
* Create a new application to add/edit a preset.
* @param {Object} object The ATL preset, or `undefined` if creating a new one from scratch
* @param {FormApplicationOptions} options Application configuration options
*/
constructor(object = {}, options = {}) {
super(object, options);

/**
* The token change preset
*/
this.preset = this.object;

/**
* Whether this app is creating a new preset or not
*/
this.newMode = !this.preset.id;
}

static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["sheet", "preset-config"],
title: "ATL Light Editor",
template: "modules/ATL/templates/preset-config.hbs",
width: 480,
height: "auto",
tabs: [
{
navSelector: '.tabs[data-group="main"]',
contentSelector: "form",
initial: "appearance",
},
{
navSelector: '.tabs[data-group="light"]',
contentSelector: '.tab[data-tab="light"]',
initial: "basic",
},
],
closeOnSubmit: true,
});
}

static savePreset(preset) {
// put all the presets into a collection
const collection = new Collection();
let presets = game.settings.get("ATL", "presets");
presets.forEach((p) => collection.set(p.id, p));

// add or update in collection
if (!preset.id) preset.id = foundry.utils.randomID();
collection.set(preset.id, preset);

// save collection
presets = collection.toJSON();
game.settings.set("ATL", "presets", presets);
}

getData(options) {
const gridUnits = game.system.gridUnits;

// prepare Preset data
const preset = foundry.utils.deepClone(this.object);

return {
object: preset,
gridUnits: gridUnits || game.i18n.localize("GridUnits"),
colorationTechniques: AdaptiveLightingShader.SHADER_TECHNIQUES,
visionModes: Object.values(CONFIG.Canvas.visionModes).filter((f) => f.tokenConfig),
lightAnimations: Object.entries(CONFIG.Canvas.lightAnimations).reduce(
(obj, e) => {
obj[e[0]] = game.i18n.localize(e[1].label);
return obj;
},
{ "": game.i18n.localize("None") }
),
scale: Math.abs(this.object.texture?.scaleX || 1),
};
}

_getSubmitData(updateData = {}) {
const formData = super._getSubmitData(updateData);

// Mirror token scale
if ("scale" in formData) {
formData["texture.scaleX"] = formData.scale * (formData.mirrorX ? -1 : 1);
formData["texture.scaleY"] = formData.scale * (formData.mirrorY ? -1 : 1);
}
["scale", "mirrorX", "mirrorY"].forEach((k) => delete formData[k]);

// Set default name if creating a new preset with no name
if (this.newMode && !formData.name) {
const presets = game.settings.get("ATL", "presets");
const count = presets?.length;
formData.name = `New Preset (${count + 1})`;
}

// Remove name change if updating a preset and trying to clear the name
if (!this.newMode && "name" in formData && !formData.name) delete formData.name;

return formData;
}

async _updateObject(event, formData) {
console.log("ATL |", "_updateObject called with formData:", formData);

// apply the changes to the original preset
Object.entries(formData)
.filter(([_, v]) => v !== "")
.forEach(([k, v]) => foundry.utils.setProperty(this.preset, k, v));
console.log("updated preset:", this.preset);

PresetConfig.savePreset(this.preset);
}
}
135 changes: 135 additions & 0 deletions src/updateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export class ATLUpdate {
ATLUpdate.v9Update()
}
case "0.3.0": {
ATLUpdate.v10Update();
}
case "0.5.0": {
console.log("ATL: no conversion needed")
}
}
Expand Down Expand Up @@ -219,5 +222,137 @@ export class ATLUpdate {
await this.flagBuster(actor)
}
}

static async v10Update() {
console.log("ATL: v10 update process starting");

// update presets
console.log("ATL: v10 update presets");
const presets = duplicate(game.settings.get("ATL", "presets"));
presets.forEach(this.v10UpdatePreset);
game.settings.set("ATL", "presets", presets);
console.log("ATL: v10 update presets done");

// update actors w/ linked tokens
console.log("ATL: v10 update actors");
for (const actor of game.actors.filter(a => a.prototypeToken.actorLink)) {
// update originals flag
const originals = actor.getFlag("ATL", "originals");
if (!!originals) {
this.v10UpdatePreset(originals);
await actor.setFlag("ATL", "originals", originals);
}

// update effects
for (const effect of actor.effects) {
const newChanges = this.v10UpdateEffectChanges(effect);
if (newChanges) await effect.update({ changes: newChanges });
}
}
console.log("ATL: v10 update actors done");

// update scenes
console.log("ATL: v10 update scenes");
for (const scene of game.scenes) {
// update the unlinked tokens
for (const token of scene.tokens.filter(t => !t.document.actorLink)) {
// update originals flag
const originals = token.getFlag("ATL", "originals");
if (!!originals) {
this.v10UpdatePreset(originals);
await token.setFlag("ATL", "originals", originals);
}
}
}
console.log("ATL: v10 update scenes done");

// update items
console.log("ATL: v10 update items");
for (const item of game.items) {
// update effects
for (const effect of item.effects) {
const newChanges = this.v10UpdateEffectChanges(effect);
if (newChanges) await effect.update({ changes: newChanges });
}
}
console.log("ATL: v10 update items done");

// record v10 update finished
await game.settings.set("ATL", "conversion", "0.5.0");
}

static v10UpdatePreset(preset) {
// rename some keys
const renamedKeys = [
["sightAngle", "sight.angle"],
["vision", "sight.enabled"]
];
for (const [oldKey, newKey] of renamedKeys) {
if (Object.hasOwn(preset, oldKey)) {
setProperty(preset, newKey, data[oldKey]);
delete data[oldKey];
}
}

// migrate old sight keys
if (Object.hasOwn(preset, "dimSight") || Object.hasOwn(preset, "brightSight")) {
// range is max previous keys
const dimSight = preset.dimSight ?? 0;
const brightSight = preset.brightSight ?? 0;
const range = Math.max(dimSight, brightSight);
setProperty(preset, "sight.range", range);
delete preset.dimSight;
delete preset.brightSight;

// compute brightness
const brightness = brightSight >= dimSight ? 1 : 0;
setProperty(preset, "sight.brightness", brightness);
}
}

static v10UpdateEffectChanges(effect) {
let changeFound = false;
const changes = duplicate(effect.changes);

// handle the renames
for (const change of changes) {
switch (change.key) {
case "ATL.sightAngle":
changeFound = true;
change.key = "ATL.sight.angle";
break;
case "ATL.vision":
changeFound = true;
change.key = "ATL.sight.enabled";
break;
}
}

// look for dimSight and brightSight to migrate to sight.range and sight.brightness
const dimSightChange = changes.find(change => change.key === "ATL.dimSight");
const brightSightChange = changes.find(change => change.key === "ATL.brightSight");
if (dimSightChange || brightSightChange) {
changeFound = true;

// calculate range and brightness
const dimSight = dimSightChange?.value ?? 0;
const brightSight = brightSightChange?.value ?? 0;
const range = Math.max(dimSight, brightSight);
const brightness = brightSight >= dimSight ? 1 : 0;

// remove the existing two changes
if (dimSightChange) changes.splice(changes.indexOf(dimSightChange), 1);
if (brightSightChange) changes.splice(changes.indexOf(brightSightChange), 1);

// create new changes
const mode = dimSightChange?.mode ?? brightSightChange?.mode ?? 0;
const priority = dimSightChange?.priority ?? brightSightChange?.priority ?? null;
changes.push({ key: "ATL.sight.range", value: range, mode, priority });
if (brightness !== 0)
changes.push({ key: "ATL.sight.brightness", value: brightness, mode, priority });
}

if (changeFound) return changes;
}
}

7 changes: 4 additions & 3 deletions styles/ATL.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.ATL-sub-group {
margin-left: 20px !important;
}
.preset-config nav.sheet-tabs.secondary-tabs {
margin-top: -4px;
border-top: none;
}
Loading

0 comments on commit 972c06c

Please sign in to comment.