annotate classes/BagButton.lua @ 221:bb13624de7e1

un-namespace Bag bar config
author Flick <flickerstreak@gmail.com>
date Sun, 21 Nov 2010 12:51:24 -0800
parents e63aefb8a555
children c4b134512c50
rev   line source
flickerstreak@175 1 local addonName, addonTable = ...
flickerstreak@175 2 local ReAction = addonTable.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"
flickerstreak@146 19 local Super = ReAction.Button
flickerstreak@218 20 local BagBase = setmetatable(
flickerstreak@218 21 {
flickerstreak@218 22 defaultBarConfig = {
flickerstreak@218 23 type = buttonTypeID,
flickerstreak@218 24 btnWidth = 30,
flickerstreak@218 25 btnHeight = 30,
flickerstreak@218 26 btnRows = 1,
flickerstreak@218 27 btnColumns = 6,
flickerstreak@221 28 spacing = 4,
flickerstreak@221 29 buttons = { }
flickerstreak@218 30 },
flickerstreak@218 31
flickerstreak@218 32 barType = L["Bag Bar"],
flickerstreak@218 33 },
flickerstreak@218 34 { __index = Super } )
flickerstreak@218 35
flickerstreak@146 36 local Bag = setmetatable( { }, { __index = BagBase } )
flickerstreak@146 37 local Backpack = setmetatable( { }, { __index = BagBase } )
flickerstreak@146 38 local Keyring = setmetatable( { }, { __index = BagBase } )
flickerstreak@146 39
flickerstreak@146 40 ReAction.Button.Bag = BagBase
flickerstreak@146 41
flickerstreak@146 42 --
flickerstreak@146 43 -- Bag Button base class
flickerstreak@146 44 --
flickerstreak@146 45
flickerstreak@221 46 function BagBase:New( idx, btnCfg, bar, idHint )
flickerstreak@146 47 local name = format("ReAction_%s_Bag_%d",bar:GetName(),idx)
flickerstreak@146 48
flickerstreak@146 49 -- use a variable private leaf implementation class
flickerstreak@146 50 -- unlike traditional OO programming, we can initialize the leaf
flickerstreak@221 51 -- class before initializing its parent
flickerstreak@146 52 local class = Bag
flickerstreak@146 53 if idx == 1 then
flickerstreak@146 54 class = Backpack
flickerstreak@146 55 elseif idx == 6 then
flickerstreak@146 56 class = Keyring
flickerstreak@146 57 end
flickerstreak@221 58 self = class:New(name, 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
flickerstreak@146 64 self:SetActionIDPool("bag",6)
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:EnableMouse(true)
flickerstreak@146 79 f:RegisterForClicks("LeftButtonUp","RightButtonUp")
flickerstreak@146 80 f:RegisterEvent("UPDATE_BINDINGS")
flickerstreak@146 81
flickerstreak@146 82 -- frame setup
flickerstreak@146 83 f:SetID(self:GetBagID())
flickerstreak@146 84
flickerstreak@146 85 if not f.hotkey then
flickerstreak@146 86 local h = f:CreateFontString(name.."HotKey","ARTWORK","NumberFontNormalSmallGray")
flickerstreak@152 87 h:SetWidth(30)
flickerstreak@146 88 h:SetHeight(10)
flickerstreak@146 89 h:SetJustifyH("RIGHT")
flickerstreak@146 90 h:SetPoint("TOPLEFT",f,"TOPLEFT",-2,-2)
flickerstreak@146 91 h:Show()
flickerstreak@146 92 f.hotkey = h
flickerstreak@146 93 end
flickerstreak@146 94
flickerstreak@146 95 if not _G[name.."ItemAnim"] then
flickerstreak@146 96 local anim = CreateFrame("Model",name.."ItemAnim",f,"ItemAnimTemplate")
flickerstreak@146 97 anim:SetPoint("BOTTOMRIGHT",f,"BOTTOMRIGHT",-10,0)
flickerstreak@146 98 anim:Hide()
flickerstreak@146 99 end
flickerstreak@146 100
flickerstreak@152 101 if not f.border then
flickerstreak@152 102 local b = f:CreateTexture(name.."Border","OVERLAY")
flickerstreak@152 103 b:SetAllPoints()
flickerstreak@152 104 b:SetWidth(f:GetWidth()*(62/36))
flickerstreak@152 105 b:SetHeight(f:GetHeight()*(62/36))
flickerstreak@152 106 b:SetTexture("Interface\\Buttons\UI-ActionButton-Border")
flickerstreak@152 107 b:SetBlendMode("ADD")
flickerstreak@152 108 b:Hide()
flickerstreak@152 109 f.border = b
flickerstreak@152 110 end
flickerstreak@152 111
flickerstreak@146 112 self.frames.count:SetDrawLayer("ARTWORK")
flickerstreak@146 113
flickerstreak@146 114 self.frames.hotkey = f.hotkey
flickerstreak@152 115 self.frames.border = _G[name.."Border"]
flickerstreak@146 116 self.frames.icon = _G[name.."IconTexture"]
flickerstreak@146 117 self.frames.anim = _G[name.."ItemAnim"]
flickerstreak@146 118
flickerstreak@146 119 -- initial display
flickerstreak@146 120 if ReAction:GetConfigMode() then
flickerstreak@146 121 self:GetFrame():Show()
flickerstreak@146 122 end
flickerstreak@146 123
flickerstreak@146 124 self:Refresh()
flickerstreak@146 125
flickerstreak@146 126 return self
flickerstreak@146 127 end
flickerstreak@146 128
flickerstreak@146 129 function BagBase:GetActionID()
flickerstreak@146 130 return self.config.bagID
flickerstreak@146 131 end
flickerstreak@146 132
flickerstreak@146 133 function BagBase:GetBagID()
flickerstreak@146 134 return self:GetActionID() - 1
flickerstreak@146 135 end
flickerstreak@146 136
flickerstreak@146 137 function BagBase:Refresh()
flickerstreak@146 138 Super.Refresh(self)
flickerstreak@146 139 self:UpdateHotkey()
flickerstreak@146 140 self:Update()
flickerstreak@146 141 end
flickerstreak@146 142
flickerstreak@146 143 function BagBase:Update()
flickerstreak@146 144 self:UpdateChecked()
flickerstreak@146 145 end
flickerstreak@146 146
flickerstreak@146 147 function BagBase:UpdateChecked(force)
flickerstreak@146 148 if force == nil then
flickerstreak@146 149 for i=1, NUM_CONTAINER_FRAMES do
flickerstreak@146 150 local c = _G["ContainerFrame"..i]
flickerstreak@146 151 if c:GetID() == self:GetBagID() and c:IsShown() then
flickerstreak@146 152 self:GetFrame():SetChecked(1)
flickerstreak@146 153 return
flickerstreak@146 154 end
flickerstreak@146 155 end
flickerstreak@146 156 self:GetFrame():SetChecked(0)
flickerstreak@146 157 end
flickerstreak@146 158 self:GetFrame():SetChecked(force)
flickerstreak@146 159 end
flickerstreak@146 160
flickerstreak@146 161 function BagBase:OnEvent(evt, ...)
flickerstreak@146 162 if self[evt] then
flickerstreak@146 163 self[evt](self, ...)
flickerstreak@146 164 end
flickerstreak@146 165 end
flickerstreak@146 166
flickerstreak@146 167 function BagBase:OnEnter()
flickerstreak@146 168 self:SetTooltip()
flickerstreak@146 169 end
flickerstreak@146 170
flickerstreak@146 171 function BagBase:OnLeave()
flickerstreak@146 172 GameTooltip:Hide()
flickerstreak@146 173 end
flickerstreak@146 174
flickerstreak@146 175 function BagBase:UPDATE_BINDINGS()
flickerstreak@146 176 self:UpdateHotkey()
flickerstreak@146 177 end
flickerstreak@146 178
flickerstreak@146 179
flickerstreak@146 180 --
flickerstreak@146 181 -- Bag Button class
flickerstreak@146 182 --
flickerstreak@146 183 function Bag:New(name, cfg, bar, idx)
flickerstreak@146 184 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" )
flickerstreak@146 185
flickerstreak@146 186 local f = self:GetFrame()
flickerstreak@146 187
flickerstreak@146 188 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight")
flickerstreak@146 189
flickerstreak@146 190 f:RegisterEvent("CURSOR_UPDATE")
flickerstreak@151 191 f:RegisterEvent("BAG_UPDATE")
flickerstreak@146 192 f:RegisterEvent("BAG_CLOSED")
flickerstreak@146 193 f:SetScript("OnDragStart", function(frame, ...) self:OnDragStart(...) end)
flickerstreak@146 194 f:RegisterForDrag("LeftButton")
flickerstreak@146 195
flickerstreak@146 196 -- attach to skinner
flickerstreak@146 197 bar:SkinButton(self,
flickerstreak@146 198 {
flickerstreak@146 199 Icon = _G[name.."IconTexture"]
flickerstreak@146 200 }
flickerstreak@146 201 )
flickerstreak@146 202
flickerstreak@146 203 return self
flickerstreak@146 204 end
flickerstreak@146 205
flickerstreak@146 206 function Bag:GetInventorySlot()
flickerstreak@146 207 return ContainerIDToInventoryID(self:GetBagID())
flickerstreak@146 208 end
flickerstreak@146 209
flickerstreak@146 210 function Bag:GetInventorySlotName()
flickerstreak@146 211 return "Bag"..(self:GetBagID()-1).."Slot"
flickerstreak@146 212 end
flickerstreak@146 213
flickerstreak@146 214 function Bag:SetTooltip()
flickerstreak@146 215 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT")
flickerstreak@146 216 if not GameTooltip:SetInventoryItem("player", self:GetInventorySlot()) then
flickerstreak@146 217 GameTooltip:SetText(EQUIP_CONTAINER, 1.0, 1.0, 1.0)
flickerstreak@146 218 end
flickerstreak@146 219 end
flickerstreak@146 220
flickerstreak@146 221 function Bag:Update()
flickerstreak@146 222 local texture = GetInventoryItemTexture("player", self:GetInventorySlot())
flickerstreak@146 223 if texture then
flickerstreak@146 224 self.frames.icon:SetTexture(texture)
flickerstreak@146 225 self.frames.icon:Show()
flickerstreak@146 226 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot2")
flickerstreak@146 227 else
flickerstreak@146 228 local _, bgTex = GetInventorySlotInfo(self:GetInventorySlotName())
flickerstreak@146 229 self.frames.icon:SetTexture(bgTex)
flickerstreak@146 230 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot")
flickerstreak@146 231 end
flickerstreak@146 232 self:UpdateChecked()
flickerstreak@146 233 end
flickerstreak@146 234
flickerstreak@146 235 function Bag:OnClick()
flickerstreak@146 236 if IsModifiedClick("OPENALLBAGS") then
flickerstreak@146 237 OpenAllBags()
flickerstreak@146 238 else
flickerstreak@146 239 if not PutItemInBag(self:GetInventorySlot()) then
flickerstreak@146 240 ToggleBag(self:GetBagID())
flickerstreak@146 241 end
flickerstreak@146 242 end
flickerstreak@146 243 self:UpdateChecked()
flickerstreak@146 244 end
flickerstreak@146 245
flickerstreak@146 246 function Bag:OnReceiveDrag()
flickerstreak@146 247 if CursorHasItem() then
flickerstreak@146 248 PutItemInBag(self:GetInventorySlot())
flickerstreak@146 249 end
flickerstreak@146 250 end
flickerstreak@146 251
flickerstreak@146 252 function Bag:OnDragStart()
flickerstreak@146 253 PickupBagFromSlot(self:GetInventorySlot())
flickerstreak@146 254 self:Update()
flickerstreak@146 255 end
flickerstreak@146 256
flickerstreak@151 257 function Bag:BAG_UPDATE(bag)
flickerstreak@151 258 if bag == self:GetBagID() then
flickerstreak@146 259 self:Update()
flickerstreak@146 260 end
flickerstreak@146 261 end
flickerstreak@146 262
flickerstreak@146 263 function Bag:CURSOR_UPDATE()
flickerstreak@146 264 if CursorCanGoInSlot(self:GetInventorySlot()) then
flickerstreak@146 265 self:GetFrame():LockHighlight()
flickerstreak@146 266 else
flickerstreak@146 267 self:GetFrame():UnlockHighlight()
flickerstreak@146 268 end
flickerstreak@146 269 end
flickerstreak@146 270
flickerstreak@146 271 function Bag:BAG_CLOSED(bag)
flickerstreak@146 272 if bag == self:GetBagID() then
flickerstreak@146 273 self:Update()
flickerstreak@146 274 end
flickerstreak@146 275 end
flickerstreak@146 276
flickerstreak@146 277
flickerstreak@146 278 --
flickerstreak@146 279 -- Backpack Button class
flickerstreak@146 280 --
flickerstreak@146 281 function Backpack:New(name, cfg, bar, idx)
flickerstreak@146 282 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" )
flickerstreak@146 283
flickerstreak@146 284 local f = self:GetFrame()
flickerstreak@146 285 local icon = _G[name.."IconTexture"]
flickerstreak@146 286 icon:SetTexture("Interface\\Buttons\\Button-Backpack-Up")
flickerstreak@146 287 icon:Show()
flickerstreak@146 288 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight")
flickerstreak@146 289 f:RegisterEvent("PLAYER_ENTERING_WORLD");
flickerstreak@146 290 f:RegisterEvent("CVAR_UPDATE");
flickerstreak@146 291 f:SetScript("OnShow", function(frame, ...) self:OnShow(...) end)
flickerstreak@146 292
flickerstreak@146 293 -- attach to skinner
flickerstreak@146 294 bar:SkinButton(self,
flickerstreak@146 295 {
flickerstreak@146 296 Icon = _G[name.."IconTexture"]
flickerstreak@146 297 }
flickerstreak@146 298 )
flickerstreak@146 299
flickerstreak@146 300 return self
flickerstreak@146 301 end
flickerstreak@146 302
flickerstreak@146 303 function Backpack:Update()
flickerstreak@146 304 self:UpdateFreeSlots()
flickerstreak@146 305 self:UpdateChecked()
flickerstreak@146 306 end
flickerstreak@146 307
flickerstreak@146 308 function Backpack:UpdateFreeSlots()
flickerstreak@146 309 if GetCVar("displayFreeBagSlots") == "1" then
flickerstreak@146 310 local total = 0
flickerstreak@146 311 for i = BACKPACK_CONTAINER, NUM_BAG_SLOTS do
flickerstreak@146 312 local free, family = GetContainerNumFreeSlots(i)
flickerstreak@146 313 if family == 0 then
flickerstreak@146 314 total = total + free
flickerstreak@146 315 end
flickerstreak@146 316 end
flickerstreak@146 317
flickerstreak@146 318 self.freeSlots = total
flickerstreak@146 319 self.frames.count:SetText(format("(%s)", self.freeSlots))
flickerstreak@146 320 self.frames.count:Show()
flickerstreak@146 321 elseif self.frames.count:IsShown() then
flickerstreak@146 322 self.frames.count:Hide()
flickerstreak@146 323 end
flickerstreak@146 324 end
flickerstreak@146 325
flickerstreak@146 326 function Backpack:SetTooltip()
flickerstreak@146 327 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT")
flickerstreak@146 328 GameTooltip:SetText(BACKPACK_TOOLTIP, 1.0, 1.0, 1.0)
flickerstreak@146 329 GameTooltip:AddLine(string.format(NUM_FREE_SLOTS, (self.freeSlots or 0)))
flickerstreak@146 330 GameTooltip:Show();
flickerstreak@146 331 end
flickerstreak@146 332
flickerstreak@146 333 function Backpack:OnShow()
flickerstreak@146 334 self:UpdateFreeSlots()
flickerstreak@146 335 end
flickerstreak@146 336
flickerstreak@146 337 function Backpack:OnClick()
flickerstreak@146 338 if IsModifiedClick("OPENALLBAGS") then
flickerstreak@146 339 OpenAllBags()
flickerstreak@146 340 else
flickerstreak@146 341 if not PutItemInBackpack() then
flickerstreak@146 342 ToggleBackpack()
flickerstreak@146 343 end
flickerstreak@146 344 end
flickerstreak@146 345 self:UpdateChecked()
flickerstreak@146 346 end
flickerstreak@146 347
flickerstreak@146 348 function Backpack:OnReceiveDrag()
flickerstreak@146 349 if CursorHasItem() then
flickerstreak@146 350 PutItemInBackpack()
flickerstreak@146 351 end
flickerstreak@146 352 end
flickerstreak@146 353
flickerstreak@146 354 function Backpack:PLAYER_ENTERING_WORLD()
flickerstreak@146 355 self:CVAR_UPDATE("DISPLAY_FREE_BAG_SLOTS", GetCVar("displayFreeBagSlots"))
flickerstreak@146 356 end
flickerstreak@146 357
flickerstreak@146 358 function Backpack:CVAR_UPDATE( cvar, value )
flickerstreak@146 359 if cvar == "DISPLAY_FREE_BAG_SLOTS" then
flickerstreak@146 360 if value == "1" then
flickerstreak@146 361 self:GetFrame():RegisterEvent("BAG_UPDATE")
flickerstreak@146 362 else
flickerstreak@146 363 self:GetFrame():UnregisterEvent("BAG_UPDATE")
flickerstreak@146 364 end
flickerstreak@146 365 self:UpdateFreeSlots()
flickerstreak@146 366 end
flickerstreak@146 367 end
flickerstreak@146 368
flickerstreak@146 369 function Backpack:BAG_UPDATE(bag)
flickerstreak@146 370 if bag >= BACKPACK_CONTAINER and bag <= NUM_BAG_SLOTS then
flickerstreak@146 371 self:UpdateFreeSlots()
flickerstreak@146 372 end
flickerstreak@146 373 end
flickerstreak@146 374
flickerstreak@146 375
flickerstreak@146 376 --
flickerstreak@146 377 -- Keyring Button class
flickerstreak@146 378 --
flickerstreak@146 379 function Keyring:New(name, cfg, bar, idx)
flickerstreak@146 380 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" )
flickerstreak@146 381
flickerstreak@146 382 local f = self:GetFrame()
flickerstreak@146 383
flickerstreak@146 384 f:SetWidth(18)
flickerstreak@146 385 f:SetHeight(39)
flickerstreak@146 386
flickerstreak@146 387 local tex = f:GetNormalTexture()
flickerstreak@146 388 tex:ClearAllPoints()
flickerstreak@152 389 tex:SetAllPoints()
flickerstreak@146 390
flickerstreak@146 391 f:SetNormalTexture("Interface\\Buttons\\UI-Button-KeyRing")
flickerstreak@146 392 f:SetHighlightTexture("Interface\\Buttons\\UI-Button-KeyRing-Highlight")
flickerstreak@146 393 f:SetPushedTexture("Interface\\Buttons\\UI-Button-KeyRing-Down")
flickerstreak@146 394 f:GetNormalTexture():SetTexCoord(0,0.5625,0,0.609375)
flickerstreak@146 395 f:GetHighlightTexture():SetTexCoord(0,0.5625,0,0.609375)
flickerstreak@146 396 f:GetPushedTexture():SetTexCoord(0,0.5625,0,0.609375)
flickerstreak@146 397
flickerstreak@146 398 if not HasKey() then
flickerstreak@146 399 f:Hide()
flickerstreak@146 400 end
flickerstreak@146 401
flickerstreak@146 402 -- DO NOT attach to skinner
flickerstreak@146 403
flickerstreak@146 404 return self
flickerstreak@146 405 end
flickerstreak@146 406
flickerstreak@146 407 function Keyring:GetBagID()
flickerstreak@146 408 return KEYRING_CONTAINER
flickerstreak@146 409 end
flickerstreak@146 410
flickerstreak@146 411 function Keyring:Refresh()
flickerstreak@146 412 local f = self:GetFrame()
flickerstreak@146 413 self.bar:PlaceButton( self, f:GetHeight(), f:GetHeight() ) -- use height x height since it's an odd size
flickerstreak@146 414 self:UpdateHotkey()
flickerstreak@146 415 self:Update()
flickerstreak@146 416 end
flickerstreak@146 417
flickerstreak@146 418 function Keyring:SetTooltip()
flickerstreak@146 419 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_RIGHT");
flickerstreak@146 420 GameTooltip:SetText(KEYRING, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b);
flickerstreak@146 421 GameTooltip:AddLine();
flickerstreak@146 422 end
flickerstreak@146 423
flickerstreak@146 424 function Keyring:OnReceiveDrag()
flickerstreak@146 425 if CursorHasItem() then
flickerstreak@146 426 PutKeyInKeyRing()
flickerstreak@146 427 end
flickerstreak@146 428 end
flickerstreak@146 429
flickerstreak@146 430 function Keyring:OnClick()
flickerstreak@146 431 if CursorHasItem() then
flickerstreak@146 432 PutKeyInKeyRing()
flickerstreak@146 433 else
flickerstreak@146 434 ToggleKeyRing()
flickerstreak@146 435 end
flickerstreak@146 436 self:UpdateChecked()
flickerstreak@146 437 end
flickerstreak@146 438
flickerstreak@146 439 function Keyring:ShowGridTemp(show)
flickerstreak@146 440 if not HasKey() then
flickerstreak@146 441 if show then
flickerstreak@146 442 self:GetFrame():Show()
flickerstreak@146 443 else
flickerstreak@146 444 self:GetFrame():Hide()
flickerstreak@146 445 end
flickerstreak@146 446 end
flickerstreak@146 447 end
flickerstreak@146 448