code update

This commit is contained in:
Chr1Z93 2023-08-29 15:27:07 +02:00
parent 58e992dd0a
commit 19a1dc3c00

View File

@ -18,6 +18,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
@ -58,7 +64,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()
@ -97,23 +103,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")
@ -154,8 +143,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 }
@ -169,15 +158,17 @@ 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
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
-- only create inputs on initial load -- setting click-/inputfunction
if loadInputs then self.setVar(buttonParameters.click_function, function(_, _, isRightClick) tokenClick(isRightClick, i) end)
inputParameters.input_function = attachIndex2("tokenInput", i) self.setVar(inputParameters.input_function, function(_, _, input, selected) tokenInput(input, selected, i) end)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
self.createInput(inputParameters) -- button/input creation
end self.createButton(buttonParameters)
self.createInput(inputParameters)
end end
-- index 10: "Update / Hide" button -- index 10: "Update / Hide" button
@ -205,7 +196,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
@ -220,18 +211,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
@ -287,10 +277,15 @@ function layout(_, _, isRightClick)
return return
end end
-- get ChaosBag and stop if not found
local chaosBag = Global.call("findChaosBag") local chaosBag = Global.call("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)
local data = {}
for i, objData in ipairs(chaosBag.getData().ContainedObjects) do for i, objData in ipairs(chaosBag.getData().ContainedObjects) do
objData["Tags"] = { "tempToken" } objData["Tags"] = { "tempToken" }
local value = tonumber(objData.Nickname) local value = tonumber(objData.Nickname)
@ -324,7 +319,6 @@ function layout(_, _, isRightClick)
local tokenName = false local tokenName = false
for i, item in ipairs(data) do for i, item in ipairs(data) do
-- this is true for the first token in a new row -- this is true for the first token in a new row
if item.value ~= currentValue then if item.value ~= currentValue then
if percentage then if percentage then
@ -339,14 +333,12 @@ function layout(_, _, isRightClick)
tokenCount.row = 0 tokenCount.row = 0
end end
local spawnedObj = spawnObjectData({ spawnObjectData({
data = item.token, data = item.token,
position = { 0.49, 2 + 0.5*i, 0 } position = location,
rotation = rotation
}) })
spawnedObj.setPosition(location) tokenName = item.token.Nickname
spawnedObj.setRotation(rotation)
spawnedObj.setLock(true)
tokenName = spawnedObj.getName()
location.z = location.z - 1.75 location.z = location.z - 1.75
tokenCount.row = tokenCount.row + 1 tokenCount.row = tokenCount.row + 1
end end