Advertisement
Guest User

Untitled

a guest
Apr 17th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.93 KB | None | 0 0
  1. function BagBuddy_OnLoad(self)
  2.     UIPanelWindows["BagBuddy"] = {
  3.         area = "left",
  4.         pushable = 1,
  5.         whileDead = 1,
  6.     }
  7.     -- Add engines icon
  8.     SetPortraitToTexture(self.portrait, "Interface\\Icons\\INV_Misc_EngGizmos_30")
  9.     -- Create the item slots
  10.     self.items = { }
  11.     for idx = 1, 24 do
  12.         local item = CreateFrame("Button", "BagBuddy_Item" .. idx, self,
  13.                         "BagBuddyItemTemplate")
  14.         self.items[idx] = item
  15.         -- Only the position of the first slot has to be calculated. The rest can just be anchored to the preceding s
  16.         -- Except when the row is finished
  17.         if idx == 1 then
  18.             item:SetPoint("TOPLEFT", 40, -73)
  19.         elseif idx == 7 or idx == 13 or idx == 19 then
  20.             item:SetPoint("TOPLEFT", self.items[idx-6], "BOTTOMLEFT", 0, -7)
  21.         else
  22.             item:SetPoint("TOPLEFT", self.items[idx-1], "TOPRIGHT", 12, 0)
  23.         end
  24.     end
  25.  
  26.     -- Create the filter buttons. Use Gem Pearls, as they are easy to be colored (in this case, with the different qu
  27.     self.filters = {}
  28.     for idx=0,5 do
  29.         local button = CreateFrame("CheckButton", "BagBuddy_Filter" .. idx, self, "BagBuddyFilterTemplate")
  30.                          SetItemButtonTexture(button, "Interface\\ICONS\\INV_Misc_Gem_Pearl_03")
  31.         self.filters[idx] = button
  32.         if idx == 0 then
  33.             button:SetPoint("BOTTOMLEFT", 40, 200)
  34.         else
  35.             button:SetPoint("TOPLEFT", self.filters[idx-1], "TOPRIGHT", 12, 0)
  36.         end
  37.         button.icon:SetVertexColor(GetItemQualityColor(idx))
  38.         button:SetChecked(false)
  39.         button.quality = idx
  40.         button.glow:Hide()
  41.     end
  42.     -- The API that occasionally returns -1 rarity for some items
  43.     self.filters[-1] = self.filters[0]
  44.     -- Initialize to show the first page
  45.     self.page = 1
  46.     self:RegisterEvent("ADDON_LOADED")
  47. end
  48.  
  49.  
  50.  
  51. -- Sorting function to sort items according to their name
  52. local function itemNameSort(a, b)
  53.     return a.name < b.name
  54. end
  55.  
  56. local function itemTimeNameSort(a, b)
  57.     -- If the two items were looted at the same time
  58.     local aTime = BagBuddy.itemTimes[a.num]
  59.     local bTime = BagBuddy.itemTimes[b.num]
  60.     if aTime == bTime then
  61.       return a.name < b.name
  62.     else
  63.       return aTime >= bTime
  64.     end
  65. end
  66.  
  67. -- Scans the different bags of the players to show the items, sorts them alfabetically
  68. -- Adds color depending on the quality of the filter TODO description
  69. -- Updates paging
  70. function BagBuddy_Update()
  71.     local items = {}
  72.     local nameFilter = BagBuddy.input:GetText():lower()
  73.     -- Scan through the bag slots, looking for items
  74.     for bag = 0, NUM_BAG_SLOTS do
  75.         for slot = 0, GetContainerNumSlots(bag) do
  76.             local texture, count, locked, quality, readable, lootable, link = GetContainerItemInfo(bag, slot)
  77.             if texture then
  78.                 local shown = true
  79.  
  80.                 if BagBuddy.qualityFilter then
  81.                     shown = shown and BagBuddy.filters[quality]:GetChecked()
  82.                 end
  83.  
  84.                 -- Important: if no filter is selected, all the items will be shown
  85.                 -- Also, filters are additive
  86.                 if #nameFilter > 0 then
  87.                     local lowerName = GetItemInfo(link):lower()
  88.                     shown = shown and string.find(lowerName, nameFilter, 1, true)
  89.                 end
  90.  
  91.                 if shown then
  92.                     -- If an item is found, grab the item number and store data
  93.                     local itemNum = tonumber(link:match("|Hitem:(%d+):"))
  94.                     if not items[itemNum] then
  95.                         items[itemNum] = {
  96.                             texture = texture,
  97.                             count = count,
  98.                             quality = quality,
  99.                             name = GetItemInfo(link),
  100.                             link = link,
  101.                             num = itemNum,
  102.                         }
  103.                     else
  104.                         -- The item already exists in our table, just update the count
  105.                         items[itemNum].count = items[itemNum].count + count
  106.                     end
  107.                 end
  108.             end
  109.         end
  110.     end
  111.  
  112.     local sortTbl = {}
  113.     for link, entry in pairs(items) do
  114.         table.insert(sortTbl, entry)
  115.     end
  116.     table.sort(sortTbl, itemTimeNameSort)
  117.  
  118.     -- Now update the BagBuddyFrame with the listed items (in order)
  119.     local max = BagBuddy.page * 24
  120.     local min = max - 23
  121.     for idx = min, max do
  122.         local button = BagBuddy.items[idx - min + 1]
  123.         local entry = sortTbl[idx]
  124.         if entry then
  125.             -- There is an item in this slot
  126.             button.link = entry.link
  127.             button.icon:SetTexture(entry.texture)
  128.             if entry.count > 1 then
  129.                 button.count:SetText(entry.count)
  130.                 button.count:Show()
  131.             else
  132.                 button.count:Hide()
  133.             end
  134.             if entry.quality > 1 then
  135.                 button.glow:SetVertexColor(GetItemQualityColor(entry.quality))
  136.                 button.glow:Show()
  137.             else
  138.                 button.glow:Hide()
  139.             end
  140.             button:Show()
  141.         else
  142.             button.link = nil
  143.             button:Hide()
  144.         end
  145.  
  146.     end
  147.  
  148.     -- Update page buttons
  149.     if min > 1 then
  150.         BagBuddy.prev:Enable()
  151.     else
  152.         BagBuddy.prev:Disable()
  153.     end
  154.     if max < #sortTbl then
  155.         BagBuddy.next:Enable()
  156.     else
  157.         BagBuddy.next:Disable()
  158.     end
  159.  
  160.     -- Update the status text
  161.     if #sortTbl > 24 then
  162.         local max = math.min(max, #sortTbl)
  163.         local msg = string.format("Showing items %d - %d of %d", min, max, #sortTbl) --TODO internationalization
  164.         BagBuddy.status:SetText(msg)
  165.     else
  166.         BagBuddy.status:SetText("Found " .. #sortTbl .. " items")
  167.     end
  168.  
  169. end
  170.  
  171. function BagBuddy_Button_OnEnter(self, motion)
  172.     if self.link then
  173.         GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
  174.         GameTooltip:SetHyperlink(self.link)
  175.         GameTooltip:Show()
  176.     end
  177. end
  178.  
  179.  
  180. function BagBuddy_Button_OnLeave(self, motion)
  181.     GameToolTip:Hide()
  182. end
  183.  
  184.  
  185. function BagBuddy_Filter_OnEnter(self, motion)
  186.     GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
  187.     GameTooltip:SetText(_G["ITEM_QUALITY" .. self.quality .. "_DESC"])
  188.     GameTooltip:Show()
  189. end
  190.  
  191.  
  192. function BagBuddy_Filter_OnLeave(self, motion)
  193.     GameTooltip:Hide()
  194. end
  195.  
  196.  
  197. function BagBuddy_Filter_OnClick(self, button)
  198.     BagBuddy.qualityFilter = false
  199.     for idx = 0, 5 do
  200.         local button = BagBuddy.filters[idx]
  201.         if button:GetChecked() then
  202.             BagBuddy.qualityFilter = true
  203.         end
  204.     end
  205.     BagBuddy.page = 1
  206.     BagBuddy_Update()
  207. end
  208.  
  209. function BagBuddy_NextPage(self)
  210.     BagBuddy.page = BagBuddy.page + 1
  211.     BagBuddy_Update(BagBuddy)
  212. end
  213.  
  214.  
  215. function BagBuddy_PrevPage(self)
  216.     BagBuddy.page = BagBuddy.page - 1
  217.     BagBuddy_Update(BagBuddy)
  218. end
  219.  
  220. function BagBuddy_ScanBag(bag, initial)
  221.     if not BagBuddy.bagCounts[bag] then
  222.         BagBuddy.bagCounts[bag] = {}
  223.     end
  224.     local itemCounts = {}
  225.     for slot = 0, GetContainerNumSlots(bag) do
  226.         local texture, count, locked, quality, readable, lootable, link = GetContainerItemInfo(bag, slot)
  227.         if texture then
  228.             local itemId = tonumber(link:match("|Hitem:(%d+):"))
  229.             if not itemCounts[itemId] then
  230.                 itemCounts[itemId] = count
  231.             else
  232.                 itemCounts[itemId] = itemCounts[itemId] + count
  233.             end
  234.         end
  235.     end
  236.     if initial then
  237.         for itemId, count in pairs(itemCounts) do
  238.             BagBuddy_ItemTimes[itemId] = BagBuddy_ItemTimes[itemId] or time()
  239.         end
  240.     else
  241.         for itemId, count in pairs(itemCounts) do
  242.             local oldCount = BagBuddy.bagCounts[bag][itemId] or 0
  243.             if count > oldCount then
  244.                 BagBuddy_ItemTimes[itemId] = time()
  245.             end
  246.         end
  247.     end
  248.     BagBuddy.bagCounts[bag] = itemCounts
  249. end
  250.  
  251. function BagBuddy_OnEvent(self, event, ...)
  252.     if event == "ADDON_LOADED" and ... == "BagBuddy" then
  253.         if not BagBuddy_ItemTimes then
  254.             BagBuddy_ItemTimes = {}
  255.         end
  256.         for bag = 0, NUM_BAG_SLOTS do
  257.             -- Use the optional flag to skip updating times
  258.             BagBuddy_ScanBag(bag, true)
  259.         end
  260.         self:UnregisterEvent("ADDON_LOADED")
  261.         self:RegisterEvent("BAG_UPDATE")
  262.     elseif event == "BAG_UPDATE" then
  263.         local bag = ...
  264.         if bag >= 0 then
  265.             BagBuddy_ScanBag(bag)
  266.             if BagBuddy:IsVisible() then
  267.                 BagBuddy_Update()
  268.             end
  269.         end
  270.     end
  271.     table.sort(sortTbl, itemTimeNameSort)
  272. end
  273.  
  274. SLASH_BAGBUDDY1 = "/cyw"
  275. SLASH_BAGBUDDY2 = "/castyourwote"
  276. SlashCmdList["CASTYOURWOTE"] = function(msg, editbox)
  277.     BagBuddy.input:SetText(msg)
  278.     ShowUIPanel(BagBuddy)
  279.   end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement