-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWicksQuestKey.lua
More file actions
341 lines (308 loc) · 12.8 KB
/
WicksQuestKey.lua
File metadata and controls
341 lines (308 loc) · 12.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
WicksQuestKeyDB = WicksQuestKeyDB or {
point = "CENTER", relativePoint = "CENTER", x = 0, y = -150,
}
local C_BG = { 0.05, 0.04, 0.08, 0.97 }
local C_BORDER = { 0.22, 0.18, 0.36, 1 }
local C_GREEN = { 0.31, 0.78, 0.47, 1 }
local C_HOVER = { 0.31, 0.78, 0.47, 0.10 }
local C_MOVE = { 0.64, 0.21, 0.93, 0.20 }
local function NewTexture(parent, layer, c)
local t = parent:CreateTexture(nil, layer)
t:SetColorTexture(unpack(c))
return t
end
local function AddBorder(frame, c)
local t = NewTexture(frame, "BORDER", c); t:SetPoint("TOPLEFT"); t:SetPoint("TOPRIGHT"); t:SetHeight(1)
local b = NewTexture(frame, "BORDER", c); b:SetPoint("BOTTOMLEFT"); b:SetPoint("BOTTOMRIGHT"); b:SetHeight(1)
local l = NewTexture(frame, "BORDER", c); l:SetPoint("TOPLEFT"); l:SetPoint("BOTTOMLEFT"); l:SetWidth(1)
local r = NewTexture(frame, "BORDER", c); r:SetPoint("TOPRIGHT"); r:SetPoint("BOTTOMRIGHT"); r:SetWidth(1)
end
local function AddCornerAccents(frame)
local L, T = 10, 2
for _, anchor in ipairs({ "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT" }) do
local h = NewTexture(frame, "OVERLAY", C_GREEN); h:SetPoint(anchor); h:SetSize(L, T)
local v = NewTexture(frame, "OVERLAY", C_GREEN); v:SetPoint(anchor); v:SetSize(T, L)
end
end
-- TBC Anniversary 2.5.5 moved GetItemCooldown into the C_Container namespace.
-- Resolve once at load and fall back to the legacy global so older clients still work.
local GetItemCooldown = (C_Container and C_Container.GetItemCooldown) or GetItemCooldown
local items = {}
local nextIndex = 1
local btn = CreateFrame("Button", "WicksQuestKeyButton", UIParent, "SecureActionButtonTemplate")
btn:SetSize(52, 52)
btn:SetFrameStrata("MEDIUM")
btn:SetClampedToScreen(true)
btn:SetMovable(true)
-- Mirror the working TotemBar pattern: type1=macro, both macrotext attrs set,
-- and registered for AnyUp + AnyDown. The SAB's internal macro path runs the
-- macrotext through a secure C-level processor (NOT the global RunMacroText,
-- which is nil in this client), so this works where type=item didn't.
btn:SetAttribute("type1", "macro")
btn:RegisterForClicks("AnyUp", "AnyDown")
btn:Hide()
local bg = NewTexture(btn, "BACKGROUND", C_BG); bg:SetAllPoints(btn)
AddBorder(btn, C_BORDER)
AddCornerAccents(btn)
local icon = btn:CreateTexture(nil, "ARTWORK")
icon:SetPoint("TOPLEFT", 4, -4)
icon:SetPoint("BOTTOMRIGHT", -4, 4)
icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
btn.icon = icon
local count = btn:CreateFontString(nil, "OVERLAY", "NumberFontNormalSmall")
count:SetPoint("BOTTOMRIGHT", -3, 3)
btn.count = count
local bindLabel = btn:CreateFontString(nil, "OVERLAY")
bindLabel:SetFont("Fonts\\FRIZQT__.TTF", 12, "OUTLINE")
bindLabel:SetPoint("TOPRIGHT", btn, "TOPRIGHT", -3, -3)
bindLabel:SetTextColor(1, 1, 1, 1)
btn.bindLabel = bindLabel
-- Cooldown countdown shown bottom-center of the icon. Driven by a throttled
-- OnUpdate so we don't recompute every frame.
local cdText = btn:CreateFontString(nil, "OVERLAY")
cdText:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
cdText:SetPoint("BOTTOM", btn, "BOTTOM", 0, 3)
cdText:SetTextColor(1, 0.92, 0.5, 1) -- warm yellow, like Blizzard's action bar CD
btn.cdText = cdText
local hover = NewTexture(btn, "HIGHLIGHT", C_HOVER); hover:SetAllPoints(btn)
local moveTint = NewTexture(btn, "OVERLAY", C_MOVE)
moveTint:SetAllPoints(btn)
moveTint:Hide()
btn:SetScript("OnEnter", function(self)
if #items == 0 then return end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
local cur = items[nextIndex]
if cur then GameTooltip:SetHyperlink("item:" .. cur.itemId) end
if #items > 1 then
GameTooltip:AddLine(" ")
GameTooltip:AddLine(("Quest item %d of %d"):format(nextIndex, #items), 0.31, 0.78, 0.47)
GameTooltip:AddLine("Right-click to switch to the next item.", 0.42, 0.35, 0.54)
end
GameTooltip:Show()
end)
btn:SetScript("OnLeave", function() GameTooltip:Hide() end)
local function ApplyPosition()
local db = WicksQuestKeyDB
btn:ClearAllPoints()
btn:SetPoint(db.point, UIParent, db.relativePoint, db.x, db.y)
end
local function SavePosition()
local point, _, relativePoint, x, y = btn:GetPoint(1)
WicksQuestKeyDB.point = point
WicksQuestKeyDB.relativePoint = relativePoint
WicksQuestKeyDB.x = x
WicksQuestKeyDB.y = y
end
local locked = true
local function SetLocked(state)
if InCombatLockdown() then
print("|cff8a5cf6Wick's Quest Key|r: cannot change lock during combat.")
return
end
locked = state
if locked then
btn:RegisterForDrag()
moveTint:Hide()
else
btn:RegisterForDrag("LeftButton")
moveTint:Show()
if #items == 0 then btn:Show() end
end
end
btn:SetScript("OnDragStart", function(self) if not locked then self:StartMoving() end end)
btn:SetScript("OnDragStop", function(self) self:StopMovingOrSizing(); SavePosition() end)
local function Arm()
local cur = items[nextIndex]
if cur then
if not InCombatLockdown() then
local macro = "/use " .. (cur.name or tostring(cur.itemId))
-- Set BOTH macrotext and macrotext1 (same as the TotemBar pattern).
-- type1=macro reads macrotext1 first, falls back to macrotext on
-- some clients but not all — setting both covers every variant.
btn:SetAttribute("macrotext", macro)
btn:SetAttribute("macrotext1", macro)
end
btn.icon:SetTexture(cur.icon)
btn.icon:Show()
if not InCombatLockdown() then btn:Show() end
else
if not InCombatLockdown() then
btn:SetAttribute("macrotext", nil)
btn:SetAttribute("macrotext1", nil)
if locked then btn:Hide() end
end
btn.icon:Hide()
end
end
local function UpdateCount()
local cur = items[nextIndex]
if cur then
local n = GetItemCount(cur.itemId) or 0
btn.count:SetText(n > 1 and tostring(n) or "")
else
btn.count:SetText("")
end
end
-- Cooldown text: "Xm" for >=60s, whole seconds for >=10s, one decimal under 10s.
local function FormatCD(seconds)
if seconds <= 0 then return "" end
if seconds >= 60 then return ("%dm"):format(math.ceil(seconds / 60)) end
if seconds >= 10 then return ("%d"):format(math.ceil(seconds)) end
return ("%.1f"):format(seconds)
end
-- Recompute and write the CD text from GetItemCooldown for the armed item.
-- Called from the throttled OnUpdate below and on every Arm().
local function UpdateCD()
local cur = items[nextIndex]
if not cur then btn.cdText:SetText(""); return end
local start, duration = GetItemCooldown(cur.itemId)
if not start or start == 0 or not duration or duration <= 1.5 then
-- duration <= GCD: not a real cooldown, hide the text
btn.cdText:SetText("")
return
end
local remaining = (start + duration) - GetTime()
if remaining <= 0 then btn.cdText:SetText(""); return end
btn.cdText:SetText(FormatCD(remaining))
end
-- Throttle the CD recompute to ~10Hz so the text updates smoothly without
-- burning CPU on every frame.
local cdElapsed = 0
btn:SetScript("OnUpdate", function(self, dt)
cdElapsed = cdElapsed + dt
if cdElapsed < 0.1 then return end
cdElapsed = 0
UpdateCD()
end)
local function ShortBind(key)
if not key or key == "" then return "" end
return (key:upper()
:gsub("ALT%-", "a")
:gsub("CTRL%-", "c")
:gsub("SHIFT%-", "s")
:gsub("NUMPAD", "n")
:gsub("BUTTON1", "M1")
:gsub("BUTTON2", "M2")
:gsub("BUTTON3", "M3")
:gsub("MOUSEWHEELUP", "MwU")
:gsub("MOUSEWHEELDOWN", "MwD"))
end
-- Bindings.xml uses the native "CLICK WicksQuestKeyButton:LeftButton" binding
-- name, which Blizzard interprets as a real secure click on the button. So we
-- only need to look up the bound key for the on-button label here. No
-- SetOverrideBindingClick needed.
local QK_BINDING = "CLICK WicksQuestKeyButton:LeftButton"
local function UpdateBindLabel()
local key = GetBindingKey(QK_BINDING)
btn.bindLabel:SetText(ShortBind(key))
end
local function Scan()
if InCombatLockdown() then return end
wipe(items)
local seen = {}
for i = 1, GetNumQuestLogEntries() do
local _, _, _, _, isHeader = GetQuestLogTitle(i)
if not isHeader then
local link, iconTex = GetQuestLogSpecialItemInfo(i)
local itemId = link and tonumber(link:match("item:(%d+)"))
if itemId and not seen[itemId] then
seen[itemId] = true
local cachedName, _, _, _, _, _, _, _, _, cachedIcon = GetItemInfo(itemId)
items[#items + 1] = {
itemId = itemId,
name = cachedName or link:match("%[(.-)%]") or ("item:" .. itemId),
icon = iconTex or cachedIcon or 134400,
}
end
end
end
if nextIndex > #items or nextIndex < 1 then nextIndex = 1 end
Arm()
UpdateCount()
end
-- HookScript instead of SetScript: SecureActionButtonTemplate's built-in OnClick
-- is the code path that resolves type1=macro / type1=item and fires the action.
-- Replacing it with SetScript silently stopped the action from running.
-- HookScript appends our handler so the secure click still happens.
-- HookScript so Blizzard's secure OnClick handler still runs and fires the
-- macrotext for left-click. Our hook only handles the right-click cycle.
-- AnyUp+AnyDown registration makes OnClick fire twice per click (down + up),
-- so filter to `down` only or the cycle would advance twice.
btn:HookScript("OnClick", function(self, button, down)
if button == "RightButton" and down and not InCombatLockdown() and #items > 1 then
nextIndex = (nextIndex % #items) + 1
Arm()
UpdateCount()
UpdateCD()
end
end)
-- Left-click / keybind use the current item but DO NOT advance the cycle, so
-- you can spam the bind on a quest that needs the item used several times.
-- Right-click is the only way to advance to the next item.
btn:HookScript("PostClick", function(self, button, down)
if button == "LeftButton" and down and not InCombatLockdown() then
UpdateCount()
UpdateCD()
end
end)
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_LOGIN")
f:RegisterEvent("QUEST_LOG_UPDATE")
f:RegisterEvent("BAG_UPDATE_DELAYED")
f:RegisterEvent("PLAYER_REGEN_ENABLED")
f:RegisterEvent("GET_ITEM_INFO_RECEIVED")
f:RegisterEvent("UPDATE_BINDINGS")
f:SetScript("OnEvent", function(_, event)
if event == "PLAYER_LOGIN" then ApplyPosition(); UpdateBindLabel() end
if event == "UPDATE_BINDINGS" then UpdateBindLabel(); return end
if event == "BAG_UPDATE_DELAYED" then UpdateCount(); return end
Scan()
end)
BINDING_HEADER_WICKSQUESTKEY = "Wicks Quest Key"
-- Binding name has spaces and a colon, so set the display label via bracket syntax.
_G["BINDING_NAME_CLICK WicksQuestKeyButton:LeftButton"] = "Use current quest item"
SLASH_WICKSQUESTKEY1 = "/wqk"
SLASH_WICKSQUESTKEY2 = "/questkey"
SlashCmdList.WICKSQUESTKEY = function(msg)
msg = (msg or ""):lower():gsub("^%s+", ""):gsub("%s+$", "")
if msg == "unlock" or msg == "move" then
SetLocked(false)
print("|cff8a5cf6Wick's Quest Key|r: unlocked. Left-drag the button to move it.")
return
elseif msg == "lock" then
SetLocked(true)
print("|cff8a5cf6Wick's Quest Key|r: locked.")
return
elseif msg == "reset" then
WicksQuestKeyDB.point, WicksQuestKeyDB.relativePoint = "CENTER", "CENTER"
WicksQuestKeyDB.x, WicksQuestKeyDB.y = 0, -150
ApplyPosition()
print("|cff8a5cf6Wick's Quest Key|r: position reset.")
return
elseif msg == "debug" then
local k1, k2 = GetBindingKey(QK_BINDING)
local cur = items[nextIndex]
print("|cff8a5cf6Wick's Quest Key|r debug:")
print((" bind keys: %s | %s"):format(k1 or "(none)", k2 or "(none)"))
print((" items loaded: %d armed index: %d"):format(#items, nextIndex))
if cur then
print((" armed item: %s (id %d)"):format(cur.name, cur.itemId))
print((" type1 attr: %s macrotext: %s"):format(
tostring(btn:GetAttribute("type1")), tostring(btn:GetAttribute("macrotext"))))
print((" macrotext1: %s"):format(tostring(btn:GetAttribute("macrotext1"))))
end
print((" button visible: %s in combat: %s"):format(
tostring(btn:IsShown()), tostring(InCombatLockdown())))
return
end
if #items == 0 then
print("|cff8a5cf6Wick's Quest Key|r: no usable quest items right now.")
else
print(("|cff8a5cf6Wick's Quest Key|r: %d quest item(s) loaded"):format(#items))
for i, it in ipairs(items) do
local marker = (i == nextIndex) and " |cff4fc77b<-- armed|r" or ""
print((" %d. %s%s"):format(i, it.name, marker))
end
end
print("Commands: /wqk unlock | lock | reset | debug | fire")
end