annotate modules/ReAction_PetAction/ReAction_PetAction.lua @ 53:7e09c02ae620

Pet Action support
author Flick <flickerstreak@gmail.com>
date Fri, 25 Apr 2008 20:35:55 +0000
parents 21bcaf8215ff
children 88283658fec4
rev   line source
flickerstreak@28 1 --[[
flickerstreak@53 2 ReAction Pet Action button module
flickerstreak@53 3
flickerstreak@53 4 The button module implements standard action button functionality by wrapping Blizzard's
flickerstreak@53 5 PetActionButton frame and associated functions. It also provides some button layout
flickerstreak@53 6 modification tools.
flickerstreak@28 7
flickerstreak@28 8 --]]
flickerstreak@28 9
flickerstreak@28 10 -- local imports
flickerstreak@28 11 local ReAction = ReAction
flickerstreak@28 12 local L = ReAction.L
flickerstreak@28 13 local _G = _G
flickerstreak@53 14 local CreateFrame = CreateFrame
flickerstreak@28 15
flickerstreak@28 16 -- module declaration
flickerstreak@28 17 local moduleID = "PetAction"
flickerstreak@28 18 local module = ReAction:NewModule( moduleID )
flickerstreak@28 19
flickerstreak@28 20 -- module methods
flickerstreak@28 21 function module:OnInitialize()
flickerstreak@53 22 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@28 23 {
flickerstreak@28 24 profile = {
flickerstreak@28 25 buttons = { }
flickerstreak@28 26 }
flickerstreak@28 27 }
flickerstreak@28 28 )
flickerstreak@53 29 self.buttons = { }
flickerstreak@28 30
flickerstreak@53 31 ReAction:RegisterOptions("global", self, {
flickerstreak@53 32 })
flickerstreak@28 33 end
flickerstreak@28 34
flickerstreak@28 35 function module:OnEnable()
flickerstreak@53 36 ReAction:RegisterBarType(L["Pet Action Bar"],
flickerstreak@53 37 {
flickerstreak@53 38 type = moduleID ,
flickerstreak@53 39 defaultButtonSize = 30,
flickerstreak@53 40 defaultBarRows = 1,
flickerstreak@53 41 defaultBarCols = 10,
flickerstreak@53 42 defaultBarSpacing = 8
flickerstreak@53 43 })
flickerstreak@28 44 end
flickerstreak@28 45
flickerstreak@28 46 function module:OnDisable()
flickerstreak@53 47 ReAction:UnregisterBarType(L["Pet Action Bar"])
flickerstreak@28 48 end
flickerstreak@28 49
flickerstreak@53 50 function module:ApplyToBar(bar)
flickerstreak@53 51 if bar.config.type == moduleID then
flickerstreak@53 52 -- auto show/hide when pet exists
flickerstreak@53 53 bar:GetFrame():SetAttribute("unit","pet")
flickerstreak@53 54 RegisterUnitWatch(bar:GetFrame())
flickerstreak@53 55 self:RefreshBar(bar)
flickerstreak@28 56 end
flickerstreak@28 57 end
flickerstreak@28 58
flickerstreak@53 59 function module:RefreshBar(bar)
flickerstreak@53 60 if bar.config.type == moduleID then
flickerstreak@53 61 if self.buttons[bar] == nil then
flickerstreak@53 62 self.buttons[bar] = { }
flickerstreak@53 63 end
flickerstreak@53 64 local btns = self.buttons[bar]
flickerstreak@53 65 local profile = self.db.profile
flickerstreak@53 66 local barName = bar:GetName()
flickerstreak@53 67 if profile.buttons[barName] == nil then
flickerstreak@53 68 profile.buttons[barName] = {}
flickerstreak@53 69 end
flickerstreak@53 70 local btnCfg = profile.buttons[barName]
flickerstreak@53 71
flickerstreak@53 72 local r, c = bar:GetButtonGrid()
flickerstreak@53 73 local n = r*c
flickerstreak@53 74 for i = 1, n do
flickerstreak@53 75 if btnCfg[i] == nil then
flickerstreak@53 76 btnCfg[i] = {}
flickerstreak@53 77 end
flickerstreak@53 78 if btns[i] == nil then
flickerstreak@53 79 local ok, b = pcall(self.BtnClass.new, self.BtnClass, bar, i, btnCfg[i])
flickerstreak@53 80 if ok and b then
flickerstreak@53 81 btns[i] = b
flickerstreak@53 82 end
flickerstreak@53 83 else
flickerstreak@53 84 btns[i]:Refresh(bar,i)
flickerstreak@53 85 end
flickerstreak@53 86 end
flickerstreak@53 87 for i = n+1, #btns do
flickerstreak@53 88 if btns[i] then
flickerstreak@53 89 btns[i] = btns[i]:Destroy()
flickerstreak@53 90 if btnCfg[i] then
flickerstreak@53 91 btnCfg[i] = nil
flickerstreak@53 92 end
flickerstreak@53 93 end
flickerstreak@53 94 end
flickerstreak@53 95 end
flickerstreak@53 96 end
flickerstreak@53 97
flickerstreak@53 98 function module:RemoveFromBar(bar)
flickerstreak@53 99 if self.buttons[bar] then
flickerstreak@53 100 local btns = self.buttons[bar]
flickerstreak@53 101 for _,b in pairs(btns) do
flickerstreak@53 102 if b then
flickerstreak@53 103 b:Destroy()
flickerstreak@53 104 end
flickerstreak@53 105 end
flickerstreak@53 106 self.buttons[bar] = nil
flickerstreak@53 107 end
flickerstreak@53 108 end
flickerstreak@53 109
flickerstreak@53 110 function module:EraseBarConfig(barName)
flickerstreak@53 111 self.db.profile.buttons[barName] = nil
flickerstreak@53 112 end
flickerstreak@53 113
flickerstreak@53 114 function module:RenameBarConfig(oldname, newname)
flickerstreak@53 115 local b = self.db.profile.buttons
flickerstreak@53 116 b[newname], b[oldname] = b[oldname], nil
flickerstreak@53 117 end
flickerstreak@53 118
flickerstreak@53 119
flickerstreak@53 120 function module:ApplyConfigMode(mode,bars)
flickerstreak@53 121 for _, bar in pairs(bars) do
flickerstreak@53 122 if bar and self.buttons[bar] then
flickerstreak@53 123 for _, b in pairs(self.buttons[bar]) do
flickerstreak@53 124 if b then
flickerstreak@53 125 if mode then
flickerstreak@53 126 self:showActionIDLabel(b)
flickerstreak@53 127 else
flickerstreak@53 128 ReAction:Print("Hiding action id "..b:GetActionID())
flickerstreak@53 129 self:hideActionIDLabel(b)
flickerstreak@53 130 end
flickerstreak@53 131 end
flickerstreak@53 132 end
flickerstreak@53 133 local f = bar:GetFrame()
flickerstreak@53 134 if mode then
flickerstreak@53 135 UnregisterUnitWatch(f)
flickerstreak@53 136 f:Show()
flickerstreak@53 137 else
flickerstreak@53 138 RegisterUnitWatch(f)
flickerstreak@53 139 end
flickerstreak@53 140 end
flickerstreak@53 141 end
flickerstreak@53 142 end
flickerstreak@53 143
flickerstreak@53 144 function module:showActionIDLabel(button)
flickerstreak@53 145 -- store the action ID label in the frame due to frame recycling
flickerstreak@53 146 if not button:GetFrame().actionIDLabel and button:GetActionID() then
flickerstreak@53 147 local label = button:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@53 148 label:SetAllPoints()
flickerstreak@53 149 label:SetJustifyH("CENTER")
flickerstreak@53 150 label:SetShadowColor(0,0,0,1)
flickerstreak@53 151 label:SetShadowOffset(2,-2)
flickerstreak@53 152 label:SetText(tostring(button:GetActionID()))
flickerstreak@53 153 button:GetFrame().actionIDLabel = label
flickerstreak@53 154 end
flickerstreak@53 155 button:GetFrame().actionIDLabel:Show()
flickerstreak@53 156 end
flickerstreak@53 157
flickerstreak@53 158 function module:hideActionIDLabel(button)
flickerstreak@53 159 if button:GetFrame().actionIDLabel then
flickerstreak@53 160 button:GetFrame().actionIDLabel:Hide()
flickerstreak@53 161 else
flickerstreak@53 162 ReAction:Print("actionIDLabel not found")
flickerstreak@53 163 end
flickerstreak@53 164 end
flickerstreak@53 165
flickerstreak@53 166
flickerstreak@28 167
flickerstreak@28 168 -- use-count of action IDs
flickerstreak@53 169 local nActionIDs = NUM_PET_ACTION_SLOTS
flickerstreak@28 170 local ActionIDList = setmetatable( {}, {
flickerstreak@28 171 __index = function(self, idx)
flickerstreak@28 172 if idx == nil then
flickerstreak@53 173 for i = 1, nActionIDs do
flickerstreak@28 174 if rawget(self,i) == nil then
flickerstreak@28 175 rawset(self,i,1)
flickerstreak@28 176 return i
flickerstreak@28 177 end
flickerstreak@28 178 end
flickerstreak@53 179 error("ran out of pet action IDs")
flickerstreak@28 180 else
flickerstreak@28 181 local c = rawget(self,idx) or 0
flickerstreak@28 182 rawset(self,idx,c+1)
flickerstreak@28 183 return idx
flickerstreak@28 184 end
flickerstreak@28 185 end,
flickerstreak@28 186 __newindex = function(self,idx,value)
flickerstreak@28 187 if value == nil then
flickerstreak@28 188 value = rawget(self,idx)
flickerstreak@28 189 if value == 1 then
flickerstreak@28 190 value = nil
flickerstreak@28 191 elseif value then
flickerstreak@28 192 value = value - 1
flickerstreak@28 193 end
flickerstreak@28 194 end
flickerstreak@28 195 rawset(self,idx,value)
flickerstreak@28 196 end
flickerstreak@28 197 })
flickerstreak@28 198
flickerstreak@53 199 local frameRecycler = {}
flickerstreak@28 200
flickerstreak@53 201
flickerstreak@53 202 ------ Button class ------
flickerstreak@28 203 local Button = { }
flickerstreak@28 204
flickerstreak@28 205 local function Constructor( self, bar, idx, config )
flickerstreak@28 206 self.bar, self.idx, self.config = bar, idx, config
flickerstreak@28 207
flickerstreak@28 208 local barFrame = bar:GetFrame()
flickerstreak@28 209
flickerstreak@53 210 local name = config.name or "ReAction_"..bar:GetName().."_Pet_"..idx
flickerstreak@53 211 config.name = name
flickerstreak@53 212 self.name = config.name
flickerstreak@28 213 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured
flickerstreak@28 214
flickerstreak@53 215 -- have to recycle frames with the same name:
flickerstreak@53 216 -- otherwise you either get references to old textures because named CreateFrame()
flickerstreak@53 217 -- doesn't overwrite existing globals (below)
flickerstreak@53 218 -- or, if you set them to nil in the global table, you get taint because of the
flickerstreak@53 219 -- crappy PetActionBar code.
flickerstreak@53 220 local f = frameRecycler[name]
flickerstreak@53 221 if f then
flickerstreak@53 222 f:SetParent(barFrame)
flickerstreak@53 223 f:Show()
flickerstreak@53 224 else
flickerstreak@53 225 f = CreateFrame("CheckButton", name, barFrame, "PetActionButtonTemplate")
flickerstreak@53 226 end
flickerstreak@53 227 if config.actionID then
flickerstreak@53 228 f:SetID(config.actionID) -- PetActionButtonTemplate isn't a proper SecureActionButton
flickerstreak@53 229 end
flickerstreak@53 230 f:SetFrameStrata("MEDIUM")
flickerstreak@53 231
flickerstreak@28 232 barFrame:SetAttribute("addchild",f)
flickerstreak@28 233
flickerstreak@53 234 self.frame = f
flickerstreak@53 235 self.icon = _G[("%sIcon"):format(name)]
flickerstreak@53 236 self.acTex = _G[("%sAutoCastable"):format(name)]
flickerstreak@53 237 self.acModel = _G[("%sAutoCast"):format(name)]
flickerstreak@53 238 self.cooldown = _G[("%sCooldown"):format(name)]
flickerstreak@53 239 self.hotkey = _G[("%sHotKey"):format(name)]
flickerstreak@53 240
flickerstreak@53 241 f:HookScript("OnDragStart", function() self:Update() end)
flickerstreak@53 242 f:HookScript("OnReceiveDrag", function() self:Update() end)
flickerstreak@53 243
flickerstreak@53 244 f:RegisterEvent("PLAYER_CONTROL_LOST");
flickerstreak@53 245 f:RegisterEvent("PLAYER_CONTROL_GAINED");
flickerstreak@53 246 f:RegisterEvent("PLAYER_FARSIGHT_FOCUS_CHANGED");
flickerstreak@53 247 f:RegisterEvent("UNIT_PET");
flickerstreak@53 248 f:RegisterEvent("UNIT_FLAGS");
flickerstreak@53 249 f:RegisterEvent("UNIT_AURA");
flickerstreak@53 250 f:RegisterEvent("PET_BAR_UPDATE");
flickerstreak@53 251 f:RegisterEvent("PET_BAR_UPDATE_COOLDOWN");
flickerstreak@53 252
flickerstreak@53 253 f:SetScript("OnEvent",
flickerstreak@53 254 function(event,arg1)
flickerstreak@53 255 if event =="PET_BAR_UPDATE_COOLDOWN" then
flickerstreak@53 256 self:UpdateCooldown()
flickerstreak@53 257 elseif event == "UPDATE_BINDINGS" then
flickerstreak@53 258 self:UpdateHotkey()
flickerstreak@53 259 else
flickerstreak@53 260 self:Update()
flickerstreak@53 261 end
flickerstreak@53 262 end)
flickerstreak@28 263
flickerstreak@28 264 self:Refresh(bar,idx)
flickerstreak@28 265 end
flickerstreak@28 266
flickerstreak@28 267 function Button:Destroy()
flickerstreak@28 268 local f = self.frame
flickerstreak@28 269 f:UnregisterAllEvents()
flickerstreak@28 270 f:Hide()
flickerstreak@28 271 f:SetParent(UIParent)
flickerstreak@28 272 f:ClearAllPoints()
flickerstreak@28 273 if self.name then
flickerstreak@53 274 frameRecycler[self.name] = f
flickerstreak@28 275 _G[self.name] = nil
flickerstreak@28 276 end
flickerstreak@53 277 if self.config.actionID then
flickerstreak@53 278 ActionIDList[self.config.actionID] = nil
flickerstreak@53 279 end
flickerstreak@28 280 self.frame = nil
flickerstreak@28 281 self.config = nil
flickerstreak@28 282 self.bar = nil
flickerstreak@28 283 end
flickerstreak@28 284
flickerstreak@53 285 function Button:Refresh(bar,idx)
flickerstreak@53 286 bar:PlaceButton(self.frame, idx, 30, 30)
flickerstreak@53 287 self:Update()
flickerstreak@53 288 self:UpdateHotkey()
flickerstreak@53 289 end
flickerstreak@53 290
flickerstreak@53 291 function Button:GetFrame()
flickerstreak@53 292 return self.frame
flickerstreak@53 293 end
flickerstreak@53 294
flickerstreak@53 295 function Button:GetName()
flickerstreak@53 296 return self.name
flickerstreak@53 297 end
flickerstreak@53 298
flickerstreak@53 299 function Button:GetActionID()
flickerstreak@53 300 return self.config.actionID
flickerstreak@53 301 end
flickerstreak@53 302
flickerstreak@53 303 function Button:Update()
flickerstreak@53 304 local id = self.frame:GetID()
flickerstreak@53 305 local name, subtext, texture, isToken, isActive, autoCastAllowed, autoCastEnabled = GetPetActionInfo(id);
flickerstreak@53 306 local f = self.frame
flickerstreak@53 307 --ReAction:Print(("id %d: '%s', '%s', '%s', '%s', '%s', '%s', '%s'"):format(tostring(id), tostring(name),tostring(subtext),tostring(texture),tostring(isToken),tostring(isActive),tostring(autoCastAllowed),tostring(autoCastEnabled)))
flickerstreak@53 308
flickerstreak@53 309 if isToken then
flickerstreak@53 310 self.icon:SetTexture(_G[texture]);
flickerstreak@53 311 f.tooltipName = _G[name];
flickerstreak@53 312 else
flickerstreak@53 313 self.icon:SetTexture(texture);
flickerstreak@53 314 f.tooltipName = name;
flickerstreak@53 315 end
flickerstreak@53 316
flickerstreak@53 317 f.isToken = isToken;
flickerstreak@53 318 f.tooltipSubtext = subtext;
flickerstreak@53 319 f:SetChecked( isActive and 1 or 0);
flickerstreak@53 320
flickerstreak@53 321 if autoCastAllowed then
flickerstreak@53 322 self.acTex:Show();
flickerstreak@53 323 else
flickerstreak@53 324 self.acTex:Hide();
flickerstreak@53 325 end
flickerstreak@53 326
flickerstreak@53 327 if autoCastEnabled then
flickerstreak@53 328 self.acModel:Show();
flickerstreak@53 329 else
flickerstreak@53 330 self.acModel:Hide();
flickerstreak@53 331 end
flickerstreak@53 332
flickerstreak@53 333 if texture then
flickerstreak@53 334 if GetPetActionsUsable() then
flickerstreak@53 335 SetDesaturation(self.icon,nil)
flickerstreak@53 336 else
flickerstreak@53 337 SetDesaturation(self.icon,1)
flickerstreak@53 338 end
flickerstreak@53 339 self.icon:Show();
flickerstreak@53 340 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot2");
flickerstreak@53 341 else
flickerstreak@53 342 self.icon:Hide();
flickerstreak@53 343 f:SetNormalTexture("Interface\\Buttons\\UI-Quickslot");
flickerstreak@53 344 end
flickerstreak@53 345
flickerstreak@53 346 self:UpdateCooldown()
flickerstreak@53 347 end
flickerstreak@53 348
flickerstreak@53 349 function Button:UpdateCooldown()
flickerstreak@53 350 local start, duration, enable = GetPetActionCooldown(self.frame:GetID());
flickerstreak@53 351 CooldownFrame_SetTimer(self.cooldown, start, duration, enable);
flickerstreak@53 352 end
flickerstreak@53 353
flickerstreak@53 354 function Button:UpdateHotkey()
flickerstreak@53 355
flickerstreak@53 356 end
flickerstreak@28 357
flickerstreak@28 358 -- export as a class-factory to module
flickerstreak@28 359 module.BtnClass = {
flickerstreak@28 360 new = function(self, ...)
flickerstreak@28 361 local x = { }
flickerstreak@28 362 for k,v in pairs(Button) do
flickerstreak@28 363 x[k] = v
flickerstreak@28 364 end
flickerstreak@28 365 Constructor(x, ...)
flickerstreak@28 366 return x
flickerstreak@28 367 end
flickerstreak@28 368 }