Repository navigation
Expand file tree
/
Copy pathLauncher.lua
More file actions
382 lines (328 loc) · 14.1 KB
/
Copy pathLauncher.lua
File metadata and controls
382 lines (328 loc) · 14.1 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
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
local ADDON, ns = ...
-- The minimap button: a door into the settings that is always visible.
--
-- === What it is for ===
--
-- There are two doors into the options window: a slash command and this
-- button. The first has to be KNOWN - remembered as the word "/rack" - and
-- that knowledge is acquired once, at install time, and lost within a week.
-- The button requires knowing nothing.
--
-- === Why ours and not LibDBIcon ===
--
-- The library does exactly this and does it well, but drags a second one
-- (LibDataBroker) and a position store of its own along with it. What is
-- needed here is a ring, an icon and an angle - seventy lines - while every
-- bundled library is one more foreign version to update, which drops
-- somewhere that is not us.
--
-- The addon ships no libraries at all, and taking on the first for an icon
-- would be a strange trade.
--
-- === Where the angle lives ===
--
-- In the SHARED part of the store rather than in a profile. There is one
-- button per account: switching profiles must not move it. A player who made
-- a raid profile does not expect the icon to move to the other side of the
-- minimap - they were changing a bar layout, not the interface around it.
local Launcher = {}
ns.Launcher = Launcher
-- The store section's name. Named for what it is: profiles lie beside it, and
-- "launcher" says whose numbers these are.
local SECTION = "launcher"
Launcher.SECTION = SECTION
-- Sizes: the button, the ring and the icon inside it.
--
-- The ring is LARGER than the button, and that is not a mistake: the game's
-- ring texture is drawn with a margin - the ring itself occupies the middle of
-- it. Set to the button's size it gives a ring half as thick as the
-- neighbours'.
local SIZE, RING_SIZE, ICON_SIZE = 31, 53, 20
-- The margin from the minimap's edge to the button's centre.
local EDGE = 10
Launcher.EDGE = EDGE
-- A radius for the case where there is no minimap at all: outside the game and
-- before it has been assembled. The tests have something to count from, and
-- the button does not drive into the middle of the screen.
local RADIUS = 80
Launcher.RADIUS = RADIUS
--- The radius the button stands at from the minimap's centre.
--
-- === Why it is COMPUTED and not written as a number ===
--
-- It was a number here - eighty - and that was visible on screen: the button
-- sat INSIDE the map, over the terrain, instead of sitting with its centre on
-- the edge.
--
-- A minimap can be any size. The player changes it in the game's settings, and
-- so does any addon that touches it, and a constant is right exactly for the
-- map it was fitted to. Half the map's width plus a margin is right for any.
--
-- The width arrives as an argument rather than being asked of the game:
-- otherwise none of this could be verified.
--
-- @param width any the minimap's width
-- @return number
function Launcher.Reach(width)
local said = tonumber(width)
if said == nil or said <= 0 then return RADIUS end
return said / 2 + EDGE
end
-- The default angle. Bottom left: that is where a round minimap has the fewest
-- foreign buttons, because the game keeps its own above and to the right.
local HOME = 225
Launcher.HOME = HOME
-- The game's ring - the same one the game's own buttons and other addons use.
--
-- A ring of our own would be a mistake, and the first attempt was exactly
-- that: a square frame in our palette among the neighbours' round rings reads
-- as a foreign thing somebody forgot to finish. A minimap button is a row, and
-- in a row one stands out by its icon rather than by its frame.
local RING = "Interface\\Minimap\\MiniMap-TrackingBorder"
-- A circular mask for the ground. It may be missing on an old client, and that
-- is no trouble: the ground's corners are hidden by the ring anyway.
local ROUND = "Interface\\CharacterFrame\\TempPortraitAlphaMask"
-- Our mark: a rack of slots (tools/branding.py).
--
-- Drawn in WHITE with transparency, with the palette giving it its colour: one
-- file for all four accent sets, and the mark changes with the window.
local MARK = "Interface\\AddOns\\" .. ADDON
.. "\\Media\\branding\\rack-icon-64"
--============================================================================
-- Trigonometry
--============================================================================
--- The button's place by angle.
--
-- Pure: an angle in degrees, the answer an offset from the minimap's centre.
-- Zero degrees is to the right, then anticlockwise: that is how the maths
-- counts and how the game counts.
--
-- Rubbish answers with the default rather than with nothing: a button with no
-- place would not be drawn at all, and the player would have nothing to find
-- it with.
--
-- @param angle any degrees
-- @param radius any distance from the centre
-- @return number, number
function Launcher.Place(angle, radius)
local degrees = tonumber(angle) or HOME
local far = tonumber(radius) or RADIUS
local radians = math.rad(degrees)
return math.cos(radians) * far, math.sin(radians) * far
end
--- The angle the cursor ended up at relative to the minimap's centre.
--
-- Pure, over two points. The answer is brought into 0..360: an angle gone
-- negative would put the button in the same place but could not be read in the
-- saved file.
--
-- A cursor exactly at the centre gives zero, and gives ALWAYS the same zero:
-- otherwise a button dragged to the middle of the minimap would jump.
--
-- @param cx any centre horizontally
-- @param cy any centre vertically
-- @param x any cursor horizontally
-- @param y any cursor vertically
-- @return number degrees
function Launcher.AngleOf(cx, cy, x, y)
local dx = (tonumber(x) or 0) - (tonumber(cx) or 0)
local dy = (tonumber(y) or 0) - (tonumber(cy) or 0)
-- atan2 was renamed in newer Lua versions, and both live in the game.
-- Whichever exists is asked: a version check would lie first here.
local atan = math.atan2 or math.atan
local degrees = math.deg(atan(dy, dx))
if degrees < 0 then degrees = degrees + 360 end
return degrees
end
--============================================================================
-- Storage
--============================================================================
--- The button's section in the store, creating it when needed.
--
-- Rubbish is replaced with the defaults: the saved file gets hand-edited,
-- broken by failed writes and copied between clients.
--
-- @param saved any the store
-- @return table|nil
function Launcher.Store(saved)
if type(saved) ~= "table" then return nil end
if type(saved[SECTION]) ~= "table" then
saved[SECTION] = {}
end
local store = saved[SECTION]
if type(store.shown) ~= "boolean" then store.shown = true end
if tonumber(store.angle) == nil then store.angle = HOME end
return store
end
--============================================================================
-- The button itself
--============================================================================
-- Created on first show and living out the session: a frame cannot be deleted
-- in this game, and creating it twice would accumulate buttons.
local button
--- Put the button at its angle.
local function place()
if button == nil then return end
local saved = ns.Bars.Store()
local store = Launcher.Store(saved)
if store == nil then return end
-- THIS map's radius rather than a general one: a map can be any size, and
-- it is asked on every placement - a foreign addon may have changed it
-- after we created the button.
local wide
if type(_G.Minimap) == "table" and type(_G.Minimap.GetWidth) == "function" then
local ok, said = pcall(_G.Minimap.GetWidth, _G.Minimap)
if ok then wide = said end
end
local x, y = Launcher.Place(store.angle, Launcher.Reach(wide))
pcall(button.ClearAllPoints, button)
pcall(button.SetPoint, button, "CENTER", _G.Minimap, "CENTER", x, y)
end
--- Create the button.
--
-- Built the same way a neighbouring addon builds it, and not by accident:
-- ground, mark, ring - three layers bottom to top.
--
-- The ground is needed because the mark is TRANSPARENT. Without it the mark
-- reads as a hole in the ring over dark terrain and disappears entirely over
-- light: under the minimap there is snow one moment and night the next, and
-- what will be under the button cannot be counted on.
local function make()
if button then return button end
if type(CreateFrame) ~= "function" then return nil end
if type(_G.Minimap) ~= "table" then return nil end
local ok, frame = pcall(CreateFrame, "Button", "RackMinimapButton",
_G.Minimap)
if not ok or type(frame) ~= "table" then return nil end
button = frame
pcall(frame.SetSize, frame, SIZE, SIZE)
pcall(frame.SetFrameStrata, frame, "MEDIUM")
-- The level is RELATIVE to the map rather than a number: the map sits at
-- its own level, and a button with a constant would end up under it on one
-- client and over foreign buttons on another.
local okLevel, level = pcall(_G.Minimap.GetFrameLevel, _G.Minimap)
pcall(frame.SetFrameLevel, frame,
((okLevel and tonumber(level)) or 0) + 8)
pcall(frame.EnableMouse, frame, true)
pcall(frame.RegisterForClicks, frame, "AnyUp")
pcall(frame.RegisterForDrag, frame, "LeftButton")
-- The ground under the mark: our blackness, made round by the mask.
local plate = frame:CreateTexture(nil, "BACKGROUND")
pcall(plate.SetSize, plate, ICON_SIZE + 2, ICON_SIZE + 2)
pcall(plate.SetPoint, plate, "TOPLEFT", frame, "TOPLEFT", 6, -4)
local r, g, b = ns.Skin.Color("ink")
if r then pcall(plate.SetColorTexture, plate, r, g, b) end
pcall(plate.SetMask, plate, ROUND)
frame.rackPlate = plate
-- The mark. Offset seven right and five down rather than centred: the
-- game's ring texture is drawn with a margin, and an icon centred on the
-- button sticks out of the ring sideways.
local icon = frame:CreateTexture(nil, "ARTWORK")
pcall(icon.SetTexture, icon, MARK)
pcall(icon.SetSize, icon, ICON_SIZE, ICON_SIZE)
pcall(icon.SetPoint, icon, "TOPLEFT", frame, "TOPLEFT", 7, -5)
frame.rackIcon = icon
-- The ring over everything.
local ring = frame:CreateTexture(nil, "OVERLAY")
pcall(ring.SetTexture, ring, RING)
pcall(ring.SetSize, ring, RING_SIZE, RING_SIZE)
pcall(ring.SetPoint, ring, "TOPLEFT", frame, "TOPLEFT", 0, 0)
frame.rackRing = ring
Launcher.Recolour()
pcall(frame.SetScript, frame, "OnEnter", function(self)
-- Under the mouse the mark lightens to white: an accent on an accent
-- does not read, while white differs from any set.
if self.rackIcon then
pcall(self.rackIcon.SetVertexColor, self.rackIcon, 1, 1, 1)
end
local tip = _G.GameTooltip
if type(tip) ~= "table" then return end
pcall(tip.SetOwner, tip, self, "ANCHOR_LEFT")
pcall(tip.ClearLines, tip)
pcall(tip.AddLine, tip, "Rack")
local dr, dg, db = ns.Skin.Color("textDim")
pcall(tip.AddLine, tip, ns.L("Click to open the settings."), dr, dg, db)
pcall(tip.AddLine, tip,
ns.L("Right-click to unlock the bars for dragging."), dr, dg, db)
pcall(tip.AddLine, tip, ns.L("Drag to move this button."), dr, dg, db)
pcall(tip.Show, tip)
end)
pcall(frame.SetScript, frame, "OnLeave", function()
Launcher.Recolour()
local tip = _G.GameTooltip
if type(tip) == "table" then pcall(tip.Hide, tip) end
end)
pcall(frame.SetScript, frame, "OnClick", function(_, click)
if click == "RightButton" then
ns.Bars.MoveMode()
else
ns.Window.Toggle()
end
end)
-- Dragging along the ring. With an OnUpdate handler of our own rather than
-- StartMoving: StartMoving would take the button off the ring, with nothing
-- to put it back on.
pcall(frame.SetScript, frame, "OnDragStart", function(self)
pcall(self.SetScript, self, "OnUpdate", function()
local okCursor, mx, my = pcall(_G.GetCursorPosition)
if not okCursor then return end
local scale = 1
if type(_G.UIParent) == "table"
and type(_G.UIParent.GetEffectiveScale) == "function" then
local okScale, said = pcall(_G.UIParent.GetEffectiveScale, _G.UIParent)
if okScale and tonumber(said) and said > 0 then scale = said end
end
local okCentre, cx, cy = pcall(_G.Minimap.GetCenter, _G.Minimap)
if not okCentre then return end
local store = Launcher.Store(ns.Bars.Store())
if store == nil then return end
store.angle = Launcher.AngleOf(cx, cy, mx / scale, my / scale)
place()
end)
end)
pcall(frame.SetScript, frame, "OnDragStop", function(self)
pcall(self.SetScript, self, "OnUpdate", nil)
end)
return frame
end
--- Repaint the mark for the chosen accent set.
--
-- The mark is drawn in white and the game gives it its colour. One file for
-- four sets: otherwise four pictures would have to be kept and all four
-- redrawn on every edit of the mark.
function Launcher.Recolour()
if button == nil or button.rackIcon == nil then return end
local r, g, b = ns.Skin.Color("accent")
if r then
pcall(button.rackIcon.SetVertexColor, button.rackIcon, r, g, b)
end
if button.rackPlate then
local ir, ig, ib = ns.Skin.Color("ink")
if ir then
pcall(button.rackPlate.SetColorTexture, button.rackPlate,
ir, ig, ib)
end
end
end
--- Show or hide the button according to the settings.
--
-- Called on entering the world and after the setting changes. Hidden by ALPHA
-- and the mouse rather than by Hide: a button hidden with Hide loses its place
-- on the ring and comes back to the centre of the minimap.
--
-- @return boolean whether it is shown
function Launcher.Apply()
local saved = ns.Bars.Store()
local store = Launcher.Store(saved)
if store == nil then return false end
if not store.shown then
if button then
pcall(button.Hide, button)
end
return false
end
if make() == nil then return false end
place()
pcall(button.Show, button)
return true
end