Skip to content

Commit 714b707

Browse files
committed
Added an Inspect Talents option to player right-click menus to immediately inspect a player's talents without having to open the Inspect window first (closes #53)
1 parent 9f5dd8d commit 714b707

File tree

1 file changed

+90
-5
lines changed

1 file changed

+90
-5
lines changed

modules/exportInspectedBuild.lua

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ local L = TTT.L;
77

88
local LEVEL_CAP = 80;
99

10-
--- @class TalentTreeTweaks_ExportInspectedBuild: AceModule, AceHook-3.0
11-
local Module = Main:NewModule('ExportInspectedBuild', 'AceHook-3.0');
10+
--- @class TalentTreeTweaks_ExportInspectedBuild: AceModule, AceHook-3.0, AceEvent-3.0
11+
local Module = Main:NewModule('ExportInspectedBuild', 'AceHook-3.0', 'AceEvent-3.0');
1212

13+
--- @type table<string, { unit: string, name: string }> # [guid] = info
14+
Module.inspecting = {};
1315
Module.overlayPool = {
1416
--- @type table<BUTTON, true>
1517
active = {},
@@ -46,35 +48,47 @@ function Module:OnInitialize()
4648
self:OnLoadoutMenuOpen(dropdown, rootDescription);
4749
end
4850
end);
51+
for which in pairs(UnitPopupMenus) do
52+
Menu.ModifyMenu('MENU_UNIT_' .. which, function(dropdown, rootDescription, contextData)
53+
if self:IsEnabled() and self.db.inspectTalentsMenuItem then
54+
self:OnUnitPopupMenu(rootDescription, contextData);
55+
end
56+
end);
57+
end
4958
end
5059

5160
function Module:OnEnable()
5261
Util:OnTalentUILoad(function()
5362
self:SetupHook();
5463
end);
64+
self:RegisterEvent("INSPECT_READY");
5565
end
5666

5767
function Module:OnDisable()
5868
self:UnhookAll();
5969
if self.linkButton then self.linkButton:Hide(); end
70+
self:UnregisterAllEvents();
6071
end
6172

6273
function Module:GetDescription()
6374
return
6475
L['Adds a right-click option to the loadout dropdown to export your build.']
6576
.. '\n\n' ..
66-
L['Adds a button to link the currently shown build in chat.'];
77+
L['Adds a button to link the currently shown build in chat.']
78+
.. '\n\n' ..
79+
L['Adds a right-click menu option to directly inspect a player\'s talents.'];
6780
end
6881

6982
function Module:GetName()
70-
return L['Export Loadouts'];
83+
return L['Export / Inspect Loadouts'];
7184
end
7285

7386
function Module:GetOptions(defaultOptionsTable, db)
7487
self.db = db;
7588
local defaults = {
7689
exportOnDropdownRightClick = true,
7790
showLinkInChatButton = true,
91+
inspectTalentsMenuItem = true,
7892
};
7993
for k, v in pairs(defaults) do
8094
if db[k] == nil then
@@ -107,11 +121,82 @@ function Module:GetOptions(defaultOptionsTable, db)
107121
order = counter(),
108122
get = get,
109123
set = set,
110-
}
124+
};
125+
defaultOptionsTable.args.inspectTalentsMenuItem = {
126+
type = 'toggle',
127+
name = L['Inspect Talents'],
128+
desc = L['Adds a right-click menu option to directly inspect a player\'s talents.'],
129+
order = counter(),
130+
get = get,
131+
set = set,
132+
};
111133

112134
return defaultOptionsTable;
113135
end
114136

137+
--- @param rootDescription RootMenuDescriptionProxy
138+
function Module:OnUnitPopupMenu(rootDescription, contextData)
139+
for i, elementDescription in rootDescription:EnumerateElementDescriptions() do
140+
if MenuUtil.GetElementText(elementDescription) == INSPECT then
141+
rootDescription:Insert(MenuUtil.CreateButton(L['Inspect Talents'], function()
142+
local unit = contextData.unit;
143+
if not unit then return; end
144+
local guid = contextData.playerLocation and contextData.playerLocation:GetGUID();
145+
self.inspecting[guid] = { unit = unit, name = UnitNameUnmodified(unit) };
146+
NotifyInspect(unit);
147+
end), i + 1);
148+
149+
break;
150+
end
151+
end
152+
end
153+
154+
--- @param unit string
155+
--- @param guid string
156+
--- @return string?
157+
local function checkUnitGuid(unit, guid)
158+
if UnitGUID(unit) ~= guid then
159+
unit = UnitTokenFromGUID(guid);
160+
if not unit then
161+
return nil;
162+
end
163+
end
164+
165+
return unit;
166+
end
167+
168+
function Module:INSPECT_READY(_, guid)
169+
local info = self.inspecting[guid];
170+
if not info then return; end
171+
self.inspecting[guid] = nil;
172+
local unit = checkUnitGuid(info.unit, guid);
173+
if not unit then
174+
Main:Print(string.format(L['Failed to inspect %s'], info.name));
175+
return;
176+
end
177+
local level = UnitLevel(unit);
178+
179+
local ticker;
180+
local startTime = GetTime();
181+
ticker = C_Timer.NewTicker(0, function()
182+
local unit = checkUnitGuid(info.unit, guid);
183+
if not unit or (GetTime() - startTime) > 10 then
184+
Main:Print(string.format(L['Failed to inspect %s'], info.name));
185+
ticker:Cancel();
186+
return;
187+
end
188+
local exportString = C_Traits.GenerateInspectImportString(unit);
189+
if not exportString or '' == exportString then return; end
190+
ticker:Cancel();
191+
192+
local talentFrame = Util:GetTalentContainerFrame();
193+
if not talentFrame or not talentFrame.SetInspectString then return end
194+
talentFrame:SetInspectString(exportString, level);
195+
if not talentFrame:IsShown() then
196+
ShowUIPanel(talentFrame);
197+
end
198+
end);
199+
end
115200

116201
function Module:SetupHook()
117202
local talentsTab = Util:GetTalentFrame();

0 commit comments

Comments
 (0)