PageRenderTime 225ms CodeModel.GetById 122ms app.highlight 8ms RepoModel.GetById 93ms app.codeStats 0ms

/Tukui/modules/tooltip/talents.lua

http://github.com/Asphyxia/Tukui
Lua | 171 lines | 128 code | 10 blank | 33 comment | 34 complexity | eeaa4e9ef48455b9346ab01a15bf4108 MD5 | raw file
  1local T, C, L = unpack(select(2, ...))
  2
  3if not C["tooltip"].enable then return end
  4
  5----------------------------------------------------------------------------------------
  6--	Target Talents(TipTacTalents by Aezay)
  7----------------------------------------------------------------------------------------
  8local gtt = GameTooltip
  9local GetTalentTabInfo = GetTalentTabInfo
 10
 11-- Constants
 12local TALENTS_PREFIX = TALENTS..":|cffffffff "
 13local CACHE_SIZE = 25	-- Change cache size here (Default 25)
 14local INSPECT_DELAY = 0.2
 15local INSPECT_FREQ = 2
 16
 17-- Variables
 18local ttt = CreateFrame("Frame", "TipTacTalents")
 19local cache = {}
 20local current = {}
 21
 22-- Time of the last inspect reuqest. Init this to zero, just to make sure. This is a global so other addons could use this variable as well
 23lastInspectRequest = 0
 24
 25-- Allow these to be accessed through other addons
 26ttt.cache = cache
 27ttt.current = current
 28ttt:Hide()
 29
 30----------------------------------------------------------------------------------------
 31--	Gather Talents
 32----------------------------------------------------------------------------------------
 33local function GatherTalents(isInspect)
 34	-- Inspect functions will always use the active spec when not inspecting
 35	local group = GetActiveTalentGroup(isInspect);
 36	-- Get points per tree, and set "primaryTree" to the tree with most points
 37	local primaryTree = 1;
 38	for i = 1, 3 do
 39		local _, _, _, _, pointsSpent = GetTalentTabInfo(i,isInspect,nil,group);
 40		current[i] = pointsSpent;
 41		if (current[i] > current[primaryTree]) then
 42			primaryTree = i;
 43		end
 44	end
 45	local _, tabName = GetTalentTabInfo(primaryTree,isInspect,nil,group);
 46	current.tree = tabName;
 47	-- Customise output. Use TipTac setting if it exists, otherwise just use formatting style one.
 48	local talentFormat = (1);
 49	if (current[primaryTree] == 0) then
 50		current.format = L.tooltip_no_talent;
 51	elseif (talentFormat == 1) then
 52		current.format = current.tree.." ("..current[1].."/"..current[2].."/"..current[3]..")";
 53	elseif (talentFormat == 2) then
 54		current.format = current.tree;
 55	elseif (talentFormat == 3) then
 56		current.format = current[1].."/"..current[2].."/"..current[3];
 57	end
 58	-- Set the tips line output, for inspect, only update if the tip is still showing a unit!
 59	if (not isInspect) then
 60		gtt:AddLine(TALENTS_PREFIX..current.format);
 61	elseif (gtt:GetUnit()) then
 62		for i = 2, gtt:NumLines() do
 63			if ((_G["GameTooltipTextLeft"..i]:GetText() or ""):match("^"..TALENTS_PREFIX)) then
 64				_G["GameTooltipTextLeft"..i]:SetFormattedText("%s%s",TALENTS_PREFIX,current.format);
 65				-- Do not call Show() if the tip is fading out, this only works with TipTac, if TipTacTalents are used alone, it might still bug the fadeout
 66				if (not gtt.fadeOut) then
 67					gtt:Show();
 68				end
 69				break;
 70			end
 71		end
 72	end
 73	-- Organise Cache
 74	local cacheSize = (CACHE_SIZE);
 75	for i = #cache, 1, -1 do
 76		if (current.name == cache[i].name) then
 77			tremove(cache,i);
 78			break;
 79		end
 80	end
 81	if (#cache > cacheSize) then
 82		tremove(cache,1);
 83	end
 84	-- Cache the new entry
 85	if (cacheSize > 0) then
 86		cache[#cache + 1] = CopyTable(current);
 87	end
 88end
 89
 90----------------------------------------------------------------------------------------
 91--	Event Handling
 92----------------------------------------------------------------------------------------
 93-- OnEvent
 94ttt:SetScript("OnEvent",function(self,event,guid)
 95	self:UnregisterEvent(event);
 96	if (guid == current.guid) then
 97		GatherTalents(1);
 98	end
 99end);
100
101-- OnUpdate
102ttt:SetScript("OnUpdate",function(self,elapsed)
103	self.nextUpdate = (self.nextUpdate - elapsed);
104	if (self.nextUpdate <= 0) then
105		self:Hide();
106		-- Make sure the mouseover unit is still our unit
107		if (UnitGUID("mouseover") == current.guid) then
108			lastInspectRequest = GetTime();
109			self:RegisterEvent("INSPECT_READY");
110			-- Az: Fix the blizzard inspect copypasta code (Blizzard_InspectUI\InspectPaperDollFrame.lua @ line 23)
111			if (InspectFrame) then
112				InspectFrame.unit = "player";
113			end
114			NotifyInspect(current.unit);
115		end
116	end
117end);
118
119-- HOOK: OnTooltipSetUnit
120gtt:HookScript("OnTooltipSetUnit",function(self,...)
121	-- Abort any delayed inspect in progress
122	ttt:Hide();
123	-- Get the unit -- Check the UnitFrame unit if this tip is from a concated unit, such as "targettarget".
124	local _, unit = self:GetUnit();
125	if (not unit) then
126		local mFocus = GetMouseFocus();
127		if (mFocus) and (mFocus.unit) then
128			unit = mFocus.unit;
129		end
130	end
131	-- No Unit or not a Player
132	if (not unit) or (not UnitIsPlayer(unit)) then
133		return;
134	end
135	-- Only bother for players over level 9
136	local level = UnitLevel(unit);
137	if (level > 9 or level == -1) then
138		-- Wipe Current Record
139		wipe(current);
140		current.unit = unit;
141		current.name = UnitName(unit);
142		current.guid = UnitGUID(unit)
143		-- No need for inspection on the player
144		if (UnitIsUnit(unit,"player")) then
145			GatherTalents();
146			return;
147		end
148		-- Show Cached Talents, If Available
149		local cacheLoaded = false;
150		for _, entry in ipairs(cache) do
151			if (current.name == entry.name) then
152				self:AddLine(TALENTS_PREFIX..entry.format);
153				current.tree = entry.tree;
154				current.format = entry.format;
155				current[1], current[2], current[3] = entry[1], entry[2], entry[3];
156				cacheLoaded = true;
157				break;
158			end
159		end
160		-- Queue an inspect request
161		local isInspectOpen = (InspectFrame and InspectFrame:IsShown()) or (Examiner and Examiner:IsShown());
162		if (CanInspect(unit)) and (not isInspectOpen) then
163			local lastInspectTime = (GetTime() - lastInspectRequest);
164			ttt.nextUpdate = (lastInspectTime > INSPECT_FREQ) and INSPECT_DELAY or (INSPECT_FREQ - lastInspectTime + INSPECT_DELAY);
165			ttt:Show();
166			if (not cacheLoaded) then
167				self:AddLine(TALENTS_PREFIX..L.tooltip_loading);
168			end
169		end
170	end
171end)