Repository navigation
Expand file tree
/
Copy pathFlyout.lua
More file actions
695 lines (588 loc) · 22.9 KB
/
Copy pathFlyout.lua
File metadata and controls
695 lines (588 loc) · 22.9 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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
local ADDON, ns = ...
-- Flyouts.
--
-- === What they are ===
--
-- A bar slot sometimes holds not a spell but a FLYOUT: a mage's portals, a
-- hunter's call pet, seals. The game draws an arrow on such a button, lays out
-- a column with the flyout's contents beside it on a press, and the choice is
-- made from that.
--
-- Without this a flyout on our bar is a button that does nothing. The player
-- puts their portals on the bar and sees that the bar is broken.
--
-- === Why this is the hardest place in the addon ===
--
-- A button that casts a spell has to be secure, and insecure code may not show
-- or move secure buttons in combat - which is exactly when a flyout is opened.
-- So the opening lives entirely in a SECURE SNIPPET: the engine runs it, and
-- for the engine there is no such restriction.
--
-- And a snippet lives in a separate environment: none of our tables or
-- functions are visible to it, and it cannot ask the game what is in a flyout.
-- So the flyout contents are ASSEMBLED INTO CODE and loaded into that
-- environment in advance, out of combat (Flyout.Code). The snippet reads what
-- is already there.
--
-- LibActionButton, which Bartender4 stands on, is built the same way, and not
-- out of love for complexity: there is no other path.
--
-- === What is deliberately absent ===
--
-- A "which way to open" setting. The direction is computed from the bar's
-- place on screen: a bar at the bottom opens upwards, one at the top
-- downwards. A flyout opening off the screen edge is not a choice worth
-- offering.
local Flyout = {}
ns.Flyout = Flyout
-- How many ids to walk when searching for flyouts. Flyouts are numbered
-- consecutively from one, and an upper bound with room to spare is cheaper
-- than reading the spellbook: the walk happens once per login.
local LIMIT = 300
-- The gap between flyout buttons and the inset from the button itself.
local GAP = 4
-- What is known about the flyouts: [flyout id] = array of spell ids.
local known = {}
-- The secure handler and its buttons.
local handler = nil
local pool = {}
-- A re-sync deferred until combat ends.
local queued = false
--============================================================================
-- Flyout contents
--============================================================================
--- A flyout's contents: the spell ids the player knows.
--
-- Pure: it asks not the game but the reader passed to it. That makes it
-- verifiable outside the client and, at the same time, puts the rule "an
-- unknown spell does not make it into a flyout" in one place.
--
-- Nothing in the "is it known" answer means "the build did not say", and then
-- the spell is taken: a flyout that lost its contents to ignorance is worse
-- than one with a row too many.
--
-- @param count any how many places in the flyout
-- @param read function (place) -> spell id, whether known
-- @return table array of ids
function Flyout.Spells(count, read)
local out = {}
if type(read) ~= "function" then return out end
local many = tonumber(count) or 0
for at = 1, many do
local ok, id, learned = pcall(read, at)
if ok then
id = tonumber(id)
if id and learned ~= false then out[#out + 1] = id end
end
end
return out
end
--- The longest flyout.
--
-- The button pool is created by it: as many buttons are needed as there are
-- places in the longest flyout, and not one more.
--
-- @param data any
-- @return number
function Flyout.Widest(data)
if type(data) ~= "table" then return 0 end
local most = 0
for _, spells in pairs(data) do
if type(spells) == "table" and #spells > most then most = #spells end
end
return most
end
--- The flyout contents assembled into code for the restricted environment.
--
-- === Why code and not a table ===
--
-- The restricted environment is a separate space: none of our tables are in
-- it and they cannot be put there. Only CODE can be put there, which the
-- engine runs, and that code creates the tables itself - by calling newtable,
-- because there is no ordinary table in that environment either.
--
-- The numbers are substituted through %d, and that is not style but
-- protection: a string arriving from the game would become executable code in
-- this place. %d drops the substitution on anything that is not a number -
-- and the tonumber below makes sure there is nothing to drop on.
--
-- The order of the ids is fixed rather than whatever came out: identical
-- contents have to produce identical code, or it cannot be verified.
--
-- Pure.
--
-- @param data any
-- @return string
function Flyout.Code(data)
local lines = { "RackFlyouts = newtable()" }
if type(data) ~= "table" then return lines[1] end
local ids = {}
for id in pairs(data) do
if tonumber(id) then ids[#ids + 1] = tonumber(id) end
end
table.sort(ids)
for _, id in ipairs(ids) do
local spells = data[id]
if type(spells) == "table" and #spells > 0 then
lines[#lines + 1] = string.format("RackFlyouts[%d] = newtable()", id)
for at = 1, #spells do
local spell = tonumber(spells[at])
if spell then
lines[#lines + 1] = string.format(
"RackFlyouts[%d][%d] = %d", id, at, spell)
end
end
end
end
return table.concat(lines, "\n")
end
--- Whether this call-pet place is empty.
--
-- Call pet is a flyout of five places, and the places are always there even
-- when there is no pet in them. The game does not show such places and
-- neither do we: a button that calls nobody is worse than a missing one.
--
-- @param spell any
-- @return boolean
local function emptyPet(spell)
if type(_G.GetCallPetSpellInfo) ~= "function" then return false end
local ok, index, name = pcall(_G.GetCallPetSpellInfo, spell)
if not ok or index == nil then return false end
return ns.Probe.Textual(name) == nil
end
--- Walk this build's flyouts.
--
-- Builds without flyouts are ordinary: there were none at all before the
-- fourth expansion. Then this is zero and nothing else happens.
--
-- @return number how many flyouts were found
function Flyout.Scan()
known = {}
if type(_G.GetFlyoutInfo) ~= "function" then return 0 end
if type(_G.GetFlyoutSlotInfo) ~= "function" then return 0 end
local found = 0
for id = 1, LIMIT do
local ok, _, _, count, learned = pcall(_G.GetFlyoutInfo, id)
if ok and ns.Probe.Numeric(count) and learned ~= false then
local spells = Flyout.Spells(count, function(at)
local okSlot, spell, _, learnedSlot =
pcall(_G.GetFlyoutSlotInfo, id, at)
if not okSlot then return nil, false end
if emptyPet(spell) then return nil, false end
return spell, learnedSlot
end)
if #spells > 0 then
known[id] = spells
found = found + 1
end
end
end
return found
end
--- What is known about the flyouts.
-- @return table
function Flyout.Known()
return known
end
--============================================================================
-- The handler and its buttons
--============================================================================
--- The secure flyout handler: create it or return the existing one.
--
-- One for the whole addon rather than one per bar: there is one open flyout on
-- screen, and a second handler would mean two flyouts open at once.
--
-- @return table|nil
function Flyout.Handler()
if handler then return handler end
if type(CreateFrame) ~= "function" then return nil end
local ok, frame = pcall(CreateFrame, "Frame", "RackFlyout", _G.UIParent,
"SecureHandlerBaseTemplate")
if not ok or type(frame) ~= "table" then return nil end
pcall(frame.Hide, frame)
pcall(frame.SetFrameStrata, frame, "DIALOG")
-- The ground under the column: the same as the bars' - a flyout has to
-- look like part of the addon rather than a foreign window that popped up.
ns.Skin.Backdrop(frame, "panel", "line")
-- Opening a flyout. The engine runs it, and that is the only reason it
-- works in combat (details at the top of the file).
--
-- The flyout id arrives as an argument; "..." is it.
pcall(frame.SetAttribute, frame, "RackOpen", [[
local parent = self:GetAttribute("rackParent")
if not parent then return end
-- A second press on the same button closes it. That is how the game
-- behaves, and how anything that unrolls is built.
if self:IsShown() and self:GetParent() == parent then
self:Hide()
return
end
local id = ...
local spells = RackFlyouts and RackFlyouts[id]
if not spells then return end
local bar = parent:GetParent()
local way = bar and bar:GetAttribute("rackFlyoutWay") or "UP"
local size = parent:GetWidth()
local gap = self:GetAttribute("rackGap") or 4
local step = size + gap
local many = self:GetAttribute("rackSlots") or 0
local shown = 0
for at = 1, many do
local slot = self:GetFrameRef("rackSlot" .. at)
if slot then
local spell = spells[at]
if spell then
shown = shown + 1
slot:SetAttribute("type", "spell")
slot:SetAttribute("spell", spell)
slot:SetWidth(size)
slot:SetHeight(size)
slot:ClearAllPoints()
local away = (at - 1) * step
if way == "DOWN" then
slot:SetPoint("TOP", self, "TOP", 0, -away)
elseif way == "LEFT" then
slot:SetPoint("RIGHT", self, "RIGHT", -away, 0)
elseif way == "RIGHT" then
slot:SetPoint("LEFT", self, "LEFT", away, 0)
else
slot:SetPoint("BOTTOM", self, "BOTTOM", 0, away)
end
slot:Show()
slot:CallMethod("RackPaint", spell)
else
-- A spare button is not merely hidden: the spell is taken
-- off it too. A hidden button with a spell would still be
-- pressable by key.
slot:Hide()
slot:SetAttribute("type", nil)
slot:SetAttribute("spell", nil)
slot:CallMethod("RackPaint")
end
end
end
if shown == 0 then
self:Hide()
return
end
local long = size * shown + gap * (shown - 1) + gap * 2
self:SetParent(parent)
self:ClearAllPoints()
if way == "DOWN" then
self:SetWidth(size + gap * 2)
self:SetHeight(long)
self:SetPoint("TOP", parent, "BOTTOM", 0, -gap)
elseif way == "LEFT" then
self:SetWidth(long)
self:SetHeight(size + gap * 2)
self:SetPoint("RIGHT", parent, "LEFT", -gap, 0)
elseif way == "RIGHT" then
self:SetWidth(long)
self:SetHeight(size + gap * 2)
self:SetPoint("LEFT", parent, "RIGHT", gap, 0)
else
self:SetWidth(size + gap * 2)
self:SetHeight(long)
self:SetPoint("BOTTOM", parent, "TOP", 0, gap)
end
self:Show()
]])
pcall(frame.SetAttribute, frame, "rackGap", GAP)
pcall(frame.SetAttribute, frame, "rackSlots", 0)
-- Closing when the mouse leaves. With a count of our own per frame rather
-- than by event: there are no events for a mouse leaving a group of
-- frames, and a flyout left on screen after the player changed their mind
-- is closed by clicking into empty space - that is, never.
--
-- Hide from here is allowed in combat too: the frame is ours and the
-- engine does not protect it. What is forbidden belongs to the protected.
pcall(frame.SetScript, frame, "OnUpdate", function(self)
if not self:IsShown() then return end
local near = false
if type(self.IsMouseOver) == "function" then
local ok, over = pcall(self.IsMouseOver, self, GAP, -GAP, -GAP, GAP)
near = ok and over and true or false
end
if not near then
local owner = self:GetParent()
if type(owner) == "table"
and type(owner.IsMouseOver) == "function" then
local ok, over = pcall(owner.IsMouseOver, owner)
near = ok and over and true or false
end
end
if not near then pcall(self.Hide, self) end
end)
handler = frame
return handler
end
--- Create as many flyout buttons as are needed.
--
-- The buttons are not taken back: a created frame cannot be given back to the
-- engine, and a spare hidden button costs nothing. They are created only out
-- of combat - as everything else is.
--
-- @param many any how many are needed
-- @return number how many there are
function Flyout.Pool(many)
local want = tonumber(many) or 0
if want <= 0 then return #pool end
local host = Flyout.Handler()
if host == nil then return #pool end
if ns.Build.InCombat() then return #pool end
for at = #pool + 1, want do
local button = ns.Build.Spare(at, host)
if button == nil then break end
pcall(host.SetFrameRef, host, "rackSlot" .. at, button)
pool[at] = button
end
pcall(host.SetAttribute, host, "rackSlots", #pool)
return #pool
end
--- The flyout buttons.
-- @return table
function Flyout.Buttons()
return pool
end
--============================================================================
-- Re-syncing
--============================================================================
--- Walk the flyouts again and hand the contents to the restricted environment.
--
-- Deferred in combat: both loading the code and creating buttons are forbidden
-- there. ns.Flyout.Settle finishes the deferred work on leaving combat.
--
-- @return boolean whether it was done
function Flyout.Sync()
if ns.Build.InCombat() then
queued = true
return false
end
Flyout.Scan()
local host = Flyout.Handler()
if host == nil then return false end
if type(host.Execute) ~= "function" then return false end
if not pcall(host.Execute, host, Flyout.Code(known)) then return false end
Flyout.Pool(Flyout.Widest(known))
return true
end
--- Finish the deferred work.
-- @return boolean
function Flyout.Settle()
if not queued then return false end
queued = false
return Flyout.Sync()
end
--- Close the open flyout.
--
-- Needed by everything that changes the bars under the player: applying
-- settings, switching profiles, dragging. A flyout left hanging on a button
-- that no longer exists hangs in empty space.
function Flyout.Shut()
if handler == nil then return end
pcall(handler.Hide, handler)
end
--============================================================================
-- Which way to open
--============================================================================
--- The direction, from the place on screen.
--
-- Pure. A bar in the lower half opens upwards, one in the upper half
-- downwards. Otherwise the flyout would leave the screen edge, where it can be
-- neither seen nor pressed.
--
-- Nothing in the answers means "we do not know", and then upwards: bars stand
-- at the bottom more often than at the top.
--
-- @param middle any the bar's vertical middle
-- @param tall any the screen height
-- @return string
function Flyout.Way(middle, tall)
local at = tonumber(middle)
local height = tonumber(tall)
if at == nil or height == nil or height <= 0 then return "UP" end
return at < height / 2 and "UP" or "DOWN"
end
--- Tell the bar which way to open its flyouts.
--
-- Stored on the bar itself rather than on every button: a bar has one
-- direction, and the snippet asks the button's parent for it (the
-- "rackFlyoutWay" attribute).
--
-- @param key any bar key
-- @return string|nil
function Flyout.Aim(key)
local frame = ns.Build.Frame(key)
if type(frame) ~= "table" then return nil end
if type(frame.SetAttribute) ~= "function" then return nil end
local middle, tall
if type(frame.GetCenter) == "function" then
local ok, _, y = pcall(frame.GetCenter, frame)
if ok then middle = ns.Probe.Numeric(y) end
end
if type(_G.UIParent) == "table"
and type(_G.UIParent.GetHeight) == "function" then
local ok, said = pcall(_G.UIParent.GetHeight, _G.UIParent)
if ok then tall = ns.Probe.Numeric(said) end
end
local way = Flyout.Way(middle, tall)
pcall(frame.SetAttribute, frame, "rackFlyoutWay", way)
return way
end
--============================================================================
-- The arrow on the button
--============================================================================
-- What the point is made of.
--
-- The arrow is built from STRIPS rather than a texture, for the same reason
-- the frames are: the game's artwork is named differently on some builds while
-- a white pixel exists everywhere. A triangle of four strips of decreasing
-- length reads as a triangle at arm's length - and nothing more is asked of it.
local ROWS = 4
--- Create the arrow on a button or return the existing one.
-- @param button table
-- @return table|nil
local function arrowOf(button)
if button.rackArrow then return button.rackArrow end
if type(button.CreateTexture) ~= "function" then return nil end
local rows = {}
for at = 1, ROWS do
local line = button:CreateTexture(nil, "OVERLAY")
local r, g, b = ns.Skin.Color("text")
if r == nil then r, g, b = 1, 1, 1 end
pcall(line.SetColorTexture, line, r, g, b, 0.9)
pcall(line.Hide, line)
rows[at] = line
end
button.rackArrow = rows
return rows
end
--- Show or remove the flyout arrow.
--
-- @param button any
-- @param way any the direction; nil - remove it
-- @return boolean whether it is visible
function Flyout.Arrow(button, way)
if type(button) ~= "table" then return false end
local rows = arrowOf(button)
if rows == nil then return false end
if way == nil then
for at = 1, ROWS do pcall(rows[at].Hide, rows[at]) end
return false
end
-- The strips go from wide to narrow with the point outwards: that way the
-- triangle points where the flyout will open.
for at = 1, ROWS do
local line = rows[at]
local long = (ROWS - at + 1) * 2 - 1
pcall(line.ClearAllPoints, line)
if way == "DOWN" then
pcall(line.SetSize, line, long, 1)
pcall(line.SetPoint, line, "BOTTOM", button, "BOTTOM", 0, at)
elseif way == "LEFT" then
pcall(line.SetSize, line, 1, long)
pcall(line.SetPoint, line, "LEFT", button, "LEFT", at, 0)
elseif way == "RIGHT" then
pcall(line.SetSize, line, 1, long)
pcall(line.SetPoint, line, "RIGHT", button, "RIGHT", -at, 0)
else
pcall(line.SetSize, line, long, 1)
pcall(line.SetPoint, line, "TOP", button, "TOP", 0, -at)
end
pcall(line.Show, line)
end
return true
end
--============================================================================
-- Drawing a flyout button
--============================================================================
--- This button's spell: icon, cooldown, state.
--
-- Called FROM THE SECURE SNIPPET through CallMethod - which is why it also
-- lives as a method on the button itself (ns.Build.Spare): the snippet finds
-- it by the method's name.
--
-- Nothing means "the button shows nothing any more".
--
-- @param button table
-- @param spell any
-- @return boolean
function Flyout.Paint(button, spell)
if type(button) ~= "table" then return false end
local id = tonumber(spell)
button.rackSpell = id
if button.rackCount then
pcall(button.rackCount.SetText, button.rackCount, "")
end
if button.rackMacro then
pcall(button.rackMacro.SetText, button.rackMacro, "")
end
if button.rackHotkey then
pcall(button.rackHotkey.SetText, button.rackHotkey, "")
end
if id == nil then
if button.rackIcon then pcall(button.rackIcon.Hide, button.rackIcon) end
return false
end
-- The icon. Modern builds answer through C_Spell, the rest by the old
-- name; both are asked in turn, as everywhere in this addon.
local texture
if type(_G.C_Spell) == "table"
and type(_G.C_Spell.GetSpellTexture) == "function" then
local ok, said = pcall(_G.C_Spell.GetSpellTexture, id)
if ok then texture = said end
end
if texture == nil and type(_G.GetSpellTexture) == "function" then
local ok, said = pcall(_G.GetSpellTexture, id)
if ok then texture = said end
end
if button.rackIcon then
if texture then
pcall(button.rackIcon.SetTexture, button.rackIcon, texture)
pcall(button.rackIcon.Show, button.rackIcon)
else
pcall(button.rackIcon.Hide, button.rackIcon)
end
end
-- Cooldown.
if button.rackCooldown then
local start, duration, draw
if type(_G.C_Spell) == "table"
and type(_G.C_Spell.GetSpellCooldown) == "function" then
local ok, said = pcall(_G.C_Spell.GetSpellCooldown, id)
if ok and type(said) == "table" then
start, duration, draw =
said.startTime, said.duration, said.isEnabled
end
end
if start == nil and type(_G.GetSpellCooldown) == "function" then
local ok, a, b, c = pcall(_G.GetSpellCooldown, id)
if ok then start, duration, draw = a, b, c end
end
start = ns.Probe.Numeric(start)
duration = ns.Probe.Numeric(duration)
-- The same setter the action slots use. The third number - "whether
-- to draw at all" - is read there, along with the reason a cooldown
-- is cleared rather than set to zero when it is over.
ns.Paint.Sweep(button.rackCooldown, start, duration, draw)
end
-- State by colour: the same rule as a bar button's, with the same single
-- owner - ns.Paint.State.
if button.rackIcon then
local usable, noPower
if type(_G.C_Spell) == "table"
and type(_G.C_Spell.IsSpellUsable) == "function" then
local ok, can, lacking = pcall(_G.C_Spell.IsSpellUsable, id)
if ok then usable, noPower = can, lacking end
end
if usable == nil and type(_G.IsUsableSpell) == "function" then
local ok, can, lacking = pcall(_G.IsUsableSpell, id)
if ok then usable, noPower = can, lacking end
end
local state = ns.Paint.State(ns.Probe.Truth(usable),
ns.Probe.Truth(noPower), nil)
local r, g, b = ns.Paint.Tint(state)
pcall(button.rackIcon.SetVertexColor, button.rackIcon, r, g, b)
end
return true
end