view Spotlight.lua @ 8:a30c29933ebc

Fix for a minor bug that would occur if Spotlight loaded out of order with LDB
author Vynn <mischivin@gmail.com>
date Wed, 01 Feb 2017 13:06:22 -0500
parents 59c355620405
children cbf645630277
line wrap: on
line source
local dataobj = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("Spotlight", {
	type = "data source", 
	text = "Nothing to track!", 
	icon = GetInventoryItemTexture("player", 16),
	OnClick = function(clickedframe, button)
		Spotlight.ShowPercent = not Spotlight.ShowPercent;
		Spotlight.Update();
	end,
})

SLASH_SPOTLIGHT1, SLASH_SPOTLIGHT2 = "/spotlight", "/Spotlight";

local function SlashHandler(msg)
	print("|T" .. dataobj.icon .. ":0|t |cffe5cc80Spotlight|r - Artifact Hidden Skin Progress:");
	if Spotlight.IDs then
		for key, value in pairs(Spotlight.IDs) do
			print("       |cffe5cc80" .. key .. " - " .. Spotlight.Format(key, false) .. " (" .. Spotlight.Format(key, true) .. ")|r");
		end
	end
end

function SlashCmdList.SPOTLIGHT(msg, editbox)
	SlashHandler(msg);
end

local function Initialize ()
	if not Spotlight.Status then
		Spotlight.Status = {
			Dungeons = {
				Completed = false,
				Progress = 0,
				Max = 100,
			},
			WorldQuests = {
				Completed = false,
				Progress = 0,
				Max = 200,
			},
			PvP = {
				Completed = false,
				Progress = 0,
				Max = 1000,
			},
		};
		Spotlight.ShowPercent = true;
	end

	Spotlight.IDs = {
		Dungeons = 11152,
		WorldQuests = 11153,
		PvP = 11154,
	};

	Spotlight.Calculate = function (key)
		local value = 0;
		for index = 1, GetAchievementNumCriteria(Spotlight.IDs[key]) do
				value = value + select(4, GetAchievementCriteriaInfo(Spotlight.IDs[key], index));
		end
		return value;
	end

	Spotlight.GetColor = function (value)
		value = value * 2;
		local r = (2 - value);
		local g = value;
		local b = 0;
		
		if r > 1 then r = 1 end
		if g > 1 then g = 1 end
		if b > 1 then b = 1 end

		r = string.format("%i", r * 255);
		g = string.format("%i", g * 255);
		b = string.format("%i", b * 255);

		return "ff" .. string.format("%02x", r) .. string.format("%02x", g) .. string.format("%02x", b);
	end

	Spotlight.Format = function (key, showPercent)
		local value = Spotlight.Status[key].Progress / Spotlight.Status[key].Max;
		if showPercent then
			value = string.format("|c" .. Spotlight.GetColor(value) .. "%.1f|cffffffff%%|r", value * 100);
		else
			value = "|c" .. Spotlight.GetColor(value) .. Spotlight.Status[key].Progress .. "|cffffffff/" .. Spotlight.Status[key].Max .. "|r";
		end
		return value;
	end

	Spotlight.Update = function (self)
		local output = "";
		for key, value in pairs(Spotlight.IDs) do
			Spotlight.Status[key].Completed = select(3, GetAchievementCriteriaInfo(value,1));
			if not Spotlight.Status[key].Completed then
				Spotlight.Status[key].Progress = Spotlight.Calculate(key);
				if output == "" then 
				else
					output = output .. " ||";
				end
				if Spotlight.Status[key].Progress > 0 then
					output = output .. string.format("|cffe5cc80 %.1s", key) .. ":|r " .. Spotlight.Format(key, Spotlight.ShowPercent);
				end
			end
		end
		dataobj.icon = GetInventoryItemTexture("player", 16);
		dataobj.text = output;
	end

	Spotlight.Update();

	return true
end

function dataobj:OnTooltipShow()
	self:AddLine("|cffe5cc80Spotlight - Hidden Artifact Skins|r|n|n");
	for k, v in pairs(Spotlight.IDs) do
		self:AddDoubleLine(k .. ":", Spotlight.Format(k, false) .. " (" .. Spotlight.Format(k, true) .. ")");
	end
	self:AddLine("|nClick to toggle display method");
end

function dataobj:OnEnter()
	GameTooltip:SetOwner(self, "ANCHOR_NONE");
	GameTooltip:SetPoint("TOP", self, "BOTTOM");
	GameTooltip:ClearLines();
	dataobj.OnTooltipShow(GameTooltip);
	GameTooltip:Show();
end

function dataobj:OnLeave()
	GameTooltip:Hide()
end

local function EventHandler(self, event, ...)
	if event == "VARIABLES_LOADED" then
		if Initialize() then 
			print("|T" .. GetInventoryItemTexture("player", 16) .. ":0|t |cffe5cc80Spotlight v" .. GetAddOnMetadata("Spotlight", "Version") .. " Loaded!|r")
		end
	end
	if event == "CRITERIA_UPDATE" or "PLAYER_ENTERING_WORLD" then
		Spotlight.Update();
	end
end

local EventListener = CreateFrame("FRAME", "Spotlight");
EventListener:RegisterEvent("VARIABLES_LOADED");
EventListener:RegisterEvent("CRITERIA_UPDATE");
EventListener:RegisterEvent("PLAYER_ENTERING_WORLD");
EventListener:SetScript("OnEvent", EventHandler);