annotate modules/ReAction_PetAction/ReAction_PetAction.lua @ 85:913857b7e123

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