annotate modules/ReAction_PetAction/ReAction_PetAction.lua @ 59:7430a8dd4e90

Added modular per-bar options
author Flick <flickerstreak@gmail.com>
date Mon, 28 Apr 2008 23:34:17 +0000
parents 88283658fec4
children 44649a10378d
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@59 33
flickerstreak@59 34 ReAction:RegisterOptions("bar", self, {
flickerstreak@59 35 type = "group",
flickerstreak@59 36 name = L["Pet Buttons"],
flickerstreak@59 37 hidden = "BarOptionsHidden",
flickerstreak@59 38 args = {
flickerstreak@59 39 }
flickerstreak@59 40 })
flickerstreak@28 41 end
flickerstreak@28 42
flickerstreak@28 43 function module:OnEnable()
flickerstreak@53 44 ReAction:RegisterBarType(L["Pet Action Bar"],
flickerstreak@53 45 {
flickerstreak@53 46 type = moduleID ,
flickerstreak@53 47 defaultButtonSize = 30,
flickerstreak@53 48 defaultBarRows = 1,
flickerstreak@53 49 defaultBarCols = 10,
flickerstreak@53 50 defaultBarSpacing = 8
flickerstreak@53 51 })
flickerstreak@28 52 end
flickerstreak@28 53
flickerstreak@28 54 function module:OnDisable()
flickerstreak@53 55 ReAction:UnregisterBarType(L["Pet Action Bar"])
flickerstreak@28 56 end
flickerstreak@28 57
flickerstreak@53 58 function module:ApplyToBar(bar)
flickerstreak@53 59 if bar.config.type == moduleID then
flickerstreak@53 60 -- auto show/hide when pet exists
flickerstreak@53 61 bar:GetFrame():SetAttribute("unit","pet")
flickerstreak@53 62 RegisterUnitWatch(bar:GetFrame())
flickerstreak@53 63 self:RefreshBar(bar)
flickerstreak@28 64 end
flickerstreak@28 65 end
flickerstreak@28 66
flickerstreak@53 67 function module:RefreshBar(bar)
flickerstreak@53 68 if bar.config.type == moduleID then
flickerstreak@53 69 if self.buttons[bar] == nil then
flickerstreak@53 70 self.buttons[bar] = { }
flickerstreak@53 71 end
flickerstreak@53 72 local btns = self.buttons[bar]
flickerstreak@53 73 local profile = self.db.profile
flickerstreak@53 74 local barName = bar:GetName()
flickerstreak@53 75 if profile.buttons[barName] == nil then
flickerstreak@53 76 profile.buttons[barName] = {}
flickerstreak@53 77 end
flickerstreak@53 78 local btnCfg = profile.buttons[barName]
flickerstreak@53 79
flickerstreak@53 80 local r, c = bar:GetButtonGrid()
flickerstreak@53 81 local n = r*c
flickerstreak@53 82 for i = 1, n do
flickerstreak@53 83 if btnCfg[i] == nil then
flickerstreak@53 84 btnCfg[i] = {}
flickerstreak@53 85 end
flickerstreak@53 86 if btns[i] == nil then
flickerstreak@53 87 local ok, b = pcall(self.BtnClass.new, self.BtnClass, bar, i, btnCfg[i])
flickerstreak@53 88 if ok and b then
flickerstreak@53 89 btns[i] = b
flickerstreak@53 90 end
flickerstreak@53 91 else
flickerstreak@53 92 btns[i]:Refresh(bar,i)
flickerstreak@53 93 end
flickerstreak@53 94 end
flickerstreak@53 95 for i = n+1, #btns do
flickerstreak@53 96 if btns[i] then
flickerstreak@53 97 btns[i] = btns[i]:Destroy()
flickerstreak@53 98 if btnCfg[i] then
flickerstreak@53 99 btnCfg[i] = nil
flickerstreak@53 100 end
flickerstreak@53 101 end
flickerstreak@53 102 end
flickerstreak@53 103 end
flickerstreak@53 104 end
flickerstreak@53 105
flickerstreak@53 106 function module:RemoveFromBar(bar)
flickerstreak@53 107 if self.buttons[bar] then
flickerstreak@53 108 local btns = self.buttons[bar]
flickerstreak@53 109 for _,b in pairs(btns) do
flickerstreak@53 110 if b then
flickerstreak@53 111 b:Destroy()
flickerstreak@53 112 end
flickerstreak@53 113 end
flickerstreak@53 114 self.buttons[bar] = nil
flickerstreak@53 115 end
flickerstreak@53 116 end
flickerstreak@53 117
flickerstreak@53 118 function module:EraseBarConfig(barName)
flickerstreak@53 119 self.db.profile.buttons[barName] = nil
flickerstreak@53 120 end
flickerstreak@53 121
flickerstreak@53 122 function module:RenameBarConfig(oldname, newname)
flickerstreak@53 123 local b = self.db.profile.buttons
flickerstreak@53 124 b[newname], b[oldname] = b[oldname], nil
flickerstreak@53 125 end
flickerstreak@53 126
flickerstreak@53 127
flickerstreak@53 128 function module:ApplyConfigMode(mode,bars)
flickerstreak@53 129 for _, bar in pairs(bars) do
flickerstreak@53 130 if bar and self.buttons[bar] then
flickerstreak@53 131 for _, b in pairs(self.buttons[bar]) do
flickerstreak@53 132 if b then
flickerstreak@53 133 if mode then
flickerstreak@53 134 self:showActionIDLabel(b)
flickerstreak@53 135 else
flickerstreak@53 136 ReAction:Print("Hiding action id "..b:GetActionID())
flickerstreak@53 137 self:hideActionIDLabel(b)
flickerstreak@53 138 end
flickerstreak@53 139 end
flickerstreak@53 140 end
flickerstreak@53 141 local f = bar:GetFrame()
flickerstreak@53 142 if mode then
flickerstreak@53 143 UnregisterUnitWatch(f)
flickerstreak@53 144 f:Show()
flickerstreak@53 145 else
flickerstreak@53 146 RegisterUnitWatch(f)
flickerstreak@53 147 end
flickerstreak@53 148 end
flickerstreak@53 149 end
flickerstreak@53 150 end
flickerstreak@53 151
flickerstreak@53 152 function module:showActionIDLabel(button)
flickerstreak@53 153 -- store the action ID label in the frame due to frame recycling
flickerstreak@53 154 if not button:GetFrame().actionIDLabel and button:GetActionID() then
flickerstreak@53 155 local label = button:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge")
flickerstreak@53 156 label:SetAllPoints()
flickerstreak@53 157 label:SetJustifyH("CENTER")
flickerstreak@53 158 label:SetShadowColor(0,0,0,1)
flickerstreak@53 159 label:SetShadowOffset(2,-2)
flickerstreak@53 160 label:SetText(tostring(button:GetActionID()))
flickerstreak@53 161 button:GetFrame().actionIDLabel = label
flickerstreak@53 162 end
flickerstreak@53 163 button:GetFrame().actionIDLabel:Show()
flickerstreak@53 164 end
flickerstreak@53 165
flickerstreak@53 166 function module:hideActionIDLabel(button)
flickerstreak@53 167 if button:GetFrame().actionIDLabel then
flickerstreak@53 168 button:GetFrame().actionIDLabel:Hide()
flickerstreak@53 169 else
flickerstreak@53 170 ReAction:Print("actionIDLabel not found")
flickerstreak@53 171 end
flickerstreak@53 172 end
flickerstreak@53 173
flickerstreak@59 174 ---- Options handlers ----
flickerstreak@59 175 function module:BarOptionsHidden(bar)
flickerstreak@59 176 return bar.config.type ~= moduleID
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 }