forked from kemayo/wow-whatsonthemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.lua
417 lines (384 loc) · 15.5 KB
/
addon.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
local myname, ns = ...
local myfullname = GetAddOnMetadata(myname, "Title")
local db
local isClassic = WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE
function ns.Print(...) print("|cFF33FF99".. myfullname.. "|r:", ...) end
-- events
local f = CreateFrame('Frame')
f:SetScript("OnEvent", function(_, event, ...)
ns[ns.events[event]](ns, event, ...)
end)
f:Hide()
ns.events = {}
function ns:RegisterEvent(event, method)
self.events[event] = method or event
f:RegisterEvent(event)
end
function ns:UnregisterEvent(...) for i=1,select("#", ...) do f:UnregisterEvent((select(i, ...))) end end
local window
local setDefaults
function ns:ADDON_LOADED(event, addon)
if addon == myname then
_G[myname.."DB"] = setDefaults(_G[myname.."DB"] or {}, {
title = true, -- show title (for dragging)
backdrop = true, -- show a backdrop on the frame
empty = false, -- show when empty
direction = true, -- show the direction the vignette is in
world = true, -- show vignettes that're on the world map
minimap = true, -- show vignettes that're on the minimap
hidden = true, -- show vignettes that're not on either
debug = false, -- show all the debug info in tooltips
})
db = _G[myname.."DB"]
self:UnregisterEvent("ADDON_LOADED")
window = self:CreateUI()
window:SetPoint("CENTER")
self:RegisterEvent("VIGNETTE_MINIMAP_UPDATED", "Refresh")
self:RegisterEvent("VIGNETTES_UPDATED", "Refresh")
self:RegisterEvent("PLAYER_ENTERING_WORLD", "Refresh")
self:RegisterEvent("PET_BATTLE_OPENING_START")
self:RegisterEvent("PET_BATTLE_CLOSE")
end
end
ns:RegisterEvent("ADDON_LOADED")
function ns:PET_BATTLE_OPENING_START()
window:Hide()
end
function ns:PET_BATTLE_CLOSE()
self:Refresh()
end
local function sort_vignette(a, b)
return ns.VignetteDistanceFromPlayer(a) < ns.VignetteDistanceFromPlayer(b)
end
function ns:Refresh()
if C_PetBattles.IsInBattle() then return end
local vignetteids = C_VignetteInfo.GetVignettes()
table.sort(vignetteids, sort_vignette)
window.linePool:ReleaseAll()
-- print("VIGNETTES_UPDATED", #vignetteids)
if db.title then
window.title:Show()
else
window.title:Hide()
end
local count = 0
local lastLine
local height = db.title and (window.title:GetHeight() + 6) or 6
for i=1, #vignetteids do
local instanceid = vignetteids[i]
-- print(i, vignetteInfo.name)
local vignetteInfo = C_VignetteInfo.GetVignetteInfo(instanceid)
if self:ShouldShowVignette(vignetteInfo) then
local line = self:AddLine(instanceid, vignetteInfo)
if count == 0 and not db.title then
line:SetPoint("TOPLEFT", window.title)
line:SetPoint("TOPRIGHT", window.title)
else
local anchor = count == 0 and window.title or lastLine
line:SetPoint("TOPLEFT", anchor, "BOTTOMLEFT")
line:SetPoint("TOPRIGHT", anchor, "BOTTOMRIGHT")
end
-- print('line was shown', vignetteInfo)
height = height + line:GetHeight()
lastLine = line
count = count + 1
end
end
if count == 0 then
if not db.empty then
return window:Hide()
end
if not db.title then
local line = window.linePool:Acquire()
line:SetPoint("TOPLEFT", window.title)
line:SetPoint("TOPRIGHT", window.title)
line.icon:SetAtlas("xmarksthespot")
line.title:SetText(NONE)
line:Show()
height = height + line:GetHeight()
end
end
if db.backdrop then
window:SetBackdropColor(0, 0, 0, .5)
window:SetBackdropBorderColor(0, 0, 0, .5)
else
window:SetBackdropColor(0, 0, 0, 0)
window:SetBackdropBorderColor(0, 0, 0, 0)
end
window:SetHeight(height)
window:Show()
end
function ns:AddLine(instanceid, vignetteInfo)
local line = window.linePool:Acquire()
line.vignetteGUID = instanceid
if vignetteInfo then
line.icon:SetAtlas(vignetteInfo.atlasName)
line.title:SetText(vignetteInfo.name)
if not (vignetteInfo.onMinimap or vignetteInfo.onWorldMap) then
line.icon:SetDesaturated(true)
line.icon:SetAlpha(0.5)
end
else
line.icon:SetAtlas("poi-nzothvision") -- "islands-questdisable"?
line.icon:SetVertexColor(0.9, 0.3, 1, 1)
line.title:SetText(UNKNOWN)
end
if db.direction then
local _, angle = ns.VignetteDistanceFromPlayer(instanceid)
line.direction:SetWidth(30)
line.direction:SetText(ns.AngleToCompassDirection(angle, true))
else
line.direction:SetWidth(0)
end
line:Show()
return line
end
local hide = {
[4582] = true, -- Ripe Purian when you have the Heightened Olfaction buff, zone-wide insanity
}
function ns:ShouldShowVignette(vignetteInfo)
if not vignetteInfo then
return db.hidden
end
if hide[vignetteInfo.vignetteID] then
return false
end
if not vignetteInfo.onMinimap then
if vignetteInfo.onWorldMap then
return db.world
end
return db.hidden
end
return db.minimap
end
local function VignettePosition(vignetteGUID)
local uiMapID = C_Map.GetBestMapForUnit('player')
if not uiMapID then return end
local position = C_VignetteInfo.GetVignettePosition(vignetteGUID, uiMapID)
if position then
return uiMapID, position, position:GetXY()
end
end
function ns.VignetteDistanceFromPlayer(vignetteGUID)
local uiMapID, position = VignettePosition(vignetteGUID)
if not (uiMapID and position) then return 0, 0 end
local player = C_Map.GetPlayerMapPosition(uiMapID, 'player')
if not player then return 0, 0 end
local width, height = C_Map.GetMapWorldSize(uiMapID)
position:Subtract(player)
local angle = (math.pi - Vector2D_CalculateAngleBetween(position.x, position.y, 0, 1))
return position:GetLength() * width, angle
end
do
local function round(x) return x + 0.5 - (x + 0.5) % 1 end
local directions_long = {"North", "Northeast", "East", "Southeast", "South", "Southwest", "West", "Northwest"}
local directions_short = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"}
function ns.AngleToCompassDirection(radians, short)
-- algorithm is: find out how many increments we've gone around the
-- circle, rounded to the nearest one, modulo the number of increments so
-- we don't go over the top at 2pi
local directions = short and directions_short or directions_long
local increment = (2*math.pi) / #directions
local direction = (round(radians / increment) % #directions) + 1
return directions[direction]
end
end
function ns:CreateUI()
local frame = CreateFrame("Frame", "WhatsOnTheMapFrame", UIParent, "BackdropTemplate")
frame:SetSize(240, 60)
frame:SetBackdrop({
edgeFile = [[Interface\Buttons\WHITE8X8]],
bgFile = [[Interface\Buttons\WHITE8X8]],
edgeSize = 1,
})
frame:EnableMouse(true)
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
frame:SetClampedToScreen(true)
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
frame:SetScript("OnMouseUp", function(w, button)
if button == "RightButton" then
return ns:ShowConfigMenu(w)
end
end)
frame:SetScript("OnUpdate", function(_, since)
if not db.direction then return end
frame.time = (frame.time or 0) + since
if frame.time <= 3 then return end
for line in frame.linePool:EnumerateActive() do
if line.vignetteGUID then
local _, angle = ns.VignetteDistanceFromPlayer(line.vignetteGUID)
line.direction:SetWidth(30)
line.direction:SetText(ns.AngleToCompassDirection(angle, true))
end
end
frame.time = 0
end)
local title = frame:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
frame.title = title
title:SetJustifyH("MIDDLE")
title:SetJustifyV("MIDDLE")
title:SetPoint("TOPLEFT", 0, -4)
title:SetPoint("TOPRIGHT", 0, -4)
title:SetText(myfullname)
local function LineTooltip(line)
if not line.vignetteGUID then return end
local anchor = (line:GetCenter() < (UIParent:GetWidth() / 2)) and "ANCHOR_RIGHT" or "ANCHOR_LEFT"
GameTooltip:SetOwner(line, anchor, 0, -60)
local vignetteInfo = C_VignetteInfo.GetVignetteInfo(line.vignetteGUID)
local _, _, x, y = VignettePosition(line.vignetteGUID)
local distance, angle = ns.VignetteDistanceFromPlayer(line.vignetteGUID)
local location = (x and y) and ("%.2f, %.2f"):format(x * 100, y * 100) or UNKNOWN
if vignetteInfo then
GameTooltip:AddDoubleLine(vignetteInfo.name or UNKNOWN, location, 1, 1, 1)
else
GameTooltip:AddDoubleLine("No data from API", location, 1, 0, 0)
end
if distance then
GameTooltip:AddDoubleLine(" ", ("%d yards away, %s"):format(distance, ns.AngleToCompassDirection(angle)))
end
if db.debug then
if vignetteInfo then
for k,v in pairs(vignetteInfo) do
if k ~= 'name' then
GameTooltip:AddDoubleLine(k, type(v) == "boolean" and (v and "true" or "false") or v)
end
end
else
GameTooltip:AddDoubleLine('vignetteGUID', line.vignetteGUID)
end
end
GameTooltip_AddInstructionLine(GameTooltip, "Control-click to add a map pin")
GameTooltip_AddInstructionLine(GameTooltip, "Shift-click to share to chat")
GameTooltip:Show()
end
local function Line_OnClick(line, button)
if button == "RightButton" then
return ns:ShowConfigMenu(line)
end
if button ~= "LeftButton" then return end
if not line.vignetteGUID then return end
local vignetteInfo = C_VignetteInfo.GetVignetteInfo(line.vignetteGUID)
local uiMapID, _, x, y = VignettePosition(line.vignetteGUID)
if IsShiftKeyDown() then
local message = ("%s|cffffff00|Hworldmap:%d:%d:%d|h[%s]|h|r"):format(
vignetteInfo and (vignetteInfo.name .. " ") or "",
uiMapID,
x * 10000,
y * 10000,
-- WoW seems to filter out anything which isn't the standard MAP_PIN_HYPERLINK
MAP_PIN_HYPERLINK
)
PlaySound(SOUNDKIT.UI_MAP_WAYPOINT_CHAT_SHARE)
-- if you have an open editbox, just paste to it
if not ChatEdit_InsertLink(message) then
-- open the chat to whatever it was on and add the text
ChatFrame_OpenChat(message)
end
elseif IsControlKeyDown() then
if uiMapID and x and y and C_Map.CanSetUserWaypointOnMap(uiMapID) then
local uiMapPoint = UiMapPoint.CreateFromCoordinates(uiMapID, x, y)
C_Map.SetUserWaypoint(uiMapPoint)
C_SuperTrack.SetSuperTrackedUserWaypoint(true)
end
end
end
frame.linePool = CreateFramePool("Frame", frame, nil, function(pool, line)
if not line.icon then
line:SetHeight(26)
line.icon = line:CreateTexture()
line.icon:SetSize(24, 24)
line.icon:SetPoint("LEFT", 4, 0)
line.title = line:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
line.title:SetPoint("LEFT", line.icon, "RIGHT", 4, 0)
line.title:SetPoint("RIGHT")
line.title:SetJustifyH("LEFT")
line.title:SetMaxLines(2)
line.direction = line:CreateFontString(nil, "ARTWORK", "GameFontHighlightLarge")
line.direction:SetPoint("RIGHT")
line.title:SetPoint("RIGHT", line.direction, "LEFT")
line:SetScript("OnEnter", LineTooltip)
line:SetScript("OnLeave", GameTooltip_Hide)
line:SetScript("OnMouseUp", Line_OnClick)
line:EnableMouse(true)
end
line.vignetteGUID = nil
line.icon:SetDesaturated(false)
line.icon:SetAlpha(1)
line.icon:SetVertexColor(1, 1, 1, 1)
line.direction:SetWidth(30)
line.direction:SetText("")
FramePool_HideAndClearAnchors(pool, line)
end)
return frame
end
-- Quick config:
local function PrintConfigLine(key, description)
ns.Print(key, '-', description, '-', db[key] and YES or NO)
end
_G["SLASH_".. myname:upper().."1"] = "/whatsonthemap"
_G["SLASH_".. myname:upper().."2"] = "/wotm"
SlashCmdList[myname:upper()] = function(msg)
msg = msg:trim()
if db[msg] ~= nil then
db[msg] = not db[msg]
ns.Print(msg, '=', db[msg] and YES or NO)
ns:Refresh()
end
if msg == "" then
ns.Print("What's On The Map?")
PrintConfigLine('title', "Show a title in the frame")
PrintConfigLine('backdrop', "Show a backdrop in the frame")
PrintConfigLine('direction', "Show the direction of the item")
PrintConfigLine('empty', "Show while empty")
PrintConfigLine('world', "Show world map items")
PrintConfigLine('minimap', "Show minimap items")
PrintConfigLine('hidden', "Show hidden map items")
PrintConfigLine('debug', "Show debug information")
ns.Print("To toggle: /whatsonthemap [type]")
end
end
do
local menuFrame, menuData
local isChecked = function(button) return db[button.value] end
local toggle = function(button, arg1, arg2, checked)
db[button.value] = not checked
ns:Refresh()
end
function ns:ShowConfigMenu(frame)
if not menuFrame then
menuFrame = CreateFrame("Frame", myname.."MenuFrame", UIParent, "UIDropDownMenuTemplate")
menuData = {
{ text=myfullname, isTitle=true, },
{ text="Show a title in the frame", value="title", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Show a backdrop in the frame", value="backdrop", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Show the direction of the item", value="direction", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Show while empty", value="empty", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Show debug information", value="debug", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Show map items...", hasArrow=true, notCheckable=true, menuList={
{ text="From the world map", value="world", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="From the minimap", value="minimap", checked=isChecked, func=toggle, isNotRadio=true, },
{ text="Hidden", value="hidden", checked=isChecked, func=toggle, isNotRadio=true, },
}, },
}
end
EasyMenu(menuData, menuFrame, "cursor", 0, 0, "MENU")
end
end
function setDefaults(options, defaults)
setmetatable(options, { __index = function(t, k)
if type(defaults[k]) == "table" then
t[k] = setDefaults({}, defaults[k])
return t[k]
end
return defaults[k]
end, })
-- and add defaults to existing tables
for k, v in pairs(options) do
if defaults[k] and type(v) == "table" then
setDefaults(v, defaults[k])
end
end
return options
end