SCED/src/playermat/InvestigatorSkillTracker.ttslua

63 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-11-12 06:43:35 -05:00
local BUTTON_PARAMETERS = {}
BUTTON_PARAMETERS.function_owner = self
BUTTON_PARAMETERS.height = 650
BUTTON_PARAMETERS.width = 700
2022-11-16 13:08:19 -05:00
BUTTON_PARAMETERS.position = { x = -4.775, y = 0.1, z = -0.03 }
2022-11-12 06:43:35 -05:00
BUTTON_PARAMETERS.color = { 0, 0, 0, 0 }
BUTTON_PARAMETERS.font_color = { 0, 0, 0, 100 }
BUTTON_PARAMETERS.font_size = 450
function onSave() return JSON.encode(stats) end
-- load stats and make buttons (left to right)
function onLoad(saved_data)
2023-09-26 10:14:37 -04:00
stats = JSON.decode(saved_data) or { 1, 1, 1, 1 }
2022-11-12 06:43:35 -05:00
2023-09-26 10:14:37 -04:00
for i = 1, 4 do
BUTTON_PARAMETERS.label = stats[i] .. " "
BUTTON_PARAMETERS.position.x = BUTTON_PARAMETERS.position.x + 1.91
BUTTON_PARAMETERS.click_function = attachIndex("button_click", i)
self.createButton(BUTTON_PARAMETERS)
end
2022-11-12 06:43:35 -05:00
2023-09-26 10:14:37 -04:00
self.addContextMenuItem("Reset to 1s", function() updateStats({ 1, 1, 1, 1 }) end)
2022-11-12 06:43:35 -05:00
end
-- helper function to carry index
function attachIndex(click_function, index)
2023-09-26 10:14:37 -04:00
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
2022-11-12 06:43:35 -05:00
end
function button_click(_, _, isRightClick, index)
2023-09-26 10:14:37 -04:00
stats[index] = math.min(math.max(stats[index] + (isRightClick and -1 or 1), 0), 99)
changeButton(index)
2022-11-12 06:43:35 -05:00
end
function changeButton(index)
2023-09-26 10:14:37 -04:00
local font_size = BUTTON_PARAMETERS.font_size
local whitespace = " "
2022-11-12 06:43:35 -05:00
2023-09-26 10:14:37 -04:00
if stats[index] > 9 then
font_size = BUTTON_PARAMETERS.font_size * 0.65
whitespace = " "
end
2022-11-12 06:43:35 -05:00
2023-09-26 10:14:37 -04:00
self.editButton({ index = index - 1, label = stats[index] .. whitespace, font_size = font_size })
2022-11-12 06:43:35 -05:00
end
-- formatting of "newStats": {Willpower, Intellect, Fight, Agility}
function updateStats(newStats)
2023-09-26 10:14:37 -04:00
if newStats and #newStats == 4 then
stats = newStats
elseif newStats then
printToAll("Provided new stats are incomplete or incorrectly formatted.", "Red")
return
end
for i = 1, 4 do changeButton(i) end
2022-11-12 06:43:35 -05:00
end