/
Core.lua
351 lines (310 loc) · 10.2 KB
/
Core.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
-- TODO: Make tooltip show up, make the OnClick handler work with map icons
local TL = CreateFrame("Frame","TownLocator",UIParent)
TL:SetScript("OnEvent", function(self,event,...) TL[event](self,...) end)
TL:RegisterEvent"ADDON_LOADED"
TL:RegisterEvent"WORLD_MAP_UPDATE"
TL.L = TL_GetLocalization()
TL.Data = TL_GetData()
local dd = CreateFrame('Frame', 'TownLocator_Menu', UIParent, 'UIDropDownMenuTemplate')
local Astrolabe = DongleStub"Astrolabe-0.4"
local miniPool = {}
local mapPool = {}
local tinsert = tinsert
-- Icons depending on the type
TL.Types = {
["Bank"] = "Interface\\Minimap\\Tracking\\Banker",
["Barber"] = "Interface\\BarberShop\\UI-Barbershop-Banner",
["Flight Master"] = "Interface\\Minimap\\Tracking\\Flightmaster",
["Mailbox"] = "Interface\\Minimap\\Tracking\\Mailbox",
["Portals"] = "Interface\\AddOns\\TownLocator\\Portals",
["Inn"] = "Interface\\Minimap\\Tracking\\Innkeeper",
["Battlemasters"] = "Interface\\WorldStateFrame\\CombatSwords",
["Higher Learning Books"] = "Interface\\Minimap\\Tracking\\Profession",
-- Every profession
["Alchemy Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Blacksmithing Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Cooking Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Enchanting Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["First Aid Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Fishing Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Herbalism Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Inscription Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Leatherworking Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Mining Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Skinning Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Tailoring Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
["Jewelcrafting Trainer"] = "Interface\\Minimap\\Tracking\\Profession",
-- Every class
["Mage Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Warrior Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Warlock Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Rogue Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Priest Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Paladin Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Hunter Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Death Knight Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Shaman Trainer"] = "Interface\\Minimap\\Tracking\\Class",
["Druid Trainer"] = "Interface\\Minimap\\Tracking\\Class",
}
-- Some utils functions
local conts = {
northrend = true,
kalimdor = true,
azeroth= true,
expansion01 = true,
crystalsongforest = true,
[""] = true
}
-- All this is stolen from Tuhljin's Astronomer
local ZoneIcons = {};
local continentNames = { GetMapContinents() };
for key, val in pairs(continentNames) do
local zoneNames = { GetMapZones(key) };
ZoneIcons[key] = {};
ZoneIcons[key][0] = {};
ZoneIcons[key][0].name = val;
for k, v in pairs(zoneNames) do
ZoneIcons[key][k] = {};
ZoneIcons[key][k].name = v;
end
end
-- Next, add entries for the "continents" whose ID numbers are 0 and -1:
ZoneIcons[0] = {};
ZoneIcons[0][0] = {}
ZoneIcons[0][0].name = "Azeroth";
ZoneIcons[-1] = {};
ZoneIcons[-1][0] = {}
ZoneIcons[-1][0].name = "Cosmic";
local function getzoneIDfromname(C, zoneName)
for k, v in ipairs(ZoneIcons[C]) do
if (ZoneIcons[C][k].name == zoneName) then
return k;
end
end
return nil;
end
function TL:ZoneCZ(zoneName, continent)
local zone
if (type(continent) == "number") then
zone = getzoneIDfromname(continent, zoneName);
if (zone) then
return continent, zone;
else
return nil;
end
end
for k, v in pairs(ZoneIcons) do
zone = getzoneIDfromname(k, zoneName);
if (zone) then
return k, zone;
end
end
return nil;
end
-- Hide minimap icons
local function HideMinimap()
for _,icon in ipairs(miniPool) do
Astrolabe:RemoveIconFromMinimap(icon)
end
end
-- Hide map icons
local function HideMap()
for _,frame in ipairs(mapPool) do
frame:Hide()
end
end
-- Hide every icon
local function HideAll()
HideMap()
HideMinimap()
end
-- TomTom integration
local function ColorGradient(perc, ...)
if perc >= 1 then
local r, g, b = select(select('#', ...) - 2, ...)
return r, g, b
elseif perc <= 0 then
local r, g, b = ...
return r, g, b
end
local num = select('#', ...) / 3
local segment, relperc = math.modf(perc*(num-1))
local r1, g1, b1, r2, g2, b2 = select((segment*3)+1, ...)
return r1 + (r2-r1)*relperc, g1 + (g2-g1)*relperc, b1 + (b2-b1)*relperc
end
function TL:GetArrowColor(angle)
local perc = math.abs((math.pi - math.abs(angle)) / math.pi)
local r,g,b = ColorGradient(perc, 1, 0, 0, 1, 1, 0, 0, 1, 0)
return r, g, b
end
function TL:GoTo(icon)
if (not TomTom:CrazyArrowIsHijacked()) then
TomTom:HijackCrazyArrow(function(self, elapsed)
-- Angle to waypoint
local direction = Astrolabe:GetDirectionToIcon(icon)
local direction = direction - GetPlayerFacing()
TomTom:SetCrazyArrowDirection(direction)
-- CrayArrowColor
local r,g,b = TL:GetArrowColor(direction)
TomTom:SetCrazyArrowColor(r,g,b)
-- Distance to waypoint
local distance, _,_ = Astrolabe:GetDistanceToIcon(icon)
-- CrazyArrow title
local title = format("[TL]: %s (%d yards)", icon.name, distance)
TomTom:SetCrazyArrowTitle(title)
if distance < 5 then TomTom:ReleaseCrazyArrow() end
end)
end
end
-- Returns the icon
function TL:GetTextureByType(type)
return TL.Types[type]
end
-- Callback
local function OnEdge()
for _,icon in ipairs(miniPool) do
if(Astrolabe:IsIconOnEdge(icon)) then
icon:Hide()
else
icon:Show()
end
end
end
Astrolabe:Register_OnEdgeChanged_Callback(OnEdge, true)
-- Scripts handlers
local function OnEnter(self)
GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT")
GameTooltip:SetText(self.type)
GameTooltip:Show()
end
local function OnLeave(self)
GameTooltip:Hide()
end
local function OnClick(self,button)
if(button ~= "RightButton") then
return
else
local w
if self.kind == "MapIcon" then
w = self.micon
else
w = self
end
local tbl = {}
local title = {text= self.type.."\n", isTitle = true}
tinsert(tbl, title)
if(IsAddOnLoaded"TomTom") then
local tomtom = {text = "Go to this waypoint with TomTom", func = function() TL:GoTo(self) end}
tinsert(tbl,tomtom)
end
local hide = {text = "Hide this POI", func = function() self:Hide() end}
tinsert(tbl,hide)
local hidemap = {text = "Hide all map POI", func = HideMap}
tinsert(tbl, hidemap)
local hidemini = {text = "Hide all minimap POI", func = HideMinimap}
tinsert(tbl, hidemini)
local hideall = {text = "Hide all POI (map and minimap)", func = HideAll}
tinsert(tbl, hideall)
local cancel = {text = "Cancel", func = CloseDropDownMenus}
tinsert(tbl, cancel)
EasyMenu(tbl,dd,self,0,0)
end
end
-- Creates the minimap and map icon
function TL:CreateIcons(type,zone)
-- Minimap button
local minimap = CreateFrame("Button",nil,Minimap)
minimap:SetWidth((type == "Barber" and 60) or 20)
minimap:SetHeight((type == "Barber" and 30) or 20)
minimap:RegisterForClicks("RightButtonUp")
minimap.type = type
minimap.kind = "MinimapIcon"
minimap.icon = minimap:CreateTexture(nil,"OVERLAY")
minimap.icon:SetTexture(self:GetTextureByType(type) or "Interface\\Minimap\\Tracking\\Flightmaster")
minimap.icon:SetPoint("CENTER",0,0)
minimap.icon:SetWidth((type == "Barber" and 60) or 20)
minimap.icon:SetHeight((type == "Barber" and 30) or 20)
minimap:SetScript("OnEnter", OnEnter)
minimap:SetScript("OnLeave", OnLeave)
minimap:SetScript("OnClick", OnClick)
minimap:SetScript("OnUpdate", OnUpdate)
-- Map button
local map = CreateFrame("Button",nil,WorldMapDetailFrame)
map:SetWidth((type == "Barber" and 60) or 20)
map:SetHeight((type == "Barber" and 30) or 20)
map:RegisterForClicks("RightButtonUp")
map:SetFrameStrata("HIGH")
map.type = type
map.micon = minimap
map.kind = "MapIcon"
map.icon = map:CreateTexture(nil,"OVERLAY")
map.icon:SetTexture(self:GetTextureByType(type) or "Interface\\Minimap\\Tracking\\Flightmaster")
map.icon:SetPoint("CENTER",0,0)
map.icon:SetWidth((type == "Barber" and 60) or 20)
map.icon:SetHeight((type == "Barber" and 30) or 20)
map:SetScript("OnEnter", OnEnter)
map:SetScript("OnLeave", OnLeave)
map:SetScript("OnClick", OnClick)
tinsert(miniPool, minimap)
tinsert(mapPool, map)
return minimap, map
end
-- Creates the POI using Astrolabe
function TL:SetPOI(zoneName, x, y, type)
continent, zone = self:ZoneCZ(zoneName)
--Flightmaster icon by default (shouldn't happen anymore)
local tex = self:GetTextureByType(type) or "Interface\\Minimap\\Tracking\\Flightmaster"
-- Because barbers rules
local width = (type == "Barber" and 60) or 20
local height = (type == "Barber" and 30) or nil
-- Let's try without Astronomer
local minimap, map = self:CreateIcons(type,zone)
-- Some data
minimap.name = zoneName.." - "..type
map.name = zoneName.." - "..type
coords = {continent, zone, x, y}
minimap.coords = coords
map.coords = coords
-- True placement
Astrolabe:PlaceIconOnMinimap(minimap,continent,zone,x,y)
Astrolabe:PlaceIconOnWorldMap(WorldMapDetailFrame,map,continent,zone,x,y)
if(type == "Higher Learning Books") then
-- Little hack to see where are those damned books
minimap.icon:SetVertexColor(0,1,1)
map.icon:SetVertexColor(0,1,1)
end
end
-- Iterate over the data
function TL:IterateData()
for k, value in pairs(TL.Data) do
local z = k
for type, point in pairs(value) do
for _, tbl in ipairs(point) do
TL:SetPOI(z, tbl.xOff, tbl.yOff,type)
end
end
end
end
function TL:ADDON_LOADED(addon)
if addon ~= "TownLocator" then return end
TL:IterateData()
end
function TL:WORLD_MAP_UPDATE()
local mapFileName = strlower(GetMapInfo() or "")
local hide = false
--[[
for _,cont in ipairs(conts) do
if mapFileName == cont then
hide = true
end
end--]]
if conts[mapFileName] then hide = true end
for _, icon in ipairs(mapPool) do
Astrolabe:PlaceIconOnWorldMap(WorldMapDetailFrame, icon, unpack(icon.coords))
if(hide == true) then
icon:SetAlpha(0)
else
icon:SetAlpha(1)
end
end
end