annotate classes/StanceButton.lua @ 170:0030201b5fc2

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