-
Notifications
You must be signed in to change notification settings - Fork 0
/
TomTom_Waypoints.lua
433 lines (352 loc) · 13.6 KB
/
TomTom_Waypoints.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
--[[--------------------------------------------------------------------------
-- TomTom - A navigational assistant for World of Warcraft
--
-- This file contains the internal implementation of TomTom's waypoints.
-- None of these functions should be called directly by addons if they want
-- the waypoints to obey normal TomTom options and behavior. In otherwords
-- don't call TomTom:SetWaypoint() or TomTom:ClearWaypoint(), use the public
-- TomTom:AddZWaypoint() and TomTom:RemoveWaypoint() instead.
----------------------------------------------------------------------------]]
local addon_name, addon = ...
local astrolabe = addon.astrolabe
-- Create a tooltip to be used when mousing over waypoints
local tooltip = CreateFrame("GameTooltip", "TomTomTooltip", UIParent, "GameTooltipTemplate")
do
-- Set the the tooltip's lines
local i = 1
tooltip.lines = {}
repeat
local line = getglobal("TomTomTooltipTextLeft"..i)
if line then
tooltip.lines[i] = line
end
i = i + 1
until not line
end
-- Store a reference to the minimap parent
local minimapParent = Minimap
-- Create a local table used as a frame pool
local pool = {}
local all_points = {}
-- Local declarations
local Minimap_OnEnter,Minimap_OnLeave,Minimap_OnUpdate,Minimap_OnClick,Minimap_OnEvent
local Arrow_OnUpdate
local World_OnEnter,World_OnLeave,World_OnClick,World_OnEvent
local square_half = math.sqrt(0.5)
local rad_135 = math.rad(135)
local function rotateArrow(self)
if self.disabled then return end
local angle = astrolabe:GetDirectionToIcon(self)
if not angle then return self:Hide() end
angle = angle + rad_135
if GetCVar("rotateMinimap") == "1" then
--local cring = MiniMapCompassRing:GetFacing()
local cring = GetPlayerFacing()
angle = angle - cring
end
local sin,cos = math.sin(angle) * square_half, math.cos(angle) * square_half
self.arrow:SetTexCoord(0.5-sin, 0.5+cos, 0.5+cos, 0.5+sin, 0.5-cos, 0.5-sin, 0.5+sin, 0.5-cos)
end
function TomTom:ReparentMinimap(minimap)
minimapParent = minimap
for idx, waypoint in ipairs(all_points) do
waypoint:SetParent(minimap)
end
end
local waypointMap = {}
function TomTom:SetWaypoint(waypoint, callbacks, show_minimap, show_world)
local m, f, x, y = unpack(waypoint)
-- Try to acquire a waypoint from the frame pool
local point = table.remove(pool)
if not point then
point = {}
local minimap = CreateFrame("Button", nil, minimapParent)
minimap:SetHeight(20)
minimap:SetWidth(20)
minimap:RegisterForClicks("RightButtonUp")
-- Add to the "All points" table so we can reparent easily
table.insert(all_points, minimap)
minimap.icon = minimap:CreateTexture("BACKGROUND")
minimap.icon:SetTexture("Interface\\AddOns\\TomTom\\Images\\GoldGreenDot")
minimap.icon:SetPoint("CENTER", 0, 0)
minimap.icon:SetHeight(12)
minimap.icon:SetWidth(12)
minimap.arrow = minimap:CreateTexture("BACKGROUND")
minimap.arrow:SetTexture("Interface\\AddOns\\TomTom\\Images\\MinimapArrow-Green")
minimap.arrow:SetPoint("CENTER", 0 ,0)
minimap.arrow:SetHeight(40)
minimap.arrow:SetWidth(40)
minimap.arrow:Hide()
-- Add the behavior scripts
minimap:SetScript("OnEnter", Minimap_OnEnter)
minimap:SetScript("OnLeave", Minimap_OnLeave)
minimap:SetScript("OnUpdate", Minimap_OnUpdate)
minimap:SetScript("OnClick", Minimap_OnClick)
minimap:RegisterEvent("PLAYER_ENTERING_WORLD")
minimap:SetScript("OnEvent", Minimap_OnEvent)
if not TomTomMapOverlay then
local overlay = CreateFrame("Frame", "TomTomMapOverlay", WorldMapButton)
overlay:SetAllPoints(true)
end
local worldmap = CreateFrame("Button", nil, TomTomMapOverlay)
worldmap:SetHeight(12)
worldmap:SetWidth(12)
worldmap:RegisterForClicks("RightButtonUp")
worldmap.icon = worldmap:CreateTexture("ARTWORK")
worldmap.icon:SetAllPoints()
worldmap.icon:SetTexture("Interface\\AddOns\\TomTom\\Images\\GoldGreenDot")
worldmap:RegisterEvent("WORLD_MAP_UPDATE")
worldmap:SetScript("OnEnter", World_OnEnter)
worldmap:SetScript("OnLeave", World_OnLeave)
worldmap:SetScript("OnClick", World_OnClick)
worldmap:SetScript("OnEvent", World_OnEvent)
point.worldmap = worldmap
point.minimap = minimap
end
waypointMap[waypoint] = point
point.m = m
point.f = f
point.x = x
point.y = y
point.show_world = show_world
point.show_minimap = show_minimap
point.callbacks = callbacks
point.worldmap.callbacks = callbacks and callbacks.world
point.minimap.callbacks = callbacks and callbacks.minimap
-- Process the callbacks table to put distances in a consumable format
if callbacks and callbacks.distance then
point.dlist = {}
for k,v in pairs(callbacks.distance) do
table.insert(point.dlist, k)
end
table.sort(point.dlist)
end
-- Clear the state for callbacks
point.state = nil
point.lastdist = nil
-- Link the actual frames back to the waypoint object
point.minimap.point = point
point.worldmap.point = point
point.uid = waypoint
-- Place the waypoint
astrolabe:PlaceIconOnMinimap(point.minimap, m, f, x, y)
if show_world then
astrolabe:PlaceIconOnWorldMap(TomTomMapOverlay, point.worldmap, m, f, x, y)
else
point.worldmap.disabled = true
end
if not show_minimap then
-- Hide the minimap icon/arrow if minimap is off
point.minimap:EnableMouse(false)
point.minimap.icon:Hide()
point.minimap.arrow:Hide()
point.minimap.disabled = true
rotateArrow(point.minimap)
else
point.minimap:EnableMouse(true)
point.minimap.disabled = false
rotateArrow(point.minimap)
end
end
function TomTom:HideWaypoint(uid, minimap, worldmap)
local point = waypointMap[uid]
if point then
if minimap then
point.minimap.disabled = true
point.minimap:Hide()
end
if worldmap then
point.worldmap.disabled = true
point.worldmap:Hide()
end
end
end
function TomTom:ShowWaypoint(uid)
local point = waypointMap[uid]
if point then
point.minimap.disabled = not point.data.show_minimap
point.minimap:Show()
point.worldmap.disabled = not point.data.show_worldmap
point.worldmap:Show()
end
end
-- This function removes the waypoint from the active set
function TomTom:ClearWaypoint(uid)
local point = waypointMap[uid]
if point then
astrolabe:RemoveIconFromMinimap(point.minimap)
point.minimap:Hide()
point.worldmap:Hide()
-- Clear our handles to the callback tables
point.callbacks = nil
point.minimap.callbacks = nil
point.worldmap.callbacks = nil
-- Clear disabled flags
point.minimap.disabled = nil
point.worldmap.disabled = nil
point.dlist = nil
point.uid = nil
table.insert(pool, point)
waypointMap[uid] = nil
end
end
function TomTom:GetDistanceToWaypoint(uid)
local point = waypointMap[uid]
return point and astrolabe:GetDistanceToIcon(point.minimap)
end
function TomTom:GetDirectionToWaypoint(uid)
local point = waypointMap[uid]
return point and astrolabe:GetDirectionToIcon(point.minimap)
end
do
local tooltip_uid,tooltip_callbacks
local function tooltip_onupdate(self, elapsed)
if tooltip_callbacks and tooltip_callbacks.tooltip_update then
local dist,x,y = TomTom:GetDistanceToWaypoint(tooltip_uid)
tooltip_callbacks.tooltip_update("tooltip_update", tooltip, tooltip_uid, dist)
end
end
function Minimap_OnClick(self, button)
local data = self.callbacks
if data and data.onclick then
data.onclick("onclick", self.point.uid, self, button)
end
end
function Minimap_OnEnter(self, motion)
local data = self.callbacks
if data and data.tooltip_show then
local uid = self.point.uid
local dist,x,y = TomTom:GetDistanceToWaypoint(uid)
tooltip_uid = uid
tooltip_callbacks = data
-- Parent to UIParent, unless it's hidden
if UIParent:IsVisible() then
tooltip:SetParent(UIParent)
else
tooltip:SetParent(self)
end
tooltip:SetOwner(self, "ANCHOR_BOTTOMLEFT")
data.tooltip_show("tooltip_show", tooltip, uid, dist)
tooltip:Show()
-- Set the update script if there is one
if data.tooltip_update then
tooltip:SetScript("OnUpdate", tooltip_onupdate)
else
tooltip:SetScript("OnUpdate", nil)
end
end
end
function Minimap_OnLeave(self, motion)
tooltip_uid,tooltip_callbacks = nil,nil
tooltip:Hide()
end
World_OnEnter = Minimap_OnEnter
World_OnLeave = Minimap_OnLeave
World_OnClick = Minimap_OnClick
local minimap_count = 0
function Minimap_OnUpdate(self, elapsed)
local dist,x,y = astrolabe:GetDistanceToIcon(self)
local disabled = self.disabled
if not dist then
self:Hide()
return
end
minimap_count = minimap_count + elapsed
if minimap_count < 0.1 then return end
-- Reset the counter
minimap_count = 0
local edge = astrolabe:IsIconOnEdge(self)
local data = self.point
local callbacks = data.callbacks
if edge then
-- Check to see if this is a transition
if not disabled then
self.icon:Hide()
self.arrow:Show()
-- Rotate the icon, as required
local angle = astrolabe:GetDirectionToIcon(self)
angle = angle + rad_135
if GetCVar("rotateMinimap") == "1" then
--local cring = MiniMapCompassRing:GetFacing()
local cring = GetPlayerFacing()
angle = angle - cring
end
local sin,cos = math.sin(angle) * square_half, math.cos(angle) * square_half
self.arrow:SetTexCoord(0.5-sin, 0.5+cos, 0.5+cos, 0.5+sin, 0.5-cos, 0.5-sin, 0.5+sin, 0.5-cos)
end
else
if not disabled then
self.icon:Show()
self.arrow:Hide()
end
end
if callbacks and callbacks.distance then
local list = data.dlist
local state = data.state
local newstate
-- Calculate the initial state
if not state then
for i=1,#list do
if dist <= list[i] then
state = i
break
end
end
-- Handle the case where we're outside the largest circle
if not state then state = -1 end
data.state = state
else
-- Calculate the new state
for i=1,#list do
if dist <= list[i] then
newstate = i
break
end
end
-- Handle the case where we're outside the largest circle
if not newstate then newstate = -1 end
end
-- If newstate is set, then this is a transition
-- If only state is set, this is the initial state
if state ~= newstate then
-- Handle the initial state
newstate = newstate or state
local distance = list[newstate]
local callback = callbacks.distance[distance]
if callback then
callback("distance", data.uid, distance, dist, data.lastdist)
end
data.state = newstate
end
-- Update the last distance with the current distance
data.lastdist = dist
end
end
function World_OnEvent(self, event, ...)
if event == "WORLD_MAP_UPDATE" then
if not self.point.uid then
return
end
local data = self.point
if data.worldmap and data.show_world and not self.disabled then
local x,y = astrolabe:PlaceIconOnWorldMap(TomTomMapOverlay, self, data.m, data.f, data.x, data.y)
local pdata = data.uid
if (x and y and (0 < x and x <= 1) and (0 < y and y <= 1)) then
self:Show()
else
self:Hide()
end
else
self:Hide()
end
end
end
function Minimap_OnEvent(self, event, ...)
if event == "PLAYER_ENTERING_WORLD" then
local data = self.point
if data and data.uid and waypointMap[data.uid] then
astrolabe:PlaceIconOnMinimap(self, data.m, data.f, data.x, data.y)
end
end
end
end