-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrade.lua
More file actions
156 lines (134 loc) · 7.12 KB
/
Copy pathTrade.lua
File metadata and controls
156 lines (134 loc) · 7.12 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
-- ============================================================
-- Trade.lua — Automatic inventory on trade.
--
-- When the player initiates a trade with a tracked bot:
-- • Their inventory frame opens automatically.
-- • Dragging an item from the inventory onto a trade slot
-- whispers "t <itemlink>" to offer it for trade.
-- • Right-clicking a filled trade slot whispers "t <itemlink>"
-- to remove it (the trade command toggles the item on/off).
-- • The inventory frame closes when the trade window closes.
-- ============================================================
local NS = CleanBotNS
-- ── Active trade state ───────────────────────────────────────────────────
-- The bot key resolved when TRADE_SHOW fires; held so TRADE_CLOSED and
-- the drag/right-click paths can reference the same bot without re-resolving.
local activeTradeKey = nil ---@type string|nil
-- Accessor used by Inventory.lua's drag hit-test to check whether
-- a trade with a specific bot is currently active.
---@return string|nil The active trade partner's key, or nil when no bot trade is open.
NS.CB_GetActiveTradeKey = function()
return activeTradeKey
end
-- ── Right-click overlays on the bot's trade slots ────────────────────────
-- TradeRecipientItem frames are plain Frames (not Buttons) so HookScript
-- "OnClick" is unavailable. Instead we lay invisible Buttons on top of each
-- slot that capture right-clicks and forward give commands to the bot.
-- Left-clicks are intentionally not consumed so normal trade UI behavior
-- (tooltip on hover, etc.) is unaffected.
-- Slots to cover: 6 regular trade slots + the no-trade enchant slot (index 7).
-- GetTradeTargetItemLink(7) corresponds to the enchant/no-trade slot.
local TRADE_SLOTS = {
{ frame = "TradeRecipientItem1", index = 1 },
{ frame = "TradeRecipientItem2", index = 2 },
{ frame = "TradeRecipientItem3", index = 3 },
{ frame = "TradeRecipientItem4", index = 4 },
{ frame = "TradeRecipientItem5", index = 5 },
{ frame = "TradeRecipientItem6", index = 6 },
{ frame = "TradeRecipientItem7", index = 7 },
}
-- Keyed by slot frame reference so CB_DragOnUpdate can LockHighlight/UnlockHighlight
-- the correct overlay during a drag without touching native mouse events.
NS.tradeSlotOverlays = {}
--- Creates the invisible right-click Button overlays over the partner's trade slots.
--- Called once at PLAYER_LOGIN; overlays start hidden and are shown during bot trades.
local function CB_CreateTradeSlotOverlays()
for _, entry in ipairs(TRADE_SLOTS) do
local slot = _G[entry.frame]
if slot then
local slotIndex = entry.index
-- The slot frame is 153x37 (icon + name text). The 37x37 icon child is
-- named TradeRecipientItem{i}ItemButton — anchor the overlay to that so
-- the highlight covers only the icon, matching native hover behavior.
local iconBtn = _G[entry.frame .. "ItemButton"] or slot
local overlay = CreateFrame("Button", "CleanBotTradeOverlay" .. slotIndex, UIParent)
overlay:SetFrameStrata("HIGH")
overlay:SetAllPoints(iconBtn)
overlay:RegisterForClicks("RightButtonUp")
NS.tradeSlotOverlays[slot] = overlay
-- Highlight texture — mirrors the native slot hover since our overlay
-- blocks mouse events from reaching the underlying frame.
-- ElvUI uses its flat normTex at low alpha; Blizz uses ButtonHilight-Square.
local hl = overlay:CreateTexture(nil, "HIGHLIGHT")
hl:SetAllPoints()
if NS.ElvUI_E and NS.ElvUI_E.media and NS.ElvUI_E.media.normTex then
hl:SetTexture(NS.ElvUI_E.media.normTex)
hl:SetVertexColor(1, 1, 1, 0.3)
else
hl:SetTexture("Interface\\Buttons\\ButtonHilight-Square")
hl:SetBlendMode("ADD")
end
NS.CB_AttachTooltip(overlay, function(tt)
local link = GetTradeTargetItemLink(slotIndex)
if not link then return false end
if slot.LockHighlight then slot:LockHighlight() end
tt:SetHyperlink(link)
end, nil, function()
if slot.UnlockHighlight then slot:UnlockHighlight() end
end)
overlay:SetScript("OnClick", function(self, button)
if not activeTradeKey then return end
-- GetTradeTargetItemLink is the correct 3.3.5a API for the partner's slots.
local link = GetTradeTargetItemLink(slotIndex)
if not link then return end
-- "t" toggles the item in the bot's trade window, so re-sending removes it.
local botEntry = CleanBot_PartyBots and CleanBot_PartyBots[activeTradeKey]
if botEntry then
NS.CB_SendBotCommand(botEntry.name, "t " .. NS.CB_CleanItemLink(link))
end
end)
-- Hidden by default; shown only while an active bot trade is open.
overlay:Hide()
end
end
end
-- Shows or hides all trade slot overlays. Called on TRADE_SHOW (bot trade
-- confirmed) and TRADE_CLOSED so overlays never intercept outside of a trade.
---@param visible boolean true to show overlays, false to hide them.
local function CB_SetTradeOverlaysVisible(visible)
for _, overlay in pairs(NS.tradeSlotOverlays) do
if visible then overlay:Show() else overlay:Hide() end
end
end
-- ── Event handler ────────────────────────────────────────────────────────
local eventFrame = CreateFrame("Frame", "CleanBotTradeFrame")
eventFrame:RegisterEvent("TRADE_SHOW")
eventFrame:RegisterEvent("TRADE_CLOSED")
eventFrame:SetScript("OnEvent", function(self, event)
if event == "TRADE_SHOW" then
-- TradeFrameRecipientNameText holds the name of the trade partner.
local recipientName = TradeFrameRecipientNameText and
TradeFrameRecipientNameText:GetText()
if not recipientName or recipientName == "" then return end
local key = strlower(recipientName)
local entry = CleanBot_PartyBots and CleanBot_PartyBots[key]
if not entry then return end -- not one of our bots, do nothing
activeTradeKey = key
CB_SetTradeOverlaysVisible(true)
NS.CB_FetchInventory(key, entry.name)
NS.CB_ShowInventory(key, entry.name, TradeFrame)
elseif event == "TRADE_CLOSED" then
CB_SetTradeOverlaysVisible(false)
if not activeTradeKey then return end
local f = NS.botInventoryFrames and NS.botInventoryFrames[activeTradeKey]
if f and f:IsShown() then f:Hide() end
activeTradeKey = nil
end
end)
-- Hook trade slots once the UI is fully loaded.
eventFrame:RegisterEvent("PLAYER_LOGIN")
eventFrame:HookScript("OnEvent", function(self, event)
if event == "PLAYER_LOGIN" then
CB_CreateTradeSlotOverlays()
end
end)