Merge pull request #541 from argonui/skill-tracker

Investigator Skill Tracker: code improvement + bugfix
This commit is contained in:
Entrox-Licher 2024-01-10 20:56:42 -05:00 committed by GitHub
commit 101565c8f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,62 +1,56 @@
local BUTTON_PARAMETERS = {} local buttonParameters = {}
BUTTON_PARAMETERS.function_owner = self buttonParameters.function_owner = self
BUTTON_PARAMETERS.height = 650 buttonParameters.height = 650
BUTTON_PARAMETERS.width = 700 buttonParameters.width = 700
BUTTON_PARAMETERS.position = { x = -4.775, y = 0.1, z = -0.03 } buttonParameters.position = { x = -4.775, y = 0.1, z = -0.03 }
BUTTON_PARAMETERS.color = { 0, 0, 0, 0 } buttonParameters.color = { 0, 0, 0, 0 }
BUTTON_PARAMETERS.font_color = { 0, 0, 0, 100 } buttonParameters.font_color = { 0, 0, 0, 100 }
BUTTON_PARAMETERS.font_size = 450 buttonParameters.font_size = 450
function onSave() return JSON.encode(stats) end function onSave() return JSON.encode(stats) end
-- load stats and make buttons (left to right) -- load stats and make buttons (left to right)
function onLoad(saved_data) function onLoad(savedData)
stats = JSON.decode(saved_data) or { 1, 1, 1, 1 } stats = JSON.decode(savedData) or { 1, 1, 1, 1 }
for i = 1, 4 do for index = 1, 4 do
BUTTON_PARAMETERS.label = stats[i] .. " " local fnName = "buttonClick" .. index
BUTTON_PARAMETERS.position.x = BUTTON_PARAMETERS.position.x + 1.91 _G[fnName] = function(_, _, isRightClick) buttonClick(isRightClick, index) end
BUTTON_PARAMETERS.click_function = attachIndex("button_click", i) buttonParameters.click_function = fnName
self.createButton(BUTTON_PARAMETERS) buttonParameters.position.x = buttonParameters.position.x + 1.91
self.createButton(buttonParameters)
updateButtonLabel(index)
end end
self.addContextMenuItem("Reset to 1s", function() updateStats({ 1, 1, 1, 1 }) end) self.addContextMenuItem("Reset to 1s", function() updateStats({ 1, 1, 1, 1 }) end)
end end
-- helper function to carry index function buttonClick(isRightClick, index)
function attachIndex(click_function, index)
local fn_name = click_function .. index
_G[fn_name] = function(obj, player_color, isRightClick)
_G[click_function](obj, player_color, isRightClick, index)
end
return fn_name
end
function button_click(_, _, isRightClick, index)
stats[index] = math.min(math.max(stats[index] + (isRightClick and -1 or 1), 0), 99) stats[index] = math.min(math.max(stats[index] + (isRightClick and -1 or 1), 0), 99)
changeButton(index) updateButtonLabel(index)
end end
function changeButton(index) -- sync the button label to the internal value
local font_size = BUTTON_PARAMETERS.font_size function updateButtonLabel(index)
local fontSize = buttonParameters.font_size
local whitespace = " " local whitespace = " "
if stats[index] > 9 then if stats[index] > 9 then
font_size = BUTTON_PARAMETERS.font_size * 0.65 fontSize = buttonParameters.font_size * 0.65
whitespace = " " whitespace = " "
end end
self.editButton({ index = index - 1, label = stats[index] .. whitespace, font_size = font_size }) self.editButton({ index = index - 1, label = stats[index] .. whitespace, font_size = fontSize })
end end
-- formatting of "newStats": {Willpower, Intellect, Fight, Agility} -- update the stats to the provided values
---@param newStats Table Contains the new values for the stats: {Willpower, Intellect, Fight, Agility}
function updateStats(newStats) function updateStats(newStats)
if newStats and #newStats == 4 then if newStats and #newStats == 4 then
stats = newStats stats = newStats
for i = 1, 4 do updateButtonLabel(i) end
elseif newStats then elseif newStats then
printToAll("Provided new stats are incomplete or incorrectly formatted.", "Red") printToAll("Provided new stats are incomplete or incorrectly formatted.", "Red")
return
end end
for i = 1, 4 do changeButton(i) end
end end