Skip to content

Commit

Permalink
Add support for 0.8.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradfordC committed May 28, 2021
1 parent b536324 commit 4f17edb
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A module that allows you to change the default settings when creating new scenes
Simply click on "Edit Scene Defaults" under "Module Settings", edit the settings to your heart's content, and save it. Any scenes you create will start out with those settings.

# Compatibility
* **Foundry Version**: Scene Defaults is compatible with Foundry VTT 0.6.6 through 0.7.9, and is safe to use with newer versions as well. (The window for editing scene defaults might get funky, though.)
* **Foundry Version**: Scene Defaults is compatible with Foundry VTT 0.6.6 through 0.8.6, and is safe to use with newer versions as well. (The window for editing scene defaults might get funky, though.)
* **Systems**: Scene Defaults should be compatible with all systems, but won't let you edit scene settings that they might add.
* **Modules**: Scene Defaults shouldn't cause issues with other modules add to scene settings, but won't enable you to change default options for them.

Expand Down
2 changes: 1 addition & 1 deletion src/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "1.0.3",
"author": "iceman76",
"minimumCoreVersion": "0.6.6",
"compatibleCoreVersion": "0.7.9",
"compatibleCoreVersion": "0.8.5",
"esmodules": [
"sceneDefaults.js"
],
Expand Down
29 changes: 28 additions & 1 deletion src/module/foundryDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const defaultSceneData = {
shiftX: 0,
shiftY: 0,
gridColor: "#000000",
gridColor: "#000000",
gridAlpha: 0.2,
tokenVision: true,
globalLight: false,
Expand Down Expand Up @@ -50,5 +49,33 @@ export const defaultSceneData = {
journal: null,
playlist: null,
weather: null
},
"0.8.5": {
backgroundColor: "#999999",
darkness: 0,
fogExploration: true,
foreground: null,
globalLight: false,
globalLightThreshold: null,
grid: 100,
gridAlpha: 0.2,
gridColor: "#000000",
gridType: 1,
height: 3000,
img: null,
journal: null,
navName: null,
navigation: true,
padding: 0.25,
permission: {
default: 0
},
playlist: null,
playlistSound: null,
shiftX: 0,
shiftY: 0,
tokenVision: true,
weather: null,
width: 4000
}
}
50 changes: 45 additions & 5 deletions src/module/presetConfigWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ export class PresetConfigWindow extends FormApplication {
let data = Settings.getActivePresetData();
data.gridTypes = this._getGridTypes();
data.weatherTypes = this._getWeatherTypes();
data.playlists = this._getEntities(game.playlists);
data.journals = this._getEntities(game.journal);
// Getting entities was deprecated in 0.8.0,
// replaced by getting documents
if(isNewerVersion("0.8.0", game.data.version)) {
// Old
data.playlists = this._getEntities(game.playlists);
data.journals = this._getEntities(game.journal);
}
else {
// New
data.playlists = this._getDocuments(game.playlists);
const playlist = game.playlists.get(data.playlist);
data.sounds = this._getDocuments(playlist?.data.sounds ?? []);
data.journals = this._getDocuments(game.journal);
}

// Global illumination threshold
data.hasGlobalThreshold = data.globalLightThreshold !== null;
Expand All @@ -51,6 +63,7 @@ export class PresetConfigWindow extends FormApplication {
activateListeners(html) {
super.activateListeners(html);
html.find('button[name="reset-all"]').click(this._resetSettings.bind(this));
html.find("select[name='playlist']").change(this._onChangePlaylist.bind(this));
}

/**
Expand Down Expand Up @@ -93,7 +106,22 @@ export class PresetConfigWindow extends FormApplication {
}
}

/* -------------------------------------------- */
/**
* Handle updating the select menu of PlaylistSound options when the Playlist is changed
* @param {Event} event The initiating select change event
* @private
*/
_onChangePlaylist(event) {
event.preventDefault();
const playlist = game.playlists.get(event.target.value);
const sounds = this._getDocuments(playlist?.sounds || []);
const options = ['<option value=""></option>'].concat(sounds.map(s => {
return `<option value="${s.id}">${s.name}</option>`;
}));
const select = this.form.querySelector(`select[name="playlistSound"]`);
select.innerHTML = options.join("");
}

/**
* Get an enumeration of the available grid types which can be applied to this Scene
* @return {Object}
Expand All @@ -114,7 +142,6 @@ export class PresetConfigWindow extends FormApplication {
}, {});
}

/* -------------------------------------------- */
/**
* Get the available weather effect types which can be applied to this Scene
* @return {Object}
Expand All @@ -128,7 +155,6 @@ export class PresetConfigWindow extends FormApplication {
return types;
}

/* -------------------------------------------- */
/**
* Get the alphabetized entities which can be chosen as a configuration for the scene
* @param {EntityCollection} collection
Expand All @@ -142,4 +168,18 @@ export class PresetConfigWindow extends FormApplication {
entities.sort((a, b) => a.name.localeCompare(b.name));
return entities;
}

/**
* Get the alphabetized Documents which can be chosen as a configuration for the Scene
* @param {WorldCollection} collection
* @return {object[]}
* @private
*/
_getDocuments(collection) {
const documents = collection.map(doc => {
return {id: doc.id, name: doc.name};
});
documents.sort((a, b) => a.name.localeCompare(b.name));
return documents;
}
}
5 changes: 3 additions & 2 deletions src/module/versionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { defaultSceneData } from "./foundryDefaults.js";
*/
export class VersionHandler {
// The Foundry versions that have been implemented
static implementedFoundryVersions = ["0.6.6", "0.7.5"];
static implementedFoundryVersions = ["0.6.6", "0.7.5", "0.8.5"];
// The version we are treating Foundry as
static effectiveVersion;

Expand Down Expand Up @@ -60,7 +60,8 @@ export class VersionHandler {
}
const templates = {
"0.6.6": "./modules/scene-defaults/templates/defaultSceneConfigs/defaultSceneConfig-0-6-6.html",
"0.7.5": "./modules/scene-defaults/templates/defaultSceneConfigs/defaultSceneConfig-0-7-5.html"
"0.7.5": "./modules/scene-defaults/templates/defaultSceneConfigs/defaultSceneConfig-0-7-5.html",
"0.8.5": "./modules/scene-defaults/templates/defaultSceneConfigs/defaultSceneConfig-0-8-6.html"
}
return templates[VersionHandler.effectiveVersion];
}
Expand Down
30 changes: 28 additions & 2 deletions src/sceneDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,41 @@ Hooks.on("init", function() {
console.log("Scene Defaults | Initializing");
Settings.registerSettings();
VersionHandler.setEffectiveVersion(game.data.version);
});

Hooks.on("ready", function() {
if(Settings.getSavedVersion() !== VersionHandler.effectiveVersion) {
Settings.migrateSavedPresetsToCurrentVersion();
}
});

Hooks.on("preCreateScene", function(data, options) {
Hooks.on("preCreateScene", function(scene, data, options) {
// With 0.8.0's document change, we need to update the scene's data differently
// Include backwards compatibility for now
// Note: This hook's parameters changed from {data, options} to {scene, data, options}
if(isNewerVersion("0.8.0", game.data.version)) {
preCreateSceneLegacy(scene, data);
return;
}

if(!options.sdUseOriginal && !isCopy(data)) {
console.log("Scene Defaults | Replacing Defaults");
const preset = Settings.getActivePresetData();
const newData = mergeObject(data, preset, {
insertKeys: true,
insertValues: true,
overwrite: true,
inplace: false
});

scene.data.update(newData);
}
});

function preCreateSceneLegacy(data, options) {
if(!options.sdUseOriginal && !isCopy(data)) {
console.log("Scene Defaults | Replacing Defaults");
const newData = Settings.getActivePresetData();
mergeObject(data, newData, true, true, true);
}
});
};
Loading

0 comments on commit 4f17edb

Please sign in to comment.