/Tukui/modules/tooltip/talents.lua

http://github.com/Asphyxia/Tukui · Lua · 171 lines · 128 code · 10 blank · 33 comment · 48 complexity · eeaa4e9ef48455b9346ab01a15bf4108 MD5 · raw file

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