View Single Post
12-14-12, 12:35 AM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I made the mistake of looking at your code.

It's actually not that bad, but I rewrote it anyway. AceAddon seemed like overkill, so I removed it. I also removed all of the localization in the Lua file in favor of just pulling the translated name from the TOC via GetAddOnMetadata, and replaced all of the unnecessary upvalues with "GLOBALS: ..." comments which will keep FindGlobals from complaining, though personally I only use FindGlobals to check for set globals, not read ones.

I also moved to a table for storing NPC/item/quest IDs. Localized names are looked up on-demand and cached in the table. This allows easily sorting the display by any criteria, while retaining whatever order you find most convenient in the source code.

By default, all quests are shown, and tooltip lines are sorted by NPC name. Click the plugin to toggle between sorting by NPC name or item name. Shift-click to toggle between showing or hiding completed quests. You could easily save the sort and filter between sessions by changing the "currentSort" and "hideComplete" local variables to more distinctly-named global variables and listing them as SavedVariables in the TOC. I did not add any tooltip hints, since that would require localization, but it would be easy to add if you wanted.

I also aligned each column so my eyes can stop bleeding while looking at it, and highlighted each line green or red depending on quest completion status.

Feel free to make this the official version.

Code:
-- GLOBALS: sort, GetAddOnMetadata, GetFactionInfoByID, GetItemInfo, IsQuestFlaggedCompleted
-- GLOBALS: COMPLETE, ITEMS, NO, YES
-- GLOBALS: LibStub

local ADDON = ...
local ADDON_TITLE = GetAddOnMetadata(ADDON, "Title")
local TILLERS

local qtip = LibStub("LibQTip-1.0")

local npcs = {
	{ factionID = 1273, itemID = 74643, questID = 30439 }, -- Jogu the Drunk
	{ factionID = 1275, itemID = 74651, questID = 30386 }, -- Ella
	{ factionID = 1276, itemID = 74649, questID = 30396 }, -- Old Hillpaw
	{ factionID = 1277, itemID = 74647, questID = 30402 }, -- Chee Chee
	{ factionID = 1278, itemID = 74645, questID = 30408 }, -- Sho
	{ factionID = 1279, itemID = 74642, questID = 30414 }, -- Haohan Mudclaw
	{ factionID = 1280, itemID = 74652, questID = 30433 }, -- Tina Mudclaw
	{ factionID = 1281, itemID = 74644, questID = 30390 }, -- Gina Mudclaw
	{ factionID = 1282, itemID = 74655, questID = 30427 }, -- Fish Fellreed
	{ factionID = 1283, itemID = 74654, questID = 30421 }, -- Farmer Fung
}

local currentSort, hideComplete, tooltip = "NAME"
local sortByName = function(a, b)
	return a.name < b.name
end
local sortByItem = function(a, b)
	return a.item < b.item
end

LibStub("LibDataBroker-1.1"):NewDataObject(ADDON, {
	type = "data source",
	text = ADDON_TITLE,
	icon = [[Interface/ICONS/Achievement_Profession_ChefHat]],
	OnClick = function(self)
		if IsShiftKeyDown() then
			hideComplete = not hideComplete
		else
			currentSort = currentSort == "NAME" and "ITEM" or "NAME"
			sort(npcs, currentSort == "NAME" and sortByName or sortByItem)
		end
		self:GetScript("OnLeave")(self)
		self:GetScript("OnEnter")(self)
	end,
	OnEnter = function(self)
		if not TILLERS then
			TILLERS = GetFactionInfoByID(1272)
			for i = 1, #npcs do
				local npc = npcs[i]
				npc.name = GetFactionInfoByID(npc.factionID)
			end
			sort(npcs, sortByName)
		end

		tooltip = qtip:Acquire("FeedTillersTT", 3, "LEFT", "LEFT", "RIGHT")
		tooltip:AddHeader(TILLERS, ITEMS, COMPLETE)
		for i = 1, #npcs do
			local npc = npcs[i]
			if not npc.item then
				npc.item = GetItemInfo(npc.itemID)
			end
			if not IsQuestFlaggedCompleted(npc.questID) then
				local line = tooltip:AddLine(npc.name, npc.item, NO)
				tooltip:SetLineColor(line, 1, 0.1, 0.1, 0.3)
			elseif not hideComplete then
				local line = tooltip:AddLine(npc.name, npc.item, YES)
				tooltip:SetLineColor(line, 0.1, 1, 0.1, 0.3)
			end
		end
		tooltip:SmartAnchorTo(self)
		tooltip:Show()
	end,
	OnLeave = function(self)
		if qtip:IsAcquired("FeedTillersTT") then
			qtip:Release(tooltip)
		end
		tooltip = nil
	end
})
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.

Last edited by Phanx : 12-14-12 at 02:13 AM.
  Reply With Quote