Quantcast

Getting things sorted

Brandon James Talbot [01-04-16 - 09:14]
Getting things sorted
Filename
bag/bag.xml
bag/button.class
bag/button.lua
bag/container.lua
bag/core.lua
bag/settings.lua
core/core.xml
diff --git a/bag/bag.xml b/bag/bag.xml
index f826814..27c9aa7 100644
--- a/bag/bag.xml
+++ b/bag/bag.xml
@@ -1,5 +1,7 @@
 <Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/  http://wowprogramming.com/FrameXML/UI.xsd">
 	<Script file="bag/settings.lua" />
+	<Script file="bag/button.lua" />
+	<Script file="bag/container.lua" />
 	<Script file="bag/core.lua" />
 	<Script file="bag/events.lua" />
 </Ui>
\ No newline at end of file
diff --git a/bag/button.class b/bag/button.class
new file mode 100644
index 0000000..4cc7dce
--- /dev/null
+++ b/bag/button.class
@@ -0,0 +1 @@
+local _, ns = ...
\ No newline at end of file
diff --git a/bag/button.lua b/bag/button.lua
new file mode 100644
index 0000000..1461f1a
--- /dev/null
+++ b/bag/button.lua
@@ -0,0 +1,112 @@
+local _, ns = ...
+
+local settings = ns.settings
+local containers = ns.containers
+
+local buttons = {}
+buttons.__index = buttons
+
+buttons.list = {}
+
+ns.buttons = buttons
+
+function buttons:GetButton(bag, slot)
+	if not self.list[bag] then
+		self.list[bag] = {}
+	end
+	if not self.list[bag][slot] then
+		self.list[bag][slot] = button(bag, slot)
+	end
+	return button(bag, slot)
+end
+
+local button = {}
+local mt = {}
+mt.__call = function(tbl, bag, slot)
+	local parentFrame = CreateFrame("Frame",
+		string.format("FJUIBagButtonParent%d_%d", bag, slot), nil)
+	local obj = CreateFrame("Button",
+		string.format("DJUIBagButton%d_%d", bag, slot), parentFrame,
+		"ContainerFrameItemButtonTemplate")
+	obj.parent = parentFrame
+	parentFrame.button = obj
+
+	button.Init(parentFrame, bag, slot)
+	return parentFrame
+end
+setmetatable(button, mt)
+
+function button.Init(obj, bag, slot)
+	setmetatable(obj, button)
+
+	obj.bag = bag
+	obj.slot = slot
+
+	obj.SetID(bag)
+	obj.button:SetID(slot)
+
+	obj:Show()
+	obj.button:Show()
+end
+
+function button:GetInformation()
+	local id = GetContainerItemID(self.bag, self.slot)
+	local texture, count, locked, quality, readable, lootable, link, isFiltered = GetContainerItemInfo(self.bag, self.slot)
+	local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(id)
+
+	return id, name, texture, count, quality, ilevel
+end
+
+function button:GetContainerName()
+	local id = GetContainerItemID(self.bag, self.slot)
+	local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(id)
+	local isInSet, setName = GetContainerItemEquipmentSetInfo(self.bag, self.slot)
+
+	if isInSet then
+		settings.setNames[setName] = true
+	end
+
+	local type = settings.saved.types[id] or (isInSet and setName) or class
+
+	if not settings.saved.bagColumns[settings.setNames[type] and "Sets" or type] then
+		settings.saved.bagColumns[type] = 1
+	end
+
+	return type
+end
+
+function button:Update()
+	local id, name, texture, count, quality, ilevel = self:GetInformation()
+
+	if id then
+		self:UpdateItem(id, texture, count, quality, ilevel)
+	else
+		self:Clear()
+	end
+end
+
+function button:UpdateItem(id, texture, count, quality, ilevel)
+	local button = self.button
+
+	button.icon:SetTexture(texture)
+
+	if count > 1 then
+		button.Count:SetText(count)
+		button.Count:SetVertexColor(1, 1, 1, 1)
+		button.Count:Show()
+	elseif IsEquipableItem(id) then
+		button.Count:SetText(ilevel)
+		button.Count:SetVertexColor(GetItemQualityColor(quality))
+		button.Count:Show()
+	else
+		button.Count:Hide()
+	end
+end
+
+function button:Clear()
+	local button = self.button
+
+	button.icon:SetTexture(nil)
+	button.Count:Hide()
+end
+
diff --git a/bag/container.lua b/bag/container.lua
new file mode 100644
index 0000000..a8a2607
--- /dev/null
+++ b/bag/container.lua
@@ -0,0 +1,101 @@
+local _, ns = ...
+
+local settings = ns.settings
+
+local containers = {}
+containers.__index = containers
+
+containers.list = {}
+
+ns.containers = containers
+
+function containers:GetContainer(name)
+	if not self.list[name] then
+		self.list[name] = container(name)
+	end
+	return self.list[name]
+end
+
+local container = {}
+local mt = {}
+mt.__call = function(tbl, name, parent)
+	local obj = setmetatable(CreateFrame("Frame", "DJUIBag" .. name, parent), container)
+
+	obj:Init(name)
+
+	return obj
+end
+setmetatable(container, mt)
+
+function container:Init(name)
+	self.name = name
+	self.items = {}
+
+	self.title = self:CreateFontString(self:GetName() .. "Title", "OVERLAY")
+	self.title:SetPoint("TOP", 0, -1)
+	self.title:SetFont("Fonts\\FRIZQT__.TTF", settings.saved.titleSize, "OUTLINE")
+	self.title:SetTextColor(0.6, 0.36, 0, 1)
+
+	self.title:SetText(name)
+
+	self.itemContainer = CreateFrame("Frame", self:GetName() .. "ItemContainer", self)
+	self.itemContainer:SetPoint("TOPLEFT", self, "TOPLEFT", settings.saved.padding, -settings.saved.titleSize - settings.saved.padding)
+	self.itemContainer:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT", -settings.saved.padding, settings.saved.padding)
+end
+
+function container:AddItem(item)
+	item:SetParent(self)
+	item.container = self
+
+	table.insert(self.items, item)
+end
+
+function container:RemoveItem(item)
+	item:SetParent(nil)
+	item.container = nil
+
+	for i = 1, #self.items do
+		if self.items[i] == item then
+			table.remove(self.items, i)
+			break
+		end
+	end
+end
+
+function container:GetWidth()
+	local numItems = settings.saved.columns
+	return settings.saved.padding * 2 +
+			(numItems - 1) * settings.saved.itemPadding +
+			numItems * settings.saved.itemSize
+end
+
+function container:GetHeight()
+	local rows = math.ceil(#self.items / settings.saved.columns)
+	return settings.saved.titleSize +
+	        settings.saved.padding * 2 +
+	        (rows - 1) * settings.saved.itemPadding +
+	        rows * settings.saved.itemSize
+end
+
+function container:Arrange()
+	local w = self:GetWidth()
+	local h = self:GetHeight()
+
+	self:SetSize(w, h)
+
+	self:ArrangeItems()
+end
+
+function container:ArrangeItems()
+	for i = 1, #self.items do
+		local col = math.ceil((i - 1) % settings.saved.columns)
+		local row = math.floor((i - 1) / settings.saved.columns)
+		local x = col * settings.saved.itemSize + col * settings.saved.itemPadding
+		local y = -(row * settings.saved.itemSize + row * settings.saved.itemPadding)
+
+		local item = self.items[i]
+		item:SetSize(settings.saved.itemSize, settings.saved.itemSize)
+		item:ClearAllPoints()
+		item:SetPoint("TOPLEFT", x, y)
+	end
+end
\ No newline at end of file
diff --git a/bag/core.lua b/bag/core.lua
index 515ac40..2087a00 100644
--- a/bag/core.lua
+++ b/bag/core.lua
@@ -7,107 +7,3 @@ impl.__index = impl

 ns.bag.impl = impl

-ns.buttons = {
-
-}
-
-impl.mainFrame = CreateFrame("Frame", "DJUIBag", UIParent)
-
-function impl:ADDON_LOADED(name)
-	if name == addonName then
-		-- TODO - Load real settings
-		self.mainFrame:SetPoint("BOTTOMRIGHT", -300, 125)
-
-		local buttons = {}
-
-		for bag = 0, NUM_BAG_SLOTS do
-			for slot = 1, GetContainerNumSlots(bag) do
-				local button = self:UpdateButton(bag, slot)
-
-				table.insert(buttons, button)
-				button:SetParent(self.mainFrame)
-			end
-		end
-
-		self:ArangeItems(self.mainFrame, buttons, settings.saved.columns, settings.saved.itemPadding, settings.saved.itemSize)
-		local w, h = self:GetButtonFrameSize(#buttons, settings.saved.columns, settings.saved.itemPadding, settings.saved.itemSize)
-		self.mainFrame:SetSize(w, h)
-		self.mainFrame:Show()
-	end
-end
-
-function impl:GetButton(bag, slot)
-	if not ns.buttons[bag] then
-		ns.buttons[bag] = {}
-	end
-	if ns.buttons[bag][slot] then
-		return ns.buttons[bag][slot]
-	end
-
-	local parentFrame = CreateFrame("Frame", nil, nil)
-	parentFrame:SetID(bag)
-
-	local button = CreateFrame("Button", string.format("DJUIBagButton%d_%d", bag, slot), parentFrame, "ContainerFrameItemButtonTemplate")
-
-	parentFrame.button = button
-
-	button:SetAllPoints()
-	button.bagID = bag
-	button.slotID = slot
-	button:SetID(slot)
-	button:Show()
-	parentFrame:Show()
-
-	ns.buttons[bag][slot] = parentFrame
-
-	return parentFrame
-end
-
-function impl:ArangeItems(container, items, columns, itemPadding, itemSize)
-	local cnt = 0
-	for k, v in pairs(items) do
-		v:ClearAllPoints()
-		v:SetParent(container)
-		v:SetSize(settings.saved.itemSize, settings.saved.itemSize)
-
-		local col = math.ceil(cnt % columns)
-		local row = math.floor(cnt / columns)
-		local x = col * itemSize + col * itemPadding
-		local y = row * itemSize + row * itemPadding
-
-		v:SetPoint("TOPLEFT", x, -y)
-
-		cnt = cnt + 1
-	end
-end
-
-function impl:GetButtonFrameSize(count, columns, itemPadding, itemSize)
-	local x = columns * itemSize + (columns - 1) * itemPadding
-	local rows = math.ceil(count / columns)
-	local y = rows * itemSize + (rows - 1) * itemPadding
-
-	return x, y
-end
-
-function impl:UpdateButton(bag, slot)
-	local parentFrame = self:GetButton(bag, slot)
-	local button = parentFrame.button
-
-	local texture, count, locked, quality, readable, lootable, link, isFiltered = GetContainerItemInfo(bag, slot)
-
-	if not texture then
-		button.Count:Hide()
-		button.icon:SetTexture(nil)
-	else
-		button.icon:SetTexture(texture)
-
-		if count > 1 then
-			button.Count:SetText(count)
-			button.Count:Show()
-		else
-			button.Count:Hide()
-		end
-	end
-
-	return parentFrame
-end
\ No newline at end of file
diff --git a/bag/settings.lua b/bag/settings.lua
index 2f18ac7..6f22c7a 100644
--- a/bag/settings.lua
+++ b/bag/settings.lua
@@ -8,11 +8,6 @@ end
 local settings = {}
 ns.bag.settings = settings

-settings.iLevelTypes = {
-	"Weapon",
-	"Armor"
-}
-
 settings.saved = {
 	itemSize = 37,
 	columns = 8,
@@ -21,61 +16,20 @@ settings.saved = {
 	bagPadding = 3,
 	titleSize = 15,
 	countFontSize = 10,
-	savedTypes = {},
-	bagSettings = {},
+	types = {},
 	bagColumns = {
-		left = {
-			"Sets",
-			"Armor",
-			"Weapon",
-		},
-		right = {
-			"Miscellaneous",
-			"Consumable",
-			"Trade Goods",
-			"Quest",
-		},
+		["Miscellaneous"] = 0,
+		["Consumable"] = 0,
+		["Trade Goods"] = 0,
+		["Quest"] = 0,
+		["Sets"] = 1,
+		["Armor"] = 1,
+		["Weapon"] = 1,
 	},
 }

 settings.setNames = {}

-settings.isSet = function(key)
-	for k, v in pairs(settings.setNames) do
-		if v == key then
-			return true
-		end
-	end
-end
-
-settings.bagColumnSorter = function (itemTable)
-	local function getKey(itm)
-		itm = settings.isSet(itm) and "Sets" or itm
-		for k, v in pairs(itemTable) do
-			if v == itm then
-				return k
-			end
-		end
-		return nil
-	end
-
-	return function(a, b)
-		local valA =  getKey(a)
-		local valB =  getKey(b)
-
-		if valA and valB then
-			if valA ~= valB then
-				return valA < valB
-			end
-		elseif valA then
-			return true
-		elseif valB then
-			return false
-		end
-		return a < b
-	end
-end
-
 settings.sortingFunction = {
 	["default"] = function (a, b)
 		if a.quality == b.quality then
@@ -89,6 +43,4 @@ settings.sortingFunction = {
 	["name"] = function (a, b)
 		return a.name < b.name
 	end,
-}
-
-_G["DJUIBagSettings"] = settings
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/core/core.xml b/core/core.xml
deleted file mode 100644
index db12b74..0000000
--- a/core/core.xml
+++ /dev/null
@@ -1,2 +0,0 @@
-<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/  http://wowprogramming.com/FrameXML/UI.xsd">
-</Ui>
\ No newline at end of file