OneBag = AceLibrary("AceAddon-2.0"):new("AceEvent-2.0", "AceHook-2.0", "AceDebug-2.0", "AceConsole-2.0", "AceDB-2.0")local commands = {"/ob", "/OneBag"}local args = { type="group", args = { cols = { name = "Columns", desc = "Sets the number of columns to use", get = function() return self.db.profile.cols end, set = function() self.db.profile.cols = true end, type = "range", min = 5, max = 20, }, }}local defaults = { cols = 10}function OneBag:OnInitialize() self.super.OnInitialize(self) self:RegisterDB("OneBagDB") self:RegisterDefaults('profile', defaults) self:RegisterChatCommand(commands, args) --self:SetDebugging(true) self.colWidth = 39 self.rowHeight = 39 self.topBorder = 2 self.bottomBorder = 24 self.rightBorder = 5 self.leftBorder = 8 OneBagFrameName:SetText(UnitName("player").. "'s Bags") self.frame = OneBagFrame self.frame.bagFrame = OBBagFram self.frame.bags = {}endfunction OneBag:OnEnable() self.frame:SetClampedToScreen(true) self:BuildFrame() self:OrganizeFrame() self:UpdateBag(0) self:Hook("IsBagOpen") self:Hook("ToggleBag") self:Hook("OpenBag") self:Hook("CloseBag") self:Hook("OpenBackpack", "OpenBag") self:Hook("CloseBackpack", "CloseBag") self:Hook("ToggleBackpack") self:RegisterEvent("BAG_UPDATE", function() self:UpdateBag(arg1) end) self:RegisterEvent("BAG_UPDATE_COOLDOWN", function() self:UpdateBag(arg1) end) self:RegisterEvent("ITEM_LOCK_CHANGED", function() for i = 0, 4 do self:UpdateBag(i) end end) self:RegisterEvent("UPDATE_INVENTORY_ALERTS", function() for i = 0, 4 do self:UpdateBag(i) end end) self:RegisterEvent("AUCTION_HOUSE_SHOW", function() self:OpenBag() end) self:RegisterEvent("AUCTION_HOUSE_CLOSED", function() self:CloseBag() end) self:RegisterEvent("BANKFRAME_OPENED", function() self:OpenBag() end) self:RegisterEvent("BANKFRAME_CLOSED", function() self:CloseBag() end) self:RegisterEvent("MAIL_CLOSED", function() self:CloseBag() end) self:RegisterEvent("MERCHANT_SHOW", function() self:OpenBag() end) self:RegisterEvent("MERCHANT_CLOSED", function() self:CloseBag() end) self:RegisterEvent("TRADE_SHOW", function() self:OpenBag() end) self:RegisterEvent("TRADE_CLOSED", function() self:CloseBag() end)end-- Building and Organizing the frame.function OneBag:BuildFrame() debugprofilestart() self.slotCount = 0 for bag = 0, 4 do local bagsize = GetContainerNumSlots(bag) self.slotCount = self.slotCount + bagsize if (bagsize > 0) then if not self.frame.bags[bag] then self.frame.bags[bag] = CreateFrame("Frame", tostring(self)..bag, self.frame) self.frame.bags[bag]:SetID(bag) self.needToOrganize = true self:Debug("Created bag %s", bag) end if self.frame.bags[bag].size ~= bagsize then self.needToOrganize = true end self.frame.bags[bag].size = bagsize for slot = 1, bagsize do if not self.frame.bags[bag][slot] then self.frame.bags[bag][slot] = CreateFrame("Button", tostring(self)..bag.."Item"..slot, self.frame.bags[bag], "OneBagItemButtonTemplate") self.frame.bags[bag][slot]:SetID(slot) self.needToOrganize = true self:Debug("Created slot at %s:%s", bag, slot) end end elseif self.frame.bags[bag] then self.frame.bags[bag].size = bagszie end end OneBagFrameSlots:SetText(format("%s/%s Used", self:GetSlotCount())) self:Debug("%s ran in %s", "BuildFrame", debugprofilestop())endfunction OneBag:OrganizeFrame() if not self.needToOrganize then return end self.needToOrganize = false local cols = 16 local curCol = 1 local curRow = 1 for k, v in ipairs(self.frame.bags) do for k2, v2 in ipairs(v) do v2:Hide() end end for bag = 0, 4 do if self.frame.bags[bag] and self.frame.bags[bag].size and self.frame.bags[bag].size > 0 then self.frame.bags[bag].isAmmo = self:IsAmmoBag(bag) self.frame.bags[bag].isSoul = self:IsSoulBag(bag) self.frame.bags[bag].isProf = self:IsProfessionsBag(bag) for slot = 1, self.frame.bags[bag].size do self.frame.bags[bag][slot]:ClearAllPoints() self.frame.bags[bag][slot]:SetPoint("TOPLEFT", self.frame:GetName(), "TOPLEFT", self.leftBorder + (self.colWidth * (curCol - 1)), 0 - self.topBorder - (self.rowHeight * curRow)) self.frame.bags[bag][slot]:Show() curCol = curCol + 1 if curCol > cols then curCol = 1 curRow = curRow + 1 end end end end self:Debug("CurrentRow: %s", curRow) if math.mod(self.slotCount, cols) == 0 then self.frame:SetHeight(curRow * self.rowHeight + self.bottomBorder + self.topBorder) else self.frame:SetHeight((curRow + 1) * self.rowHeight + self.bottomBorder + self.topBorder) end self.frame:SetWidth(cols * self.colWidth + self.leftBorder + self.rightBorder)end-- Helper Functionsfunction OneBag:SetBorderColor(slot) local color = {r = 1, g = 1, b = 1} local specialcolor = {r= 1, g =1, b=0} local bag = slot:GetParent() if bag.isAmmo then color = specialcolor elseif bag.isSoul then color = specialcolor elseif bag.isProf then color = specialcolor else local _, _, hex = strfind(GetContainerItemLink(bag:GetID(), slot:GetID()) or "", "(|cff%x%x%x%x%x%x)") for k, v in ipairs(ITEM_QUALITY_COLORS) do if hex == v.hex then color = v end end --self:Debug("Bag: %s Slot: %s; Quality %s; color : %s %s %s %s", slot:GetParent():GetID(), slot:GetID(), quality, unpack(color)) end slot:GetNormalTexture():SetVertexColor(color.r, color.g, color.b)endfunction OneBag:HighlightBagSlots(bag) if not self.frame.bags[bag] then return end local color = {r = 0, g = .7, b = 1} for k, v in ipairs(self.frame.bags[bag]) do v:GetNormalTexture():SetVertexColor(color.r, color.g, color.b) endendfunction OneBag:UnhighlightBagSlots(bag) if not self.frame.bags[bag] then return end for k, v in ipairs(self.frame.bags[bag]) do self:SetBorderColor(v) endendfunction OneBag:GetSlotCount() local usedSlots = 0 for bag = 0, 4 do for slot = 1, GetContainerNumSlots(bag) do if( GetContainerItemInfo(bag, slot) ) then usedSlots = usedSlots + 1 end end end return usedSlots, self.slotCountendfunction OneBag:IsAmmoBag(bag) if( bag <= 0 ) then return end local _, _, id = strfind(GetInventoryItemLink("player", ContainerIDToInventoryID(bag)) or "", "item:(%d+)"); if not id then return end local _, _, _, _, itemType, subType = GetItemInfo(id); return itemType == "Quiver" or falseendfunction OneBag:IsSoulBag(bag) if( bag <= 0 ) then return end local _, _, id = string.find(GetInventoryItemLink("player", ContainerIDToInventoryID(bag)) or "", "item:(%d+)") if not id then return end local _, _, _, _, itemType, subType = GetItemInfo(id) return subType == "Soul Bag" or falseend--returns if the given bag is a profession bag (herb bag, engineering bag, etc)function OneBag:IsProfessionsBag(bag) if( bag <= 0 ) then return end local _, _, id = string.find(GetInventoryItemLink("player", ContainerIDToInventoryID(bag)) or "", "item:(%d+)") if not id then return end local _, _, _, _, itemType, subType = GetItemInfo(id) return ( itemType == "Container" and not (subType == "Bag" or subType == "Soul Bag") ) or falseend-- Bag update handlingfunction OneBag:UpdateBag(bag) debugprofilestart() if not self.frame.bags[bag] then return end self:BuildFrame() self:OrganizeFrame() if not self.frame.bags[bag].colorLocked then for k, v in ipairs(self.frame.bags[bag]) do self:SetBorderColor(v) end end if self.frame.bags[bag].size then ContainerFrame_Update(self.frame.bags[bag]) end self:Debug("%s ran in %s", "UpdateBag", debugprofilestop())end--Hook responsesfunction OneBag:ToggleBackpack() self:Debug("Toggleing Backpack") if self.frame:IsVisible() then self.frame:Hide() else self.frame:Show() endendfunction OneBag:ToggleBag(bag) self:Debug("Toggling bag %s", bag) if bag < 0 or bag > 4 then return self.hooks.ToggleBag.orig(bag) end if self.frame:IsVisible() then self.frame:Hide() else self.frame:Show() endendfunction OneBag:IsBagOpen(bag) self:Debug("Checking if bag %s is open", bag) if bag < 0 or bag > 4 then return self.hooks.IsBagOpen.orig(bag) end if self.frame:IsVisible() then return bag else return nil endendfunction OneBag:OpenBag(bag) self:Debug("Opening bag %s", bag) if bag and (bag < 0 or bag > 4) then return self.hooks.OpenBag.orig(bag) end self.frame:Show()endfunction OneBag:CloseBag(bag) self:Debug("Closing bag %s", bag) if bag and (bag < 0 or bag > 4) then return self.hooks.CloseBag.orig(bag) end self.frame:Hide()end