SCED/src/accessories/TokenArranger.ttslua

283 lines
7.8 KiB
Plaintext
Raw Normal View History

2022-11-11 02:59:55 -05:00
-- common parameters
2022-11-13 06:11:21 -05:00
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.label = ""
buttonParameters.tooltip = "Add / Remove"
buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 325
buttonParameters.height = 325
2023-03-01 08:42:10 -05:00
local inputParameters = {}
inputParameters.function_owner = self
inputParameters.font_size = 100
inputParameters.width = 250
inputParameters.height = inputParameters.font_size + 23
inputParameters.alignment = 3
inputParameters.validation = 2
inputParameters.tab = 2
local latestLoad = "XXX"
local updating = false
local tokenPrecedence = {}
local TOKEN_NAMES = {
"Elder Sign",
"Skull",
"Cultist",
"Tablet",
"Elder Thing",
"Auto-fail",
"Bless",
"Curse",
"Frost",
""
}
2022-11-13 06:11:21 -05:00
2023-03-01 08:42:10 -05:00
-- saving the precedence settings and information on the most recently loaded data
function onSave()
return JSON.encode({
tokenPrecedence = tokenPrecedence,
latestLoad = latestLoad
})
end
2022-11-11 02:59:55 -05:00
2023-02-27 19:24:00 -05:00
function onLoad(saveState)
2023-03-01 08:42:10 -05:00
if saveState ~= nil and saveState ~= "" then
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 }
2023-02-28 06:35:35 -05:00
else
2023-03-01 08:42:10 -05:00
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
2022-11-11 02:59:55 -05:00
end
2023-03-01 08:42:10 -05:00
buttonParameters.click_function = attachIndex("tokenClick", i)
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
2022-11-11 02:59:55 -05:00
2022-11-13 06:11:21 -05:00
self.createButton(buttonParameters)
2023-03-01 08:42:10 -05:00
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)
-- 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)
2022-11-13 06:11:21 -05:00
2023-03-01 08:42:10 -05:00
Wait.time(layout, 2)
2022-11-11 02:59:55 -05:00
end
2023-02-28 06:35:35 -05:00
-- delete temporary tokens when destroyed
function onDestroy() deleteCopiedTokens() end
2023-02-27 19:24:00 -05:00
-- layout tokens when dropped (after 2 seconds)
function onDrop() Wait.time(layout, 2) end
2023-02-28 06:35:35 -05:00
-- delete temporary tokens when picked up
2023-02-27 19:24:00 -05:00
function onPickUp() deleteCopiedTokens() end
2022-12-19 08:39:32 -05:00
2022-11-11 02:59:55 -05:00
-- helper functions to carry index
function attachIndex(click_function, index)
2023-03-01 08:42:10 -05:00
local fn_name = click_function .. index
_G[fn_name] = function(_, _, isRightClick)
_G[click_function](isRightClick, index)
end
return fn_name
2022-11-11 02:59:55 -05:00
end
function attachIndex2(input_function, index)
2023-03-01 08:42:10 -05:00
local fn_name = input_function .. index
_G[fn_name] = function(_, _, input, selected)
_G[input_function](input, selected, index)
end
return fn_name
2022-11-11 02:59:55 -05:00
end
-- click_function for buttons on chaos tokens
2023-02-28 06:35:35 -05:00
function tokenClick(isRightClick, index)
2023-03-01 08:42:10 -05:00
local change = tonumber(isRightClick and "-1" or "1")
tokenPrecedence[TOKEN_NAMES[index]][1] = tokenPrecedence[TOKEN_NAMES[index]][1] + change
self.editInput({ index = index - 1, value = tokenPrecedence[TOKEN_NAMES[index]][1] })
layout()
2022-11-11 02:59:55 -05:00
end
-- input_function for input_boxes
2023-02-28 06:35:35 -05:00
function tokenInput(input, selected, index)
2023-03-01 08:42:10 -05:00
if selected == false then
local num = tonumber(input)
if num ~= nil then
tokenPrecedence[TOKEN_NAMES[index]][1] = num
2022-11-11 02:59:55 -05:00
end
2023-03-01 08:42:10 -05:00
layout()
end
2022-11-11 02:59:55 -05:00
end
2023-02-28 06:35:35 -05:00
-- loads the default precedence table
function loadDefaultValues()
2023-03-01 08:42:10 -05:00
-- token modifiers for sorting (and order for same modifier)
-- order starts at 2 because there is a "+1" token
tokenPrecedence = {
["Elder Sign"] = { 100, 2 },
["Skull"] = { -1, 3 },
["Cultist"] = { -2, 4 },
["Tablet"] = { -3, 5 },
["Elder Thing"] = { -4, 6 },
["Auto-fail"] = { -100, 7 },
["Bless"] = { 101, 8 },
["Curse"] = { -101, 9 },
["Frost"] = { -99, 10 },
[""] = { 0, 11 }
}
2023-02-28 06:35:35 -05:00
end
-- update input fields
function updateUI()
2023-03-01 08:42:10 -05:00
for i = 1, 10 do
self.editInput({ index = i - 1, value = tokenPrecedence[TOKEN_NAMES[i]][1] })
end
2023-02-28 06:35:35 -05:00
end
2022-11-11 02:59:55 -05:00
-- order function for data sorting
function token_value_comparator(left, right)
2023-03-01 08:42:10 -05:00
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
2022-11-11 02:59:55 -05:00
end
2023-02-28 06:35:35 -05:00
-- checks scripting zone for chaos bag
function findChaosBag()
2023-03-01 08:42:10 -05:00
for _, item in ipairs(getObjectFromGUID("83ef06").getObjects()) do
if item.getDescription() == "Chaos Bag" then
return item
2022-11-11 02:59:55 -05:00
end
2023-03-01 08:42:10 -05:00
end
2022-11-11 02:59:55 -05:00
end
2022-12-19 08:39:32 -05:00
-- deletes previously placed tokens
function deleteCopiedTokens()
2023-03-01 08:42:10 -05:00
for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end
2022-12-19 08:39:32 -05:00
end
2022-11-11 02:59:55 -05:00
-- main function (delete old tokens, clone chaos bag content, sort it and position it)
function layout(_, _, isRightClick)
2023-03-01 08:42:10 -05:00
if updating then return end
updating = true
deleteCopiedTokens()
-- stop here if right-clicked
if isRightClick then
updating = false
return
end
local chaosBag = findChaosBag()
local data = {}
-- clone tokens from chaos bag (default position above trash can)
for i, obj in ipairs(chaosBag.getData().ContainedObjects) do
obj["Tags"] = { "tempToken" }
local spawnedObj = spawnObjectData({
data = obj,
position = { 0.49, 3, 0 }
})
local value = tonumber(obj["Nickname"])
local precedence = tokenPrecedence[obj["Nickname"]]
data[i] = {
token = spawnedObj,
value = value or precedence[1]
}
if precedence ~= nil then
data[i].order = precedence[2]
else
data[i].order = value
2022-11-11 02:59:55 -05:00
end
2023-03-01 08:42:10 -05:00
end
2022-11-11 02:59:55 -05:00
2023-03-01 08:42:10 -05:00
-- sort table by value (symbols last if same value)
table.sort(data, token_value_comparator)
2022-11-11 02:59:55 -05:00
2023-03-01 08:42:10 -05:00
-- error handling for removal of token arranger
if self == nil then
for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end
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
2022-11-11 02:59:55 -05:00
end
2023-03-01 08:42:10 -05:00
item.token.setPosition(location)
item.token.setRotation(self.getRotation())
location.z = location.z - 1.75
end
Wait.time(function() updating = false end, 0.1)
2022-11-11 02:59:55 -05:00
end
2023-02-27 19:24:00 -05:00
-- called from outside to set default values for tokens
2023-03-01 08:42:10 -05:00
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
tokenPrecedence[key][1] = modifier
end
updateUI()
layout()
2023-02-27 19:24:00 -05:00
end