Quantcast

Add an option to trigger 'combat' changed based on party members

James Whitehead II [10-12-10 - 10:24]
Add an option to trigger 'combat' changed based on party members

This option, when enabled, will do a bit more work trying to check for
when your party or raid members enter combat. When this happen, your
click set will be changed and your 'ooc' clicks will be removed, even
though you aren't strictly in combat. This feature is very experimental,
and should be enabled with care.
Filename
Clique.lua
OptionsPanel.lua
Utils.lua
diff --git a/Clique.lua b/Clique.lua
index 4c3a5e4..8e3dee2 100755
--- a/Clique.lua
+++ b/Clique.lua
@@ -111,11 +111,13 @@ function addon:Initialize()
     self:ChangeProfile()

     -- Register for combat events to ensure we can swap between the two states
-    self:RegisterEvent("PLAYER_REGEN_DISABLED", "UpdateAttributes")
-    self:RegisterEvent("PLAYER_REGEN_ENABLED", "UpdateAttributes")
+    self:RegisterEvent("PLAYER_REGEN_DISABLED", "EnteringCombat")
+    self:RegisterEvent("PLAYER_REGEN_ENABLED", "LeavingCombat")
     self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED", function()
         self:ChangeProfile()
     end)
+    -- Handle combat watching so we can change ooc based on party combat status
+    addon:UpdateCombatWatch()
 end

 function addon:RegisterFrame(button)
@@ -218,6 +220,20 @@ function REMATTR(prefix, attr, suffix, value)
     return fmt:format(prefix, #prefix > 0 and "-" or "", attr, tonumber(suffix) and "" or "-", suffix)
 end

+-- A sort function that determines in what order bindings should be applied.
+-- This function should be treated with care, it can drastically change behavior
+
+local function ApplicationOrder(a, b)
+    local acnt, bcnt = 0, 0
+    for k,v in pairs(a.sets) do acnt = acnt + 1 end
+    for k,v in pairs(b.sets) do bcnt = bcnt + 1 end
+    if a.sets.default and not b.sets.default then
+        return true
+    elseif a.sets.default and b.sets.default then
+        return acnt < bcnt
+    end
+end
+
 -- This function will create an attribute that when run for a given frame
 -- will set the correct set of SAB attributes.
 function addon:GetClickAttributes(global)
@@ -231,6 +247,8 @@ function addon:GetClickAttributes(global)
         "local button = setupbutton or self",
     }

+    table.sort(self.bindings, ApplicationOrder)
+
     for idx, entry in ipairs(self.bindings) do
         if self:ShouldSetBinding(entry, global) then
             local prefix, suffix = addon:GetBindingPrefixSuffix(entry)
@@ -476,6 +494,47 @@ function addon:ChangeProfile(profileName)
     CliqueConfig:UpdateList()
 end

+function addon:UpdateCombatWatch()
+    if self.settings.fastooc then
+        self:RegisterEvent("UNIT_FLAGS", "CheckPartyCombat")
+    else
+        self:UnregisterEvent("UNIT_FLAGS")
+    end
+end
+
+function addon:EnteringCombat()
+    addon:UpdateAttributes()
+    addon:UpdateGlobalAttributes()
+end
+
+function addon:LeavingCombat()
+    self.partyincombat = false
+    addon:UpdateAttributes()
+    addon:UpdateGlobalAttributes()
+end
+
+function addon:CheckPartyCombat(event, unit)
+    if InCombatLockdown() or not unit then return end
+    if self.settings.fastooc then
+        if UnitInParty(unit) or UnitInRaid(unit) then
+            if UnitAffectingCombat(unit) == 1 then
+                -- Trigger pre-combat switch for fastooc
+                self.partyincombat = true
+                self.combattrigger = UnitGUID(unit)
+                addon:UpdateAttributes()
+                addon:UpdateGlobalAttributes()
+            elseif self.partyincombat then
+                -- The unit is out of combat, so try to clear our flag
+                if self.combattrigger == UnitGUID(unit) then
+                    self.partyincombat = false
+                    addon:UpdateAttributes()
+                    addon:UpdateGlobalAttributes()
+                end
+            end
+        end
+    end
+end
+
 SLASH_CLIQUE1 = "/clique"
 SlashCmdList["CLIQUE"] = function(msg, editbox)
     ShowUIPanel(CliqueConfig)
diff --git a/OptionsPanel.lua b/OptionsPanel.lua
index f596a6a..f63a34d 100644
--- a/OptionsPanel.lua
+++ b/OptionsPanel.lua
@@ -42,6 +42,9 @@ function panel:CreateOptions()
     self.updown = make_checkbox("CliqueOptionsUpDownClick", self)
     self.updown.text:SetText(L["Trigger bindings on the 'down' portion of the click (requires reload)"])

+    self.fastooc = make_checkbox("CliqueOptionsFastOoc", self)
+    self.fastooc.text:SetText(L["Disable out of combat clicks when party members enter combat"])
+
     self.specswap = make_checkbox("CliqueOptionsSpecSwap", self)
     self.specswap.text:SetText(L["Swap profiles based on talent spec"])
     self.specswap.EnableDisable = function()
@@ -73,8 +76,8 @@ function panel:CreateOptions()
     UIDropDownMenu_SetWidth(self.profiledd, 150)

     -- Collect and anchor the bits together
-    table.insert(bits, self.intro)
     table.insert(bits, self.updown)
+    table.insert(bits, self.fastooc)
     table.insert(bits, self.specswap)
     table.insert(bits, self.prispeclabel)
     table.insert(bits, self.prispec)
@@ -348,6 +351,7 @@ function panel.refresh()
     UIDropDownMenu_SetText(panel.profiledd, settings.profileKey)

     panel.updown:SetChecked(settings.downclick)
+    panel.fastooc:SetChecked(settings.fastooc)
     panel.specswap:SetChecked(settings.specswap)
     panel.specswap.EnableDisable()
 end
@@ -357,11 +361,13 @@ function panel.okay()

     -- Update the saved variables
     settings.downclick = not not panel.updown:GetChecked()
+    settings.fastooc = not not panel.fastooc:GetChecked()
     settings.specswap = not not panel.specswap:GetChecked()
     settings.pri_profileKey = UIDropDownMenu_GetSelectedValue(panel.prispec)
     settings.sec_profileKey = UIDropDownMenu_GetSelectedValue(panel.secspec)
     settings.profileKey = UIDropDownMenu_GetSelectedValue(panel.profiledd)

+    addon:UpdateCombatWatch()
     addon:ChangeProfile()
 end

diff --git a/Utils.lua b/Utils.lua
index e4f8a7c..39f8fc8 100644
--- a/Utils.lua
+++ b/Utils.lua
@@ -240,7 +240,7 @@ function addon:ShouldSetBinding(binding, global)
     end

     if binding.sets.ooc then
-        if UnitAffectingCombat("player") then
+        if UnitAffectingCombat("player") or addon.partyincombat then
             apply = false
         else
             apply = true