diff --git a/dev-module.json b/dev-module.json index a43625c..3b2220f 100644 --- a/dev-module.json +++ b/dev-module.json @@ -3,9 +3,9 @@ "title": "Dynamic Illumination (DEV)", "description": "Adds additional setting for lighting time of day color, additional lighting 'times' (Dawn/Dusk), and make scene lighting impact global illumination setting.", "author": "delVhar", - "version": "0.7.7", + "version": "0.9.0", "minimumCoreVersion": "0.7.6", - "compatibleCoreVersion": "0.7.7", + "compatibleCoreVersion": "0.7.9", "scripts": ["scripts/dynamic-illumination.js"], "languages": [ { diff --git a/lang/en.json b/lang/en.json index d1e21cb..4fcf65a 100644 --- a/lang/en.json +++ b/lang/en.json @@ -27,5 +27,7 @@ "dynamic-illumination.dayColor.name": "Day Color", "dynamic-illumination.dayColor.hint": "The lighting overlay color for Day. Use hex color reference (ie: #FFFEFE)", "dynamic-illumination.dayLevel.name": "Day Darkness Level", - "dynamic-illumination.dayLevel.hint": "The darkness level to set when Day is selected. If global illumination linking is enabled this will determine whether it is off at Day." + "dynamic-illumination.dayLevel.hint": "The darkness level to set when Day is selected. If global illumination linking is enabled this will determine whether it is off at Day.", + "dynamic-illumination.allowInterrupt.name": "Allow Interrupt (Expermental)", + "dynamic-illumination.allowInterrupt.hint": "Whether or not to allow interrupting an in flight change with a new change. May result in incorrect color/darkness level" } \ No newline at end of file diff --git a/module.json b/module.json index 0f73d0b..d3a5545 100644 --- a/module.json +++ b/module.json @@ -3,9 +3,9 @@ "title": "Dynamic Illumination", "description": "Adds additional setting for lighting time of day color, additional lighting 'times' (Dawn/Dusk)", "author": "delVhar", - "version": "0.7.7", + "version": "0.9.0", "minimumCoreVersion": "0.7.6", - "compatibleCoreVersion": "0.7.7", + "compatibleCoreVersion": "0.7.9", "scripts": ["scripts/dynamic-illumination.js"], "languages": [ @@ -18,6 +18,6 @@ "socket": true, "url": "https://github.com/delVhariant/illumination", "manifest": "https://raw.githubusercontent.com/delVhariant/illumination/master/module.json", - "download": "https://github.com/delVhariant/illumination/archive/0.7.7.zip" + "download": "https://github.com/delVhariant/illumination/archive/v0.9.0.zip" } diff --git a/scripts/dynamic-illumination.js b/scripts/dynamic-illumination.js index 3e3c4b8..1ebb5de 100644 --- a/scripts/dynamic-illumination.js +++ b/scripts/dynamic-illumination.js @@ -30,9 +30,33 @@ class SceneColorChange function changeLighting(level, color) { + //console.log(`Called with col: ${color} and level: ${level}`); + if(game.settings.get("dynamic-illumination","animateDarknessChange")) { - canvas.scene.update({darkness: level}, {animateDarkness: true}).then(() => { + if(canvas.getLayer("LightingLayer")._animating || canvas.scene.getFlag("dynamic-illumination","_animating")) + { + + if(game.settings.get("dynamic-illumination","allowInterrupt")) + { + // An attempt to gracefully interrupt ongoing changes that doesn't quite work correctly. + // console.log(CanvasAnimation.animations) + // console.log("Cancelling darkness animation"); + CanvasAnimation.terminateAnimation("lighting.animateDarkness"); + CanvasAnimation.terminateAnimation("lighting.darknessColor") + canvas.getLayer("LightingLayer")._animating = false; + canvas.scene.setFlag("dynamic-illumination","_animating", false); + // console.log(CanvasAnimation.animations) + // console.log(canvas.getLayer("LightingLayer")._animating); + } + else + { + ui.notifications.notify('Scene color/darkness already animating. You can enable interrupting ongiong changes in settings.', "warning"); + return; + } + } + + canvas.scene.update({darkness: level}, {animateDarkness: true}).then(() => { interpolateSceneColor(color) }); } @@ -47,32 +71,31 @@ function changeLighting(level, color) async function interpolateSceneColor(target="#FFFEFF") { - if(canvas.scene.getFlag("dynamic-illumination","_animating")) - return - - const interpolationData = [{ - parent: {interpolationSteps: 0}, - attribute: "interpolationSteps", - to: 20 - }]; - - canvas.scene.setFlag("dynamic-illumination","_animating", true); - return CanvasAnimation.animateLinear(interpolationData, { - name: "lighting.darknessColor", - duration: game.settings.get("dynamic-illumination","animationColorChangeDelay") * 1000, - ontick: (dt, attributes) => { - color = interpolateColor(canvas.scene.getFlag("dynamic-illumination","darknessColor"), target, attributes[0].parent.interpolationSteps/attributes[0].to) - // Only update if we actually changed color - if(color.toLowerCase() != canvas.scene.getFlag("dynamic-illumination","darknessColor").toLowerCase()) - { - SendColorChange(color); - } - } - }).then(() => { - canvas.scene.setFlag("dynamic-illumination","_animating", false); - //Set it to the target at the end in case it wasn't there for some reason - SendColorChange(target); - }); + const interpolationData = [{ + parent: {interpolationSteps: 0}, + attribute: "interpolationSteps", + to: 20 + }]; + + canvas.scene.setFlag("dynamic-illumination","_animating", true); + return CanvasAnimation.animateLinear(interpolationData, { + name: "lighting.darknessColor", + duration: game.settings.get("dynamic-illumination","animationColorChangeDelay") * 1000, + ontick: (dt, attributes) => { + color = interpolateColor(canvas.scene.getFlag("dynamic-illumination","darknessColor"), target, attributes[0].parent.interpolationSteps/attributes[0].to) + // Only update if we actually changed color + if(color.toLowerCase() != canvas.scene.getFlag("dynamic-illumination","darknessColor").toLowerCase()) + { + SendColorChange(color); + } + } + }).then(() => { + canvas.scene.setFlag("dynamic-illumination","_animating", false); + //Set it to the target at the end in case it wasn't there for some reason + SendColorChange(target); + console.log("finished color change"); + }); + } function interpolateColor(color1, color2, factor) { @@ -108,7 +131,6 @@ function SendColorChange(color) CONFIG.Canvas.exploredColor = convertedColor; canvas.sight.refresh(); } - game.socket.emit("module.dynamic-illumination"); canvas.getLayer("LightingLayer").refresh(); }).then(() => { @@ -126,7 +148,6 @@ function ReceiveColorChange() CONFIG.Canvas.exploredColor = convertedColor; canvas.sight.refresh(); } - canvas.getLayer("LightingLayer").refresh(); } @@ -142,12 +163,13 @@ Hooks.on('getSceneControlButtons', controls => { if(dayButton) { control.tools[dayButton].onClick = () => {changeLighting(game.settings.get("dynamic-illumination","dayLevel"), game.settings.get("dynamic-illumination","dayColor"))}; - control.tools.splice(dayButton, 0, { + control.tools.splice(dayButton, 0, { name: "dawn", title: "Transition to Dawn", icon: "far fa-sun", visible: game.settings.get("dynamic-illumination", "showDawnDusk"), - onClick: () => {changeLighting(game.settings.get("dynamic-illumination","dawnLevel"), game.settings.get("dynamic-illumination","dawnColor"))} + onClick: () => {changeLighting(game.settings.get("dynamic-illumination","dawnLevel"), game.settings.get("dynamic-illumination","dawnColor"))}, + button: true }); } else @@ -165,7 +187,8 @@ Hooks.on('getSceneControlButtons', controls => { title: "Transition to Dusk", icon: "far fa-moon", visible: game.settings.get("dynamic-illumination", "showDawnDusk"), - onClick: () => {changeLighting(game.settings.get("dynamic-illumination","duskLevel"), game.settings.get("dynamic-illumination","duskColor"))} + onClick: () => {changeLighting(game.settings.get("dynamic-illumination","duskLevel"), game.settings.get("dynamic-illumination","duskColor"))}, + button: true }); control.tools.splice(nightButton+2, 0, { @@ -179,7 +202,8 @@ Hooks.on('getSceneControlButtons', controls => { var colorChange = new SceneColorChange(); colorChange.displayWindow() - } + }, + button: true }); } else @@ -212,6 +236,15 @@ Hooks.once("init", () => range: {min: 0.0, max: 1.0, step: 0.05} });*/ + game.settings.register("dynamic-illumination", "allowInterrupt", { + name: game.i18n.localize("dynamic-illumination.allowInterrupt.name"), + hint: game.i18n.localize("dynamic-illumination.allowInterrupt.hint"), + scope: "world", + config: true, + default: false, + type: Boolean + }); + game.settings.register("dynamic-illumination", "animateDarknessChange", { name: game.i18n.localize("dynamic-illumination.animateDarknessChange.name"), hint: game.i18n.localize("dynamic-illumination.animateDarknessChange.hint"), @@ -328,11 +361,12 @@ Hooks.once("init", () => }) -Hooks.once("canvasInit", () => { +Hooks.on("canvasReady", () => { var color = canvas.scene.getFlag("dynamic-illumination","darknessColor"); if(game.user.isGM) { canvas.scene.unsetFlag("core","darknessColor"); // Delete darknessColor Flag to clean up old usage...Replace this with a button in options? + canvas.scene.setFlag("dynamic-illumination","_animating", false); // Get rid of any left over animating flags // Set Canvas Darkness color to match flag if(color == undefined)