-
Notifications
You must be signed in to change notification settings - Fork 0
/
NameplateContextMenu.lua
102 lines (86 loc) · 3.2 KB
/
NameplateContextMenu.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---@diagnostic disable: undefined-field
-- Localize globals
local _G = _G
local CreateFrame, UIParent, InCombatLockdown, C_NamePlate, UnitCanAttack, UnitIsUnit, Plater, C_Timer = _G.CreateFrame,
_G.UIParent, _G.InCombatLockdown, _G.C_NamePlate, _G.UnitCanAttack, _G.UnitIsUnit, _G.Plater, _G.C_Timer
local GetNamePlateForUnit, GetNamePlates = C_NamePlate.GetNamePlateForUnit, C_NamePlate.GetNamePlates
-- Create the main frame for event handling
local NameplateContextFrame = CreateFrame("Frame", "NameplateContextFrame", UIParent)
NameplateContextFrame:Hide()
NameplateContextFrame.attachedVisibleFrames = {}
NameplateContextFrame:RegisterEvent("NAME_PLATE_UNIT_ADDED")
NameplateContextFrame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
NameplateContextFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
NameplateContextFrame:RegisterEvent("LOADING_SCREEN_DISABLED")
-- Create buttons for the player's and enemy/target nameplates
local function CreatePlateButton(name)
local button = CreateFrame("BUTTON", name, UIParent, "SecureUnitButtonTemplate")
button:EnableMouse(true)
button:RegisterForClicks("LeftButtonUp", "RightButtonUp")
button:SetSize(300, 70)
button:Hide()
return button
end
local PersonalPlate_Btn = CreatePlateButton("PersonalPlate_Btn")
local EnemyPlate_Btn = CreatePlateButton("EnemyPlate_Btn")
-- Helper functions
local function AnchorBtn(Button, frame, unit)
Button:ClearAllPoints()
Button:SetPoint("CENTER", frame, "CENTER", -20, -10)
Button:SetAttribute('unit', unit)
Button:SetAttribute('type1', 'target') -- Left-click to target the unit
Button:SetAttribute('type2', 'togglemenu') -- Right-click for context menu
Button:Show()
end
local function UpdateBtnPosition()
if InCombatLockdown() then
return
end
local playerFrame = GetNamePlateForUnit("player")
if playerFrame and Plater and playerFrame.unitFrame.PlaterOnScreen then
AnchorBtn(PersonalPlate_Btn, playerFrame, "player")
end
for _, nameplate in ipairs(GetNamePlates()) do
local nameplateUnit = nameplate.namePlateUnitToken
if UnitCanAttack("player", nameplateUnit) then
AnchorBtn(EnemyPlate_Btn, nameplate, nameplateUnit)
end
end
end
local function HandlePlate_Added(unit)
if InCombatLockdown() then
return
end
local frame = GetNamePlateForUnit(unit)
if not frame then
return
end
if UnitIsUnit(unit, "player") then
AnchorBtn(PersonalPlate_Btn, frame, "player") -- Personal nameplate
elseif UnitCanAttack("player", unit) then
AnchorBtn(EnemyPlate_Btn, frame, unit) -- Enemy nameplate
end
UpdateBtnPosition()
end
local function HandlePlate_Removed(unit)
if InCombatLockdown() then
return
end
if UnitIsUnit(unit, "player") then
PersonalPlate_Btn:Hide() -- Hide when personal plate is removed
elseif UnitCanAttack("player", unit) then
EnemyPlate_Btn:Hide() -- Hide when enemy plate is removed
end
UpdateBtnPosition()
end
local function OnEvent_Callback(_, event, unit)
if event == "NAME_PLATE_UNIT_ADDED" then
HandlePlate_Added(unit)
elseif event == "NAME_PLATE_UNIT_REMOVED" then
HandlePlate_Removed(unit)
else
C_Timer.After(1, UpdateBtnPosition)
end
end
-- Register the callback function
NameplateContextFrame:SetScript("OnEvent", OnEvent_Callback)