annotate classes/StanceButton.lua @ 134:d186e041ca14

Added stance bar support
author Flick <flickerstreak@gmail.com>
date Tue, 17 Mar 2009 23:38:38 +0000
parents
children b8a14165b807
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@134 43 -- "UPDATE_INVENTORY_ALERTS" -- WTF?
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@134 52 "SPELLS_CHANGED"
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 self:UpdateAction()
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@134 176 self:Update()
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@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@134 271 else
flickerstreak@134 272 self:Update()
flickerstreak@134 273 end
flickerstreak@134 274 end
flickerstreak@134 275
flickerstreak@134 276 function Stance:ShowGridTemp(show)
flickerstreak@134 277 if show then
flickerstreak@134 278 self:GetFrame():Show()
flickerstreak@134 279 else
flickerstreak@134 280 self:UpdateAction()
flickerstreak@134 281 end
flickerstreak@134 282 end