diff --git a/objects/TokenSource.124381/ResourceCounter.498ec0.json b/objects/TokenSource.124381/ResourceCounter.498ec0.json index beb03661..c54a1d52 100644 --- a/objects/TokenSource.124381/ResourceCounter.498ec0.json +++ b/objects/TokenSource.124381/ResourceCounter.498ec0.json @@ -33,8 +33,8 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, + "LuaScript": "require(\"core/GenericCounter\")", "LuaScriptState": "0", - "LuaScript_path": "TokenSource.124381/ResourceCounter.498ec0.ttslua", "MeasureMovement": false, "Name": "Custom_Token", "Nickname": "Resource Counter", diff --git a/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua b/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua deleted file mode 100644 index 75c9182d..00000000 --- a/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua +++ /dev/null @@ -1,43 +0,0 @@ -value = 0 - -function onSave() return JSON.encode(value) end - -function onLoad(savedData) - if savedData ~= "" then - value = JSON.decode(savedData) - end - - self.createButton({ - label = tostring(value), - click_function = "addOrSubtract", - function_owner = self, - position = { 0, 0.06, 0.1 }, - height = 600, - width = 1000, - scale = { 1.5, 1.5, 1.5 }, - font_size = 600, - font_color = { 1, 1, 1, 100 }, - color = { 0, 0, 0, 0 } - }) - self.addContextMenuItem("Value from description", function(color) setToDescription(color) end) -end - -function setToDescription(color) - local newValue = tonumber(self.getDescription()) - if type(newValue) == "number" and newValue <= 99 and newValue >= 0 then - updateVal(newValue) - else - printToColor("Description does not contain a valid one or two digit number!", color, "Red") - end -end - -function addOrSubtract(_, _, isRightClick) - local mod = isRightClick and -1 or 1 - newValue = math.min(math.max(value + mod, 0), 99) - updateVal(newValue) -end - -function updateVal(newValue) - value = newValue - self.editButton({ index = 0, label = tostring(newValue) }) -end diff --git a/src/core/GenericCounter.ttslua b/src/core/GenericCounter.ttslua index c3f9cce8..3400d123 100644 --- a/src/core/GenericCounter.ttslua +++ b/src/core/GenericCounter.ttslua @@ -4,47 +4,57 @@ val = 0 function onSave() return JSON.encode(val) end -function onLoad(saved_data) - if saved_data ~= nil then - val = JSON.decode(saved_data) - end +function onLoad(savedData) + if savedData ~= nil then + val = JSON.decode(savedData) + end - local name = self.getName() - local position = {} + local name = self.getName() + local position = {} - if name == "Damage" or name == "Resources" then - position = { 0, 0.06, 0.1 } - elseif name == "Horror" then - position = { -0.025, 0.06, -0.025 } - else - position = { 0, 0.06, 0 } - end + if name == "Damage" or name == "Resources" or name == "Resource Counter" then + position = { 0, 0.06, 0.1 } + elseif name == "Horror" then + position = { -0.025, 0.06, -0.025 } + else + position = { 0, 0.06, 0 } + end - self.createButton({ - label = tostring(val), - click_function = "addOrSubtract", - function_owner = self, - position = position, - height = 600, - width = 1000, - scale = { 1.5, 1.5, 1.5 }, - font_size = 600, - font_color = { 1, 1, 1, 100 }, - color = { 0, 0, 0, 0 } - }) + self.createButton({ + label = tostring(val), + click_function = "addOrSubtract", + function_owner = self, + position = position, + height = 600, + width = 1000, + scale = { 1.5, 1.5, 1.5 }, + font_size = 600, + font_color = { 1, 1, 1, 100 }, + color = { 0, 0, 0, 0 } + }) + + if name == "Resource Counter" then + self.addContextMenuItem("Set to description", function(color) setToDescription(color) end) + end +end + +function setToDescription(color) + local newVal = tonumber(self.getDescription()) + if type(newVal) == "number" and newVal <= 99 and newVal >= 0 then + updateVal(newVal) + else + printToColor("Description does not contain a valid number!", color, "Orange") + end end function updateVal(newVal) - if tonumber(newVal) then - val = newVal - self.editButton({ - index = 0, - label = tostring(val) - }) - end + if tonumber(newVal) then + val = newVal + self.editButton({ index = 0, label = tostring(val) }) + end end -function addOrSubtract(_, _, alt_click) - val = math.min(math.max(val + (alt_click and -1 or 1), MIN_VALUE), MAX_VALUE) - self.editButton({ index = 0, label = tostring(val) }) +function addOrSubtract(_, _, isRightClick) + val = math.min(math.max(val + (isRightClick and -1 or 1), MIN_VALUE), MAX_VALUE) + self.editButton({ index = 0, label = tostring(val) }) end diff --git a/src/playermat/Playmat.ttslua b/src/playermat/Playmat.ttslua index cb6be6fb..e5ef0b9a 100644 --- a/src/playermat/Playmat.ttslua +++ b/src/playermat/Playmat.ttslua @@ -409,15 +409,17 @@ function replenishTokens(card, count, replenish) -- get current amount of resource tokens on the card local search = searchArea(cardPos, { 2.5, 0.5, 3.5 }) + local clickableResourceCounter = nil local foundTokens = 0 + for _, obj in ipairs(search) do local obj = obj.hit_object if obj.getCustomObject().image == "http://cloud-3.steamusercontent.com/ugc/1758068501357192910/11DDDC7EF621320962FDCF3AE3211D5EDC3D1573/" then foundTokens = foundTokens + math.abs(obj.getQuantity()) obj.destruct() elseif obj.getName() == "Resource Counter" then - foundTokens = obj.getVar("value") - obj.destruct() + foundTokens = obj.getVar("val") + clickableResourceCounter = obj break end end @@ -438,7 +440,12 @@ function replenishTokens(card, count, replenish) local newCount = foundTokens + replenish if newCount > count then newCount = count end - tokenManager.spawnTokenGroup(card, "resource", newCount) + + if clickableResourceCounter then + clickableResourceCounter.call("updateVal", newCount) + else + tokenManager.spawnTokenGroup(card, "resource", newCount) + end end function syncCustomizableMetadata(card)