also grab data onLoad

This commit is contained in:
Chr1Z93 2023-03-01 14:42:10 +01:00
parent b9fe13b658
commit 5850a81d68
6 changed files with 220 additions and 187 deletions

View File

@ -34,7 +34,7 @@
"LayoutGroupSortIndex": 0,
"Locked": false,
"LuaScript": "require(\"accessories/TokenArranger\")",
"LuaScriptState_path": "Fan-MadeAccessories.aa8b38/TokenArranger.022907.luascriptstate",
"LuaScriptState": "",
"MeasureMovement": false,
"Name": "Custom_Token",
"Nickname": "Token Arranger",

View File

@ -1 +0,0 @@
{"":[0,11],"Auto-fail":[-100,7],"Bless":[101,8],"Cultist":[-2,4],"Curse":[-101,9],"Elder Sign":[100,2],"Elder Thing":[-4,6],"Frost":[-99,10],"Skull":[-1,3],"Tablet":[-3,5]}

View File

@ -1,17 +1,3 @@
local TOKEN_PRECEDENCE = {}
local TOKEN_NAMES = {
"Elder Sign",
"Skull",
"Cultist",
"Tablet",
"Elder Thing",
"Auto-fail",
"Bless",
"Curse",
"Frost",
""
}
-- common parameters
local buttonParameters = {}
buttonParameters.function_owner = self
@ -30,13 +16,35 @@ inputParameters.alignment = 3
inputParameters.validation = 2
inputParameters.tab = 2
updating = false
local latestLoad = "XXX"
local updating = false
local tokenPrecedence = {}
local TOKEN_NAMES = {
"Elder Sign",
"Skull",
"Cultist",
"Tablet",
"Elder Thing",
"Auto-fail",
"Bless",
"Curse",
"Frost",
""
}
function onSave() return JSON.encode(TOKEN_PRECEDENCE) end
-- saving the precedence settings and information on the most recently loaded data
function onSave()
return JSON.encode({
tokenPrecedence = tokenPrecedence,
latestLoad = latestLoad
})
end
function onLoad(saveState)
if saveState ~= nil then
TOKEN_PRECEDENCE = JSON.decode(saveState)
if saveState ~= nil and saveState ~= "" then
local loadedData = JSON.decode(saveState)
tokenPrecedence = loadedData.tokenPrecedence
latestLoad = loadedData.latestLoad or "XXX"
else
loadDefaultValues()
end
@ -57,7 +65,7 @@ function onLoad(saveState)
buttonParameters.click_function = attachIndex("tokenClick", i)
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = TOKEN_PRECEDENCE[TOKEN_NAMES[i]][1]
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
self.createButton(buttonParameters)
self.createInput(inputParameters)
@ -79,6 +87,12 @@ function onLoad(saveState)
updateUI()
layout()
end)
-- grab token metadata from mythos area
local mythosArea = getObjectFromGUID("9f334f")
Wait.time(function() mythosArea.call("fireTokenDataChangedEvent") end, 0.5)
Wait.time(layout, 2)
end
-- delete temporary tokens when destroyed
@ -110,8 +124,8 @@ end
-- click_function for buttons on chaos tokens
function tokenClick(isRightClick, index)
local change = tonumber(isRightClick and "-1" or "1")
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] + change
self.editInput({ index = index - 1, value = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] })
tokenPrecedence[TOKEN_NAMES[index]][1] = tokenPrecedence[TOKEN_NAMES[index]][1] + change
self.editInput({ index = index - 1, value = tokenPrecedence[TOKEN_NAMES[index]][1] })
layout()
end
@ -120,7 +134,7 @@ function tokenInput(input, selected, index)
if selected == false then
local num = tonumber(input)
if num ~= nil then
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = num
tokenPrecedence[TOKEN_NAMES[index]][1] = num
end
layout()
end
@ -130,7 +144,7 @@ end
function loadDefaultValues()
-- token modifiers for sorting (and order for same modifier)
-- order starts at 2 because there is a "+1" token
TOKEN_PRECEDENCE = {
tokenPrecedence = {
["Elder Sign"] = { 100, 2 },
["Skull"] = { -1, 3 },
["Cultist"] = { -2, 4 },
@ -147,17 +161,22 @@ end
-- update input fields
function updateUI()
for i = 1, 10 do
self.editInput({ index = i-1, value = TOKEN_PRECEDENCE[TOKEN_NAMES[i]][1] })
self.editInput({ index = i - 1, value = tokenPrecedence[TOKEN_NAMES[i]][1] })
end
end
-- order function for data sorting
function token_value_comparator(left, right)
if left.value > right.value then return true
elseif right.value > left.value then return false
elseif left.order < right.order then return true
elseif right.order < left.order then return false
else return left.token.getGUID() > right.token.getGUID()
if left.value > right.value then
return true
elseif right.value > left.value then
return false
elseif left.order < right.order then
return true
elseif right.order < left.order then
return false
else
return left.token.getGUID() > right.token.getGUID()
end
end
@ -182,7 +201,10 @@ function layout(_, _, isRightClick)
deleteCopiedTokens()
-- stop here if right-clicked
if isRightClick then return end
if isRightClick then
updating = false
return
end
local chaosBag = findChaosBag()
local data = {}
@ -196,7 +218,7 @@ function layout(_, _, isRightClick)
})
local value = tonumber(obj["Nickname"])
local precedence = TOKEN_PRECEDENCE[obj["Nickname"]]
local precedence = tokenPrecedence[obj["Nickname"]]
data[i] = {
token = spawnedObj,
@ -238,13 +260,23 @@ function layout(_, _, isRightClick)
end
-- called from outside to set default values for tokens
function onTokenDataChanged(tokenData)
function onTokenDataChanged(parameters)
local tokenData = parameters.tokenData or {}
local currentScenario = parameters.currentScenario or ""
local useFrontData = parameters.useFrontData or "true"
-- only update if this data is new
local info = currentScenario .. useFrontData
if latestLoad == info then return end
latestLoad = info
-- update token precedence
for key, table in pairs(tokenData) do
local modifier = table.modifier
if modifier == -999 then modifier = 0 end
TOKEN_PRECEDENCE[key][1] = modifier
tokenPrecedence[key][1] = modifier
end
updateUI()
layout()
end

View File

@ -13,8 +13,12 @@ do
-- updates the token modifiers with the provided data
---@param tokenData Table Contains the chaos token metadata
TokenArrangerApi.onTokenDataChanged = function(tokenData)
callIfExistent("onTokenDataChanged", tokenData)
TokenArrangerApi.onTokenDataChanged = function(tokenData, currentScenario, useFrontData)
callIfExistent("onTokenDataChanged", {
tokenData = tokenData,
currentScenario = currentScenario,
useFrontData = useFrontData
})
end
-- deletes already laid out tokens

View File

@ -919,16 +919,7 @@ function spawnHelperObject(name, position, rotation, color)
return
end
local spawnTable = {
position = position,
callback_function = function(object)
if name == "Hand Helper" then
Wait.time(function() object.call("externalColorChange", color) end, 0.1)
elseif name == "Token Arranger" then
Wait.time(function() object.call("layout") end, 0.1)
end
end
}
local spawnTable = {position = position}
-- only overrride rotation if there is one provided (object's rotation used instead)
if rotation then

View File

@ -13,6 +13,8 @@ local ENCOUNTER_DISCARD_AREA = {
local currentScenario
local useFrontData
local tokenData
-- we use this to turn off collision handling until onLoad() is complete
local collisionEnabled = false
@ -21,6 +23,7 @@ function onLoad(saveState)
local loadedState = JSON.decode(saveState) or {}
currentScenario = loadedState.currentScenario or ""
useFrontData = loadedState.useFrontData or true
tokenData = loadedState.tokenData or {}
end
collisionEnabled = true
end
@ -28,7 +31,8 @@ end
function onSave()
return JSON.encode({
currentScenario = currentScenario,
useFrontData = useFrontData
useFrontData = useFrontData,
tokenData = tokenData
})
end
@ -54,12 +58,13 @@ function onCollisionEnter(collisionInfo)
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)
tokenData = metadata["tokens"][(useFrontData and "front" or "back")]
fireTokenDataChangedEvent()
end
end
@ -78,13 +83,15 @@ function onObjectEnterContainer(container, object)
end
end
-- fires if the scenario title changes
function fireScenarioChangedEvent()
Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20)
playAreaApi.onScenarioChanged(currentScenario)
end
function fireTokenDataChangedEvent(tokenData)
tokenArrangerApi.onTokenDataChanged(tokenData)
-- fires if the scenario title or the difficulty changes
function fireTokenDataChangedEvent()
tokenArrangerApi.onTokenDataChanged(tokenData, currentScenario, tostring(useFrontData))
end
-- Simple method to check if the given point is in a specified area. Local use only,