From a23c3d8150394748ee319c1ec1ec03e371414d0b Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Tue, 28 Feb 2023 01:24:00 +0100 Subject: [PATCH] first part of token arranger updates --- .../TokenArranger.022907.json | 3 ++ src/accessories/TokenArranger.ttslua | 45 ++++++++++++----- src/accessories/TokenArrangerApi.ttslua | 13 +++++ src/core/MythosArea.ttslua | 50 ++++++++++++++----- 4 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 src/accessories/TokenArrangerApi.ttslua diff --git a/objects/Fan-MadeAccessories.aa8b38/TokenArranger.022907.json b/objects/Fan-MadeAccessories.aa8b38/TokenArranger.022907.json index c5c2ddad..951952de 100644 --- a/objects/Fan-MadeAccessories.aa8b38/TokenArranger.022907.json +++ b/objects/Fan-MadeAccessories.aa8b38/TokenArranger.022907.json @@ -40,6 +40,9 @@ "Nickname": "Token Arranger", "Snap": true, "Sticky": true, + "Tags": [ + "TokenArranger" + ], "Tooltip": true, "Transform": { "posX": 22.951, diff --git a/src/accessories/TokenArranger.ttslua b/src/accessories/TokenArranger.ttslua index b47dede3..b0d6d217 100644 --- a/src/accessories/TokenArranger.ttslua +++ b/src/accessories/TokenArranger.ttslua @@ -49,9 +49,9 @@ updating = false function onSave() return JSON.encode(TOKEN_PRECEDENCE) end -function onLoad(save_state) - if save_state ~= nil then - TOKEN_PRECEDENCE = JSON.decode(save_state) +function onLoad(saveState) + if saveState ~= nil then + TOKEN_PRECEDENCE = JSON.decode(saveState) end -- create UI @@ -85,26 +85,22 @@ function onLoad(save_state) buttonParameters.width = 675 buttonParameters.height = 175 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 Wait.time(function() getObjectFromGUID("5933fb").setVar("tokenArranger", self) end, 1) end +-- delete tokens and remove reference from bless/curse manager function onDestroy() deleteCopiedTokens() -- remove object reference from bless/curse manager getObjectFromGUID("5933fb").setVar("tokenArranger", nil) end -function onPickUp() - deleteCopiedTokens() -end +-- layout tokens when dropped (after 2 seconds) +function onDrop() Wait.time(layout, 2) end + +-- delete tokens when picked up +function onPickUp() deleteCopiedTokens() end -- helper functions to carry index function attachIndex(click_function, index) @@ -245,3 +241,26 @@ function layout(_, _, isRightClick) end updating = false 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 diff --git a/src/accessories/TokenArrangerApi.ttslua b/src/accessories/TokenArrangerApi.ttslua new file mode 100644 index 00000000..dab706a5 --- /dev/null +++ b/src/accessories/TokenArrangerApi.ttslua @@ -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 diff --git a/src/core/MythosArea.ttslua b/src/core/MythosArea.ttslua index 5877a4fa..adc8d9d6 100644 --- a/src/core/MythosArea.ttslua +++ b/src/core/MythosArea.ttslua @@ -1,5 +1,6 @@ -local playArea = require("core/PlayAreaApi") -local tokenSpawnTracker = require("core/token/TokenSpawnTrackerApi") +local playAreaApi = require("core/PlayAreaApi") +local tokenArrangerApi = require("accessories/TokenArrangerApi") +local tokenSpawnTrackerApi = require("core/token/TokenSpawnTrackerApi") local ENCOUNTER_DECK_AREA = { upperLeft = { x = 0.9, z = 0.42 }, @@ -11,39 +12,60 @@ local ENCOUNTER_DISCARD_AREA = { } local currentScenario +local useFrontData -- we use this to turn off collision handling until onLoad() is complete -local COLLISION_ENABLED = false +local collisionEnabled = false function onLoad(saveState) if saveState ~= nil then local loadedState = JSON.decode(saveState) or { } - currentScenario = loadedState.currentScenario + currentScenario = loadedState.currentScenario or "" + useFrontData = loadedState.useFrontData or true end - COLLISION_ENABLED = true + collisionEnabled = true end function onSave() return JSON.encode({ - currentScenario = currentScenario + currentScenario = currentScenario, + useFrontData = useFrontData }) end -- TTS event handler. Handles scenario name event triggering and encounter card token resets. function onCollisionEnter(collisionInfo) - if not COLLISION_ENABLED then + if not collisionEnabled then return end local object = collisionInfo.collision_object if object.getName() == "Scenario" then - if currentScenario ~= object.getDescription() then - currentScenario = object.getDescription() + local updateNeeded = false + 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() 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 local localPos = self.positionToLocal(object.getPosition()) if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then - tokenSpawnTracker.resetTokensSpawned(object.getGUID()) + tokenSpawnTrackerApi.resetTokensSpawned(object.getGUID()) end end @@ -52,13 +74,17 @@ end function onObjectEnterContainer(container, object) local localPos = self.positionToLocal(container.getPosition()) if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then - tokenSpawnTracker.resetTokensSpawned(object.getGUID()) + tokenSpawnTrackerApi.resetTokensSpawned(object.getGUID()) end end function fireScenarioChangedEvent() Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20) - playArea.onScenarioChanged(currentScenario) + playAreaApi.onScenarioChanged(currentScenario) +end + +function fireTokenDataChangedEvent(tokenData) + tokenArrangerApi.onTokenDataChanged(tokenData) end -- Simple method to check if the given point is in a specified area. Local use only,