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, "LayoutGroupSortIndex": 0,
"Locked": false, "Locked": false,
"LuaScript": "require(\"accessories/TokenArranger\")", "LuaScript": "require(\"accessories/TokenArranger\")",
"LuaScriptState_path": "Fan-MadeAccessories.aa8b38/TokenArranger.022907.luascriptstate", "LuaScriptState": "",
"MeasureMovement": false, "MeasureMovement": false,
"Name": "Custom_Token", "Name": "Custom_Token",
"Nickname": "Token Arranger", "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 -- common parameters
local buttonParameters = {} local buttonParameters = {}
buttonParameters.function_owner = self buttonParameters.function_owner = self
@ -21,64 +7,92 @@ buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 325 buttonParameters.width = 325
buttonParameters.height = 325 buttonParameters.height = 325
local inputParameters = {} local inputParameters = {}
inputParameters.function_owner = self inputParameters.function_owner = self
inputParameters.font_size = 100 inputParameters.font_size = 100
inputParameters.width = 250 inputParameters.width = 250
inputParameters.height = inputParameters.font_size + 23 inputParameters.height = inputParameters.font_size + 23
inputParameters.alignment = 3 inputParameters.alignment = 3
inputParameters.validation = 2 inputParameters.validation = 2
inputParameters.tab = 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) function onLoad(saveState)
if saveState ~= nil then if saveState ~= nil and saveState ~= "" then
TOKEN_PRECEDENCE = JSON.decode(saveState) local loadedData = JSON.decode(saveState)
tokenPrecedence = loadedData.tokenPrecedence
latestLoad = loadedData.latestLoad or "XXX"
else
loadDefaultValues()
end
-- create UI
local offset = 0.725
local pos = { x = { -1.067, 0.377 }, z = -2.175 }
-- button and inputs index 0-9
for i = 1, 10 do
if i < 6 then
buttonParameters.position = { pos.x[1], 0, pos.z + i * offset }
inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset }
else else
loadDefaultValues() buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end end
-- create UI buttonParameters.click_function = attachIndex("tokenClick", i)
local offset = 0.725 inputParameters.input_function = attachIndex2("tokenInput", i)
local pos = { x = { -1.067, 0.377 }, z = -2.175 } inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
-- button and inputs index 0-9
for i = 1, 10 do
if i < 6 then
buttonParameters.position = { pos.x[1], 0, pos.z + i * offset }
inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset }
else
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end
buttonParameters.click_function = attachIndex("tokenClick", i)
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = TOKEN_PRECEDENCE[TOKEN_NAMES[i]][1]
self.createButton(buttonParameters)
self.createInput(inputParameters)
end
-- index 10: "Update / Hide" button
buttonParameters.label = "Update / Hide"
buttonParameters.click_function = "layout"
buttonParameters.tooltip = "Left-Click: Update!\nRight-Click: Hide Tokens!"
buttonParameters.position = { 0.725, 0.1, 2.025 }
buttonParameters.color = { 1, 1, 1 }
buttonParameters.width = 675
buttonParameters.height = 175
self.createButton(buttonParameters) self.createButton(buttonParameters)
self.createInput(inputParameters)
end
-- reset context menu -- index 10: "Update / Hide" button
self.addContextMenuItem("Load default values", function() buttonParameters.label = "Update / Hide"
loadDefaultValues() buttonParameters.click_function = "layout"
updateUI() buttonParameters.tooltip = "Left-Click: Update!\nRight-Click: Hide Tokens!"
layout() buttonParameters.position = { 0.725, 0.1, 2.025 }
end) buttonParameters.color = { 1, 1, 1 }
buttonParameters.width = 675
buttonParameters.height = 175
self.createButton(buttonParameters)
-- reset context menu
self.addContextMenuItem("Load default values", function()
loadDefaultValues()
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 end
-- delete temporary tokens when destroyed -- delete temporary tokens when destroyed
@ -92,159 +106,177 @@ function onPickUp() deleteCopiedTokens() end
-- helper functions to carry index -- helper functions to carry index
function attachIndex(click_function, index) function attachIndex(click_function, index)
local fn_name = click_function .. index local fn_name = click_function .. index
_G[fn_name] = function(_, _, isRightClick) _G[fn_name] = function(_, _, isRightClick)
_G[click_function](isRightClick, index) _G[click_function](isRightClick, index)
end end
return fn_name return fn_name
end end
function attachIndex2(input_function, index) function attachIndex2(input_function, index)
local fn_name = input_function .. index local fn_name = input_function .. index
_G[fn_name] = function(_, _, input, selected) _G[fn_name] = function(_, _, input, selected)
_G[input_function](input, selected, index) _G[input_function](input, selected, index)
end end
return fn_name return fn_name
end end
-- click_function for buttons on chaos tokens -- click_function for buttons on chaos tokens
function tokenClick(isRightClick, index) function tokenClick(isRightClick, index)
local change = tonumber(isRightClick and "-1" or "1") local change = tonumber(isRightClick and "-1" or "1")
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] + change tokenPrecedence[TOKEN_NAMES[index]][1] = tokenPrecedence[TOKEN_NAMES[index]][1] + change
self.editInput({ index = index - 1, value = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] }) self.editInput({ index = index - 1, value = tokenPrecedence[TOKEN_NAMES[index]][1] })
layout() layout()
end end
-- input_function for input_boxes -- input_function for input_boxes
function tokenInput(input, selected, index) function tokenInput(input, selected, index)
if selected == false then if selected == false then
local num = tonumber(input) local num = tonumber(input)
if num ~= nil then if num ~= nil then
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = num tokenPrecedence[TOKEN_NAMES[index]][1] = num
end
layout()
end end
layout()
end
end end
-- loads the default precedence table -- loads the default precedence table
function loadDefaultValues() function loadDefaultValues()
-- token modifiers for sorting (and order for same modifier) -- token modifiers for sorting (and order for same modifier)
-- order starts at 2 because there is a "+1" token -- order starts at 2 because there is a "+1" token
TOKEN_PRECEDENCE = { tokenPrecedence = {
["Elder Sign"] = { 100, 2 }, ["Elder Sign"] = { 100, 2 },
["Skull"] = { -1, 3 }, ["Skull"] = { -1, 3 },
["Cultist"] = { -2, 4 }, ["Cultist"] = { -2, 4 },
["Tablet"] = { -3, 5 }, ["Tablet"] = { -3, 5 },
["Elder Thing"] = { -4, 6 }, ["Elder Thing"] = { -4, 6 },
["Auto-fail"] = { -100, 7 }, ["Auto-fail"] = { -100, 7 },
["Bless"] = { 101, 8 }, ["Bless"] = { 101, 8 },
["Curse"] = { -101, 9 }, ["Curse"] = { -101, 9 },
["Frost"] = { -99, 10 }, ["Frost"] = { -99, 10 },
[""] = { 0, 11 } [""] = { 0, 11 }
} }
end end
-- update input fields -- update input fields
function updateUI() function updateUI()
for i = 1, 10 do 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
end end
-- order function for data sorting -- order function for data sorting
function token_value_comparator(left, right) function token_value_comparator(left, right)
if left.value > right.value then return true if left.value > right.value then
elseif right.value > left.value then return false return true
elseif left.order < right.order then return true elseif right.value > left.value then
elseif right.order < left.order then return false return false
else return left.token.getGUID() > right.token.getGUID() elseif left.order < right.order then
end return true
elseif right.order < left.order then
return false
else
return left.token.getGUID() > right.token.getGUID()
end
end end
-- checks scripting zone for chaos bag -- checks scripting zone for chaos bag
function findChaosBag() function findChaosBag()
for _, item in ipairs(getObjectFromGUID("83ef06").getObjects()) do for _, item in ipairs(getObjectFromGUID("83ef06").getObjects()) do
if item.getDescription() == "Chaos Bag" then if item.getDescription() == "Chaos Bag" then
return item return item
end
end end
end
end end
-- deletes previously placed tokens -- deletes previously placed tokens
function deleteCopiedTokens() function deleteCopiedTokens()
for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end
end end
-- main function (delete old tokens, clone chaos bag content, sort it and position it) -- main function (delete old tokens, clone chaos bag content, sort it and position it)
function layout(_, _, isRightClick) function layout(_, _, isRightClick)
if updating then return end if updating then return end
updating = true updating = true
deleteCopiedTokens() deleteCopiedTokens()
-- stop here if right-clicked -- stop here if right-clicked
if isRightClick then return end if isRightClick then
updating = false
return
end
local chaosBag = findChaosBag() local chaosBag = findChaosBag()
local data = {} local data = {}
-- clone tokens from chaos bag (default position above trash can) -- clone tokens from chaos bag (default position above trash can)
for i, obj in ipairs(chaosBag.getData().ContainedObjects) do for i, obj in ipairs(chaosBag.getData().ContainedObjects) do
obj["Tags"] = { "tempToken" } obj["Tags"] = { "tempToken" }
local spawnedObj = spawnObjectData({ local spawnedObj = spawnObjectData({
data = obj, data = obj,
position = { 0.49, 3, 0 } position = { 0.49, 3, 0 }
}) })
local value = tonumber(obj["Nickname"]) local value = tonumber(obj["Nickname"])
local precedence = TOKEN_PRECEDENCE[obj["Nickname"]] local precedence = tokenPrecedence[obj["Nickname"]]
data[i] = { data[i] = {
token = spawnedObj, token = spawnedObj,
value = value or precedence[1] value = value or precedence[1]
} }
if precedence ~= nil then if precedence ~= nil then
data[i].order = precedence[2] data[i].order = precedence[2]
else else
data[i].order = value data[i].order = value
end
end end
end
-- sort table by value (symbols last if same value) -- sort table by value (symbols last if same value)
table.sort(data, token_value_comparator) table.sort(data, token_value_comparator)
-- error handling for removal of token arranger -- error handling for removal of token arranger
if self == nil then if self == nil then
for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end
return return
end
-- laying out the tokens
local pos = self.getPosition() + Vector(3.55, -0.05, -3.95)
local location = { x = pos.x, y = pos.y, z = pos.z }
local current_value = data[1].value
for _, item in ipairs(data) do
if item.value ~= current_value then
location.x = location.x - 1.75
location.z = pos.z
current_value = item.value
end end
item.token.setPosition(location)
-- laying out the tokens item.token.setRotation(self.getRotation())
local pos = self.getPosition() + Vector(3.55, -0.05, -3.95) location.z = location.z - 1.75
local location = { x = pos.x, y = pos.y, z = pos.z } end
local current_value = data[1].value Wait.time(function() updating = false end, 0.1)
for _, item in ipairs(data) do
if item.value ~= current_value then
location.x = location.x - 1.75
location.z = pos.z
current_value = item.value
end
item.token.setPosition(location)
item.token.setRotation(self.getRotation())
location.z = location.z - 1.75
end
Wait.time(function() updating = false end, 0.1)
end end
-- called from outside to set default values for tokens -- called from outside to set default values for tokens
function onTokenDataChanged(tokenData) function onTokenDataChanged(parameters)
-- update token precedence local tokenData = parameters.tokenData or {}
for key, table in pairs(tokenData) do local currentScenario = parameters.currentScenario or ""
local modifier = table.modifier local useFrontData = parameters.useFrontData or "true"
if modifier == -999 then modifier = 0 end
TOKEN_PRECEDENCE[key][1] = modifier -- only update if this data is new
end local info = currentScenario .. useFrontData
updateUI() if latestLoad == info then return end
layout() latestLoad = info
-- update token precedence
for key, table in pairs(tokenData) do
local modifier = table.modifier
if modifier == -999 then modifier = 0 end
tokenPrecedence[key][1] = modifier
end
updateUI()
layout()
end end

View File

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

View File

@ -919,16 +919,7 @@ function spawnHelperObject(name, position, rotation, color)
return return
end end
local spawnTable = { local spawnTable = {position = position}
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
}
-- only overrride rotation if there is one provided (object's rotation used instead) -- only overrride rotation if there is one provided (object's rotation used instead)
if rotation then if rotation then

View File

@ -13,14 +13,17 @@ local ENCOUNTER_DISCARD_AREA = {
local currentScenario local currentScenario
local useFrontData local useFrontData
local tokenData
-- 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 collisionEnabled = 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 or "" currentScenario = loadedState.currentScenario or ""
useFrontData = loadedState.useFrontData or true useFrontData = loadedState.useFrontData or true
tokenData = loadedState.tokenData or {}
end end
collisionEnabled = true collisionEnabled = true
end end
@ -28,7 +31,8 @@ end
function onSave() function onSave()
return JSON.encode({ return JSON.encode({
currentScenario = currentScenario, currentScenario = currentScenario,
useFrontData = useFrontData useFrontData = useFrontData,
tokenData = tokenData
}) })
end end
@ -54,12 +58,13 @@ function onCollisionEnter(collisionInfo)
updateNeeded = true updateNeeded = true
fireScenarioChangedEvent() fireScenarioChangedEvent()
end end
-- trigger update if a change was detected and push new data -- trigger update if a change was detected and push new data
if updateNeeded then if updateNeeded then
local metadata = JSON.decode(object.getGMNotes()) or {} local metadata = JSON.decode(object.getGMNotes()) or {}
if not metadata["tokens"] then return end if not metadata["tokens"] then return end
local tokenData = metadata["tokens"][(useFrontData and "front" or "back")] tokenData = metadata["tokens"][(useFrontData and "front" or "back")]
fireTokenDataChangedEvent(tokenData) fireTokenDataChangedEvent()
end end
end end
@ -78,13 +83,15 @@ function onObjectEnterContainer(container, object)
end end
end end
-- fires if the scenario title changes
function fireScenarioChangedEvent() function fireScenarioChangedEvent()
Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20) Wait.frames(function() Global.call('titleSplash', currentScenario) end, 20)
playAreaApi.onScenarioChanged(currentScenario) playAreaApi.onScenarioChanged(currentScenario)
end end
function fireTokenDataChangedEvent(tokenData) -- fires if the scenario title or the difficulty changes
tokenArrangerApi.onTokenDataChanged(tokenData) function fireTokenDataChangedEvent()
tokenArrangerApi.onTokenDataChanged(tokenData, currentScenario, tostring(useFrontData))
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,