Language:
Lua     Change language:
Pastebin: 128357
Author: Cargor
Subject: Re: Untitled
Created: 2009-11-12 08:55:17
Download and save
Toggle line numbers
1cargBags_Nivaya = CreateFrame('Frame', 'cargBags_Nivaya', UIParent) 
2cargBags_Nivaya:SetScript('OnEvent', function(self, event, ...) self[event](self, event, ...) end
3cargBags_Nivaya:RegisterEvent("ADDON_LOADED"
4cargBags_Nivaya:RegisterEvent("TRADE_SHOW"
5if (cargBags.Handler["Anywhere"]) then cargBags:SetActiveHandler("Anywhere") end 
6 
7local CatDropDown = CreateFrame("Frame", "CatDropDown", UIParent, "UIDropDownMenuTemplate"
8 
9--------------------------------------------- 
10--------------------------------------------- 
11local L = cBnivL 
12cB_KnownItems = {} 
13cBniv_CatInfo = {} 
14cB_Bags = {} 
15cB_BagHidden = {} 
16 
17-- Those are default values only, change them ingame via "/cbniv": 
18local optDefaults = { 
19                    NewItems = true
20                    TradeGoods = true
21                    Armor = true
22                    CoolStuff = false
23                    AmmoAlwaysHidden = false
24                    BankBlack = false
25                    scale = 0.8
26                    FilterBank = true
27                    CompressEmpty = true
28                    Unlocked = true
29                    SortBags = true
30                    SortBank = true
31                    } 
32 
33-- Those are internal settings, don't touch them at all: 
34local defaults =    { 
35                    showAmmo = false
36                    } 
37 
38local ItemSetCaption = (IsAddOnLoaded('ClosetGnome') and "ClosetGnome ") or (IsAddOnLoaded('ItemRack') and "ItemRack ") or (IsAddOnLoaded('Outfitter') and "Outfitter ") or "Item " 
39local bankOpenState = false 
40 
41function cargBags_Nivaya:LoadDefaults() 
42    cBniv = cBniv or {} 
43    for k,v in pairs(defaults) do 
44        if(type(cBniv[k]) == 'nil') then cBniv[k] = v end 
45    end 
46    cBnivCfg = cBnivCfg or {} 
47    for k,v in pairs(optDefaults) do 
48        if(type(cBnivCfg[k]) == 'nil') then cBnivCfg[k] = v end 
49    end 
50end 
51 
52function cargBags_Nivaya:ADDON_LOADED(event, addon) 
53    if (addon ~= 'cargBags_Nivaya') then return end 
54    self:UnregisterEvent(event) 
55    self:LoadDefaults() 
56    if cBnivCfg.optAmmoAlwaysHidden then cBniv.showAmmo = false end 
57    UIDropDownMenu_Initialize(CatDropDown, cargBags_Nivaya.CatDropDownInit, "MENU"
58 
59    ----------------- 
60    -- Frame Spawns 
61    ----------------- 
62 
63    -- Bank bags 
64    local t = cargBags:Spawn("cBniv_BankArmor"
65    t:SetFilter(cB_Filters.fBank, true
66    t:SetFilter(cB_Filters.fBankFilter, true
67    t:SetFilter(cB_Filters.fArmorWep, true
68    cB_Bags.bankArmor = t 
69 
70    local t = cargBags:Spawn("cBniv_BankCons"
71    t:SetFilter(cB_Filters.fBank, true
72    t:SetFilter(cB_Filters.fBankFilter, true
73    t:SetFilter(cB_Filters.fConsumables, true
74    cB_Bags.bankConsumables = t 
75 
76    local t = cargBags:Spawn("cBniv_BankQuest"
77    t:SetFilter(cB_Filters.fBank, true
78    t:SetFilter(cB_Filters.fBankFilter, true
79    t:SetFilter(cB_Filters.fQuest, true
80    cB_Bags.bankQuest = t 
81 
82    local t = cargBags:Spawn("cBniv_BankTrade"
83    t:SetFilter(cB_Filters.fBank, true
84    t:SetFilter(cB_Filters.fBankFilter, true
85    t:SetFilter(cB_Filters.fTrade, true
86    cB_Bags.bankTrade = t 
87 
88    local t = cargBags:Spawn("cBniv_Bank"
89    t:SetFilter(cB_Filters.fBank, true
90    if cBnivCfg.CompressEmpty then t:SetFilter(cB_Filters.fHideEmpty, true) end 
91    cB_Bags.bank = t 
92 
93    -- Keyring 
94    local t = cargBags:Spawn("cBniv_Keyring"
95    t:SetFilter(cB_Filters.fKeyring, true
96    t:SetFilter(cB_Filters.fHideEmpty, true
97    cB_Bags.key = t 
98 
99    -- Soul Shards frame 
100    local t = cargBags:Spawn("cBniv_Soulshards"
101    t:SetFilter(cB_Filters.fSoulShards, true
102    cB_Bags.bagSoul = t 
103 
104    -- Ammo Frame 
105    local t = cargBags:Spawn("cBniv_Ammo"
106    t:SetFilter(cB_Filters.fAmmo, true
107    cB_Bags.bagAmmo = t 
108 
109    -- Frame for Item Sets from Closetgnome, Item Rack and Outfitter 
110    local t = cargBags:Spawn("cBniv_ItemSets"
111    t:SetFilter(cB_Filters.fItemSets, true
112    t:SetFilter(cB_Filters.fHideEmpty, true
113    cB_Bags.bagItemSets = t 
114 
115    -- New Items frame 
116    local t = cargBags:Spawn("cBniv_NewItems"
117    t:SetFilter(cB_Filters.fNewItems, true
118    t:SetFilter(cB_Filters.fHideEmpty, true
119    cB_Bags.bagNew = t 
120 
121    -- Cool Stuff(tm) 
122    t = cargBags:Spawn("cBniv_Stuff"
123    t:SetFilter(cB_Filters.fStuff, true
124    t:SetFilter(cB_Filters.fHideEmpty, true
125    cB_Bags.bagStuff = t 
126 
127    -- Armor and weapon frame 
128    local t = cargBags:Spawn("cBniv_Armor"
129    t:SetFilter(cB_Filters.fArmorWep, true
130    t:SetFilter(cB_Filters.fHideEmpty, true
131    cB_Bags.armor = t 
132 
133    -- Quest frame 
134    local t = cargBags:Spawn("cBniv_Quest"
135    t:SetFilter(cB_Filters.fQuest, true
136    t:SetFilter(cB_Filters.fHideEmpty, true
137    cB_Bags.quest = t 
138 
139    -- Consumable 
140    local t = cargBags:Spawn("cBniv_Consumables"
141    t:SetFilter(cB_Filters.fConsumables, true
142    t:SetFilter(cB_Filters.fHideEmpty, true
143    cB_Bags.consumables = t 
144 
145    -- Trade Goods 
146    local t = cargBags:Spawn("cBniv_TradeGoods"
147    t:SetFilter(cB_Filters.fTrade, true
148    t:SetFilter(cB_Filters.fHideEmpty, true
149    cB_Bags.tradegoods = t 
150 
151    -- Bagpack and bags 
152    local t = cargBags:Spawn("cBniv_Bag"
153    t:SetFilter(cB_Filters.fBags, true
154    if cBnivCfg.CompressEmpty then t:SetFilter(cB_Filters.fHideEmpty, true) end 
155    cB_Bags.main = t 
156 
157    ----------------------------------------------- 
158    -- Store the anchoring order: 
159    -- read: "tar" is anchored to "src" in the direction denoted by "dir". 
160    ----------------------------------------------- 
161    local function CreateAnchorInfo(src,tar,dir) 
162        tar.AnchorTo = src 
163        tar.AnchorDir = dir 
164        if src then 
165            if not src.AnchorTargets then src.AnchorTargets = {} end 
166            src.AnchorTargets[tar] = true 
167        end 
168    end 
169 
170    -- Main Anchors: 
171    CreateAnchorInfo(nil, cB_Bags.main, "Bottom"
172    CreateAnchorInfo(nil, cB_Bags.bank, "Bottom"
173 
174    cB_Bags.main:SetPoint("BOTTOMRIGHT", -20, 150
175    cB_Bags.bank:SetPoint("LEFT", 15, 0
176 
177    -- Bank Anchors: 
178    CreateAnchorInfo(cB_Bags.bank, cB_Bags.bankArmor, "Right"
179    CreateAnchorInfo(cB_Bags.bankArmor, cB_Bags.bankTrade, "Bottom"
180 
181    CreateAnchorInfo(cB_Bags.bank, cB_Bags.bankConsumables, "Bottom"
182    CreateAnchorInfo(cB_Bags.bankConsumables, cB_Bags.bankQuest, "Bottom"
183 
184    -- Bag Anchors: 
185    CreateAnchorInfo(cB_Bags.main, cB_Bags.key, "Bottom"
186 
187    CreateAnchorInfo(cB_Bags.main, cB_Bags.bagItemSets, "Left"
188    CreateAnchorInfo(cB_Bags.bagItemSets, cB_Bags.armor, "Top"
189    CreateAnchorInfo(cB_Bags.armor, cB_Bags.bagNew, "Top"
190    CreateAnchorInfo(cB_Bags.bagNew, cB_Bags.bagSoul, "Top"
191    CreateAnchorInfo(cB_Bags.bagSoul, cB_Bags.bagAmmo, "Top"
192 
193    CreateAnchorInfo(cB_Bags.main, cB_Bags.tradegoods, "Top"
194    CreateAnchorInfo(cB_Bags.tradegoods, cB_Bags.consumables, "Top"
195    CreateAnchorInfo(cB_Bags.consumables, cB_Bags.quest, "Top"
196    CreateAnchorInfo(cB_Bags.quest, cB_Bags.bagStuff, "Top"
197 
198    -- To toggle containers when entering / leaving a bank 
199    local bankToggle = CreateFrame"Frame" 
200    bankToggle:RegisterEvent"BANKFRAME_OPENED" 
201    bankToggle:RegisterEvent"BANKFRAME_CLOSED" 
202    bankToggle:SetScript("OnEvent", function(self, event) 
203        if(event == "BANKFRAME_OPENED") then 
204            OpenCargBank() 
205            OpenCargBags() 
206            bankOpenState = true 
207        else 
208            CloseCargBank() 
209            bankOpenState = false 
210        end 
211    end
212 
213    -- Close real bank frame when our bank frame is hidden 
214    cB_Bags.bank:SetScript("OnHide", CloseBankFrame) 
215 
216    -- Hide the original bank frame 
217    BankFrame:UnregisterAllEvents() 
218 
219    -- Blizzard Replacement Functions 
220    ToggleBackpack = ToggleCargBags 
221    ToggleBag = function() ToggleCargBags() end 
222    OpenAllBags = ToggleCargBags 
223    CloseAllBags = CloseCargBags 
224    OpenBackpack = OpenCargBags 
225    CloseBackpack = CloseCargBags 
226end 
227 
228function cargBags_Nivaya:TRADE_SHOW(event) 
229    OpenCargBags() 
230end 
231 
232local resetNewItems = function(self) 
233    cB_KnownItems = {} 
234    for i = 0,4 do 
235        local tNumSlots = self:GetHandler().GetContainerNumSlots(i) 
236        if tNumSlots > 0 then for j = 1,tNumSlots do 
237            local tLink = GetContainerItemLink(i,j) 
238            if tLink then 
239                local tName = GetItemInfo(tLink) 
240                local _,tStackCount = GetContainerItemInfo(i,j) 
241                if cB_KnownItems[tName] then 
242                    cB_KnownItems[tName] = cB_KnownItems[tName] + tStackCount 
243                else 
244                    cB_KnownItems[tName] = tStackCount 
245                end 
246            end 
247        end end 
248    end 
249    cargBags:UpdateBags() 
250end 
251 
252local function ShowBag(bag) 
253    if not cB_BagHidden[bag.Name] then bag:Show() end 
254end 
255 
256-- This function is only used inside the layout, so the cargBags-core doesn't care about it 
257-- It creates the border for glowing process in UpdateButton() 
258local createGlow = function(button, rarity) 
259    local glow = button:CreateTexture(nil, "OVERLAY"
260    glow:SetTexture("Interface\\Buttons\\UI-ActionButton-Border"
261    glow:SetBlendMode("ADD"
262    glow:SetAlpha((rarity > 1) and .6 or .4
263    glow:SetWidth(65
264    glow:SetHeight(65
265    glow:SetPoint("CENTER", button) 
266    button.Glow = glow 
267end 
268 
269-- The main function for updating an item button, 
270-- the item-table holds all data known about the item the button is holding, e.g. 
271--   bagID, slotID, texture, count, locked, quality - from GetContainerItemInfo() 
272--   link - well, from GetContainerItemLink() ofcourse ;) 
273--   name, link, rarity, level, minLevel, type, subType, stackCount, equipLoc - from GetItemInfo() 
274-- if you need cooldown item data, use self:RequestCooldownData() 
275local UpdateButton = function(self, button, item) 
276    button.Icon:SetTexture(item.texture) 
277    if IsAddOnLoaded("Tabard-O-Matic") then 
278        local slot = button:GetID() 
279        local link = item.link 
280        if (link) then 
281            local itemID = tonumber(link:match("item:(%d+)")) 
282            local TabardValue = TabardTextures[itemID] 
283            if TabardValue then Tabard_O_Matic:SetTheButtons(button, TabardValue.ItemName) end 
284        end 
285    end 
286    SetItemButtonCount(button, item.count) 
287    SetItemButtonDesaturated(button, item.locked, 0.5, 0.5, 0.5
288 
289    -- create minLevel text 
290    if(not button.levelText) then 
291        local text = button:CreateFontString(nil, "OVERLAY"
292        local font, _, flags = NumberFontNormal:GetFont() 
293        text:SetFont(font, 14, flags) -- Second argument is the font size 
294        text:SetPoint("TOPRIGHT", -5, -5) -- corner, xOffset, yOffset 
295        button.levelText = text 
296    end 
297 
298    -- Updating minLevel text 
299    if(item.minLevel and item.minLevel > 0) then 
300        if(item.minLevel > UnitLevel("player")) then 
301            button.levelText:SetTextColor(1, 0, 0, 1) -- red, green, blue, alpha 
302        else 
303            button.levelText:SetTextColor(0, 1, 0, 1
304        end 
305        button.levelText:SetText(item.minLevel) 
306        button.levelText:Show() 
307    else 
308        button.levelText:Hide() 
309    end 
310 
311    -- Color the button's border based on the item's rarity / quality! 
312    if(item.rarity and item.rarity >= 1) then 
313        if(not button.Glow) then createGlow(button, item.rarity) end 
314        button.Glow:SetVertexColor(GetItemQualityColor(item.rarity)) 
315        button.Glow:Show() 
316    else 
317        if(button.Glow) then button.Glow:Hide() end 
318    end 
319end 
320 
321-- Updates if the item is locked (currently moved by user) 
322--   bagID, slotID, texture, count, locked, quality - from GetContainerItemInfo() 
323-- if you need all item data, use self:RequestItemData() 
324local UpdateButtonLock = function(self, button, item) 
325    SetItemButtonDesaturated(button, item.locked, 0.5, 0.5, 0.5
326end 
327 
328-- Updates the item's cooldown 
329--   cdStart, cdFinish, cdEnable - from GetContainerItemCooldown() 
330-- if you need all item data, use self:RequestItemData() 
331local UpdateButtonCooldown = function(self, button, item) 
332    if(button.Cooldown) then 
333        CooldownFrame_SetTimer(button.Cooldown, item.cdStart, item.cdFinish, item.cdEnable) 
334    end 
335end 
336 
337local GetNumFreeSlots = function(bagType) 
338    local free, max = 0, 0 
339    if bagType == "bag" then 
340        for i = 0,4 do 
341            free = free + cargBags:GetHandler().GetContainerNumFreeSlots(i) 
342            max = max + cargBags:GetHandler().GetContainerNumSlots(i) 
343        end 
344    else 
345        local containerIDs = {-1,5,6,7,8,9,10,11} 
346        for _,i in next, containerIDs do 
347            free = free + cargBags:GetHandler().GetContainerNumFreeSlots(i) 
348            max = max + cargBags:GetHandler().GetContainerNumSlots(i) 
349        end 
350    end 
351    return free, max 
352end 
353 
354local GetFirstFreeSlot = function(bagtype) 
355    if bagtype == "bag" then 
356        for i = 0,4 do 
357            local t = cargBags:GetHandler().GetContainerNumFreeSlots(i) 
358            if t > 0 then 
359                local tNumSlots = cargBags:GetHandler().GetContainerNumSlots(i) 
360                for j = 1,tNumSlots do 
361                    local tLink = GetContainerItemLink(i,j) 
362                    if not tLink then return i,j end 
363                end 
364            end 
365        end 
366    else 
367        local containerIDs = {-1,5,6,7,8,9,10,11} 
368        for _,i in next, containerIDs do 
369            local t = cargBags:GetHandler().GetContainerNumFreeSlots(i) 
370            if t > 0 then 
371                local tNumSlots = cargBags:GetHandler().GetContainerNumSlots(i) 
372                for j = 1,tNumSlots do 
373                    local tLink = GetContainerItemLink(i,j) 
374                    if not tLink then return i,j end 
375                end 
376            end 
377        end 
378    end 
379    return false 
380end 
381 
382local function QuickSort(tTable) 
383    local function QS_int(left,right,t,c) 
384        if left < right then 
385            local l, r = left, right 
386            local pe = t[floor((l+r)/2)][c] -- pivot element 
387            while l < r do 
388                while t[l][c] > pe do l = l + 1 end 
389                while t[r][c] < pe do r = r - 1 end 
390                if l <= r then 
391                    local tItem = t[l] 
392                    t[l] = t[r] 
393                    t[r] = tItem 
394                    l = l + 1 
395                    r = r - 1 
396                end 
397            end 
398            if left < r then QS_int(left, r, t, c) end 
399            if right > l then QS_int(l, right, t, c) end 
400        end 
401    end 
402 
403    local function tcount(tTable) 
404        local n = #tTable 
405        if (n == 0) then for _ in pairs(tTable) do n = n + 1 end end 
406        return
407    end 
408 
409    -- sort by quality first: 
410    local tNum = tcount(tTable) 
411    QS_int(1, tNum, tTable, 2
412 
413    -- then sort by itemID: 
414    local s, tQ = 1, 0 
415    if tTable[1] then tQ = tTable[1][2] end 
416    for e,v in ipairs(tTable) do 
417        if (v[2] ~= tQ) or (e >= tNum) then 
418            local b = (e >= tNum) and (v[2] == tQ) 
419            QS_int(s, b and e or e-1, tTable, 1
420            s, tQ = e, v[2
421        end 
422    end 
423 
424    return tTable 
425--    local tAr = {} 
426    --for i,v in ipairs(tTable) do tAr[i] = v[3] end 
427    --return tAr 
428end 
429 
430-- The function for positioning the item buttons in the bag object 
431local UpdateButtonPositions = function(self) 
432    local col, row = 0, 0 
433    local yPosOffs = self.Caption and 20 or 0 
434    local isEmpty = true 
435 
436    local tName = self.Name 
437    local tBankBags = string.find(tName, "cBniv_Bank%a+"
438    local tBank = tBankBags or (tName == "cBniv_Bank"
439 
440    local buttonIDs = {} 
441    for i, button in self:IterateButtons() do 
442        local tLink = GetContainerItemLink(button.bagID, button.slotID) 
443        if tLink then 
444            local _,_,tStr = string.find(tLink, "^|c%x+|H(.+)|h%[.*%]"
445            local _,tID = strsplit(":", tStr) 
446            local _,_,tQ = GetItemInfo(tLink) 
447            buttonIDs[i] = { tonumber(tID), tQ, button } 
448        else 
449            buttonIDs[i] = { 0, -2, button } 
450        end 
451    end 
452    if (tBank and cBnivCfg.SortBank) or (not tBank and cBnivCfg.SortBags) then buttonIDs = QuickSort(buttonIDs) end 
453 
454    for _,v in ipairs(buttonIDs) do 
455        local button = v[3
456        button:ClearAllPoints() 
457 
458        local xPos = col * 38 
459        local yPos = (-1 * row * 38) - yPosOffs 
460 
461        button:SetPoint("TOPLEFT", self, "TOPLEFT", xPos, yPos) 
462        if(col >= self.Columns-1) then 
463            col = 0 
464            row = row + 1 
465        else 
466            col = col + 1 
467        end 
468        isEmpty = false 
469    end 
470 
471    if cBnivCfg.CompressEmpty then 
472        local xPos = col * 38 
473        local yPos = (-1 * row * 38) - yPosOffs 
474 
475        local tDrop = self.DropTarget 
476        if tDrop then 
477            tDrop:ClearAllPoints() 
478            tDrop:SetPoint("TOPLEFT", self, "TOPLEFT", xPos, yPos) 
479            if(col >= self.Columns-1) then 
480                col = 0 
481                row = row + 1 
482            else 
483                col = col + 1 
484            end 
485        end 
486 
487        cB_Bags.main.EmptySlotCounter:SetText(GetNumFreeSlots("bag")) 
488        cB_Bags.bank.EmptySlotCounter:SetText(GetNumFreeSlots("bank")) 
489    end 
490 
491    -- This variable stores the size of the item button container 
492    self.ContainerHeight = (row + (col>0 and 1 or 0)) * 38 
493 
494    if (self.UpdateDimensions) then self:UpdateDimensions() end -- Update the bag's height 
495    local t = (tName == "cBniv_Bag") or (tName == "cBniv_Bank") or (tName == "cBniv_Keyring"
496    if (not tBankBags and cB_Bags.main:IsShown() and not t) or (tBankBags and cB_Bags.bank:IsShown()) then 
497        if isEmpty then self:Hide() else self:Show() end 
498    end 
499 
500    cB_BagHidden[tName] = (not t) and isEmpty or false 
501    cargBags_Nivaya:UpdateAnchors(self) 
502    collectgarbage("collect"
503end 
504 
505-- Function is called after a button was added to an object 
506-- We color the borders of the button to see if it is an ammo bag or else 
507-- Please note that the buttons are in most cases recycled and not new created 
508local PostAddButton = function(self, button, bag) 
509    if(not button.NormalTexture) then return end 
510 
511    local bagType = cargBags.Bags[button.bagID].bagType 
512    if(button.bagID == KEYRING_CONTAINER) then 
513        button.NormalTexture:SetVertexColor(1, 0.7, 0)        -- Key ring 
514    elseif(bagType and bagType > 0 and bagType < 8) then 
515        button.NormalTexture:SetVertexColor(1, 1, 0)      -- Ammo bag 
516    elseif(bagType and bagType > 4) then 
517        button.NormalTexture:SetVertexColor(0, 1, 0)      -- Profession bags 
518    else 
519        button.NormalTexture:SetVertexColor(1, 1, 1)      -- Normal bags 
520    end 
521 
522    button:SetScript('OnMouseUp', function(self, mouseButton) 
523        if (mouseButton == 'RightButton') and (IsAltKeyDown()) then 
524            local tLink = GetContainerItemLink(button.bagID, button.slotID) 
525            if tLink then 
526                local tName = GetItemInfo(tLink) 
527                CatDropDown.item = tName 
528                ToggleDropDownMenu(1, nil, CatDropDown, button, 0, 0
529            end 
530        end 
531    end
532end 
533 
534local function PostSetPlayer() OpenCargBank() end 
535 
536-- More slot buttons -> more space! 
537local UpdateDimensions = function(self) 
538    local height = 0           -- Normal margin space 
539    if self.BagBar and self.BagBar:IsShown() then 
540        height = height + 43  -- Bag button space 
541    end 
542    if self.Space then 
543        height = height + 16  -- additional info display space 
544    end 
545    if self.Money or self.bagToggle then 
546        height = height + 24 
547    end 
548    if self.Caption then       -- Space for captions 
549        height = height + 20 
550    end 
551    self:SetHeight(self.ContainerHeight + height) 
552end 
553 
554local function createTextButton(name, parent, width, height) 
555    local button = CreateFrame("Button", nil, parent) 
556    button:SetNormalFontObject(GameFontHighlightSmall) 
557    button:SetText(name) 
558    button:SetWidth(width) 
559    button:SetHeight(height) 
560    button:SetScript("OnEnter", buttonEnter) 
561    button:SetScript("OnLeave", buttonLeave) 
562    button:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square", "ADD"
563    button:SetBackdrop({ 
564        bgFile = "Interface\\Tooltips\\UI-Tooltip-Background"
565        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border"
566        tile = true, tileSize = 16, edgeSize = 16
567        insets = {left = 4, right = 4, top = 4, bottom = 4}
568    }
569    button:SetBackdropColor(.3, .3, .3, .9
570    button:SetBackdropBorderColor(.5, .5, .5, .7
571    return button 
572end 
573 
574local SetFrameMovable = function(f, v) 
575    f:SetMovable(true
576    f:SetUserPlaced(true
577    f:RegisterForClicks("LeftButton", "RightButton"
578    if v then 
579        f:SetScript("OnMouseDown", function() 
580            f:ClearAllPoints() 
581            f:StartMoving() 
582        end
583        f:SetScript("OnMouseUp",  f.StopMovingOrSizing) 
584    else 
585        f:SetScript("OnMouseDown", nil
586        f:SetScript("OnMouseUp", nil
587    end 
588end 
589 
590-- Style of the bag and its contents 
591local func = function(settings, self, type
592    local tName = self.Name 
593    local tBag, tBank, tKey = (tName == "cBniv_Bag"), (tName == "cBniv_Bank"), (tName == "cBniv_Keyring"
594    local tBankBags = string.find(tName, "cBniv_Bank"
595    self:EnableMouse(true
596 
597    self.UpdateDimensions = UpdateDimensions 
598    self.UpdateButtonPositions = UpdateButtonPositions 
599    self.UpdateButton = UpdateButton 
600    self.UpdateButtonLock = UpdateButtonLock 
601    self.UpdateButtonCooldown = UpdateButtonCooldown 
602    self.PostAddButton = PostAddButton 
603    cargBags:GetHandler().PostSetPlayer = PostSetPlayer 
604 
605    self:SetFrameStrata("HIGH"
606    tinsert(UISpecialFrames, self:GetName()) -- Close on "Esc" 
607 
608    if tBag or tBank then 
609        SetFrameMovable(self, cBnivCfg.Unlocked) 
610    end 
611 
612    self.Columns = (tBank and not cBnivCfg.FilterBank) and 14 or 8 
613    self.ContainerHeight = 0 
614    self:UpdateDimensions() 
615    self:SetWidth(38*self.Columns) 
616 
617    -- The frame background 
618    local tBankCustom = (tBankBags and not cBnivCfg.BankBlack) 
619    local color_rb = 0 
620    local color_gb = tBankCustom and .4 or 0 
621    local color_bb = tBankCustom and .6 or 0 
622    local alpha_fb = .9 
623 
624    local background = CreateFrame("Frame", nil, self) 
625    background:SetBackdrop{ 
626        bgFile = "Interface\\Tooltips\\UI-Tooltip-Background"
627        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border"
628        tile = true, tileSize = 16, edgeSize = 16
629        insets = {left = 4, right = 4, top = 4, bottom = 4}
630    } 
631    background:SetFrameStrata("HIGH"
632    background:SetFrameLevel(1
633    background:SetBackdropColor(color_rb,color_gb,color_bb,alpha_fb) 
634    background:SetBackdropBorderColor(color_rb,color_gb,color_bb,1
635 
636    background:SetPoint("TOPLEFT", -6, 6
637    background:SetPoint("BOTTOMRIGHT", 6, -6
638 
639    -- Caption and close button 
640    local caption = background:CreateFontString(background, "OVERLAY", "GameFontNormal"
641    if(caption) then 
642        local t = L.bagCaptions[self.Name] 
643        if not t then t = self.Name end 
644        if self.Name == "cBniv_ItemSets" then t=ItemSetCaption..t end 
645        caption:SetText(t) 
646        caption:SetPoint("TOPLEFT", 8, -6
647        self.Caption = caption 
648 
649        if tBag or tBank then 
650            local close = CreateFrame("Button", nil, self, "UIPanelCloseButton"
651            close:SetPoint("TOPRIGHT", 5, 8
652            if tBank then 
653                close:SetScript("OnClick", function(self) self:GetParent():Hide() end
654            else 
655                close:SetScript("OnClick", function(self) CloseCargBags() end
656            end 
657        end 
658    end 
659 
660    local tBtnOffs = 0 
661    if tBag or tBank then 
662        -- The money display 
663        if tBag then 
664            local money = self:SpawnPlugin("Money"
665            if money then money:SetPoint("BOTTOMLEFT", self, 0, 4) end 
666            self.anywhere = self:SpawnPlugin("Anywhere"
667            if self.anywhere then 
668                self.anywhere:SetPoint("TOPRIGHT", self, -20, 4
669                tBtnOffs = 20 
670            end 
671        end 
672 
673         -- Bag bar for changing bags 
674        local bagType = tBag and "bags" or "bank" 
675        local bagButtons = self:SpawnPlugin("BagBar", bagType) 
676        if(bagButtons) then 
677            bagButtons:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", 0, 23
678            bagButtons:Hide() 
679 
680            -- main window gets a fake bag button for toggling key ring 
681            if tBag then 
682                local keytoggle = bagButtons:CreateKeyRingButton() 
683                keytoggle:SetScript("OnClick", function() 
684                    if cBniv_Keyring:IsShown() then 
685                        cBniv_Keyring:Hide() 
686                        keytoggle:SetChecked(0
687                    else 
688                        cBniv_Keyring:Show() 
689                        keytoggle:SetChecked(1
690                    end 
691                end
692            end 
693        end 
694        self.bagButtons = bagButtons 
695 
696        -- We don't need the bag bar every time, so let's create a toggle button for them to show 
697        self.bagToggle = createTextButton("Bags", self, 40, 20
698        self.bagToggle:SetPoint("BOTTOMRIGHT", self,"BOTTOMRIGHT",0,0
699        self.bagToggle:SetScript("OnClick", function() 
700            if(self.BagBar:IsShown()) then self.BagBar:Hide() else self.BagBar:Show() end 
701            self:UpdateDimensions() 
702        end
703 
704        -- Button to reset new items: 
705        if tBag and cBnivCfg.NewItems then 
706            local resetBtn = createTextButton("Reset New", self, 70, 20
707            resetBtn:SetPoint("TOPRIGHT", self, "TOPRIGHT", -22-tBtnOffs, 2
708            resetBtn:SetScript("OnClick", function() resetNewItems(self) end
709        end 
710 
711        if tBag then 
712            -- Button to toggle Ammo/Shard bag: 
713            local AmmoBtn = createTextButton("A/S", self, 35, 20
714            AmmoBtn:SetPoint("BOTTOMRIGHT", self.bagToggle, "BOTTOMLEFT", 0, 0
715            AmmoBtn:SetScript("OnClick", function() 
716                if not cBniv.showAmmo then 
717                    cBniv.showAmmo = true 
718                    ShowBag(cB_Bags.bagSoul) 
719                    ShowBag(cB_Bags.bagAmmo) 
720                else 
721                    cBniv.showAmmo = false 
722                    cB_Bags.bagSoul:Hide() 
723                    cB_Bags.bagAmmo:Hide() 
724                end 
725            end
726 
727            -- Button to open bank when Anywhere is active: 
728            if self.anywhere then 
729                local BankBtn = createTextButton("Bank", self, 45, 20
730                BankBtn:SetPoint("RIGHT", AmmoBtn, "LEFT"
731                BankBtn:SetScript("OnClick", function() ToggleCargBank() end
732            end 
733        end 
734    end 
735 
736    -- For purchasing bank slots 
737    if tBank then 
738        local purchase = self:SpawnPlugin("Purchase"
739        if(purchase) then 
740            purchase:SetText(BANKSLOTPURCHASE) 
741            purchase:SetPoint("TOPLEFT", self.bagButtons, "BOTTOMLEFT", 0, -2
742            if(self.BagBar) then purchase:SetParent(self.BagBar) end 
743 
744            purchase.Cost = self:SpawnPlugin("Money", "static"
745            purchase.Cost:SetParent(purchase) 
746            purchase.Cost:SetPoint("LEFT", purchase, "RIGHT", 4, 0
747        end 
748    end 
749 
750    -- Item drop target 
751    if cBnivCfg.CompressEmpty and (tBag or tBank) then 
752        self.DropTarget = CreateFrame("Button", nil, self, "ItemButtonTemplate"
753        self.DropTarget:SetWidth(37
754        self.DropTarget:SetHeight(37
755 
756        local DropTargetProcessItem = function() 
757            if CursorHasItem() then 
758                local bID, sID = GetFirstFreeSlot(tBag and "bag" or "bank"
759                if bID then PickupContainerItem(bID, sID) end 
760            end 
761        end 
762        self.DropTarget:SetScript("OnMouseUp", DropTargetProcessItem) 
763        self.DropTarget:SetScript("OnReceiveDrag", DropTargetProcessItem) 
764 
765        local fs = self:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall"
766        fs:SetShadowColor(0,0,0
767        fs:SetShadowOffset(0.8, -0.8
768        fs:SetTextColor(1,1,1
769        fs:SetJustifyH("LEFT"
770        fs:SetPoint("BOTTOMRIGHT", self.DropTarget, "BOTTOMRIGHT", -5, 3
771        self.EmptySlotCounter = fs 
772    end 
773 
774    self:SetScale(cBnivCfg.scale) 
775    return self 
776end 
777 
778cargBags:RegisterStyle("Nivaya", setmetatable({}, {__call = func})) 
779 
780-- Opening / Closing Functions 
781function cargBags_Nivaya:UpdateAnchors(self) 
782    if not self.AnchorTargets then return end 
783    for v,_ in pairs(self.AnchorTargets) do 
784        local t, u = v.AnchorTo, v.AnchorDir 
785        if t then 
786            local h = cB_BagHidden[t.Name] 
787            v:ClearAllPoints() 
788            if      not h   and u == "Top"      then v:SetPoint("BOTTOM", t, "TOP", 0, 15
789            elseif  h       and u == "Top"      then v:SetPoint("BOTTOM", t, "BOTTOM"
790            elseif  not h   and u == "Bottom"   then v:SetPoint("TOP", t, "BOTTOM", 0, -15
791            elseif  h       and u == "Bottom"   then v:SetPoint("TOP", t, "TOP"
792            elseif u == "Left" then v:SetPoint("BOTTOMRIGHT", t, "BOTTOMLEFT", -15, 0
793            elseif u == "Right" then v:SetPoint("TOPLEFT", t, "TOPRIGHT", 15, 0) end 
794        end 
795    end 
796end 
797 
798function OpenCargBags() 
799    cB_Bags.main:Show() 
800    ShowBag(cB_Bags.armor) 
801    ShowBag(cB_Bags.bagNew) 
802    ShowBag(cB_Bags.bagItemSets) 
803    ShowBag(cB_Bags.quest) 
804    ShowBag(cB_Bags.consumables) 
805    ShowBag(cB_Bags.tradegoods) 
806    ShowBag(cB_Bags.bagStuff) 
807    if cBniv.showAmmo and not cBnivCfg.AmmoAlwaysHidden then 
808        ShowBag(cB_Bags.bagSoul) 
809        ShowBag(cB_Bags.bagAmmo) 
810    end 
811end 
812 
813function CloseCargBags() 
814    cB_Bags.main:Hide() 
815    cB_Bags.armor:Hide() 
816    cB_Bags.bagNew:Hide() 
817    cB_Bags.bagItemSets:Hide() 
818    cB_Bags.quest:Hide() 
819    cB_Bags.consumables:Hide() 
820    cB_Bags.tradegoods:Hide() 
821    cB_Bags.bagStuff:Hide() 
822    cB_Bags.key:Hide() 
823    cB_Bags.bagSoul:Hide() 
824    cB_Bags.bagAmmo:Hide() 
825    if cBnivCfg.AmmoAlwaysHidden then cBniv.showAmmo = false end 
826end 
827 
828function ToggleCargBags(forceopen) 
829    if (cB_Bags.main:IsShown() and not forceopen) then CloseCargBags() else OpenCargBags() end 
830end 
831 
832function OpenCargBank() 
833    cB_Bags.bank:Show() 
834    ShowBag(cB_Bags.bankArmor) 
835    ShowBag(cB_Bags.bankQuest) 
836    ShowBag(cB_Bags.bankTrade) 
837    ShowBag(cB_Bags.bankConsumables) 
838end 
839 
840function CloseCargBank() 
841    cB_Bags.bank:Hide() 
842    cB_Bags.bankArmor:Hide() 
843    cB_Bags.bankQuest:Hide() 
844    cB_Bags.bankTrade:Hide() 
845    cB_Bags.bankConsumables:Hide() 
846end 
847 
848function ToggleCargBank() 
849    if cB_Bags.bank:IsShown() then CloseCargBank() else OpenCargBank() end 
850end 
851 
852local function StatusMsg(str1, str2, data, name, short) 
853    local R,G,t = '|cFFFF0000', '|cFF00FF00', '' 
854    if (data ~= nil) then t = data and G..(short and 'on|r' or 'enabled|r') or R..(short and 'off|r' or 'disabled|r') end 
855    if name then t = '|cFFFFFF00cargBags_Nivaya:|r '..str1..t..str2 else t = str1..t..str2 end 
856    ChatFrame1:AddMessage(t) 
857end 
858 
859local function StatusMsgVal(str1, str2, data, name) 
860    local G,t = '|cFF00FF00', '' 
861    if (data ~= nil) then t = G..data..'|r' end 
862    if name then t = '|cFFFFFF00cargBags_Nivaya:|r '..str1..t..str2 else t = str1..t..str2 end 
863    ChatFrame1:AddMessage(t) 
864end 
865 
866local function HandleSlash(str) 
867    local str,str2 = strsplit(" ", str) 
868    if str == 'new' then 
869        cBnivCfg.NewItems = not cBnivCfg.NewItems 
870        cargBags:UpdateBags() 
871        StatusMsg('The "New Items" filter is now ', '.', cBnivCfg.NewItems, true, false
872    elseif str == 'trade' then 
873        cBnivCfg.TradeGoods = not cBnivCfg.TradeGoods 
874        cargBags:UpdateBags() 
875        StatusMsg('The "Trade Goods" filter is now ', '.', cBnivCfg.TradeGoods, true, false
876    elseif str == 'armor' then 
877        cBnivCfg.Armor = not cBnivCfg.Armor 
878        cargBags:UpdateBags() 
879        StatusMsg('The "Armor and Weapons" filter is now ', '.', cBnivCfg.Armor, true, false
880    elseif str == 'cool' then 
881        cBnivCfg.CoolStuff = not cBnivCfg.CoolStuff 
882        cargBags:UpdateBags() 
883        StatusMsg('The "Cool Stuff" filter is now ', '.', cBnivCfg.CoolStuff, true, false
884    elseif str == 'ammo' then 
885        cBnivCfg.AmmoAlwaysHidden = not cBnivCfg.AmmoAlwaysHidden 
886        StatusMsg('Hiding Ammo/Shard bags by default is now ', '.', cBnivCfg.AmmoAlwaysHidden, true, false
887    elseif str == 'bankbg' then 
888        cBnivCfg.BankBlack = not cBnivCfg.BankBlack 
889        StatusMsg('Black background color for the bank is now ', '. Reload your UI for this change to take effect!', cBnivCfg.BankBlack, true, false
890    elseif str == 'bankfilter' then 
891        cBnivCfg.FilterBank = not cBnivCfg.FilterBank 
892        StatusMsg('Bank filtering is now ', '. Reload your UI for this change to take effect!', cBnivCfg.FilterBank, true, false
893    elseif str == 'empty' then 
894        cBnivCfg.CompressEmpty = not cBnivCfg.CompressEmpty 
895        StatusMsg('Empty bagspace compression is now ', '. Reload your UI for this change to take effect!', cBnivCfg.CompressEmpty, true, false
896    elseif str == 'unlock' then 
897        cBnivCfg.Unlocked = not cBnivCfg.Unlocked 
898        SetFrameMovable(cB_Bags.main, cBnivCfg.Unlocked) 
899        SetFrameMovable(cB_Bags.bank, cBnivCfg.Unlocked) 
900        StatusMsg('Movable bags are now ', '.', cBnivCfg.Unlocked, true, false
901    elseif str == 'sortbags' then 
902        cBnivCfg.SortBags = not cBnivCfg.SortBags 
903        StatusMsg('Auto sorting bags is now ', '. Reload your UI for this change to take effect!', cBnivCfg.SortBags, true, false
904    elseif str == 'sortbank' then 
905        cBnivCfg.SortBank = not cBnivCfg.SortBank 
906        StatusMsg('Auto sorting bank is now ', '. Reload your UI for this change to take effect!', cBnivCfg.SortBank, true, false
907    elseif str == 'scale' then 
908        local t = tonumber(str2) 
909        if t then 
910            cBnivCfg.scale = t 
911            for _,v in pairs(cB_Bags) do v:SetScale(cBnivCfg.scale) end 
912            StatusMsgVal('Overall scale has been set to ', '.', cBnivCfg.scale, true
913        else 
914            StatusMsg('You have to specify a value, e.g. /cbniv scale 0.8.', '', nil, true, false
915        end 
916    else 
917        ChatFrame1:AddMessage('|cFFFFFF00cargBags_Nivaya:|r'
918        StatusMsg('(', ') unlock - Toggle unlocked status.', cBnivCfg.Unlocked, false, true
919        StatusMsg('(', ') new - Toggle the "New Items" filter.', cBnivCfg.NewItems, false, true
920        StatusMsg('(', ') trade - Toggle the "Trade Goods" filter .', cBnivCfg.TradeGoods, false, true
921        StatusMsg('(', ') armor - Toggle the "Armor and Weapons" filter .', cBnivCfg.Armor, false, true
922        StatusMsg('(', ') cool - Toggle the "Cool Stuff" filter.', cBnivCfg.CoolStuff, false, true
923        StatusMsg('(', ') ammo - Toggle Hiding Ammo/Shard bags by default.', cBnivCfg.AmmoAlwaysHidden, false, true
924        StatusMsg('(', ') bankbg - Toggle black bank background color.', cBnivCfg.BankBlack, false, true
925        StatusMsg('(', ') bankfilter - Toggle bank filtering.', cBnivCfg.FilterBank, false, true
926        StatusMsg('(', ') empty - Toggle empty bagspace compression.', cBnivCfg.CompressEmpty, false, true
927        StatusMsg('(', ') sortbags - Toggle auto sorting the bags.', cBnivCfg.SortBags, false, true
928        StatusMsg('(', ') sortbank - Toggle auto sorting the bank.', cBnivCfg.SortBank, false, true
929        StatusMsgVal('(', ') scale [number] - Set the overall scale.', cBnivCfg.scale, false
930    end 
931end 
932 
933SLASH_CBNIV1 = '/cbniv' 
934SlashCmdList.CBNIV = HandleSlash 
935 
936function cargBags_Nivaya:CatDropDownInit() 
937    level = 1 
938    local info = UIDropDownMenu_CreateInfo() 
939 
940    local function AddInfoItem(caption) 
941        local t = L.bagCaptions[caption] 
942        info.text = t and t or caption 
943        info.value = caption 
944        info.func = function() cargBags_Nivaya:CatDropDownOnClick() end 
945        info.owner = this:GetParent() 
946        UIDropDownMenu_AddButton(info, level) 
947    end 
948 
949    AddInfoItem("cBniv_Armor"
950    AddInfoItem("cBniv_Consumables"
951    AddInfoItem("cBniv_Quest"
952    AddInfoItem("cBniv_TradeGoods"
953    AddInfoItem("cBniv_Stuff"
954    AddInfoItem("cBniv_ItemSets"
955    AddInfoItem("cBniv_Ammo"
956    AddInfoItem("cBniv_Bag"
957end 
958 
959function cargBags_Nivaya:CatDropDownOnClick() 
960    local value = this.value 
961    local item = CatDropDown.item 
962    cBniv_CatInfo[item] = value 
963    cargBags:UpdateBags() 
964end 
Download and save
Toggle line numbers
Thread:
[128356] Untitled by Cargor at 2009-11-12 08:54:08 (diff)
  [128357] Re: Untitled by Cargor at 2009-11-12 08:55:17
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.