Mercurial > wow > reaction
comparison modules/ReAction_PetAction/ReAction_PetAction.lua @ 77:da8ba8783924
- added revision updater to each code file
- Changed button/bar class mechanic to metatable-based
- Changed buttons to live within a sub-frame, to play nicely between show-empty-buttons and hidestates
- bar frame is now available only via accessor
- Changed some semantics with AddButton/PlaceButton
- Cleaned up action buttons options, fixed hide-when-empty option
- moved show-action-ID-label as a button method
- converted drag overlay from nested-frame to :Raise()
- fixed ReAction:SetConfigMode() to not call event when mode doesn't change
- Fixed ordering for dynamic state tab (always last)
| author | Flick <flickerstreak@gmail.com> |
|---|---|
| date | Mon, 23 Jun 2008 22:27:50 +0000 |
| parents | 06cd74bdc7da |
| children | 42ec2938d65a |
comparison
equal
deleted
inserted
replaced
| 76:c8c8610fd864 | 77:da8ba8783924 |
|---|---|
| 1 --[[ | 1 --[[ |
| 2 ReAction Pet Action button module | 2 ReAction Pet Action button module |
| 3 | 3 |
| 4 The button module implements standard action button functionality by wrapping Blizzard's | 4 The button module implements standard action button functionality by wrapping Blizzard's |
| 5 PetActionButton frame and associated functions. It also provides some button layout | 5 PetActionButton frame and associated functions. |
| 6 modification tools. | |
| 7 | 6 |
| 8 --]] | 7 --]] |
| 9 | 8 |
| 10 -- local imports | 9 -- local imports |
| 11 local ReAction = ReAction | 10 local ReAction = ReAction |
| 12 local L = ReAction.L | 11 local L = ReAction.L |
| 13 local _G = _G | 12 local _G = _G |
| 14 local CreateFrame = CreateFrame | 13 local CreateFrame = CreateFrame |
| 15 | 14 |
| 15 ReAction:UpdateRevision("$Revision: 103 $") | |
| 16 | |
| 16 -- module declaration | 17 -- module declaration |
| 17 local moduleID = "PetAction" | 18 local moduleID = "PetAction" |
| 18 local module = ReAction:NewModule( moduleID ) | 19 local module = ReAction:NewModule( moduleID ) |
| 20 | |
| 21 -- Button class declaration | |
| 22 local Button = { } | |
| 19 | 23 |
| 20 -- module methods | 24 -- module methods |
| 21 function module:OnInitialize() | 25 function module:OnInitialize() |
| 22 self.db = ReAction.db:RegisterNamespace( moduleID, | 26 self.db = ReAction.db:RegisterNamespace( moduleID, |
| 23 { | 27 { |
| 79 for i = 1, n do | 83 for i = 1, n do |
| 80 if btnCfg[i] == nil then | 84 if btnCfg[i] == nil then |
| 81 btnCfg[i] = {} | 85 btnCfg[i] = {} |
| 82 end | 86 end |
| 83 if btns[i] == nil then | 87 if btns[i] == nil then |
| 84 local ok, b = pcall(self.BtnClass.new, self.BtnClass, bar, i, btnCfg[i]) | 88 local b = Button:New(bar,i,btnCfg[i]) |
| 85 if ok and b then | 89 btns[i] = b |
| 86 btns[i] = b | 90 bar:AddButton(i,b) |
| 87 bar:AddButton(i,b) | 91 end |
| 88 end | 92 btns[i]:Refresh() |
| 89 else | |
| 90 btns[i]:Refresh(bar,i) | |
| 91 end | |
| 92 end | 93 end |
| 93 for i = n+1, #btns do | 94 for i = n+1, #btns do |
| 94 if btns[i] then | 95 if btns[i] then |
| 95 bar:RemoveButton(b) | 96 bar:RemoveButton(btns[i]) |
| 96 btns[i] = btns[i]:Destroy() | 97 btns[i] = btns[i]:Destroy() |
| 97 if btnCfg[i] then | 98 if btnCfg[i] then |
| 98 btnCfg[i] = nil | 99 btnCfg[i] = nil |
| 99 end | 100 end |
| 100 end | 101 end |
| 123 b[newname], b[oldname] = b[oldname], nil | 124 b[newname], b[oldname] = b[oldname], nil |
| 124 end | 125 end |
| 125 | 126 |
| 126 | 127 |
| 127 function module:OnConfigModeChanged(event, mode) | 128 function module:OnConfigModeChanged(event, mode) |
| 129 for _, buttons in pairs(self.buttons) do | |
| 130 for _, b in pairs(buttons) do | |
| 131 b:ShowActionIDLabel(mode) | |
| 132 end | |
| 133 end | |
| 128 for _, bar in ReAction:IterateBars() do | 134 for _, bar in ReAction:IterateBars() do |
| 129 if bar and self.buttons[bar] then | 135 if bar and self.buttons[bar] then |
| 130 for _, b in pairs(self.buttons[bar]) do | |
| 131 if b then | |
| 132 if mode then | |
| 133 self:showActionIDLabel(b) | |
| 134 else | |
| 135 self:hideActionIDLabel(b) | |
| 136 end | |
| 137 end | |
| 138 end | |
| 139 local f = bar:GetFrame() | 136 local f = bar:GetFrame() |
| 140 if mode then | 137 if mode then |
| 141 UnregisterUnitWatch(f) | 138 UnregisterUnitWatch(f) |
| 142 f:Show() | 139 f:Show() |
| 143 else | 140 else |
| 145 end | 142 end |
| 146 end | 143 end |
| 147 end | 144 end |
| 148 end | 145 end |
| 149 | 146 |
| 150 function module:showActionIDLabel(button) | |
| 151 -- store the action ID label in the frame due to frame recycling | |
| 152 if not button:GetFrame().actionIDLabel and button:GetActionID() then | |
| 153 local label = button:GetFrame():CreateFontString(nil,"OVERLAY","GameFontNormalLarge") | |
| 154 label:SetAllPoints() | |
| 155 label:SetJustifyH("CENTER") | |
| 156 label:SetShadowColor(0,0,0,1) | |
| 157 label:SetShadowOffset(2,-2) | |
| 158 label:SetText(tostring(button:GetActionID())) | |
| 159 button:GetFrame().actionIDLabel = label | |
| 160 end | |
| 161 button:GetFrame().actionIDLabel:Show() | |
| 162 end | |
| 163 | |
| 164 function module:hideActionIDLabel(button) | |
| 165 if button:GetFrame().actionIDLabel then | |
| 166 button:GetFrame().actionIDLabel:Hide() | |
| 167 end | |
| 168 end | |
| 169 | 147 |
| 170 ---- Options ---- | 148 ---- Options ---- |
| 171 function module:GetBarOptions(bar) | 149 function module:GetBarOptions(bar) |
| 172 return { | 150 return { |
| 173 type = "group", | 151 type = "group", |
| 177 } | 155 } |
| 178 } | 156 } |
| 179 end | 157 end |
| 180 | 158 |
| 181 | 159 |
| 160 | |
| 161 ------ Button class ------ | |
| 182 | 162 |
| 183 -- use-count of action IDs | 163 -- use-count of action IDs |
| 184 local nActionIDs = NUM_PET_ACTION_SLOTS | 164 local nActionIDs = NUM_PET_ACTION_SLOTS |
| 185 local ActionIDList = setmetatable( {}, { | 165 local ActionIDList = setmetatable( {}, { |
| 186 __index = function(self, idx) | 166 __index = function(self, idx) |
| 211 end | 191 end |
| 212 }) | 192 }) |
| 213 | 193 |
| 214 local frameRecycler = {} | 194 local frameRecycler = {} |
| 215 | 195 |
| 216 | 196 function Button:New( bar, idx, config ) |
| 217 ------ Button class ------ | 197 -- create new self |
| 218 local Button = { } | 198 self = setmetatable( { }, { __index = Button } ) |
| 219 | |
| 220 local function Constructor( self, bar, idx, config ) | |
| 221 self.bar, self.idx, self.config = bar, idx, config | 199 self.bar, self.idx, self.config = bar, idx, config |
| 222 | |
| 223 local barFrame = bar:GetFrame() | |
| 224 | 200 |
| 225 local name = config.name or ("ReAction_%s_Pet_%d"):format(bar:GetName(),idx) | 201 local name = config.name or ("ReAction_%s_Pet_%d"):format(bar:GetName(),idx) |
| 226 config.name = name | 202 config.name = name |
| 227 self.name = config.name | 203 self.name = name |
| 228 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured | 204 config.actionID = ActionIDList[config.actionID] -- gets a free one if none configured |
| 229 | 205 |
| 230 -- have to recycle frames with the same name: | 206 -- have to recycle frames with the same name: |
| 231 -- otherwise you either get references to old textures because named CreateFrame() | 207 -- otherwise you either get references to old textures because named CreateFrame() |
| 232 -- doesn't overwrite existing globals (below) | 208 -- doesn't overwrite existing globals (below) |
| 233 -- or, if you set them to nil in the global table, you get taint because of the | 209 -- or, if you set them to nil in the global table, you get taint because of the |
| 234 -- crappy PetActionBar code. | 210 -- crappy PetActionBar code. |
| 211 local parent = bar:GetButtonFrame() | |
| 235 local f = frameRecycler[name] | 212 local f = frameRecycler[name] |
| 236 if f then | 213 if f then |
| 237 f:SetParent(barFrame) | 214 f:SetParent(parent) |
| 238 f:Show() | 215 else |
| 239 else | 216 f = CreateFrame("CheckButton", name, parent, "PetActionButtonTemplate") |
| 240 f = CreateFrame("CheckButton", name, barFrame, "PetActionButtonTemplate") | |
| 241 end | 217 end |
| 242 if config.actionID then | 218 if config.actionID then |
| 243 f:SetID(config.actionID) -- PetActionButtonTemplate isn't a proper SecureActionButton | 219 f:SetID(config.actionID) -- PetActionButtonTemplate isn't a proper SecureActionButton |
| 244 end | 220 end |
| 245 f:SetFrameStrata("MEDIUM") | 221 f:SetFrameStrata("MEDIUM") |
| 246 | |
| 247 barFrame:SetAttribute("addchild",f) | |
| 248 | |
| 249 self.frame = f | 222 self.frame = f |
| 250 self.icon = _G[("%sIcon"):format(name)] | 223 self.icon = _G[("%sIcon"):format(name)] |
| 251 self.acTex = _G[("%sAutoCastable"):format(name)] | 224 self.acTex = _G[("%sAutoCastable"):format(name)] |
| 252 self.acModel = _G[("%sAutoCast"):format(name)] | 225 self.acModel = _G[("%sAutoCast"):format(name)] |
| 253 self.cooldown = _G[("%sCooldown"):format(name)] | 226 self.cooldown = _G[("%sCooldown"):format(name)] |
| 254 self.hotkey = _G[("%sHotKey"):format(name)] | 227 self.hotkey = _G[("%sHotKey"):format(name)] |
| 255 | 228 |
| 256 f:HookScript("OnDragStart", function() self:Update() end) | 229 f:HookScript("OnDragStart", function() self:Update() end) |
| 257 f:HookScript("OnReceiveDrag", function() self:Update() end) | 230 f:HookScript("OnReceiveDrag", function() self:Update() end) |
| 258 | 231 |
| 259 f:RegisterEvent("PLAYER_CONTROL_LOST"); | 232 f:RegisterEvent("PLAYER_CONTROL_LOST"); |
| 260 f:RegisterEvent("PLAYER_CONTROL_GAINED"); | 233 f:RegisterEvent("PLAYER_CONTROL_GAINED"); |
| 261 f:RegisterEvent("PLAYER_FARSIGHT_FOCUS_CHANGED"); | 234 f:RegisterEvent("PLAYER_FARSIGHT_FOCUS_CHANGED"); |
| 262 f:RegisterEvent("UNIT_PET"); | 235 f:RegisterEvent("UNIT_PET"); |
| 263 f:RegisterEvent("UNIT_FLAGS"); | 236 f:RegisterEvent("UNIT_FLAGS"); |
| 264 f:RegisterEvent("UNIT_AURA"); | 237 f:RegisterEvent("UNIT_AURA"); |
| 274 else | 247 else |
| 275 self:Update() | 248 self:Update() |
| 276 end | 249 end |
| 277 end) | 250 end) |
| 278 | 251 |
| 279 self:Refresh(bar,idx) | 252 self:Refresh() |
| 253 return self | |
| 280 end | 254 end |
| 281 | 255 |
| 282 function Button:Destroy() | 256 function Button:Destroy() |
| 283 local f = self.frame | 257 local f = self.frame |
| 284 f:UnregisterAllEvents() | 258 f:UnregisterAllEvents() |
| 295 self.frame = nil | 269 self.frame = nil |
| 296 self.config = nil | 270 self.config = nil |
| 297 self.bar = nil | 271 self.bar = nil |
| 298 end | 272 end |
| 299 | 273 |
| 300 function Button:Refresh(bar,idx) | 274 function Button:Refresh() |
| 301 bar:PlaceButton(self, 30, 30) | 275 self.bar:PlaceButton(self, 30, 30) |
| 302 self:Update() | 276 self:Update() |
| 303 self:UpdateHotkey() | 277 self:UpdateHotkey() |
| 304 end | 278 end |
| 305 | 279 |
| 306 function Button:GetFrame() | 280 function Button:GetFrame() |
| 368 | 342 |
| 369 function Button:UpdateHotkey() | 343 function Button:UpdateHotkey() |
| 370 | 344 |
| 371 end | 345 end |
| 372 | 346 |
| 373 -- export as a class-factory to module | 347 function Button:ShowActionIDLabel(show) |
| 374 module.BtnClass = { | 348 if show then |
| 375 new = function(self, ...) | 349 -- store the action ID label in the frame due to frame recycling |
| 376 local x = { } | 350 if not self.actionIDLabel and self:GetActionID() then |
| 377 for k,v in pairs(Button) do | 351 local label = self.frame:CreateFontString(nil,"OVERLAY","GameFontNormalLarge") |
| 378 x[k] = v | 352 label:SetAllPoints() |
| 379 end | 353 label:SetJustifyH("CENTER") |
| 380 Constructor(x, ...) | 354 label:SetShadowColor(0,0,0,1) |
| 381 return x | 355 label:SetShadowOffset(2,-2) |
| 382 end | 356 label:SetText(tostring(self:GetActionID())) |
| 383 } | 357 self.actionIDLabel = label |
| 358 end | |
| 359 self.actionIDLabel:Show() | |
| 360 elseif self.actionIDLabel then | |
| 361 self.actionIDLabel:Hide() | |
| 362 end | |
| 363 end |
