From 38b342557605aaf259bb8efaa1da595e9a8a2f0a Mon Sep 17 00:00:00 2001 From: Vollmer Date: Mon, 2 Sep 2024 17:18:31 +0200 Subject: [PATCH] implement functions for applying/removing click casting --- UnitFrames/UnitButton.lua | 66 +++++++++++++++++++++++++++++++++++++++ UnitFrames/Units.lua | 3 -- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/UnitFrames/UnitButton.lua b/UnitFrames/UnitButton.lua index be65f55..a9b086e 100644 --- a/UnitFrames/UnitButton.lua +++ b/UnitFrames/UnitButton.lua @@ -393,3 +393,69 @@ local function UpdateAppearance(kind) end end CUF:RegisterCallback("UpdateAppearance", "UpdateAppearance", UpdateAppearance) + +------------------------------------------------- +-- MARK: Update Click Casting +------------------------------------------------- + +local previousClickCastings + +local function GetMouseWheelBindKey(fullKey, noTypePrefix) + local modifier, key = strmatch(fullKey, "^(.*)type%-(.+)$") + modifier = string.gsub(modifier, "-", "") + + if noTypePrefix then + return modifier .. key + else + return "type-" .. modifier .. key -- type-ctrlSCROLLUP + end +end + +---@param button CUFUnitButton +local function ClearClickCastings(button) + if not previousClickCastings then return end + button:SetAttribute("cell", nil) + button:SetAttribute("menu", nil) + for _, t in pairs(previousClickCastings) do + local bindKey = t[1] + if strfind(bindKey, "SCROLL") then + bindKey = GetMouseWheelBindKey(t[1]) + end + + button:SetAttribute(bindKey, nil) + local attr = string.gsub(bindKey, "type", "spell") + button:SetAttribute(attr, nil) + attr = string.gsub(bindKey, "type", "macro") + button:SetAttribute(attr, nil) + attr = string.gsub(bindKey, "type", "macrotext") + button:SetAttribute(attr, nil) + attr = string.gsub(bindKey, "type", "item") + button:SetAttribute(attr, nil) + end +end + +---@param noReload boolean? +---@param onlyqueued boolean? +---@param which string? +function U.UpdateClickCasting(noReload, onlyqueued, which) + Util:IterateAllUnitButtons(function(button, unit) + if not which or which == unit then + if CUF.DB.CurrentLayoutTable()[unit].clickCast then + F:UpdateClickCastings(noReload, onlyqueued) + local snippet = F:GetBindingSnippet() + CUF:DevAdd(snippet, "snippet") + F:UpdateClickCastOnFrame(button, snippet) + else + ClearClickCastings(button) + + button:SetAttribute('*type1', 'target') -- makes left click target the unit + button:SetAttribute('*type2', 'togglemenu') -- makes right click toggle a unit menu + end + end + end) + + previousClickCastings = F:Copy(Cell.vars.clickCastings["useCommon"] and Cell.vars.clickCastings["common"] or + Cell.vars.clickCastings[Cell.vars.playerSpecID]) +end + +CUF:RegisterCallback("UpdateClickCasting", "UpdateClickCasting", U.UpdateClickCasting) diff --git a/UnitFrames/Units.lua b/UnitFrames/Units.lua index 90aaf7a..5fc6074 100644 --- a/UnitFrames/Units.lua +++ b/UnitFrames/Units.lua @@ -87,8 +87,5 @@ function U:InitUnitButtons() local button = CreateUnitButton(unit, unitFrame) RegisterUnitButtonCallbacks(unit, button, unitFrame, anchorFrame, hoverFrame, config) - - button:SetAttribute('*type1', 'target') -- makes left click target the unit - button:SetAttribute('*type2', 'togglemenu') -- makes right click toggle a unit menu end end