annotate classes/StanceButton.lua @ 175:df68b5a40490

Remove ReAction global in favor of built in addon table (drycoded)
author Flick <flickerstreak@gmail.com>
date Wed, 20 Oct 2010 17:11:50 +0000
parents 8cc187143acd
children bf64e71701e2
rev   line source
flickerstreak@175 1 local addonName, addonTable = ...
flickerstreak@175 2 local ReAction = addonTable.ReAction
flickerstreak@134 3 local L = ReAction.L
flickerstreak@134 4 local _G = _G
flickerstreak@134 5 local CreateFrame = CreateFrame
flickerstreak@134 6 local format = string.format
flickerstreak@134 7 local GetCVar = GetCVar
flickerstreak@134 8 local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor
flickerstreak@134 9 local CooldownFrame_SetTimer = CooldownFrame_SetTimer
flickerstreak@134 10 local InCombatLockdown = InCombatLockdown
flickerstreak@134 11 local GetNumShapeshiftForms = GetNumShapeshiftForms
flickerstreak@134 12 local GetShapeshiftFormInfo = GetShapeshiftFormInfo
flickerstreak@134 13 local IsUsableSpell = IsUsableSpell
flickerstreak@134 14 local GetSpellInfo = GetSpellInfo
flickerstreak@134 15
flickerstreak@134 16 --
flickerstreak@134 17 -- private
flickerstreak@134 18 --
flickerstreak@134 19 local playerClass = select(2,UnitClass("player"))
flickerstreak@134 20
flickerstreak@134 21 local aspects = {
flickerstreak@134 22 -- All rank one, so that the usable check works
flickerstreak@134 23 GetSpellInfo(13163), -- monkey
flickerstreak@134 24 GetSpellInfo(13165), -- hawk
flickerstreak@134 25 GetSpellInfo(5118), -- cheetah
flickerstreak@134 26 GetSpellInfo(34074), -- viper
flickerstreak@134 27 GetSpellInfo(13161), -- beast
flickerstreak@134 28 GetSpellInfo(13159), -- pack
flickerstreak@134 29 GetSpellInfo(20043), -- wild
flickerstreak@134 30 GetSpellInfo(61846), -- dragonhawk
flickerstreak@134 31 }
flickerstreak@134 32
flickerstreak@134 33 local aspectLinks = { }
flickerstreak@134 34
flickerstreak@134 35 local eventList = {
flickerstreak@134 36 "PLAYER_REGEN_ENABLED",
flickerstreak@134 37 "PLAYER_ENTERING_WORLD",
flickerstreak@134 38 "UPDATE_SHAPESHIFT_FORM",
flickerstreak@134 39 "UPDATE_SHAPESHIFT_FORMS",
flickerstreak@134 40 "UPDATE_SHAPESHIFT_USABLE",
flickerstreak@134 41 "UPDATE_SHAPESHIFT_COOLDOWN",
flickerstreak@137 42 "UPDATE_BINDINGS",
flickerstreak@134 43 }
flickerstreak@134 44
flickerstreak@134 45 local eventListHunter = {
flickerstreak@134 46 "PLAYER_REGEN_ENABLED",
flickerstreak@134 47 "PLAYER_ENTERING_WORLD",
flickerstreak@134 48 "SPELL_UPDATE_COOLDOWN",
flickerstreak@134 49 "SPELL_UPDATE_USABLE",
flickerstreak@134 50 "UNIT_AURA",
flickerstreak@137 51 "SPELLS_CHANGED",
flickerstreak@137 52 "UPDATE_BINDINGS",
flickerstreak@134 53 }
flickerstreak@134 54
flickerstreak@134 55 if playerClass == "HUNTER" then
flickerstreak@134 56 eventList = eventListHunter
flickerstreak@134 57 end
flickerstreak@134 58
flickerstreak@134 59 --
flickerstreak@134 60 -- Stance Button class
flickerstreak@134 61 --
flickerstreak@134 62 local Super = ReAction.Button
flickerstreak@134 63 local Stance = setmetatable( { }, { __index = Super } )
flickerstreak@134 64 ReAction.Button.Stance = Stance
flickerstreak@134 65
flickerstreak@134 66 function Stance:New( idx, moduleConfig, bar, idHint )
flickerstreak@134 67 local name = format("ReAction_%s_Stance_%d",bar:GetName(),idx)
flickerstreak@134 68
flickerstreak@134 69 self = Super.New(self, name, moduleConfig.buttons[bar:GetName()][idx], bar, idx, "SecureActionButtonTemplate, ActionButtonTemplate" )
flickerstreak@134 70 self.moduleConfig = moduleConfig
flickerstreak@134 71 self.hunterIdx = 1
flickerstreak@134 72
flickerstreak@134 73 local f = self:GetFrame()
flickerstreak@134 74 local barFrame = bar:GetFrame()
flickerstreak@134 75 local config = self:GetConfig()
flickerstreak@134 76
flickerstreak@134 77 -- set up the base stance ID
flickerstreak@134 78 self:SetActionIDPool("stance",8)
flickerstreak@134 79 config.stanceID = self:AcquireActionID(config.stanceID, idHint, true)
flickerstreak@134 80
flickerstreak@134 81 -- attribute setup
flickerstreak@134 82 f:SetAttribute("type","spell")
flickerstreak@134 83
flickerstreak@134 84 -- non secure scripts
flickerstreak@134 85 f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end)
flickerstreak@134 86 f:SetScript("OnEnter", function(frame) self:OnEnter() end)
flickerstreak@134 87 f:SetScript("OnLeave", function(frame) self:OnLeave() end)
flickerstreak@134 88 f:SetScript("PreClick", function(frame, ...) self:PreClick(...) end)
flickerstreak@134 89
flickerstreak@134 90 -- secure handlers
flickerstreak@134 91 -- (none)
flickerstreak@134 92
flickerstreak@134 93 -- event registration
flickerstreak@134 94 f:EnableMouse(true)
flickerstreak@134 95 f:RegisterForClicks("AnyUp")
flickerstreak@134 96 for _, evt in pairs(eventList) do
flickerstreak@134 97 f:RegisterEvent(evt)
flickerstreak@134 98 end
flickerstreak@134 99
flickerstreak@134 100 -- attach to skinner
flickerstreak@134 101 bar:SkinButton(self)
flickerstreak@134 102
flickerstreak@134 103 -- initial display
flickerstreak@134 104 if ReAction:GetConfigMode() then
flickerstreak@134 105 self:GetFrame():Show()
flickerstreak@134 106 end
flickerstreak@134 107
flickerstreak@134 108 self:Refresh()
flickerstreak@134 109
flickerstreak@134 110 return self
flickerstreak@134 111 end
flickerstreak@134 112
flickerstreak@134 113 function Stance:GetModuleConfig()
flickerstreak@134 114 -- this is the Stance module config structure,
flickerstreak@134 115 -- not the config structure of the bar itself
flickerstreak@134 116 return self.moduleConfig
flickerstreak@134 117 end
flickerstreak@134 118
flickerstreak@134 119 function Stance:GetActionID()
flickerstreak@134 120 return self.config.stanceID
flickerstreak@134 121 end
flickerstreak@134 122
flickerstreak@134 123 function Stance:UpdateAction()
flickerstreak@134 124 if InCombatLockdown() then
flickerstreak@134 125 self.updatePending = true
flickerstreak@134 126 else
flickerstreak@134 127 self.updatePending = false
flickerstreak@134 128 local idx = self:GetActionID()
flickerstreak@134 129 local f = self:GetFrame()
flickerstreak@134 130 local c = self:GetModuleConfig()
flickerstreak@134 131 if playerClass == "HUNTER" then
flickerstreak@134 132 if c.showHunterAspects then
flickerstreak@134 133 -- re-map the index in the case of "hide monkey/hawk"
flickerstreak@134 134 if c.hideMonkeyHawk then
flickerstreak@134 135 local usable, outOfMana = IsUsableSpell(aspects[8])
flickerstreak@134 136 if usable or outOfMana then
flickerstreak@134 137 idx = idx + 2
flickerstreak@134 138 if idx > 8 then
flickerstreak@134 139 f:Hide()
flickerstreak@134 140 return
flickerstreak@134 141 end
flickerstreak@134 142 end
flickerstreak@134 143 end
flickerstreak@134 144 self.hunterIdx = idx
flickerstreak@134 145 -- cache the highest rank spellID
flickerstreak@134 146 if not aspectLinks[aspects[idx]] then
flickerstreak@134 147 aspectLinks[aspects[idx]] = GetSpellLink(aspects[idx],"")
flickerstreak@134 148 end
flickerstreak@134 149 local usable, outOfMana = IsUsableSpell(aspects[idx])
flickerstreak@134 150 if usable or outOfMana then
flickerstreak@134 151 f:SetAttribute("spell",aspects[idx])
flickerstreak@134 152 f:Show()
flickerstreak@134 153 self:Update()
flickerstreak@134 154 else
flickerstreak@134 155 -- haven't learned this spell yet
flickerstreak@134 156 f:Hide()
flickerstreak@134 157 end
flickerstreak@134 158 else
flickerstreak@134 159 f:Hide()
flickerstreak@134 160 end
flickerstreak@134 161 elseif idx > GetNumShapeshiftForms() or
flickerstreak@134 162 playerClass == "PALADIN" and c.hidePaladinAuras or
flickerstreak@134 163 playerClass == "DEATHKNIGHT" and c.hideDKPresences then
flickerstreak@134 164 f:Hide()
flickerstreak@134 165 else
flickerstreak@134 166 f:SetAttribute("spell", select(2,GetShapeshiftFormInfo(idx)))
flickerstreak@134 167 f:Show()
flickerstreak@134 168 self:Update()
flickerstreak@134 169 end
flickerstreak@134 170 end
flickerstreak@134 171 end
flickerstreak@134 172
flickerstreak@134 173 function Stance:Refresh()
flickerstreak@134 174 Super.Refresh(self)
flickerstreak@137 175 self:UpdateHotkey()
flickerstreak@136 176 self:UpdateAction()
flickerstreak@134 177 end
flickerstreak@134 178
flickerstreak@134 179 function Stance:Update()
flickerstreak@134 180 local texture, isActive, isCastable
flickerstreak@134 181 if playerClass == "HUNTER" then
flickerstreak@134 182 local name, rank
flickerstreak@134 183 local spell = aspects[self.hunterIdx]
flickerstreak@134 184 name, rank, texture = GetSpellInfo(spell)
flickerstreak@134 185 isCastable = IsUsableSpell(spell)
flickerstreak@134 186 for i = 1, 40 do
flickerstreak@134 187 local buff = UnitBuff("player",i,true)
flickerstreak@134 188 if not buff then break end
flickerstreak@134 189 if buff == spell then
flickerstreak@134 190 isActive = true
flickerstreak@134 191 texture = "Interface\\Icons\\Spell_Nature_WispSplode"
flickerstreak@134 192 break
flickerstreak@134 193 end
flickerstreak@134 194 end
flickerstreak@134 195 else
flickerstreak@134 196 local _
flickerstreak@134 197 texture, _, isActive, isCastable = GetShapeshiftFormInfo(self:GetActionID())
flickerstreak@134 198 end
flickerstreak@134 199
flickerstreak@134 200 local icon = self.frames.icon
flickerstreak@134 201 icon:SetTexture(texture)
flickerstreak@134 202 self:GetFrame():SetChecked( isActive and 1 or 0 )
flickerstreak@134 203 if isCastable then
flickerstreak@137 204 self.frames.hotkey:Show()
flickerstreak@134 205 icon:SetVertexColor(1.0, 1.0, 1.0)
flickerstreak@134 206 else
flickerstreak@134 207 icon:SetVertexColor(0.4, 0.4, 0.4)
flickerstreak@134 208 end
flickerstreak@134 209
flickerstreak@134 210 self:UpdateCooldown()
flickerstreak@134 211 end
flickerstreak@134 212
flickerstreak@134 213 function Stance:UpdateCooldown()
flickerstreak@134 214 local start, duration, enabled
flickerstreak@134 215 if playerClass == "HUNTER" then
flickerstreak@134 216 local spell = aspects[self.hunterIdx]
flickerstreak@134 217 if spell then
flickerstreak@134 218 start, duration, enabled = GetSpellCooldown(spell)
flickerstreak@134 219 end
flickerstreak@134 220 else
flickerstreak@134 221 start, duration, enabled = GetShapeshiftFormCooldown(self:GetActionID())
flickerstreak@134 222 end
flickerstreak@134 223 if start then
flickerstreak@134 224 CooldownFrame_SetTimer(self.frames.cooldown, start, duration, enabled)
flickerstreak@134 225 end
flickerstreak@134 226 end
flickerstreak@134 227
flickerstreak@134 228 function Stance:SetTooltip()
flickerstreak@134 229 if GetCVar("UberTooltips") == "1" then
flickerstreak@134 230 GameTooltip_SetDefaultAnchor(GameTooltip, self:GetFrame())
flickerstreak@134 231 else
flickerstreak@134 232 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_RIGHT")
flickerstreak@134 233 end
flickerstreak@134 234 if playerClass == "HUNTER" then
flickerstreak@134 235 local aspect = aspects[self.hunterIdx]
flickerstreak@134 236 if aspect and aspectLinks[aspect] then
flickerstreak@134 237 GameTooltip:SetHyperlink(aspectLinks[aspect])
flickerstreak@134 238 end
flickerstreak@134 239 else
flickerstreak@134 240 GameTooltip:SetShapeshift(self:GetActionID())
flickerstreak@134 241 end
flickerstreak@134 242 end
flickerstreak@134 243
flickerstreak@134 244 function Stance:OnEnter()
flickerstreak@134 245 self:SetTooltip()
flickerstreak@134 246 end
flickerstreak@134 247
flickerstreak@134 248 function Stance:OnLeave()
flickerstreak@134 249 GameTooltip:Hide()
flickerstreak@134 250 end
flickerstreak@134 251
flickerstreak@134 252 function Stance:PreClick()
flickerstreak@134 253 local f = self:GetFrame()
flickerstreak@134 254 f:SetChecked( not f:GetChecked() )
flickerstreak@134 255 end
flickerstreak@134 256
flickerstreak@134 257 function Stance:OnEvent(event, arg)
flickerstreak@134 258 if event == "PLAYER_REGEN_ENABLED" then
flickerstreak@134 259 if self.updatePending then
flickerstreak@134 260 self:UpdateAction()
flickerstreak@134 261 end
flickerstreak@134 262 elseif event == "UPDATE_SHAPESHIFT_COOLDOWN" then
flickerstreak@134 263 self:UpdateCooldown()
flickerstreak@134 264 elseif event == "UPDATE_SHAPESHIFT_FORMS" or
flickerstreak@134 265 event == "SPELLS_CHANGED" then
flickerstreak@134 266 aspectLinks = { } -- force repopulate of the spellIDs
flickerstreak@134 267 self:UpdateAction()
flickerstreak@134 268 elseif event == "UNIT_AURA" then
flickerstreak@134 269 if arg == "player" then
flickerstreak@134 270 self:Update()
flickerstreak@134 271 end
flickerstreak@137 272 elseif event == "UPDATE_BINDINGS" then
flickerstreak@137 273 self:UpdateHotkey()
flickerstreak@134 274 else
flickerstreak@134 275 self:Update()
flickerstreak@134 276 end
flickerstreak@134 277 end
flickerstreak@134 278
flickerstreak@134 279 function Stance:ShowGridTemp(show)
flickerstreak@134 280 if show then
flickerstreak@134 281 self:GetFrame():Show()
flickerstreak@134 282 else
flickerstreak@134 283 self:UpdateAction()
flickerstreak@134 284 end
flickerstreak@134 285 end