annotate modules/ReAction_PetAction/ReAction_PetAction.lua @ 74:00e28094e1a3

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