annotate BagButton.lua @ 301:6fbb55e5c624

Add [extrabar] as a checkbox rule option
author Flick
date Fri, 09 Nov 2012 13:25:41 -0800
parents d1a56601267b
children
rev   line source
Flick@276 1 local _, ns = ...
Flick@276 2 local ReAction = ns.ReAction
flickerstreak@146 3 local L = ReAction.L
flickerstreak@146 4 local _G = _G
flickerstreak@146 5 local CreateFrame = CreateFrame
flickerstreak@146 6 local format = string.format
flickerstreak@146 7 local GetCVar = GetCVar
flickerstreak@146 8 local ContainerIDToInventoryID = ContainerIDToInventoryID
flickerstreak@146 9 local NUM_CONTAINER_FRAMES = NUM_CONTAINER_FRAMES
flickerstreak@146 10 local IsModifiedClick = IsModifiedClick
flickerstreak@146 11 local CursorHasItem = CursorHasItem
flickerstreak@146 12 local GetInventoryItemTexture = GetInventoryItemTexture
flickerstreak@146 13 local GetInventorySlotInfo = GetInventorySlotInfo
flickerstreak@146 14 local PickupBagFromSlot = PickupBagFromSlot
flickerstreak@146 15 local CursorCanGoInSlot = CursorCanGoInSlot
flickerstreak@146 16
flickerstreak@146 17 -- class declarations
flickerstreak@218 18 local buttonTypeID = "Bag"
Flick@234 19 local weak = { __mode = "k" }
flickerstreak@146 20 local Super = ReAction.Button
flickerstreak@218 21 local BagBase = setmetatable(
flickerstreak@218 22 {
flickerstreak@218 23 defaultBarConfig = {
flickerstreak@218 24 type = buttonTypeID,
flickerstreak@218 25 btnWidth = 30,
flickerstreak@218 26 btnHeight = 30,
flickerstreak@218 27 btnRows = 1,
Flick@298 28 btnColumns = 5,
flickerstreak@221 29 spacing = 4,
flickerstreak@221 30 buttons = { }
flickerstreak@218 31 },
flickerstreak@218 32
flickerstreak@218 33 barType = L["Bag Bar"],
Flick@234 34 buttonTypeID = buttonTypeID,
Flick@234 35
Flick@234 36 allButtons = setmetatable( { }, weak )
flickerstreak@218 37 },
flickerstreak@218 38 { __index = Super } )
flickerstreak@218 39
flickerstreak@146 40 local Bag = setmetatable( { }, { __index = BagBase } )
flickerstreak@146 41 local Backpack = setmetatable( { }, { __index = BagBase } )
flickerstreak@146 42
flickerstreak@146 43 ReAction.Button.Bag = BagBase
flickerstreak@223 44 ReAction:RegisterBarType(BagBase)
flickerstreak@146 45
flickerstreak@146 46 --
flickerstreak@146 47 -- Bag Button base class
flickerstreak@146 48 --
flickerstreak@146 49
Flick@234 50 function BagBase:New( btnCfg, bar, idx, idHint )
flickerstreak@146 51 -- use a variable private leaf implementation class
flickerstreak@146 52 -- unlike traditional OO programming, we can initialize the leaf
flickerstreak@221 53 -- class before initializing its parent
flickerstreak@146 54 local class = Bag
flickerstreak@146 55 if idx == 1 then
flickerstreak@146 56 class = Backpack
flickerstreak@146 57 end
Flick@277 58 self = class:New(btnCfg, bar, idx)
flickerstreak@146 59
flickerstreak@146 60 local f = self:GetFrame()
flickerstreak@146 61 local config = self:GetConfig()
flickerstreak@146 62
flickerstreak@146 63 -- set up the bag ID pool
Flick@298 64 self:SetActionIDPool("bag",5)
flickerstreak@146 65 config.bagID = self:AcquireActionID(config.bagID, idHint, true)
flickerstreak@146 66
flickerstreak@146 67 -- non secure scripts
flickerstreak@146 68 f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end)
flickerstreak@146 69 f:SetScript("OnEnter", function(frame) self:OnEnter() end)
flickerstreak@146 70 f:SetScript("OnLeave", function(frame) self:OnLeave() end)
flickerstreak@146 71 f:SetScript("OnReceiveDrag", function(frame, ...) self:OnReceiveDrag(...) end)
flickerstreak@146 72 f:SetScript("OnClick", function(frame, ...) self:OnClick(...) end)
flickerstreak@146 73
flickerstreak@146 74 -- secure handlers
flickerstreak@146 75 -- (none)
flickerstreak@146 76
flickerstreak@146 77 -- event registration
flickerstreak@146 78 f:RegisterEvent("UPDATE_BINDINGS")
flickerstreak@146 79
flickerstreak@146 80 -- frame setup
flickerstreak@146 81 f:SetID(self:GetBagID())
flickerstreak@146 82
flickerstreak@146 83 if not f.hotkey then
Flick@298 84 local h = f:CreateFontString(self:GetName().."HotKey","ARTWORK","NumberFontNormalSmallGray")
flickerstreak@152 85 h:SetWidth(30)
flickerstreak@146 86 h:SetHeight(10)
flickerstreak@146 87 h:SetJustifyH("RIGHT")
flickerstreak@146 88 h:SetPoint("TOPLEFT",f,"TOPLEFT",-2,-2)
flickerstreak@146 89 h:Show()
flickerstreak@146 90 f.hotkey = h
flickerstreak@146 91 end
flickerstreak@146 92
Flick@298 93 if not _G[self:GetName().."ItemAnim"] then
Flick@298 94 local anim = CreateFrame("Model",self:GetName().."ItemAnim",f,"ItemAnimTemplate")
flickerstreak@146 95 anim:SetPoint("BOTTOMRIGHT",f,"BOTTOMRIGHT",-10,0)
flickerstreak@146 96 anim:Hide()
flickerstreak@146 97 end
flickerstreak@146 98
flickerstreak@152 99 if not f.border then
Flick@298 100 local b = f:CreateTexture(self:GetName().."Border","OVERLAY")
flickerstreak@152 101 b:SetAllPoints()
flickerstreak@152 102 b:SetWidth(f:GetWidth()*(62/36))
flickerstreak@152 103 b:SetHeight(f:GetHeight()*(62/36))
flickerstreak@152 104 b:SetTexture("Interface\\Buttons\UI-ActionButton-Border")
flickerstreak@152 105 b:SetBlendMode("ADD")
flickerstreak@152 106 b:Hide()
flickerstreak@152 107 f.border = b
flickerstreak@152 108 end
flickerstreak@152 109
flickerstreak@146 110 self.frames.count:SetDrawLayer("ARTWORK")
flickerstreak@146 111
flickerstreak@146 112 self.frames.hotkey = f.hotkey
Flick@298 113 self.frames.border = _G[self:GetName().."Border"]
Flick@298 114 self.frames.icon = _G[self:GetName().."IconTexture"]
Flick@298 115 self.frames.anim = _G[self:GetName().."ItemAnim"]
flickerstreak@146 116
flickerstreak@146 117 -- initial display
flickerstreak@146 118 if ReAction:GetConfigMode() then
flickerstreak@146 119 self:GetFrame():Show()
flickerstreak@146 120 end
flickerstreak@146 121
flickerstreak@146 122 self:Refresh()
flickerstreak@146 123
Flick@234 124 BagBase.allButtons[self] = true
Flick@234 125
flickerstreak@146 126 return self
flickerstreak@146 127 end
flickerstreak@146 128
Flick@234 129 function BagBase:Destroy()
Flick@234 130 BagBase.allButtons[self] = nil
Flick@234 131 Super.Destroy(self)
Flick@234 132 end
Flick@234 133
Flick@234 134
flickerstreak@146 135 function BagBase:GetActionID()
flickerstreak@146 136 return self.config.bagID
flickerstreak@146 137 end
flickerstreak@146 138
flickerstreak@146 139 function BagBase:GetBagID()
flickerstreak@146 140 return self:GetActionID() - 1
flickerstreak@146 141 end
flickerstreak@146 142
flickerstreak@146 143 function BagBase:Refresh()
flickerstreak@146 144 Super.Refresh(self)
flickerstreak@146 145 self:UpdateHotkey()
flickerstreak@146 146 self:Update()
flickerstreak@146 147 end
flickerstreak@146 148
flickerstreak@146 149 function BagBase:Update()
flickerstreak@146 150 self:UpdateChecked()
flickerstreak@146 151 end
flickerstreak@146 152
flickerstreak@146 153 function BagBase:UpdateChecked(force)
flickerstreak@146 154 if force == nil then
flickerstreak@146 155 for i=1, NUM_CONTAINER_FRAMES do
flickerstreak@146 156 local c = _G["ContainerFrame"..i]
flickerstreak@146 157 if c:GetID() == self:GetBagID() and c:IsShown() then
flickerstreak@146 158 self:GetFrame():SetChecked(1)
flickerstreak@146 159 return
flickerstreak@146 160 end
flickerstreak@146 161 end
flickerstreak@146 162 self:GetFrame():SetChecked(0)
flickerstreak@146 163 end
flickerstreak@146 164 self:GetFrame():SetChecked(force)
flickerstreak@146 165 end
flickerstreak@146 166
flickerstreak@146 167 function BagBase:OnEvent(evt, ...)
flickerstreak@146 168 if self[evt] then
flickerstreak@146 169 self[evt](self, ...)
flickerstreak@146 170 end
flickerstreak@146 171 end
flickerstreak@146 172
flickerstreak@146 173 function BagBase:OnEnter()
flickerstreak@146 174 self:SetTooltip()
flickerstreak@146 175 end
flickerstreak@146 176
flickerstreak@146 177 function BagBase:OnLeave()
flickerstreak@146 178 GameTooltip:Hide()
flickerstreak@146 179 end
flickerstreak@146 180
flickerstreak@146 181 function BagBase:UPDATE_BINDINGS()
flickerstreak@146 182 self:UpdateHotkey()
flickerstreak@146 183 end
flickerstreak@146 184
Flick@234 185 function BagBase:IterateAllButtons()
Flick@234 186 return pairs(self.allButtons)
Flick@234 187 end
Flick@234 188
flickerstreak@146 189
flickerstreak@146 190 --
flickerstreak@146 191 -- Bag Button class
flickerstreak@146 192 --
Flick@298 193 function Bag:New(cfg, bar, idx)
Flick@298 194 self = Super.New(self, cfg, bar, idx, "ItemButtonTemplate" )
flickerstreak@146 195
flickerstreak@146 196 local f = self:GetFrame()
flickerstreak@146 197
flickerstreak@146 198 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight")
flickerstreak@146 199
flickerstreak@146 200 f:RegisterEvent("CURSOR_UPDATE")
flickerstreak@151 201 f:RegisterEvent("BAG_UPDATE")
flickerstreak@146 202 f:RegisterEvent("BAG_CLOSED")
flickerstreak@146 203 f:SetScript("OnDragStart", function(frame, ...) self:OnDragStart(...) end)
flickerstreak@146 204 f:RegisterForDrag("LeftButton")
flickerstreak@146 205
flickerstreak@146 206 -- attach to skinner
flickerstreak@146 207 bar:SkinButton(self,
flickerstreak@146 208 {
Flick@298 209 Icon = _G[self:GetName().."IconTexture"]
flickerstreak@146 210 }
flickerstreak@146 211 )
flickerstreak@146 212
flickerstreak@146 213 return self
flickerstreak@146 214 end
flickerstreak@146 215
flickerstreak@146 216 function Bag:GetInventorySlot()
flickerstreak@146 217 return ContainerIDToInventoryID(self:GetBagID())
flickerstreak@146 218 end
flickerstreak@146 219
flickerstreak@146 220 function Bag:GetInventorySlotName()
flickerstreak@146 221 return "Bag"..(self:GetBagID()-1).."Slot"
flickerstreak@146 222 end
flickerstreak@146 223
flickerstreak@146 224 function Bag:SetTooltip()
flickerstreak@146 225 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT")
flickerstreak@146 226 if not GameTooltip:SetInventoryItem("player", self:GetInventorySlot()) then
flickerstreak@146 227 GameTooltip:SetText(EQUIP_CONTAINER, 1.0, 1.0, 1.0)
flickerstreak@146 228 end
flickerstreak@146 229 end
flickerstreak@146 230
flickerstreak@146 231 function Bag:Update()
flickerstreak@146 232 local texture = GetInventoryItemTexture("player", self:GetInventorySlot())
flickerstreak@146 233 if texture then
flickerstreak@146 234 self.frames.icon:SetTexture(texture)
flickerstreak@146 235 self.frames.icon:Show()
flickerstreak@146 236 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
flickerstreak@146 237 else
flickerstreak@146 238 local _, bgTex = GetInventorySlotInfo(self:GetInventorySlotName())
flickerstreak@146 239 self.frames.icon:SetTexture(bgTex)
flickerstreak@146 240 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
flickerstreak@146 241 end
flickerstreak@146 242 self:UpdateChecked()
flickerstreak@146 243 end
flickerstreak@146 244
flickerstreak@146 245 function Bag:OnClick()
flickerstreak@146 246 if IsModifiedClick("OPENALLBAGS") then
flickerstreak@146 247 OpenAllBags()
flickerstreak@146 248 else
flickerstreak@146 249 if not PutItemInBag(self:GetInventorySlot()) then
flickerstreak@146 250 ToggleBag(self:GetBagID())
flickerstreak@146 251 end
flickerstreak@146 252 end
flickerstreak@146 253 self:UpdateChecked()
flickerstreak@146 254 end
flickerstreak@146 255
flickerstreak@146 256 function Bag:OnReceiveDrag()
flickerstreak@146 257 if CursorHasItem() then
flickerstreak@146 258 PutItemInBag(self:GetInventorySlot())
flickerstreak@146 259 end
flickerstreak@146 260 end
flickerstreak@146 261
flickerstreak@146 262 function Bag:OnDragStart()
flickerstreak@146 263 PickupBagFromSlot(self:GetInventorySlot())
flickerstreak@146 264 self:Update()
flickerstreak@146 265 end
flickerstreak@146 266
flickerstreak@151 267 function Bag:BAG_UPDATE(bag)
flickerstreak@151 268 if bag == self:GetBagID() then
flickerstreak@146 269 self:Update()
flickerstreak@146 270 end
flickerstreak@146 271 end
flickerstreak@146 272
flickerstreak@146 273 function Bag:CURSOR_UPDATE()
flickerstreak@146 274 if CursorCanGoInSlot(self:GetInventorySlot()) then
flickerstreak@146 275 self:GetFrame():LockHighlight()
flickerstreak@146 276 else
flickerstreak@146 277 self:GetFrame():UnlockHighlight()
flickerstreak@146 278 end
flickerstreak@146 279 end
flickerstreak@146 280
flickerstreak@146 281 function Bag:BAG_CLOSED(bag)
flickerstreak@146 282 if bag == self:GetBagID() then
flickerstreak@146 283 self:Update()
flickerstreak@146 284 end
flickerstreak@146 285 end
flickerstreak@146 286
flickerstreak@146 287
flickerstreak@146 288 --
flickerstreak@146 289 -- Backpack Button class
flickerstreak@146 290 --
Flick@298 291 function Backpack:New(cfg, bar, idx)
Flick@298 292 self = Super.New(self, cfg, bar, idx, "ItemButtonTemplate" )
flickerstreak@146 293
flickerstreak@146 294 local f = self:GetFrame()
Flick@298 295 local icon = _G[self:GetName().."IconTexture"]
flickerstreak@146 296 icon:SetTexture("Interface\\Buttons\\Button-Backpack-Up")
flickerstreak@146 297 icon:Show()
flickerstreak@146 298 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight")
flickerstreak@146 299 f:RegisterEvent("PLAYER_ENTERING_WORLD");
flickerstreak@146 300 f:RegisterEvent("CVAR_UPDATE");
flickerstreak@146 301 f:SetScript("OnShow", function(frame, ...) self:OnShow(...) end)
flickerstreak@146 302
flickerstreak@146 303 -- attach to skinner
flickerstreak@146 304 bar:SkinButton(self,
flickerstreak@146 305 {
Flick@298 306 Icon = _G[self:GetName().."IconTexture"]
flickerstreak@146 307 }
flickerstreak@146 308 )
flickerstreak@146 309
flickerstreak@146 310 return self
flickerstreak@146 311 end
flickerstreak@146 312
flickerstreak@146 313 function Backpack:Update()
flickerstreak@146 314 self:UpdateFreeSlots()
flickerstreak@146 315 self:UpdateChecked()
flickerstreak@146 316 end
flickerstreak@146 317
flickerstreak@146 318 function Backpack:UpdateFreeSlots()
flickerstreak@146 319 if GetCVar("displayFreeBagSlots") == "1" then
flickerstreak@146 320 local total = 0
flickerstreak@146 321 for i = BACKPACK_CONTAINER, NUM_BAG_SLOTS do
flickerstreak@146 322 local free, family = GetContainerNumFreeSlots(i)
flickerstreak@146 323 if family == 0 then
flickerstreak@146 324 total = total + free
flickerstreak@146 325 end
flickerstreak@146 326 end
flickerstreak@146 327
flickerstreak@146 328 self.freeSlots = total
flickerstreak@146 329 self.frames.count:SetText(format("(%s)", self.freeSlots))
flickerstreak@146 330 self.frames.count:Show()
flickerstreak@146 331 elseif self.frames.count:IsShown() then
flickerstreak@146 332 self.frames.count:Hide()
flickerstreak@146 333 end
flickerstreak@146 334 end
flickerstreak@146 335
flickerstreak@146 336 function Backpack:SetTooltip()
flickerstreak@146 337 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT")
flickerstreak@146 338 GameTooltip:SetText(BACKPACK_TOOLTIP, 1.0, 1.0, 1.0)
flickerstreak@146 339 GameTooltip:AddLine(string.format(NUM_FREE_SLOTS, (self.freeSlots or 0)))
flickerstreak@146 340 GameTooltip:Show();
flickerstreak@146 341 end
flickerstreak@146 342
flickerstreak@146 343 function Backpack:OnShow()
flickerstreak@146 344 self:UpdateFreeSlots()
flickerstreak@146 345 end
flickerstreak@146 346
flickerstreak@146 347 function Backpack:OnClick()
flickerstreak@146 348 if IsModifiedClick("OPENALLBAGS") then
flickerstreak@146 349 OpenAllBags()
flickerstreak@146 350 else
flickerstreak@146 351 if not PutItemInBackpack() then
flickerstreak@146 352 ToggleBackpack()
flickerstreak@146 353 end
flickerstreak@146 354 end
flickerstreak@146 355 self:UpdateChecked()
flickerstreak@146 356 end
flickerstreak@146 357
flickerstreak@146 358 function Backpack:OnReceiveDrag()
flickerstreak@146 359 if CursorHasItem() then
flickerstreak@146 360 PutItemInBackpack()
flickerstreak@146 361 end
flickerstreak@146 362 end
flickerstreak@146 363
flickerstreak@146 364 function Backpack:PLAYER_ENTERING_WORLD()
flickerstreak@146 365 self:CVAR_UPDATE("DISPLAY_FREE_BAG_SLOTS", GetCVar("displayFreeBagSlots"))
flickerstreak@146 366 end
flickerstreak@146 367
flickerstreak@146 368 function Backpack:CVAR_UPDATE( cvar, value )
flickerstreak@146 369 if cvar == "DISPLAY_FREE_BAG_SLOTS" then
flickerstreak@146 370 if value == "1" then
flickerstreak@146 371 self:GetFrame():RegisterEvent("BAG_UPDATE")
flickerstreak@146 372 else
flickerstreak@146 373 self:GetFrame():UnregisterEvent("BAG_UPDATE")
flickerstreak@146 374 end
flickerstreak@146 375 self:UpdateFreeSlots()
flickerstreak@146 376 end
flickerstreak@146 377 end
flickerstreak@146 378
flickerstreak@146 379 function Backpack:BAG_UPDATE(bag)
flickerstreak@146 380 if bag >= BACKPACK_CONTAINER and bag <= NUM_BAG_SLOTS then
flickerstreak@146 381 self:UpdateFreeSlots()
flickerstreak@146 382 end
flickerstreak@146 383 end
flickerstreak@146 384
flickerstreak@146 385
flickerstreak@146 386
Flick@234 387
Flick@234 388
Flick@234 389 -- hook some functions to propagate to our bag buttons
Flick@234 390 hooksecurefunc("Disable_BagButtons",
Flick@234 391 function()
Flick@234 392 for b in BagBase:IterateAllButtons() do
Flick@234 393 local f = b:GetFrame()
Flick@234 394 f:Disable()
Flick@234 395 SetDesaturation(b.frames.icon,1)
Flick@234 396 end
Flick@234 397 end)
Flick@234 398
Flick@234 399 hooksecurefunc("Enable_BagButtons",
Flick@234 400 function()
Flick@234 401 for b in BagBase:IterateAllButtons() do
Flick@234 402 local f = b:GetFrame()
Flick@234 403 f:Enable()
Flick@234 404 SetDesaturation(b.frames.icon,nil)
Flick@234 405 end
Flick@234 406 end)
Flick@234 407
Flick@234 408 hooksecurefunc("ContainerFrame_OnHide",
Flick@234 409 function()
Flick@234 410 for b in BagBase:IterateAllButtons() do
Flick@234 411 b:Update()
Flick@234 412 end
Flick@234 413 end)
Flick@234 414
Flick@234 415 hooksecurefunc("ContainerFrame_OnShow",
Flick@234 416 function()
Flick@234 417 for b in BagBase:IterateAllButtons() do
Flick@234 418 b:Update()
Flick@234 419 end
Flick@234 420 end)