Quantcast

Add equipment set overlay.

Michael Tindal [02-23-14 - 22:36]
Add equipment set overlay.
Filename
ElvUI_SLE/config/sle_private.lua
ElvUI_SLE/locales/english.lua
ElvUI_SLE/modules/bags/baginfo.lua
ElvUI_SLE/modules/bags/load_bags.xml
ElvUI_SLE/modules/equipmanager/options.lua
ElvUI_SLE/modules/load_modules.xml
diff --git a/ElvUI_SLE/config/sle_private.lua b/ElvUI_SLE/config/sle_private.lua
index 2dec43a..0964e6c 100755
--- a/ElvUI_SLE/config/sle_private.lua
+++ b/ElvUI_SLE/config/sle_private.lua
@@ -39,6 +39,7 @@ V['sle'] = {
 		['secondary'] = "NONE",
 		['instance'] = "NONE",
 		['pvp'] = "NONE",
+		['setoverlay'] = false,
 	},

 	--Minimap Moduel
diff --git a/ElvUI_SLE/locales/english.lua b/ElvUI_SLE/locales/english.lua
index ecae31f..c3614ef 100755
--- a/ElvUI_SLE/locales/english.lua
+++ b/ElvUI_SLE/locales/english.lua
@@ -179,7 +179,8 @@ L["Equip this set when switching to primary talents."] = true
 L["Equip this set when switching to secondary talents."] =true
 L["Equip this set after entering dungeons or raids."] = true
 L["Equip this set after entering battlegrounds or arens."] = true
-
+L['Equipment Set Overlay'] = true
+L['Show the associated equipment sets for the items in your bags (or bank).'] = true

 --Farm--
 L['Farm'] = true
diff --git a/ElvUI_SLE/modules/bags/baginfo.lua b/ElvUI_SLE/modules/bags/baginfo.lua
new file mode 100755
index 0000000..5e2b83b
--- /dev/null
+++ b/ElvUI_SLE/modules/bags/baginfo.lua
@@ -0,0 +1,136 @@
+local E, L, V, P, G = unpack(ElvUI); --Inport: Engine, Locales, PrivateDB, ProfileDB, GlobalDB, Localize Underscore
+local BI = E:NewModule('BagInfo', 'AceHook-3.0', 'AceEvent-3.0', 'AceTimer-3.0')
+local B = E:GetModule('Bags')
+
+local byte, format = string.byte, string.format
+local tinsert, twipe = table.insert, table.wipe
+
+local updateTimer
+local containers = {}
+local infoArray = {}
+local equipmentMap = {}
+
+local function Utf8Sub(str, start, numChars)
+  local currentIndex = start
+  while numChars > 0 and currentIndex <= #str do
+    local char = byte(str, currentIndex)
+    if char > 240 then
+      currentIndex = currentIndex + 4
+    elseif char > 225 then
+      currentIndex = currentIndex + 3
+    elseif char > 192 then
+      currentIndex = currentIndex + 2
+    else
+      currentIndex = currentIndex + 1
+    end
+    numChars = numChars -1
+  end
+  return str:sub(start, currentIndex - 1)
+end
+
+local function MapKey(bag, slot)
+	return format("%d_%d", bag, slot)
+end
+
+local quickFormat = {
+	[0] = function(font, map) font:SetText() end,
+	[1] = function(font, map) font:SetFormattedText("|cffffffaa%s|r", Utf8Sub(map[1], 1, 4)) end,
+	[2] = function(font, map) font:SetFormattedText("|cffffffaa%s %s|r", Utf8Sub(map[1], 1, 4), Utf8Sub(map[2], 1, 4)) end,
+	[3] = function(font, map) font:SetFormattedText("|cffffffaa%s %s %s|r", Utf8Sub(map[1], 1, 4), Utf8Sub(map[2], 1, 4), Utf8Sub(map[3], 1, 4)) end,
+}
+
+function BI:BuildEquipmentMap(clear)
+	-- clear mapped names
+	for k, v in pairs(equipmentMap) do
+		twipe(v)
+	end
+	if clear then return end
+
+	local name, player, bank, bags, slot, bag, key
+	for i = 1, GetNumEquipmentSets() do
+		name = GetEquipmentSetInfo(i)
+		GetEquipmentSetLocations(name, infoArray)
+		for _, location in pairs(infoArray) do
+			if location < -1 or location > 1 then
+				player, bank, bags, _, slot, bag = EquipmentManager_UnpackLocation(location)
+				if ((bank or bags) and slot and bag) then
+					key = MapKey(bag, slot)
+					equipmentMap[key] = equipmentMap[key] or {}
+					tinsert(equipmentMap[key], name)
+				end
+			end
+		end
+	end
+end
+
+function BI:UpdateContainerFrame(frame, bag, slot)
+	if (not frame.equipmentinfo) then
+		frame.equipmentinfo = frame:CreateFontString(nil, "OVERLAY")
+		frame.equipmentinfo:FontTemplate(E.media.font, 12, "THINOUTLINE")
+		frame.equipmentinfo:SetWordWrap(true)
+		frame.equipmentinfo:SetJustifyH('CENTER')
+		frame.equipmentinfo:SetJustifyV('MIDDLE')
+	end
+
+	if (frame.equipmentinfo) then
+		frame.equipmentinfo:SetAllPoints(frame)
+
+		local key = MapKey(bag, slot)
+		if equipmentMap[key] then
+			quickFormat[#equipmentMap[key] < 4 and #equipmentMap[key] or 3](frame.equipmentinfo, equipmentMap[key])
+		else
+			quickFormat[0](frame.equipmentinfo, nil)
+		end
+	end
+end
+
+function BI:UpdateBagInformation(clear)
+	updateTimer = nil
+
+	self:BuildEquipmentMap(clear)
+	for _, container in pairs(containers) do
+		for _, bagID in ipairs(container.BagIDs) do
+			for slotID = 1, GetContainerNumSlots(bagID) do
+				self:UpdateContainerFrame(container.Bags[bagID][slotID], bagID, slotID)
+			end
+		end
+	end
+end
+
+function BI:DelayUpdateBagInformation(event)
+	-- delay to make sure multiple bag events are consolidated to one update.
+	if not updateTimer then
+		updateTimer = BI:ScheduleTimer("UpdateBagInformation", .25)
+	end
+end
+
+function BI:ToggleSettings()
+	if updateTimer then
+		self:CancelTimer(updateTimer)
+	end
+
+	if E.private.sle.equip.setoverlay then
+		self:RegisterEvent("EQUIPMENT_SETS_CHANGED", "DelayUpdateBagInformation")
+		self:RegisterEvent("BAG_UPDATE", "DelayUpdateBagInformation")
+		BI:UpdateBagInformation()
+	else
+		self:UnregisterEvent("EQUIPMENT_SETS_CHANGED")
+		self:UnregisterEvent("BAG_UPDATE")
+		BI:UpdateBagInformation(true)
+	end
+end
+
+function BI:Initialize()
+	if not E.private.bags.enable then return end
+
+	tinsert(containers, _G["ElvUI_ContainerFrame"])
+	self:SecureHook(B, "OpenBank", function()
+		self:Unhook(B, "OpenBank")
+		tinsert(containers, _G["ElvUI_BankContainerFrame"])
+		BI:ToggleSettings()
+	end)
+
+	BI:ToggleSettings()
+end
+
+E:RegisterModule(BI:GetName())
\ No newline at end of file
diff --git a/ElvUI_SLE/modules/bags/load_bags.xml b/ElvUI_SLE/modules/bags/load_bags.xml
new file mode 100755
index 0000000..51d1946
--- /dev/null
+++ b/ElvUI_SLE/modules/bags/load_bags.xml
@@ -0,0 +1,3 @@
+<Ui xmlns="http://www.blizzard.com/wow/ui/">
+	<Script file='baginfo.lua'/>
+</Ui>
\ No newline at end of file
diff --git a/ElvUI_SLE/modules/equipmanager/options.lua b/ElvUI_SLE/modules/equipmanager/options.lua
index 18de8fe..ef4e76e 100755
--- a/ElvUI_SLE/modules/equipmanager/options.lua
+++ b/ElvUI_SLE/modules/equipmanager/options.lua
@@ -1,5 +1,6 @@
 local E, L, V, P, G, _ = unpack(ElvUI); --Inport: Engine, Locales, PrivateDB, ProfileDB, GlobalDB, Localize Underscore
 local EM = E:GetModule('EquipManager')
+local BI = E:GetModule('BagInfo')

 local function configTable()

@@ -86,6 +87,14 @@ E.Options.args.sle.args.equipmanager = {
 					set = function(info, value) E.private.sle.equip.pvp = value end,
 					values = EM.equipSets
 				},
+				setoverlay = {
+					type = "toggle",
+					order = 1,
+					name = L['Equipment Set Overlay'],
+					desc = L['Show the associated equipment sets for the items in your bags (or bank).'],
+					get = function(info) return E.private.sle.equip.setoverlay end,
+					set = function(info, value) E.private.sle.equip.setoverlay = value; BI:ToggleSettings(); end,
+				}
 			},
 		},
 	},
diff --git a/ElvUI_SLE/modules/load_modules.xml b/ElvUI_SLE/modules/load_modules.xml
index 4e0b990..d8898da 100755
--- a/ElvUI_SLE/modules/load_modules.xml
+++ b/ElvUI_SLE/modules/load_modules.xml
@@ -7,6 +7,7 @@
 	<Include file='characterframe\load_characterframe.xml'/>
 	<Include file='chat\load_chat.xml'/>
 	<Include file='datatexts\load_datatexts.xml'/>
+	<Include file='bags\load_bags.xml'/>
 	<Include file='equipmanager\load_equipmanager.xml'/>
 	<Include file='exprepbar\load_exprepbar.xml'/>
 	<Include file='farm\load_farm.xml'/>
@@ -18,4 +19,4 @@
 	<Include file='tooltip\load_tooltip.xml'/>
 	<Include file='uibuttons\load_uibuttons.xml'/>
 	<Include file='unitframes\load_unitframes.xml'/>
-</Ui>
\ No newline at end of file
+</Ui>