Merge pull request #365 from argonui/token-arranger

Token Arranger: more resilience to latency
This commit is contained in:
BootleggerFinn 2023-09-25 15:55:04 -05:00 committed by GitHub
commit e16470c65f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
local mythosAreaApi = require("core/MythosAreaApi") local mythosAreaApi = require("core/MythosAreaApi")
local chaosBagApi = require("chaosbag/ChaosBagApi")
-- common parameters -- common parameters
local buttonParameters = {} local buttonParameters = {}
@ -18,6 +19,12 @@ inputParameters.alignment = 3
inputParameters.validation = 2 inputParameters.validation = 2
inputParameters.tab = 2 inputParameters.tab = 2
local percentageLabel = {}
percentageLabel.function_owner = self
percentageLabel.click_function = "none"
percentageLabel.width = 0
percentageLabel.height = 0
-- variables with save function -- variables with save function
local tokenPrecedence = {} local tokenPrecedence = {}
local percentage = false local percentage = false
@ -37,8 +44,6 @@ local TOKEN_NAMES = {
"" ""
} }
local chaosBagApi = require("chaosbag/ChaosBagApi")
-- saving the precedence settings and information on the most recently loaded data -- saving the precedence settings and information on the most recently loaded data
function onSave() function onSave()
return JSON.encode({ return JSON.encode({
@ -60,7 +65,7 @@ function onLoad(saveState)
Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2) Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2)
end end
createButtonsAndInputs(true) createButtonsAndInputs()
-- context menu items -- context menu items
self.addContextMenuItem("Load default values", function() self.addContextMenuItem("Load default values", function()
@ -99,23 +104,6 @@ function onDrop() Wait.time(layout, 1.5) end
-- delete temporary tokens when picked up -- delete temporary tokens when picked up
function onPickUp() deleteCopiedTokens() end function onPickUp() deleteCopiedTokens() end
-- helper functions to carry index
function attachIndex(click_function, index)
local fn_name = click_function .. index
_G[fn_name] = function(_, _, isRightClick)
_G[click_function](isRightClick, index)
end
return fn_name
end
function attachIndex2(input_function, index)
local fn_name = input_function .. index
_G[fn_name] = function(_, _, input, selected)
_G[input_function](input, selected, index)
end
return fn_name
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")
@ -156,8 +144,8 @@ function loadDefaultValues()
} }
end end
-- creates buttons and inputs (if argument is true) -- creates buttons and inputs
function createButtonsAndInputs(loadInputs) function createButtonsAndInputs()
local offset = 0.725 local offset = 0.725
local pos = { x = { -1.067, 0.377 }, z = -2.175 } local pos = { x = { -1.067, 0.377 }, z = -2.175 }
@ -171,16 +159,18 @@ function createButtonsAndInputs(loadInputs)
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset } inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end end
buttonParameters.click_function = attachIndex("tokenClick", i) buttonParameters.click_function = "tokenClick" .. i
self.createButton(buttonParameters) inputParameters.input_function = "tokenInput" .. i
-- only create inputs on initial load
if loadInputs then
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1] inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
-- setting click-/inputfunction
self.setVar(buttonParameters.click_function, function(_, _, isRightClick) tokenClick(isRightClick, i) end)
self.setVar(inputParameters.input_function, function(_, _, input, selected) tokenInput(input, selected, i) end)
-- button/input creation
self.createButton(buttonParameters)
self.createInput(inputParameters) self.createInput(inputParameters)
end end
end
-- index 10: "Update / Hide" button -- index 10: "Update / Hide" button
self.createButton({ self.createButton({
@ -207,7 +197,7 @@ end
-- order function for data sorting -- order function for data sorting
function tokenValueComparator(left, right) function tokenValueComparator(left, right)
if (left.value ~= right.value) then if left.value ~= right.value then
return left.value > right.value return left.value > right.value
elseif left.order ~= right.order then elseif left.order ~= right.order then
return left.order < right.order return left.order < right.order
@ -222,18 +212,17 @@ function deleteCopiedTokens()
token.destruct() token.destruct()
end end
-- this removes the percentage buttons -- this removes the percentage buttons (by index 11+)
self.clearButtons() local buttonCount = #self.getButtons()
createButtonsAndInputs() if buttonCount < 12 then return end
for i = buttonCount, 12, -1 do
self.removeButton(i - 1)
end
end end
-- creates buttons as labels as display for percentage values -- creates buttons as labels as display for percentage values
function createPercentageButton(tokenCount, valueCount, tokenName) function createPercentageButton(tokenCount, valueCount, tokenName)
-- parameters for button creation
local percentageLabel = {}
percentageLabel.click_function = "none"
percentageLabel.width = 0
percentageLabel.height = 0
local startPos = Vector(2.3, -0.05, 0.875 * valueCount) local startPos = Vector(2.3, -0.05, 0.875 * valueCount)
if percentage == "cumulative" then if percentage == "cumulative" then
@ -289,21 +278,22 @@ function layout(_, _, isRightClick)
return return
end end
-- get ChaosBag and stop if not found
local chaosBag = chaosBagApi.findChaosBag() local chaosBag = chaosBagApi.findChaosBag()
local data = {} if not chaosBag then
updating = false
return
end
-- 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 local data = {}
obj["Tags"] = { "tempToken" } for i, objData in ipairs(chaosBag.getData().ContainedObjects) do
local value = tonumber(obj.Nickname) objData["Tags"] = { "tempToken" }
local precedence = tokenPrecedence[obj.Nickname] local value = tonumber(objData.Nickname)
local spawnedObj = spawnObjectData({ local precedence = tokenPrecedence[objData.Nickname]
data = obj,
position = { 0.49, 3, 0 }
})
data[i] = { data[i] = {
token = spawnedObj, token = objData,
value = value or precedence[1] value = value or precedence[1]
} }
@ -323,6 +313,7 @@ function layout(_, _, isRightClick)
if percentage then pos.z = pos.z - 3.05 end if percentage then pos.z = pos.z - 3.05 end
local location = { x = pos.x, y = pos.y, z = pos.z } local location = { x = pos.x, y = pos.y, z = pos.z }
local rotation = self.getRotation()
local currentValue = data[1].value local currentValue = data[1].value
local tokenCount = { row = 0, sum = 0, total = #data } local tokenCount = { row = 0, sum = 0, total = #data }
local valueCount = 1 local valueCount = 1
@ -343,11 +334,13 @@ function layout(_, _, isRightClick)
tokenCount.row = 0 tokenCount.row = 0
end end
item.token.setPosition(location) spawnObjectData({
item.token.setRotation(self.getRotation()) data = item.token,
item.token.setLock(true) position = location,
rotation = rotation
})
tokenName = item.token.Nickname
location.z = location.z - 1.75 location.z = location.z - 1.75
tokenName = item.token.getName()
tokenCount.row = tokenCount.row + 1 tokenCount.row = tokenCount.row + 1
end end