first part of token arranger updates

This commit is contained in:
Chr1Z93 2023-02-28 01:24:00 +01:00
parent c68667a2ce
commit a23c3d8150
4 changed files with 86 additions and 25 deletions

View File

@ -40,6 +40,9 @@
"Nickname": "Token Arranger", "Nickname": "Token Arranger",
"Snap": true, "Snap": true,
"Sticky": true, "Sticky": true,
"Tags": [
"TokenArranger"
],
"Tooltip": true, "Tooltip": true,
"Transform": { "Transform": {
"posX": 22.951, "posX": 22.951,

View File

@ -49,9 +49,9 @@ updating = false
function onSave() return JSON.encode(TOKEN_PRECEDENCE) end function onSave() return JSON.encode(TOKEN_PRECEDENCE) end
function onLoad(save_state) function onLoad(saveState)
if save_state ~= nil then if saveState ~= nil then
TOKEN_PRECEDENCE = JSON.decode(save_state) TOKEN_PRECEDENCE = JSON.decode(saveState)
end end
-- create UI -- create UI
@ -85,26 +85,22 @@ function onLoad(save_state)
buttonParameters.width = 675 buttonParameters.width = 675
buttonParameters.height = 175 buttonParameters.height = 175
self.createButton(buttonParameters) self.createButton(buttonParameters)
self.addContextMenuItem("More Information", function()
printToAll("------------------------------", "White")
printToAll("Token Arranger by Chr1Z", "Orange")
printToAll("original concept by Whimsical", "White")
end)
-- send object reference to bless/curse manager -- send object reference to bless/curse manager
Wait.time(function() getObjectFromGUID("5933fb").setVar("tokenArranger", self) end, 1) Wait.time(function() getObjectFromGUID("5933fb").setVar("tokenArranger", self) end, 1)
end end
-- delete tokens and remove reference from bless/curse manager
function onDestroy() function onDestroy()
deleteCopiedTokens() deleteCopiedTokens()
-- remove object reference from bless/curse manager -- remove object reference from bless/curse manager
getObjectFromGUID("5933fb").setVar("tokenArranger", nil) getObjectFromGUID("5933fb").setVar("tokenArranger", nil)
end end
function onPickUp() -- layout tokens when dropped (after 2 seconds)
deleteCopiedTokens() function onDrop() Wait.time(layout, 2) end
end
-- delete tokens when picked up
function onPickUp() deleteCopiedTokens() end
-- helper functions to carry index -- helper functions to carry index
function attachIndex(click_function, index) function attachIndex(click_function, index)
@ -245,3 +241,26 @@ function layout(_, _, isRightClick)
end end
updating = false updating = false
end end
-- called from outside to set default values for tokens
function onTokenDataChanged(tokenData)
-- Skull
local table = tokenData["Skull"] or {}
local skullModifier = table.modifier or ""
print("Skull: " .. skullModifier)
-- Cultist
local table = tokenData.Cultist or {}
local cultistModifier = table.modifier or ""
print("Cultist: " .. cultistModifier)
-- Tablet
local table = tokenData.Tablet or {}
local tabletModifier = table.modifier or ""
print("Tablet: " .. tabletModifier)
-- Elder Thing
local table = tokenData.ElderThing or {}
local elderThingModifier = table.modifier or ""
print("Elder Thing: " .. elderThingModifier)
end

View File

@ -0,0 +1,13 @@
do
local TokenArrangerApi = {}
TokenArrangerApi.onTokenDataChanged = function(tokenData)
local tokenArranger = getObjectsWithTag("TokenArranger")[1]
if tokenArranger ~= nil then
tokenArranger.call("onTokenDataChanged", tokenData)
end
end
return TokenArrangerApi
end

View File

@ -1,5 +1,6 @@
local playArea = require("core/PlayAreaApi") local playAreaApi = require("core/PlayAreaApi")
local tokenSpawnTracker = require("core/token/TokenSpawnTrackerApi") local tokenArrangerApi = require("accessories/TokenArrangerApi")
local tokenSpawnTrackerApi = require("core/token/TokenSpawnTrackerApi")
local ENCOUNTER_DECK_AREA = { local ENCOUNTER_DECK_AREA = {
upperLeft = { x = 0.9, z = 0.42 }, upperLeft = { x = 0.9, z = 0.42 },
@ -11,39 +12,60 @@ local ENCOUNTER_DISCARD_AREA = {
} }
local currentScenario local currentScenario
local useFrontData
-- we use this to turn off collision handling until onLoad() is complete -- we use this to turn off collision handling until onLoad() is complete
local COLLISION_ENABLED = false local collisionEnabled = false
function onLoad(saveState) function onLoad(saveState)
if saveState ~= nil then if saveState ~= nil then
local loadedState = JSON.decode(saveState) or { } local loadedState = JSON.decode(saveState) or { }
currentScenario = loadedState.currentScenario currentScenario = loadedState.currentScenario or ""
useFrontData = loadedState.useFrontData or true
end end
COLLISION_ENABLED = true collisionEnabled = true
end end
function onSave() function onSave()
return JSON.encode({ return JSON.encode({
currentScenario = currentScenario currentScenario = currentScenario,
useFrontData = useFrontData
}) })
end end
-- TTS event handler. Handles scenario name event triggering and encounter card token resets. -- TTS event handler. Handles scenario name event triggering and encounter card token resets.
function onCollisionEnter(collisionInfo) function onCollisionEnter(collisionInfo)
if not COLLISION_ENABLED then if not collisionEnabled then
return return
end end
local object = collisionInfo.collision_object local object = collisionInfo.collision_object
if object.getName() == "Scenario" then if object.getName() == "Scenario" then
if currentScenario ~= object.getDescription() then local updateNeeded = false
currentScenario = object.getDescription() local description = object.getDescription()
-- detect if orientation of scenario card changed (flipped to other side)
if object.is_face_down == useFrontData then
useFrontData = not useFrontData
updateNeeded = true
end
-- detect if another scenario card is placed down
if currentScenario ~= description then
currentScenario = description
updateNeeded = true
fireScenarioChangedEvent() fireScenarioChangedEvent()
end end
-- trigger update if a change was detected and push new data
if updateNeeded then
local metadata = JSON.decode(object.getGMNotes()) or {}
if not metadata["tokens"] then return end
local tokenData = metadata["tokens"][(useFrontData and "front" or "back")]
fireTokenDataChangedEvent(tokenData)
end
end end
local localPos = self.positionToLocal(object.getPosition()) local localPos = self.positionToLocal(object.getPosition())
if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then
tokenSpawnTracker.resetTokensSpawned(object.getGUID()) tokenSpawnTrackerApi.resetTokensSpawned(object.getGUID())
end end
end end
@ -52,13 +74,17 @@ end
function onObjectEnterContainer(container, object) function onObjectEnterContainer(container, object)
local localPos = self.positionToLocal(container.getPosition()) local localPos = self.positionToLocal(container.getPosition())
if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then
tokenSpawnTracker.resetTokensSpawned(object.getGUID()) tokenSpawnTrackerApi.resetTokensSpawned(object.getGUID())
end end
end end
function fireScenarioChangedEvent() function fireScenarioChangedEvent()
Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20) Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20)
playArea.onScenarioChanged(currentScenario) playAreaApi.onScenarioChanged(currentScenario)
end
function fireTokenDataChangedEvent(tokenData)
tokenArrangerApi.onTokenDataChanged(tokenData)
end end
-- Simple method to check if the given point is in a specified area. Local use only, -- Simple method to check if the given point is in a specified area. Local use only,