Skip to content

Commit

Permalink
## 1.0.0 (Jan 30, 2021)
Browse files Browse the repository at this point in the history
* First Release
  • Loading branch information
zarmstrong committed Jan 30, 2021
1 parent 7dace39 commit 8310900
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.PNG
artifacts/
build-release.txt
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0 (Jan 30, 2021)

* First Release
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# fvtt-lightswitch
This module allows players to trigger a macro that turns on and off lights (through the GM)
# LightSwitch
This module allows players to trigger a macro that turns on and off lights (through the GM).

## How to use
###### **NOTE:** A GM and player are required for this module to work.

1. Open up any light configuration panel and set the name of the light. (Multiple lights can share a name and be triggered as a group.) When you're done, click Update Light Source.
![Screenshot 1](image/screen1.webp)
2. Toggle the light(s) on or off in any combination by right clicking a light.
3. Create a macro with the type *script*, with the following contents, replacing *lightgroup1* with the name of your light(s): `game.LightSwitch.flipTheSwitch("lightgroup1")`
4. Use whatever module you wish to trigger the macro. I recommend the module [Trigger Happy](https://foundryvtt.com/packages/trigger-happy/)
5. Repeat the process for any other lights

When the macro is triggered, all lights matching the name you set in the macro will be toggled. If there are multiple lights, this could mean that some switch on, while others switch off.
Binary file added image/screen1.webp
Binary file not shown.
10 changes: 10 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"LIGHTSWITCH": {
"app": {
"title": "LightSwitch"
},
"LightName": {
"title": "Light Name"
}
}
}
105 changes: 105 additions & 0 deletions lightswitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
class LightSwitch {
static debug=false;

static async init() {
console.log("%c Light%cSwitch %c| initializing",'color: #7bf542','color: #d8eb34','color: #ffffff');
LightSwitch.SOCKET = "module.LightSwitch";

// init socket
game.socket.on(LightSwitch.SOCKET, data => {
if (debug) {
console.log("%c Light%cSwitch %c| received socket data",'color: #7bf542','color: #d8eb34','color: #ffffff')
console.log(data)
}
if (data.switchLight == true && game.user.isGM) {
LightSwitch.switchLight(data);
}
});
}

static switchLight(data)
{
console.log("%c Light%cSwitch %c| switching lights",'color: #7bf542','color: #d8eb34','color: #ffffff')
const light = data.lightName;
console.log(`light name im searching for: ${light}`);
const allLights = LightingLayer.instance.objects.children
var lightOnIDs=[];
var lightOffIDs=[];
for (let i = 0; i <= allLights.length - 1; i++)
{
if (allLights[i].data.flags.LightSwitch && allLights[i].data.flags.LightSwitch.lightName==light) {
if (debug)
console.log(`found a light named ${light}`)
//var lightStatus=allLights[i].data.hidden;
if (allLights[i].data.hidden == false)
lightOffIDs.push(allLights[i].data._id)
else
lightOnIDs.push(allLights[i].data._id)
}
}
if (debug) {
console.log("on")
console.log(lightOnIDs)
console.log("off")
console.log(lightOffIDs)
}
console.log(`%c Light%cSwitch %c| switching lights off: ${lightOffIDs.join(', ')}`,'color: #7bf542','color: #d8eb34','color: #ffffff')
canvas.lighting.updateAll({hidden: true}, (light => lightOffIDs.includes(light.id)))
console.log(`%c Light%cSwitch %c| switching lights on: ${lightOnIDs.join(', ')}`,'color: #7bf542','color: #d8eb34','color: #ffffff')
canvas.lighting.updateAll({hidden: false}, (light => lightOnIDs.includes(light.id)))
}

static async onRenderLightConfig(objectConfig, html, data) {
var prefix = "lightSwitch";
var lightNameTitle = game.i18n.localize("LIGHTSWITCH.LightName.title");
var lightTypeSelector = html.find("[name='t']").parent(); // Get parent div
var windowDiv = lightTypeSelector.parent().parent().parent();
var newwheight=windowDiv.height()+30;
windowDiv.height(newwheight);
var currentValue;
const lightObj = objectConfig.object;
currentValue=lightObj.getFlag("LightSwitch", "lightName") || "light1"
var customNameEl = $(
`<div class="form-group">
<label>${lightNameTitle}</label>
<input type="text" name="lightSwitch.lightName" value="${currentValue}" data-dtype="String">
</div>`);
lightTypeSelector.before(customNameEl);
}
static onUpdateLight(scene, object, changes, diff){
if(changes.lightSwitch && !diff.loadedProperty){ // Only attempt to save if lightSwitch prop has changed
LightSwitch.saveCustomProperties(object, changes);
}
}
static async saveCustomProperties(object, changes) {

var customProperties = JSON.parse(JSON.stringify(changes.lightSwitch)); // Clone changes
var placeable = canvas.lighting.get(object._id);

if(!customProperties || Object.getOwnPropertyNames(customProperties).length == 0){
await placeable.unsetFlag("LightSwitch", "lightName"); // Remove flag if no custom vars
} else {
await placeable.setFlag("LightSwitch", "lightName", customProperties.lightName); // Set flag with custom vars
}
}
}

export async function flipTheSwitch(lightName) {
if (debug)
console.log("%c Light%cSwitch %c| Starting to send to socket",'color: #7bf542','color: #d8eb34','color: #ffffff')
game.socket.emit(
LightSwitch.SOCKET, {
switchLight: true,
lightName: lightName
});
}

Hooks.once('init', LightSwitch.init);
Hooks.on("renderLightConfig", LightSwitch.onRenderLightConfig);
Hooks.on("updateAmbientLight", LightSwitch.onUpdateLight);
Hooks.on('ready', () => {
console.log("%c Light%cSwitch %c| Creating Macro Hooks",'color: #7bf542','color: #d8eb34','color: #ffffff')
game['LightSwitch'] = {
flipTheSwitch: flipTheSwitch
};
});
28 changes: 28 additions & 0 deletions module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "LightSwitch",
"title": "LightSwitch",
"description": "A module that allows players to trigger lights through macros.",
"author": "slate",
"version": "0.0.1",
"minimumCoreVersion": "0.7.6",
"compatibleCoreVersion": "0.7.9",
"scripts": [],
"esmodules": [
"lightswitch.js"
],
"languages": [
{
"lang": "en",
"name": "English",
"path": "lang/en.json"
}
],
"socket": true,
"url": "https://github.com/zarmstrong/fvtt-lightswitch",
"manifest": "https://raw.githubusercontent.com/zarmstrong/fvtt-lightswitch/main/module.json",
"download": "https://github.com/zarmstrong/fvtt-lightswitch/releases/download/LightSwitch-0.0.1/LightSwitch-0.0.1.zip",
"license": "MIT",
"readme": "https://github.com/zarmstrong/fvtt-lightswitch",
"bugs": "https://github.com/zarmstrong/fvtt-lightswitch/issues",
"changelog": "https://github.com/zarmstrong/fvtt-lightswitch/blob/main/CHANGELOG.md"
}

0 comments on commit 8310900

Please sign in to comment.