Quantcast

Sorta working version

Brandon James Talbot [01-04-16 - 12:22]
Sorta working version
Filename
DJUI-WOW.toc
bag/bag.xml
bag/button.lua
bag/container.lua
bag/core.lua
bag/events.lua
bag/settings.lua
diff --git a/DJUI-WOW.toc b/DJUI-WOW.toc
index e7ce21a..7dea62e 100644
--- a/DJUI-WOW.toc
+++ b/DJUI-WOW.toc
@@ -2,8 +2,6 @@
 ## Author: Darkjaguar91
 ## Version: 0.01
 ## Title: DJUI
-## Notes: NOTE!!.
-
-core/core.xml
+## Notes: NOTE!!

 bag/bag.xml
\ No newline at end of file
diff --git a/bag/bag.xml b/bag/bag.xml
index 27c9aa7..34af90c 100644
--- a/bag/bag.xml
+++ b/bag/bag.xml
@@ -1,7 +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/button.lua" />
 	<Script file="bag/core.lua" />
 	<Script file="bag/events.lua" />
 </Ui>
\ No newline at end of file
diff --git a/bag/button.lua b/bag/button.lua
index 1461f1a..73f8178 100644
--- a/bag/button.lua
+++ b/bag/button.lua
@@ -3,6 +3,9 @@ local _, ns = ...
 local settings = ns.settings
 local containers = ns.containers

+local button = {}
+button.__index = button
+
 local buttons = {}
 buttons.__index = buttons

@@ -17,84 +20,111 @@ function buttons:GetButton(bag, slot)
 	if not self.list[bag][slot] then
 		self.list[bag][slot] = button(bag, slot)
 	end
-	return button(bag, slot)
+	return self.list[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",
+	local btn = CreateFrame("Button",
 		string.format("DJUIBagButton%d_%d", bag, slot), parentFrame,
 		"ContainerFrameItemButtonTemplate")
-	obj.parent = parentFrame
-	parentFrame.button = obj

-	button.Init(parentFrame, bag, slot)
+	btn.parent = parentFrame
+	parentFrame.button = btn
+
+	for k, v in pairs(button) do
+		parentFrame[k] = v
+	end
+
+	parentFrame:Init(bag, slot)
 	return parentFrame
 end
 setmetatable(button, mt)

-function button.Init(obj, bag, slot)
-	setmetatable(obj, button)
+function button:Init(bag, slot)
+	self.bag = bag
+	self.slot = slot

-	obj.bag = bag
-	obj.slot = slot
+	self:SetID(bag)
+	self.button:SetID(slot)
+	self.button:SetAllPoints()

-	obj.SetID(bag)
-	obj.button:SetID(slot)
-
-	obj:Show()
-	obj.button:Show()
+	self:Show()
+	self.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)
+	if id then
+		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
+		return id, name, texture, count, quality, ilevel
+	end
 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
+	if id then
+		local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(id)
+		local isInSet, setName = GetContainerItemEquipmentSetInfo(self.bag, self.slot)

-	local type = settings.saved.types[id] or (isInSet and setName) or class
+		if isInSet then
+			settings.setNames[setName] = true
+		end

-	if not settings.saved.bagColumns[settings.setNames[type] and "Sets" or type] then
-		settings.saved.bagColumns[type] = 1
+		return settings.saved.types[id] or (isInSet and setName) or class
 	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)
+	if id then
+		self:UpdateItem(id, name, texture, count, quality, ilevel)
 	else
 		self:Clear()
 	end
+
+	self:UpdateContainer()
+end
+
+function button:UpdateContainer()
+	local name = self:GetContainerName()
+
+	if self.container then
+		if self.container.name == name then
+			return
+		end
+		self.container:RemoveItem(self)
+	end
+
+	if name then
+		local container = containers:GetContainer(name)
+
+		container:AddItem(self)
+	else
+		self:Hide() -- TODO fix this!!
+	end
 end

-function button:UpdateItem(id, texture, count, quality, ilevel)
+function button:UpdateItem(id, name, texture, count, quality, ilevel)
 	local button = self.button

+	self.name = name
+	self.quality = quality
+	self.count = count
+
 	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
+	elseif IsEquippableItem(id) then
 		button.Count:SetText(ilevel)
 		button.Count:SetVertexColor(GetItemQualityColor(quality))
 		button.Count:Show()
@@ -106,6 +136,10 @@ end
 function button:Clear()
 	local button = self.button

+	self.name = nil
+	self.quality = nil
+	self.count = nil
+
 	button.icon:SetTexture(nil)
 	button.Count:Hide()
 end
diff --git a/bag/container.lua b/bag/container.lua
index a8a2607..7d76c95 100644
--- a/bag/container.lua
+++ b/bag/container.lua
@@ -2,6 +2,8 @@ local _, ns = ...

 local settings = ns.settings

+local container = {}
+container.__index = container
 local containers = {}
 containers.__index = containers

@@ -9,17 +11,52 @@ containers.list = {}

 ns.containers = containers

+function containers:GetCurrencyBar()
+	if not self.currencyBar then
+		self.currencyBar = CreateFrame("Frame", "DJUICurrencyBar", UIParent)
+		self.currencyBar:SetSize(container:GetWidth(), settings.saved.titleSize)
+		self.currencyBar:SetPoint("BOTTOMRIGHT", -300, 125)
+		self.currencyBar.background = self.currencyBar:CreateTexture("DJUICurrencyBarBackground", "BACKGROUND")
+		self.currencyBar.background:SetAllPoints()
+		self.currencyBar.background:SetTexture(0, 0, 0, 0.6)
+
+		self.currencyBar:SetMovable(true)
+		self.currencyBar:EnableMouse(true)
+		self.currencyBar:RegisterForDrag("LeftButton")
+		self.currencyBar:SetScript("OnDragStart", self.currencyBar.StartMoving)
+		self.currencyBar:SetScript("OnDragStop", self.currencyBar.StopMovingOrSizing)
+	end
+
+	return self.currencyBar
+end
+
 function containers:GetContainer(name)
 	if not self.list[name] then
-		self.list[name] = container(name)
+		self.list[name] = container(name, UIParent)
 	end
+
 	return self.list[name]
 end

-local container = {}
+function containers:Show()
+	for k, v in pairs(self.list) do
+		v:Show()
+	end
+end
+
+function containers:Hide()
+	for k, v in pairs(self.list) do
+		v:Hide()
+	end
+end
+
 local mt = {}
 mt.__call = function(tbl, name, parent)
-	local obj = setmetatable(CreateFrame("Frame", "DJUIBag" .. name, parent), container)
+	local obj = CreateFrame("Frame", "DJUIBag" .. name, parent)
+
+	for k, v in pairs(container) do
+		obj[k] = v
+	end

 	obj:Init(name)

@@ -32,15 +69,85 @@ function container:Init(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.title:SetPoint("TOP", 0, -1)
+	self.title:SetTextColor(0.6, 0.36, 0, 1)

 	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)
+
+	self.background = self:CreateTexture(self:GetName() .. "Background", "BACKGROUND")
+	self.background:SetAllPoints()
+	self.background:SetTexture(0, 0, 0, 0.6)
+
+	self:Position()
+end
+
+function container:Position()
+	local col, index = self:GetBagPosition()
+
+	self:ClearAllPoints()
+
+	local parentContainer = self:GetParentContainer(col, index)
+
+	if not parentContainer then
+		local parentPoint = col == 1 and "TOPRIGHT" or "BOTTOMLEFT"
+		local offsetX = col == 1 and 0 or (settings.saved.bagPadding * (col - 1) + (col - 2) * self:GetWidth())
+		local offsetY = col == 1 and settings.saved.bagPadding or 0
+		self:SetPoint("BOTTOMRIGHT", containers:GetCurrencyBar(), parentPoint, -offsetX, offsetY)
+	else
+		local parentPoint = "TOPRIGHT"
+		local offsetX = 0
+		local offsetY = settings.saved.bagPadding
+		self:SetPoint("BOTTOMRIGHT", parentContainer, parentPoint, offsetX, offsetY)
+	end
+end
+
+function container:RepositionChildren()
+	local col, index = self:GetBagPosition()
+
+	local child = nil
+
+	repeat
+		index = index + 1
+		child = settings.saved.bagColumns[col][index]
+
+		if child then
+			containers:GetContainer(child):Position()
+		end
+	until child == nil
+end
+
+function container:GetParentContainer(col, index)
+	while index > 0 do
+		if index == 1 then
+			return nil
+		else
+			local container = containers:GetContainer(settings.saved.bagColumns[col][index - 1])
+			if container:GetHeight() > 0 then
+				return container
+			end
+		end
+
+		index = index - 1
+	end
+end
+
+function container:GetBagPosition()
+	for c, list in pairs(settings.saved.bagColumns) do
+		for i, name in pairs(list) do
+			if name == self.name then
+				return c, i
+			end
+		end
+	end
+
+	table.insert(settings.saved.bagColumns[#settings.saved.bagColumns], self.name)
+	local col = #settings.saved.bagColumns
+	local index = #settings.saved.bagColumns[#settings.saved.bagColumns]
+	return col, index
 end

 function container:AddItem(item)
@@ -48,6 +155,11 @@ function container:AddItem(item)
 	item.container = self

 	table.insert(self.items, item)
+
+	self:Arrange()
+	if #self.items % settings.saved.columns == 1 then
+		self:RepositionChildren()
+	end
 end

 function container:RemoveItem(item)
@@ -57,6 +169,7 @@ function container:RemoveItem(item)
 	for i = 1, #self.items do
 		if self.items[i] == item then
 			table.remove(self.items, i)
+			self:Arrange()
 			break
 		end
 	end
@@ -70,6 +183,8 @@ function container:GetWidth()
 end

 function container:GetHeight()
+	if #self.items == 0 then return 0 end
+
 	local rows = math.ceil(#self.items / settings.saved.columns)
 	return settings.saved.titleSize +
 	        settings.saved.padding * 2 +
@@ -81,12 +196,22 @@ function container:Arrange()
 	local w = self:GetWidth()
 	local h = self:GetHeight()

+	if h == 0 then
+		self:Hide()
+		return
+	end
+
 	self:SetSize(w, h)

 	self:ArrangeItems()
+	self:Position()
 end

 function container:ArrangeItems()
+	if #self.items == 0 then return end
+
+	table.sort(self.items, settings.sortingFunction["default"]) -- TODO add bag settings
+
 	for i = 1, #self.items do
 		local col = math.ceil((i - 1) % settings.saved.columns)
 		local row = math.floor((i - 1) / settings.saved.columns)
@@ -96,6 +221,7 @@ function container:ArrangeItems()
 		local item = self.items[i]
 		item:SetSize(settings.saved.itemSize, settings.saved.itemSize)
 		item:ClearAllPoints()
-		item:SetPoint("TOPLEFT", x, y)
+		item:SetPoint("TOPLEFT", self.itemContainer, "TOPLEFT", x, y)
+		item:Show()
 	end
 end
\ No newline at end of file
diff --git a/bag/core.lua b/bag/core.lua
index 2087a00..9f0f860 100644
--- a/bag/core.lua
+++ b/bag/core.lua
@@ -1,9 +1,31 @@
 local addonName, ns = ...

-local settings = ns.bag.settings
+local settings = ns.settings
+local containers = ns.containers
+local buttons = ns.buttons

 local impl = {}
 impl.__index = impl

-ns.bag.impl = impl
+ns.impl = impl

+function impl:ADDON_LOADED(name)
+	if addonName == name then
+		for bag = 0, NUM_BAG_SLOTS do
+			for slot = 1, GetContainerNumSlots(bag) do
+				local button = buttons:GetButton(bag, slot)
+				button:Update()
+			end
+		end
+		containers:Show()
+	end
+end
+
+function impl:BAG_UPDATE(bag, slot)
+	if bag >= 0 and bag <= NUM_BAG_SLOTS then
+		for slot = 1, GetContainerNumSlots(bag) do
+			local button = buttons:GetButton(bag, slot)
+			button:Update()
+		end
+	end
+end
\ No newline at end of file
diff --git a/bag/events.lua b/bag/events.lua
index c1eb1b3..05bbebc 100644
--- a/bag/events.lua
+++ b/bag/events.lua
@@ -1,15 +1,17 @@
 local _, ns = ...

-local impl = ns.bag.impl
+local impl = ns.impl
+local eventFrame = CreateFrame("Frame", "DJUIBagEventFrame", nil)
+eventFrame:Hide()

 SLASH_DJBAG1, SLASH_DJBAG2 = '/djb', '/djbag'
 function SlashCmdList.DJBAG(msg, editbox)

 end

-impl.mainFrame:RegisterEvent("ADDON_LOADED")
-
-impl.mainFrame:SetScript("OnEvent", function(self, event, ...)
+eventFrame:RegisterEvent("ADDON_LOADED")
+eventFrame:RegisterEvent("BAG_UPDATE")
+eventFrame:SetScript("OnEvent", function(self, event, ...)
 	if impl[event] then
 		impl[event](impl, ...)
 	end
diff --git a/bag/settings.lua b/bag/settings.lua
index 6f22c7a..df3571e 100644
--- a/bag/settings.lua
+++ b/bag/settings.lua
@@ -1,12 +1,7 @@
 local _, ns = ...

-if not ns.bag then
-	ns.bag = {}
-	ns.bag.__index = ns.bag
-end
-
 local settings = {}
-ns.bag.settings = settings
+ns.settings = settings

 settings.saved = {
 	itemSize = 37,
@@ -18,13 +13,16 @@ settings.saved = {
 	countFontSize = 10,
 	types = {},
 	bagColumns = {
-		["Miscellaneous"] = 0,
-		["Consumable"] = 0,
-		["Trade Goods"] = 0,
-		["Quest"] = 0,
-		["Sets"] = 1,
-		["Armor"] = 1,
-		["Weapon"] = 1,
+		{
+			"Miscellaneous",
+			"Consumable",
+			"Trade Goods",
+			"Quest",
+		},
+		{
+			"Armor",
+			"Weapon"
+		}
 	},
 }