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