token arranger code update

This commit is contained in:
Chr1Z93 2023-08-11 12:02:54 +02:00
parent 1cff0cb5be
commit 5b8fa8c6f8

View File

@ -73,8 +73,7 @@ function onLoad(saveState)
percentage = false percentage = false
else else
percentage = "basic" percentage = "basic"
broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow")
"Yellow")
end end
layout() layout()
end) end)
@ -85,8 +84,7 @@ function onLoad(saveState)
else else
percentage = "cumulative" percentage = "cumulative"
end end
broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow")
"Yellow")
layout() layout()
end) end)
end end
@ -228,51 +226,54 @@ function deleteCopiedTokens()
createButtonsAndInputs() createButtonsAndInputs()
end end
-- creates percentage representation buttons -- creates buttons as labels as display for percentage values
function createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage) function createPercentageButton(tokenCount, valueCount, tokenName)
local buttonScale, offset, textColor, labelString -- parameters for button creation
local percentageLabel = {}
percentageLabel.click_function = "none"
percentageLabel.width = 0
percentageLabel.height = 0
percentageLabel.position = Vector(2.3, -0.05, 0.875 * valueCount)
if percentage == "cumulative" then if percentage == "cumulative" then
buttonScale = {1.5, 1.5, 1.5} percentageLabel.scale = {1.5, 1.5, 1.5}
offset = -2.85 percentageLabel.position.z = percentageLabel.position.z - 2.85
else else
buttonScale = {2, 2, 2} percentageLabel.scale = {2, 2, 2}
offset = -2.675 percentageLabel.position.z = percentageLabel.position.z - 2.675
end end
-- if this is a cumulative button (bottom one of the two created buttons) -- determine font_color
if cumulativePercentage then if tokenName == "Elder Sign" then
offset = -2.45 percentageLabel.font_color = {0.35, 0.71, 0.85}
textColor = {1, 1, 1} elseif tokenName == "Auto-fail" then
percentageLabel.font_color = {0.86, 0.1, 0.1}
-- check if the tokenName contains letters (e.g. symbol token)
elseif string.match(tokenName, "%a") ~= nil then
percentageLabel.font_color = {0.68, 0.53, 0.86}
else
percentageLabel.font_color = {0.85, 0.67, 0.33}
end
-- create label for base percentage
local basePercentage = math.floor((tokenCount.row / tokenCount.total) * 10000) / 100
percentageLabel.label = string.format("%s", string.format("%05.2f", basePercentage) .. "%")
self.createButton(percentageLabel)
-- optionally create label for cumulative percentage
if percentage == "cumulative" then
percentageLabel.position.z = percentageLabel.position.z - 2.45
percentageLabel.font_color = {1, 1, 1}
-- only display one digit for 100% -- only display one digit for 100%
if cumulativePercentage == 100 then if tokenCount.sum == tokenCount.total then
labelString = string.format("%s", string.format("%05.1f", cumulativePercentage) .. "%") percentageLabel.label = "100.0%"
else else
labelString = string.format("%s", string.format("%05.2f", cumulativePercentage) .. "%") local cumulativePercentage = math.floor((tokenCount.sum / tokenCount.total) * 10000) / 100
end percentageLabel.label = string.format("%s", string.format("%05.2f", cumulativePercentage) .. "%")
else
labelString = string.format("%s", string.format("%05.2f", basePercentage) .. "%")
if tokenName == "Elder Sign" then
textColor = {0.35, 0.71, 0.85}
elseif tokenName == "Auto-fail" then
textColor = {0.86, 0.1, 0.1}
elseif tokenName then
textColor = {0.68, 0.53, 0.86}
else
textColor = {0.85, 0.67, 0.33}
end end
self.createButton(percentageLabel)
end end
self.createButton({
label = labelString,
click_function = "none",
width = 0,
height = 0,
scale = buttonScale,
font_color = textColor,
position = Vector(2.3, -0.05, offset + 0.875 * valueCount)
})
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)
@ -293,14 +294,13 @@ function layout(_, _, isRightClick)
-- 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 value = tonumber(obj.Nickname)
local precedence = tokenPrecedence[obj.Nickname]
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 precedence = tokenPrecedence[obj.Nickname]
data[i] = { data[i] = {
token = spawnedObj, token = spawnedObj,
value = value or precedence[1] value = value or precedence[1]
@ -323,50 +323,37 @@ function layout(_, _, isRightClick)
local location = { x = pos.x, y = pos.y, z = pos.z } local location = { x = pos.x, y = pos.y, z = pos.z }
local currentValue = data[1].value local currentValue = data[1].value
local tokenCount = 0 local tokenCount = { row = 0, sum = 0, total = #data }
local valueCount = 1 local valueCount = 1
local cumulativePercentage = 0
local tokenName = false local tokenName = false
for _, item in ipairs(data) do for i, item in ipairs(data) do
-- 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
local basePercentage = math.floor((tokenCount / #data) * 10000) / 100 tokenCount.sum = tokenCount.sum + tokenCount.row
createPercentageButton(basePercentage, valueCount, tokenName) createPercentageButton(tokenCount, valueCount, tokenName)
if percentage == "cumulative" then
cumulativePercentage = cumulativePercentage + basePercentage
createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage)
end
end end
location.x = location.x - 1.75 location.x = location.x - 1.75
location.z = pos.z location.z = pos.z
currentValue = item.value currentValue = item.value
valueCount = valueCount + 1 valueCount = valueCount + 1
tokenCount = 0 tokenCount.row = 0
end end
item.token.setPosition(location) item.token.setPosition(location)
item.token.setRotation(self.getRotation()) item.token.setRotation(self.getRotation())
item.token.setLock(true)
location.z = location.z - 1.75 location.z = location.z - 1.75
tokenCount = tokenCount + 1
tokenName = item.token.getName() tokenName = item.token.getName()
tokenCount.row = tokenCount.row + 1
-- set tokenName to false if it does not contain letters
-- tokenName is used by `createPercentageButton()` to determine the textcolor for percentages
if string.match(tokenName, "%a") == nil then
tokenName = false
end
end end
-- this is repeated to create the button for the last token -- this is repeated to create the button for the last token
if percentage then if percentage then
local basePercentage = math.floor((tokenCount / #data) * 10000) / 100 tokenCount.sum = tokenCount.sum + tokenCount.row
createPercentageButton(basePercentage, valueCount, tokenName) createPercentageButton(tokenCount, valueCount, tokenName)
if percentage == "cumulative" then
cumulativePercentage = cumulativePercentage + basePercentage
createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage)
end
end end
-- introducing a small delay to limit update calls -- introducing a small delay to limit update calls