-
Notifications
You must be signed in to change notification settings - Fork 2
/
Coords.lua
179 lines (149 loc) · 4.36 KB
/
Coords.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
--[[
Copyright (c) 2009-2016, Hendrik "Nevcairiel" Leppkes < [email protected] >
All rights reserved.
]]
local Mapster = LibStub("AceAddon-3.0"):GetAddon("Mapster")
local L = LibStub("AceLocale-3.0"):GetLocale("Mapster")
local MODNAME = "Coords"
local Coords = Mapster:NewModule(MODNAME)
local GetCursorPosition = GetCursorPosition
local GetPlayerMapPosition = C_Map.GetPlayerMapPosition
local WorldMapScrollChild = WorldMapFrame.ScrollContainer.Child
local display, cursortext, playertext
local texttemplate, text = "%%s: %%.%df, %%.%df"
local MouseXY, OnUpdate
local db
local defaults = {
profile = {
accuracy = 1,
fontSize = 11,
}
}
local optGetter, optSetter
do
local mod = Coords
function optGetter(info)
local key = info[#info]
return Coords.db.profile[key]
end
function optSetter(info, value)
local key = info[#info]
Coords.db.profile[key] = value
mod:Refresh()
end
end
local options
local function getOptions()
if not options then
options = {
type = "group",
name = L["Coordinates"],
arg = MODNAME,
get = optGetter,
set = optSetter,
args = {
intro = {
order = 1,
type = "description",
name = L["The Coordinates module adds a display of your current location, and the coordinates of your mouse cursor to the World Map frame."],
},
enabled = {
order = 2,
type = "toggle",
name = L["Enable Coordinates"],
get = function() return Mapster:GetModuleEnabled(MODNAME) end,
set = function(info, value) Mapster:SetModuleEnabled(MODNAME, value) end,
},
accuracydesc = {
order = 3,
type = "description",
name = "\n" .. L["You can control the accuracy of the coordinates, e.g. if you need very exact coordinates you can set this to 2."],
},
accuracy = {
order = 4,
type = "range",
name = L["Accuracy"],
min = 0, max = 2, step = 1,
},
fontSize = {
order = 5,
type = "range",
name = L["Font Size"],
desc = L["Font Size on the normal map."],
min = 6, max = 18, step = 1,
},
},
}
end
return options
end
function Coords:OnInitialize()
self.db = Mapster.db:RegisterNamespace(MODNAME, defaults)
db = self.db.profile
self:SetEnabledState(Mapster:GetModuleEnabled(MODNAME))
Mapster:RegisterModuleOptions(MODNAME, getOptions, L["Coordinates"])
end
function Coords:OnEnable()
if not display then
display = CreateFrame("Frame", "Mapster_CoordsFrame", WorldMapFrame.ScrollContainer)
cursortext = display:CreateFontString(nil, "OVERLAY")
playertext = display:CreateFontString(nil, "OVERLAY")
self:UpdateMapSize()
cursortext:SetTextColor(1, 1, 1)
playertext:SetTextColor(1, 1, 1)
cursortext:SetPoint("TOPLEFT", WorldMapFrame.ScrollContainer, "BOTTOM", 30, -5)
playertext:SetPoint("TOPRIGHT", WorldMapFrame.ScrollContainer, "BOTTOM", -30, -5)
tinsert(Mapster.elementsToHide, display)
end
display:SetScript("OnUpdate", OnUpdate)
display:Show()
self:Refresh()
end
function Coords:OnDisable()
display:SetScript("OnUpdate", nil)
display:Hide()
end
function Coords:Refresh()
db = self.db.profile
if not self:IsEnabled() then return end
local acc = tonumber(db.accuracy) or 1
text = texttemplate:format(acc, acc)
self:UpdateMapSize()
end
function Coords:UpdateMapSize()
if not cursortext or not playertext then return end
cursortext:SetFont(GameFontNormal:GetFont(), db.fontSize, "OUTLINE")
playertext:SetFont(GameFontNormal:GetFont(), db.fontSize, "OUTLINE")
end
function MouseXY()
local left, top = WorldMapScrollChild:GetLeft(), WorldMapScrollChild:GetTop()
local width, height = WorldMapScrollChild:GetWidth(), WorldMapScrollChild:GetHeight()
local scale = WorldMapScrollChild:GetEffectiveScale()
if not left or not top then return end -- this can occur while the map is being moved
local x, y = GetCursorPosition()
local cx = (x/scale - left) / width
local cy = (top - y/scale) / height
if cx < 0 or cx > 1 or cy < 0 or cy > 1 then
return
end
return cx, cy
end
local cursor, player = L["Cursor"], L["Player"]
function OnUpdate()
local cx, cy = MouseXY()
local px, py
local xy = GetPlayerMapPosition(WorldMapFrame:GetMapID(), "player")
if xy then
px, py = xy:GetXY()
end
if cx then
cursortext:SetFormattedText(text, cursor, 100 * cx, 100 * cy)
else
cursortext:SetText("")
end
if not px or px == 0 then
playertext:SetText("")
else
playertext:SetFormattedText(text, player, 100 * px, 100 * py)
end
end