Quantcast

So I'm a perfectionist, sue me!

p3lim [10-14-10 - 23:03]
So I'm a perfectionist, sue me!
Filename
Broker_Equipment.lua
Localization.lua
diff --git a/Broker_Equipment.lua b/Broker_Equipment.lua
index 0c5e611..03f0572 100644
--- a/Broker_Equipment.lua
+++ b/Broker_Equipment.lua
@@ -9,48 +9,13 @@

 --]]

-local addonName, ns = ...
-
-local pending = {}
-local addon = CreateFrame('Frame', addonName)
-local broker = LibStub('LibDataBroker-1.1'):NewDataObject(addonName, {
-	text = UNKNOWN,
-	icon = [=[Interface\Icons\INV_Misc_QuestionMark]=],
-	iconCoords = {0.08, 0.92, 0.08, 0.92},
-	type = 'data source'
-})
-
--- Borrowed from tekkub's EquipSetUpdater (modified)
--- We really need a proper API for this
-local function GetTextureIndex(tex)
-	RefreshEquipmentSetIconInfo()
-	tex = tex:lower()
-	local numicons = GetNumMacroIcons()
-	for i=INVSLOT_FIRST_EQUIPPED,INVSLOT_LAST_EQUIPPED do if GetInventoryItemTexture("player", i) then numicons = numicons + 1 end end
-	for i=1,numicons do
-		local texture, index = GetEquipmentSetIconInfo(i)
-		if texture and texture:lower() == tex then return index end
-	end
-	return 1
-end
-
-local function ModifiedClick(button, name, icon)
-	if(IsShiftKeyDown()) then
-		local dialog = StaticPopup_Show('CONFIRM_OVERWRITE_EQUIPMENT_SET', name)
-		dialog.data = name
-		dialog.selectedIcon = GetTextureIndex(icon)
-	elseif(IsControlKeyDown()) then
-		local dialog = StaticPopup_Show('CONFIRM_DELETE_EQUIPMENT_SET', name)
-		dialog.data = name
-	else
-		EquipmentManager_EquipSet(name)
+local LDB
+local pendingName
+local pendingIcon

-		if(InCombatLockdown()) then
-			pending.name, pending.icon = name, icon
-			addon:RegisterEvent('PLAYER_REGEN_ENABLED')
-		end
-	end
-end
+local menu = {}
+local locale = select(2, ...).L
+local parent = CreateFrame('Frame', 'tester')

 local function EquipmentLocated(name)
 	for slot, location in pairs(GetEquipmentSetLocations(name)) do
@@ -70,90 +35,132 @@ local function EquipmentLocated(name)
 	return true
 end

-function broker:OnTooltipShow()
+local function UpdateDisplay()
+	if(InCombatLockdown() and pendingName) then
+		LDB.text = '|cffff0000'..pendingName
+		LDB.icon = pendingIcon
+	else
+		for index = 1, GetNumEquipmentSets() do
+			local name, icon = GetEquipmentSetInfo(index)
+			if(EquipmentLocated(name)) then
+				LDB.text = name
+				LDB.icon = icon
+				return
+			else
+				LDB.text = UNKNOWN
+				LDB.icon = [=[Interface\Icons\INV_Misc_QuestionMark]=]
+			end
+		end
+	end
+end
+
+-- Borrowed from tekkub's EquipSetUpdater
+local function GetTextureIndex(texture)
+	RefreshEquipmentSetIconInfo()
+
+	local numIcons = GetNumMacroIcons()
+	for index = INVSLOT_FIRST_EQUIPPED, INVSLOT_LAST_EQUIPPED do
+		if(GetInventoryItemTexture('player', index)) then
+			numIcons = numIcons + 1
+		end
+	end
+
+	for index = 1, numIcons do
+		local icon, textureIndex = GetEquipmentSetIconInfo(index)
+		if(icon == texture) then
+			return textureIndex
+		end
+	end
+end
+
+local function ModifiedClick(button, name, icon)
+	if(IsShiftKeyDown() and not pendingName) then
+		local dialog = StaticPopup_Show('CONFIRM_OVERWRITE_EQUIPMENT_SET', name)
+		dialog.data = name
+		dialog.selectedIcon = GetTextureIndex(icon)
+	elseif(IsControlKeyDown() and not pendingName) then
+		local dialog = StaticPopup_Show('CONFIRM_DELETE_EQUIPMENT_SET', name)
+		dialog.data = name
+	else
+		EquipmentManager_EquipSet(name)
+
+		if(InCombatLockdown()) then
+			parent:RegisterEvent('PLAYER_REGEN_ENABLED')
+
+			pendingName = name
+			pendingIcon = icon
+			UpdateDisplay()
+		end
+	end
+end
+
+local function OnTooltipShow(self)
 	self:AddLine('|cff0090ffBroker Equipment|r')
-	self:AddLine(ns.L[1])
-	self:AddLine(ns.L[2])
+	self:AddLine(locale[1])
+	self:AddLine(locale[2])
+	self:AddLine(locale[3])
+	self:AddLine(locale[4])
 end

-function broker:OnClick(button)
+local function OnClick(self, button)
 	if(button ~= 'RightButton' and GetNumEquipmentSets() > 0) then
-		ToggleDropDownMenu(1, nil, addon, self, 0, 0)
+		ToggleDropDownMenu(1, nil, parent, self, 0, 0)
 	else
-		local paperdoll = PaperDollFrame:IsVisible()
-		if(not paperdoll) then
+		if(not PaperDollFrame:IsVisible()) then
 			ToggleCharacter('PaperDollFrame')
-			GearManagerDialog:Show()
-		elseif(paperdoll and not GearManagerDialog:IsVisible()) then
+		end
+
+		if(not GearManagerDialog:IsVisible()) then
 			GearManagerDialog:Show()
 		end
 	end
 end

-function addon:initialize(...)
-	local info = wipe(self.info)
-	info.isTitle = 1
-	info.notCheckable = 1
-	info.text = '|cff0090ffBroker Equipment|r\n '
-	UIDropDownMenu_AddButton(info, ...)
-
-	wipe(info)
+local function CreateMenu()
+	menu = wipe(menu)
 	for index = 1, GetNumEquipmentSets() do
 		local name, icon = GetEquipmentSetInfo(index)
-		info.text = '|T'..icon..':20|t '..name
-		info.arg1 = name
-		info.arg2 = icon
-		info.func = ModifiedClick
-		info.checked = EquipmentLocated(name) or pending.name and pending.name == name
-		UIDropDownMenu_AddButton(info, ...)
-	end
-
-	if(SHOW_NEWBIE_TIPS == '1') then
-		wipe(info)
-		info.disabled = 1
-		info.notCheckable = 1
-
-		info.text = ns.L[3]
-		UIDropDownMenu_AddButton(info, ...)
+		menu.text = string.format('|T%s:26|t %s%s', icon, EquipmentLocated(name) and '|cff00ff00' or pendingName and pendingName == name and '|cffff0000' or '', name)
+		menu.func = ModifiedClick
+		menu.arg1 = name
+		menu.arg2 = icon
+		menu.notCheckable = 1

-		info.text = ns.L[4]
-		UIDropDownMenu_AddButton(info, ...)
+		UIDropDownMenu_AddButton(menu)
 	end
 end

-function addon:PLAYER_LOGIN()
-	self.info = {}
-	self.displayMode = 'MENU'
+function parent:PLAYER_LOGIN()
+	LDB = LibStub('LibDataBroker-1.1'):NewDataObject('Broker_Equipment', {
+		icon = [=[Interface\Icons\INV_Misc_QuestionMark]=],
+		iconCoords = {0.08, 0.92, 0.08, 0.92},
+		text = UNKNOWN,
+		type = 'data source',
+		OnTooltipShow = OnTooltipShow,
+		OnClick = OnClick,
+	})
+
 	self:RegisterEvent('UNIT_INVENTORY_CHANGED')
-	self:UNIT_INVENTORY_CHANGED()
-end
+	self:RegisterEvent('EQUIPMENT_SETS_CHANGED')
+	self.EQUIPMENT_SETS_CHANGED = UpdateDisplay

-function addon:UNIT_INVENTORY_CHANGED(event, unit)
-	if(unit and unit ~= 'player') then return end
+	self.initialize = CreateMenu
+	self.displayMode = 'MENU'

-	if(InCombatLockdown() and pending.name) then
-		broker.text = '|cffff0000'..pending.name
-		broker.icon = pending.icon
-	else
-		for index = 1, GetNumEquipmentSets() do
-			local name, icon = GetEquipmentSetInfo(index)
-			if(EquipmentLocated(name)) then
-				broker.text = name
-				broker.icon = icon
-				return
-			else
-				broker.text = UNKNOWN
-				broker.icon = [=[Interface\Icons\INV_Misc_QuestionMark]=]
-			end
-		end
+	UpdateDisplay()
+end
+
+function parent:UNIT_INVENTORY_CHANGED(unit)
+	if(unit == 'player') then
+		UpdateDisplay()
 	end
 end

-function addon:PLAYER_REGEN_ENABLED(event)
-	ModifiedClick(nil, pending.name, pending.icon)
-	self:UnregisterEvent(event)
-	pending = {}
+function parent:PLAYER_REGEN_ENABLED()
+	self:UnregisterEvent('PLAYER_REGEN_ENABLED')
+	ModifiedClick(nil, pendingName, pendingIcon)
+	pendingName, pendingIcon = nil, nil
 end

-addon:SetScript('OnEvent', function(self, event, ...) self[event](self, event, ...) end)
-addon:RegisterEvent('PLAYER_LOGIN')
+parent:SetScript('OnEvent', function(self, event, ...) self[event](self, ...) end)
+parent:RegisterEvent('PLAYER_LOGIN')
diff --git a/Localization.lua b/Localization.lua
index d62e738..204653d 100644
--- a/Localization.lua
+++ b/Localization.lua
@@ -7,46 +7,46 @@ local _, ns = ...
 ns.L = GetLocale() == 'deDE' and {
 	'Links-Klick um Set zu wechseln',
 	'Rechts-Klocl um den Ausrüstungsmanager zu öffnen',
-	'|cff00ff00Shift + Links-Klick um Set zu aktualisieren|r',
-	'|cff00ff00Strg + Links-Klick um Set zu löschen|r',
+	'Shift + Links-Klick um Set zu aktualisieren',
+	'Strg + Links-Klick um Set zu löschen',
 } or GetLocale() == 'frFR' and {
 	'Clic gauche pour changer d\'équipement',
 	'Clic droit pour ouvrir le gestionnaire d\'équipement',
-	'|cff00ff00Maj-clic pour mettre à jour le set|r',
-	'|cff00ff00Ctrl-clic pour supprimer le set|r',
+	'Maj-clic pour mettre à jour le set',
+	'Ctrl-clic pour supprimer le set',
 } or GetLocale() == 'zhCN' and {
 	'左键点击切换套装',
 	'右键打开套装管理器',
-	'|cff00ff00Shift点击覆盖套装|r',
-	'|cff00ff00Ctrl点击删除套装|r',
+	'Shift点击覆盖套装',
+	'Ctrl点击删除套装',
 } or GetLocale() == 'zhTW' and {
 	'左鍵點擊切換套裝',
 	'右鍵點擊打開套裝管理器',
-	'|cff00ff00Shift點擊覆蓋套裝|r',
-	'|cff00ff00Ctrl點擊刪除套裝|r',
+	'Shift點擊覆蓋套裝',
+	'Ctrl點擊刪除套裝',
 } or GetLocale() == 'koKR' and {
 	'좌-클릭 세트 변경',
 	'우-클릭 장비 관리창 열기',
-	'|cff00ff00Shift-클릭 하면 세트 업데이트|r',
-	'|cff00ff00Ctrl-클릭 하면 세트 삭제|r',
+	'Shift-클릭 하면 세트 업데이트',
+	'Ctrl-클릭 하면 세트 삭제',
 } or GetLocale() == 'esES' and {
 	'Click izquierdo para cambiar tu set',
 	'Click derecho para abrir el Administrador de Equipo',
-	'|cff00ff00Shift-click para modificar set|r',
-	'|cff00ff00Ctrl-click para eliminar set',
+	'Shift-click para modificar set',
+	'Ctrl-click para eliminar set',
 } or GetLocale() == 'esMX' and {
 	'Click izquierdo para cambiar tu set',
 	'Click derecho para abrir el Administrador de Equipo',
-	'|cff00ff00Shift-click para modificar set|r',
-	'|cff00ff00Ctrl-click para eliminar set|r',
+	'Shift-click para modificar set',
+	'Ctrl-click para eliminar set',
 } or GetLocale() == 'ruRU' and {
 	'Левый клик, чтобы изменить комплект',
 	'Правый клик, чтобы открыть менеджер экипировки',
-	'|cff00ff00Shift-клик, чтобы обновить комплект|r',
-	'|cff00ff00Ctrl-клик, чтобы удалить комплект|r',
+	'Shift-клик, чтобы обновить комплект',
+	'Ctrl-клик, чтобы удалить комплект',
 } or {
 	'Left-click to change your set',
 	'Right-click to open GearManager',
-	'|cff00ff00Shift-click to update set|r',
-	'|cff00ff00Ctrl-click to delete set|r',
+	'Shift-click to update set',
+	'Ctrl-click to delete set',
 }