From e3a1a0ce2da84c7b9b30c3da3228a9c245322853 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Mon, 2 Jan 2023 02:23:34 +0100 Subject: [PATCH 1/4] initial commit --- objects/TokenSource.124381.json | 3 +- .../ResourceCounter.498ec0.json | 57 +++++++++++++++++++ .../ResourceCounter.498ec0.ttslua | 43 ++++++++++++++ src/core/Global.ttslua | 7 ++- src/core/token/TokenManager.ttslua | 12 ++++ src/playermat/Playmat.ttslua | 4 ++ xml/OptionPanel.xml | 14 +++++ 7 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 objects/TokenSource.124381/ResourceCounter.498ec0.json create mode 100644 objects/TokenSource.124381/ResourceCounter.498ec0.ttslua diff --git a/objects/TokenSource.124381.json b/objects/TokenSource.124381.json index 9b468f66..503bb56f 100644 --- a/objects/TokenSource.124381.json +++ b/objects/TokenSource.124381.json @@ -18,7 +18,8 @@ "Damage.cd2a02", "Horror.36be72", "ClueDoom.a3fb6c", - "Resource.00d19a" + "Resource.00d19a", + "ResourceCounter.498ec0" ], "ContainedObjects_path": "TokenSource.124381", "Description": "", diff --git a/objects/TokenSource.124381/ResourceCounter.498ec0.json b/objects/TokenSource.124381/ResourceCounter.498ec0.json new file mode 100644 index 00000000..beb03661 --- /dev/null +++ b/objects/TokenSource.124381/ResourceCounter.498ec0.json @@ -0,0 +1,57 @@ +{ + "AltLookAngle": { + "x": 0, + "y": 0, + "z": 0 + }, + "Autoraise": true, + "ColorDiffuse": { + "b": 1, + "g": 1, + "r": 1 + }, + "CustomImage": { + "CustomToken": { + "MergeDistancePixels": 5, + "Stackable": false, + "StandUp": false, + "Thickness": 0.1 + }, + "ImageScalar": 1, + "ImageSecondaryURL": "", + "ImageURL": "http://cloud-3.steamusercontent.com/ugc/949599153663401115/EAA6D40FC6E15204BBE551BCDED35CC8C75111BF/", + "WidthScale": 0 + }, + "Description": "0", + "DragSelectable": true, + "GMNotes": "resourceCounter", + "GUID": "498ec0", + "Grid": true, + "GridProjection": false, + "Hands": false, + "HideWhenFaceDown": false, + "IgnoreFoW": false, + "LayoutGroupSortIndex": 0, + "Locked": false, + "LuaScriptState": "0", + "LuaScript_path": "TokenSource.124381/ResourceCounter.498ec0.ttslua", + "MeasureMovement": false, + "Name": "Custom_Token", + "Nickname": "Resource Counter", + "Snap": false, + "Sticky": true, + "Tooltip": false, + "Transform": { + "posX": 0, + "posY": 3, + "posZ": 0, + "rotX": 0, + "rotY": 270, + "rotZ": 0, + "scaleX": 0.26, + "scaleY": 1, + "scaleZ": 0.26 + }, + "Value": 0, + "XmlUI": "" +} \ No newline at end of file diff --git a/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua b/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua new file mode 100644 index 00000000..75c9182d --- /dev/null +++ b/objects/TokenSource.124381/ResourceCounter.498ec0.ttslua @@ -0,0 +1,43 @@ +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/Global.ttslua b/src/core/Global.ttslua index c6e3321c..27bcdbd3 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -1,5 +1,3 @@ -local tokenManager = require("core/token/TokenManager") - --------------------------------------------------------- -- general setup --------------------------------------------------------- @@ -36,6 +34,7 @@ local IS_RESHUFFLING = false local bagSearchers = {} local hideTitleSplashWaitFunctionId = nil local playmatAPI = require("playermat/PlaymatApi") +local tokenManager = require("core/token/TokenManager") --------------------------------------------------------- -- data for tokens @@ -819,6 +818,10 @@ function applyOptionPanelChange(id, state) -- update master clue counter getObjectFromGUID("4a3aa4").setVar("useClickableCounters", state) + -- option: Clickable resource counters + elseif id == "useResourceCounters" then + optionPanel[id] = state + -- option: Show Title on placing scenarios elseif id == "showTitleSplash" then optionPanel[id] = state diff --git a/src/core/token/TokenManager.ttslua b/src/core/token/TokenManager.ttslua index e5c62cb4..84d2daa3 100644 --- a/src/core/token/TokenManager.ttslua +++ b/src/core/token/TokenManager.ttslua @@ -149,8 +149,12 @@ do -- spawned state object rather than spawning multiple tokens ---@param shiftDown An offset for the z-value of this group of tokens TokenManager.spawnTokenGroup = function(card, tokenType, tokenCount, shiftDown) + local optionPanel = Global.getTable("optionPanel") + if tokenType == "damage" or tokenType == "horror" then TokenManager.spawnCounterToken(card, tokenType, tokenCount, shiftDown) + elseif tokenType == "resource" and optionPanel["useResourceCounters"] then + TokenManager.spawnResourceCounterToken(card, tokenCount) else TokenManager.spawnMultipleTokens(card, tokenType, tokenCount, shiftDown) end @@ -184,6 +188,14 @@ do end) end + TokenManager.spawnResourceCounterToken = function(card, tokenCount) + local pos = card.getPosition() + Vector(0, 0.2, 0) + local rot = card.getRotation() + TokenManager.spawnToken(pos, "resourceCounter", rot, function(spawned) + spawned.call("updateVal", tokenCount) + end) + end + -- Spawns a number of tokens. ---@param tokenType String type of token to spawn, valid values are resource", "doom", or "clue". -- Other types should use spawnCounterToken() diff --git a/src/playermat/Playmat.ttslua b/src/playermat/Playmat.ttslua index d98e3a31..cb6be6fb 100644 --- a/src/playermat/Playmat.ttslua +++ b/src/playermat/Playmat.ttslua @@ -415,6 +415,10 @@ function replenishTokens(card, count, replenish) 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() + break end end diff --git a/xml/OptionPanel.xml b/xml/OptionPanel.xml index c63c85d9..c3085e23 100644 --- a/xml/OptionPanel.xml +++ b/xml/OptionPanel.xml @@ -142,6 +142,20 @@ + + + + + Use clickable resource counters + This enables spawning of clickable resource tokens for player cards. + + + + + + + From 19b69a3cee41cbe7991a62476aee0b1ae68b9482 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Mon, 2 Jan 2023 02:27:45 +0100 Subject: [PATCH 2/4] add upwards shift --- src/core/token/TokenManager.ttslua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/token/TokenManager.ttslua b/src/core/token/TokenManager.ttslua index 84d2daa3..ef7dd013 100644 --- a/src/core/token/TokenManager.ttslua +++ b/src/core/token/TokenManager.ttslua @@ -189,7 +189,7 @@ do end TokenManager.spawnResourceCounterToken = function(card, tokenCount) - local pos = card.getPosition() + Vector(0, 0.2, 0) + local pos = card.positionToWorld(card.positionToLocal(card.getPosition()) + Vector(0, 0.2, -0.5)) local rot = card.getRotation() TokenManager.spawnToken(pos, "resourceCounter", rot, function(spawned) spawned.call("updateVal", tokenCount) From efbf9e885faeeaffc4d292841fc5428abdeb3464 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Tue, 3 Jan 2023 23:47:34 +0100 Subject: [PATCH 3/4] resolving comments --- .../ResourceCounter.498ec0.json | 2 +- .../ResourceCounter.498ec0.ttslua | 43 ---------- src/core/GenericCounter.ttslua | 80 +++++++++++-------- src/playermat/Playmat.ttslua | 13 ++- 4 files changed, 56 insertions(+), 82 deletions(-) delete mode 100644 objects/TokenSource.124381/ResourceCounter.498ec0.ttslua 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) From e1106d2d3d5ab04b8718a24eff55abef89c26fa5 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Wed, 4 Jan 2023 00:36:55 +0100 Subject: [PATCH 4/4] resolving comments --- src/core/GenericCounter.ttslua | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/core/GenericCounter.ttslua b/src/core/GenericCounter.ttslua index 3400d123..d21ff1c7 100644 --- a/src/core/GenericCounter.ttslua +++ b/src/core/GenericCounter.ttslua @@ -1,5 +1,5 @@ -MIN_VALUE = -99 -MAX_VALUE = 999 +MIN_VALUE = 0 +MAX_VALUE = 99 val = 0 function onSave() return JSON.encode(val) end @@ -33,23 +33,15 @@ function onLoad(savedData) 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 + self.addContextMenuItem("Add 5", function() updateVal(val + 5) end) + self.addContextMenuItem("Subtract 5", function() updateVal(val - 5) end) + self.addContextMenuItem("Add 10", function() updateVal(val + 10) end) + self.addContextMenuItem("Subtract 10", function() updateVal(val - 10) end) end function updateVal(newVal) if tonumber(newVal) then - val = newVal + val = math.min(math.max(newVal, MIN_VALUE), MAX_VALUE) self.editButton({ index = 0, label = tostring(val) }) end end