Skip to content

Commit

Permalink
fix: add guards against there not being a canvas.scene
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost91- committed Dec 26, 2021
1 parent 9b7da09 commit bc866bf
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/module/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ function getSceneControlButtons(controls) {
title: "CONTROLS.SaveMacro",
icon: "fas fa-save",
onClick: () => {
let flags = canvas.scene.getFlag("fxmaster", "effects");
if (!flags) flags = {};
let flags = canvas.scene?.getFlag("fxmaster", "effects") ?? {};
let objs = Object.values(flags);
let img = "icons/svg/windmill.svg";
let name = "Weather";
Expand All @@ -66,11 +65,13 @@ function getSceneControlButtons(controls) {
title: "CONTROLS.InvertMask",
icon: "fas fa-mask",
onClick: () => {
const invert = canvas.scene.getFlag("fxmaster", "invert");
canvas.scene.setFlag("fxmaster", "invert", !invert);
if (canvas.scene) {
const invert = canvas.scene.getFlag("fxmaster", "invert") ?? false;
canvas.scene.setFlag("fxmaster", "invert", !invert);
}
},
visible: game.user.isGM,
active: canvas.scene?.getFlag("fxmaster", "invert"),
active: canvas.scene?.getFlag("fxmaster", "invert") ?? false,
toggle: true,
},
{
Expand All @@ -92,8 +93,10 @@ function getSceneControlButtons(controls) {
title: game.i18n.localize("FXMASTER.Delete"),
content: game.i18n.localize("FXMASTER.DeleteConfirm"),
yes: () => {
filterManager.removeAll();
canvas.scene.unsetFlag("fxmaster", "effects");
if (canvas.scene) {
filterManager.removeAll();
canvas.scene.unsetFlag("fxmaster", "effects");
}
},
defaultYes: true,
});
Expand Down
9 changes: 9 additions & 0 deletions src/module/filterEffects/FilterManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class FilterManager {
* @returns {Promise<void>} A promise that resolves as soon as the filters have been updated
*/
async update({ skipFading = false } = {}) {
if (!canvas.scene) {
return;
}
this.filterInfos = canvas.scene.getFlag("fxmaster", "filters") ?? {};
this.filteredLayers = canvas.scene.getFlag("fxmaster", "filteredLayers") ?? this.filteredLayers;

Expand Down Expand Up @@ -133,6 +136,9 @@ class FilterManager {
* @returns {Promise<void>} A promise that resolves when the filter has been removed from the scene's fxmaster flags
*/
async removeFilter(name) {
if (!canvas.scene) {
return;
}
const filter = this.filters[name];
if (filter === undefined) return;
await filter.stop();
Expand All @@ -148,6 +154,9 @@ class FilterManager {
* flags
*/
async removeAll() {
if (!canvas.scene) {
return;
}
await canvas.scene.unsetFlag("fxmaster", "filters");
}

Expand Down
7 changes: 5 additions & 2 deletions src/module/filterEffects/applications/filters-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export class FiltersConfig extends FormApplication {
* @return {Object} The data provided to the template when rendering the form
*/
getData() {
const currentFilters = canvas.scene.getFlag("fxmaster", "filters") ?? {};
const currentFilters = canvas.scene?.getFlag("fxmaster", "filters") ?? {};
const activeFilters = Object.fromEntries(
Object.values(currentFilters).map((filter) => [filter.type, filter.options]),
);

const filteredLayers = canvas.scene.getFlag("fxmaster", "filteredLayers") ?? {
const filteredLayers = canvas.scene?.getFlag("fxmaster", "filteredLayers") ?? {
background: true,
foreground: true,
tokens: true,
Expand Down Expand Up @@ -102,6 +102,9 @@ export class FiltersConfig extends FormApplication {
* @private
*/
async _updateObject(_, formData) {
if (!canvas.scene) {
return;
}
const filtersDB = CONFIG.fxmaster.filters;

const filters = Object.fromEntries(
Expand Down
20 changes: 15 additions & 5 deletions src/module/fxmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ Hooks.on("updateScene", (scene, data) => {
if (
!game.settings.get("fxmaster", "enable") ||
game.settings.get("fxmaster", "disableAll") ||
!isOnTargetMigration()
!isOnTargetMigration() ||
scene !== canvas.scene
) {
return;
}
Expand All @@ -110,7 +111,7 @@ Hooks.on("updateScene", (scene, data) => {
});

Hooks.on("dropCanvasData", async (canvas, data) => {
if (!(canvas.activeLayer instanceof SpecialsLayer)) return;
if (!(canvas.activeLayer instanceof SpecialsLayer) || !canvas.scene) return;
if (data.type !== "SpecialEffect") return;

await new Promise((resolve) => {
Expand Down Expand Up @@ -160,15 +161,24 @@ Hooks.on("hotbarDrop", (hotbar, data) => {
};
});

Hooks.on("updateDrawing", () => {
Hooks.on("updateDrawing", (drawing) => {
if (drawing.parent !== canvas.scene) {
return;
}
canvas.fxmaster.updateMask();
});

Hooks.on("createDrawing", () => {
Hooks.on("createDrawing", (drawing) => {
if (drawing.parent !== canvas.scene) {
return;
}
canvas.fxmaster.updateMask();
});

Hooks.on("deleteDrawing", () => {
Hooks.on("deleteDrawing", (drawing) => {
if (drawing.parent !== canvas.scene) {
return;
}
canvas.fxmaster.updateMask();
});

Expand Down
3 changes: 3 additions & 0 deletions src/module/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const registerHooks = function () {
* @param {{name: string, type: string, options: object}} parameters The parameters that define the named weather effect
*/
async function onSwitchWeather(parameters) {
if (!canvas.scene) {
return;
}
const newEffect = { [parameters.name]: { type: parameters.type, options: parameters.options } };

let flags = (await canvas.scene.getFlag("fxmaster", "effects")) ?? {};
Expand Down
7 changes: 5 additions & 2 deletions src/module/weatherEffects/WeatherLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class WeatherLayer extends CanvasLayer {
* @type {boolean}
*/
get shouldMaskToScene() {
return !!canvas.scene.img;
return !!canvas.scene?.img;
}

_createInvertedMask() {
Expand Down Expand Up @@ -114,7 +114,7 @@ export class WeatherLayer extends CanvasLayer {
}

updateMask() {
if (!this.weather) return;
if (!this.weather || !canvas.scene) return;

if (this.weather.mask) {
this.weather.removeChild(this.weather.mask);
Expand Down Expand Up @@ -158,6 +158,9 @@ export class WeatherLayer extends CanvasLayer {
}

async drawWeather({ soft = false } = {}) {
if (!canvas.scene) {
return;
}
if (!this.weather) {
this.weather = this.addChild(new PIXI.Container());
if (this._sceneMaskFilter) {
Expand Down
2 changes: 1 addition & 1 deletion src/module/weatherEffects/applications/weather-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class WeatherConfig extends FormApplication {
* @return {Object} The data provided to the template when rendering the form
*/
getData() {
const currentEffects = canvas.scene.getFlag("fxmaster", "effects") ?? {};
const currentEffects = canvas.scene?.getFlag("fxmaster", "effects") ?? {};

const activeEffects = Object.fromEntries(
Object.values(currentEffects).map((effect) => [effect.type, effect.options]),
Expand Down

0 comments on commit bc866bf

Please sign in to comment.