annotate classes/StanceButton.lua @ 137:4c1b85ec40d2

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