Merge pull request #243 from Entrox-Licher/token-arranger-percentages

Token Arranger Percentages
This commit is contained in:
Chr1Z 2023-03-27 23:07:37 +02:00 committed by GitHub
commit 9a858811c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@ inputParameters.tab = 2
local latestLoad = "XXX"
local updating = false
local percentage = false
local tokenPrecedence = {}
local TOKEN_NAMES = {
"Elder Sign",
@ -38,7 +39,8 @@ local TOKEN_NAMES = {
function onSave()
return JSON.encode({
tokenPrecedence = tokenPrecedence,
latestLoad = latestLoad
latestLoad = latestLoad,
percentage = percentage
})
end
@ -47,41 +49,12 @@ function onLoad(saveState)
local loadedData = JSON.decode(saveState)
tokenPrecedence = loadedData.tokenPrecedence
latestLoad = loadedData.latestLoad or "XXX"
percentage = loadedData.percentage
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 }
else
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end
buttonParameters.click_function = attachIndex("tokenClick", i)
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
self.createButton(buttonParameters)
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)
createDefaultButtonsAndInputs(true)
-- reset context menu
self.addContextMenuItem("Load default values", function()
@ -89,7 +62,24 @@ function onLoad(saveState)
updateUI()
layout()
end)
self.addContextMenuItem("Toggle Percentages", function()
if percentage then
percentage = false
else
percentage = "basic"
end
layout()
end)
self.addContextMenuItem("Toggle Cumulative", function()
if percentage == "basic" or not percentage then
percentage = "cumulative"
broadcastToAll("Cumulative percentages are unreliable when using tokens that draw other tokens (bless or curse for example)", Color.Yellow)
else
percentage = "basic"
end
layout()
end)
layout()
-- grab token metadata from mythos area
Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.5)
end
@ -157,6 +147,56 @@ function loadDefaultValues()
}
end
-- creates all starting buttons and inputs
function createDefaultButtonsAndInputs(loadInputs)
loadInputs = loadInputs or false
buttonParameters.function_owner = self
buttonParameters.label = ""
buttonParameters.tooltip = "Increase / Decrease"
buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 325
buttonParameters.height = 325
-- 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 }
if loadInputs then
inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset }
end
else
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
if loadInputs then
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end
end
buttonParameters.click_function = attachIndex("tokenClick", i)
self.createButton(buttonParameters)
if loadInputs then
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
self.createInput(inputParameters)
end
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)
end
-- update input fields
function updateUI()
for i = 1, 10 do
@ -188,11 +228,54 @@ function findChaosBag()
end
end
-- deletes previously placed tokens
-- deletes previously placed tokens AND percentage buttons
function deleteCopiedTokens()
self.clearButtons()
createDefaultButtonsAndInputs()
for _, token in ipairs(getObjectsWithTag("tempToken")) do token.destruct() end
end
-- creates percentage representation buttons
function createPercentageButton(token_count, value_count, data, token_name, cumulative_percentage)
local offset = -2.675
local label_string = string.format("%s", string.format("%05.2f", math.floor((token_count / #data) * 10000) / 100) .. "%")
local buttonScale = {2, 2, 2}
local textColor = {1, 1, 1}
if percentage == "cumulative" then
buttonScale = {1.5, 1.5, 1.5}
offset = -2.85
end
if cumulative_percentage then
offset = -2.45
textColor = {1, 1, 1}
label_string = string.format("%s", string.format("%05.2f", cumulative_percentage) .. "%")
if cumulative_percentage == 100 then
label_string = string.format("%s", string.format("%05.1f", cumulative_percentage) .. "%")
end
else
if token_name == "Elder Sign" then
textColor = {0.35, 0.71, 0.85}
elseif token_name == "Auto-fail" then
textColor = {0.86, 0.1, 0.1}
elseif token_name then
textColor = {0.68, 0.53, 0.86}
else
textColor = {0.85, 0.67, 0.33}
end
end
self.createButton({
label = label_string,
click_function = "print",
width = 0,
height = 0,
scale = buttonScale,
font_color = textColor,
position = (Vector(2.2, -0.05, offset) + Vector(0.1, 0, 0.875 * value_count))
})
end
-- main function (delete old tokens, clone chaos bag content, sort it and position it)
function layout(_, _, isRightClick)
if updating then return end
@ -242,18 +325,47 @@ function layout(_, _, isRightClick)
-- laying out the tokens
local pos = self.getPosition() + Vector(3.55, -0.05, -3.95)
if percentage then
pos.z = self.getPosition().z - 7
end
local location = { x = pos.x, y = pos.y, z = pos.z }
local current_value = data[1].value
local token_count = 0
local value_count = 1
local cumulative_percentage = 0
local current_token = false
for _, item in ipairs(data) do
if item.value ~= current_value then
if percentage then
cumulative_percentage = cumulative_percentage + math.floor((token_count / #data) * 10000) / 100
createPercentageButton(token_count, value_count, data, current_token)
if percentage == "cumulative" then
createPercentageButton(token_count, value_count, data, current_token, cumulative_percentage)
end
end
location.x = location.x - 1.75
location.z = pos.z
current_value = item.value
value_count = value_count + 1
token_count = 0
current_token = false
end
if string.match(item.token.getName(), "%a") ~= nil then
current_token = item.token.getName()
end
item.token.setPosition(location)
item.token.setRotation(self.getRotation())
location.z = location.z - 1.75
token_count = token_count + 1
end
if percentage then
cumulative_percentage = cumulative_percentage + math.floor((token_count / #data) * 10000) / 100
createPercentageButton(token_count, value_count, data, current_token)
if percentage == "cumulative" then
createPercentageButton(token_count, value_count, data, current_token, cumulative_percentage)
end
end
Wait.time(function() updating = false end, 0.1)
end