This repository was archived by the owner on Apr 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.lua
More file actions
585 lines (495 loc) · 17.6 KB
/
main.lua
File metadata and controls
585 lines (495 loc) · 17.6 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
local _G = getfenv(0)
local revision = 1.0
local bars = {
'Action',
'BonusAction',
'MultiBarBottomLeft',
'MultiBarBottomRight',
'MultiBarRight',
'MultiBarLeft'
}
local flyouts = {}
FLYOUT_DEFAULT_CONFIG = {
['REVISION'] = revision,
['BUTTON_SIZE'] = 28,
['BORDER_COLOR'] = { 0, 0, 0 },
['ARROW_SCALE'] = 5/9,
}
local ARROW_RATIO = 0.6 -- Height to width.
-- upvalues
local ActionButton_GetPagedID = ActionButton_GetPagedID
local ChatEdit_SendText = ChatEdit_SendText
local GetActionText = GetActionText
local GetContainerItemLink = GetContainerItemLink
local GetContainerNumSlots = GetContainerNumSlots
local GetNumSpellTabs = GetNumSpellTabs
local GetSpellName = GetSpellName
local GetSpellTabInfo = GetSpellTabInfo
local GetScreenHeight = GetScreenHeight
local GetScreenWidth = GetScreenWidth
local HasAction = HasAction
local GetMacroIndexByName = GetMacroIndexByName
local GetMacroInfo = GetMacroInfo
local insert = table.insert
local rawset = rawset
local remove = table.remove
local sizeof = table.getn
local strfind = string.find
local strgsub = string.gsub
local strlower = string.lower
local strsub = string.sub
-- helper functions
local function strtrim(str)
local _, e = strfind(str, '^%s*')
local s, _ = strfind(str, '%s*$', e + 1)
return strsub(str, e + 1, s - 1)
end
local function wipe(tbl)
if type(tbl) ~= 'table' then
return
end
for i = sizeof(tbl), 1, -1 do
remove(tbl, i)
end
-- Credit: https://stackoverflow.com/a/27287723
for k in next, tbl do
rawset(tbl, k, nil)
end
end
local tsplit = {}
local function strsplit(str, delimiter, fillTable)
fillTable = fillTable or tsplit
wipe(fillTable)
strgsub(str, '([^' .. delimiter .. ']+)', function(value)
insert(fillTable, strtrim(value))
end)
return fillTable
end
function GetBagPosition(name)
local link
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
item = GetContainerItemLink(bag, slot)
if item and strfind(strlower(item), strlower(name)) then
return bag, slot
end
end
end
end
function GetBagItemByName(name)
local item
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
item = GetContainerItemLink(bag, slot)
if item and strfind(strlower(item), strlower(name)) then
local _, _, itemLink = strfind(GetContainerItemLink(bag, slot), '(item:%d+)')
return itemLink, bag, slot
end
end
end
end
-- credit: https://github.com/DanielAdolfsson/CleverMacro
local function GetSpellSlotByName(name)
local count, offset, spell, subSpell
name = strlower(name)
local b, _, rank = strfind(name, '%(%s*rank%s+(%d+)%s*%)')
if b then name = (b > 1) and strtrim(strsub(name, 1, b - 1)) or '' end
for tabIndex = GetNumSpellTabs(), 1, -1 do
_, _, offset, count = GetSpellTabInfo(tabIndex)
for index = offset + count, offset + 1, -1 do
spell, subSpell = GetSpellName(index, 'spell')
spell = strlower(spell)
if name == spell and (not rank or subSpell == 'Rank ' .. rank) then
return index
end
end
end
end
-- Returns <action>, <actionType>, <actionTexture>
local function GetFlyoutActionInfo(action)
if GetSpellSlotByName(action) then
local spell = GetSpellSlotByName(action)
return spell, 0, GetSpellTexture(spell, 'spell')
elseif GetBagItemByName(action) then
local item, bag, slot = GetBagItemByName(action)
return item, 2, GetContainerItemInfo(bag, slot), bag, slot
elseif GetMacroIndexByName(action) then
local macro = GetMacroIndexByName(action)
local _, texture = GetMacroInfo(macro)
return macro, 1, texture
end
end
local function GetFlyoutDirection(button)
if button.flyoutDirection then
return button.flyoutDirection
end
local horizontal = false
local bar = button:GetParent()
if bar:GetWidth() > bar:GetHeight() then
horizontal = true
end
local direction = horizontal and 'TOP' or 'LEFT'
local centerX, centerY = button:GetCenter()
if centerX and centerY then
if horizontal then
local halfScreen = GetScreenHeight() / 2
direction = centerY < halfScreen and 'TOP' or 'BOTTOM'
else
local halfScreen = GetScreenWidth() / 2
direction = centerX > halfScreen and 'LEFT' or 'RIGHT'
end
end
return direction
end
local function FlyoutBarButton_OnLeave()
this.updateTooltip = nil
GameTooltip:Hide()
local focus = GetMouseFocus()
if focus and not strfind(focus:GetName(), 'Flyout') then
Flyout_Hide()
end
end
local function FlyoutBarButton_OnEnter()
Flyout_Show(this)
pcall(ActionButton_SetTooltip)
end
local function UpdateBarButton(slot)
local button = Flyout_GetActionButton(slot)
if button then
local arrow = _G[button:GetName() .. 'FlyoutArrow']
if arrow then
arrow:Hide()
end
if not HasAction(slot) or not GetActionText(slot) then
-- Reset button to pre-Flyout condition.
button.flyoutActionType = nil
button.flyoutAction = nil
if button.preFlyoutOnEnter then
button:SetScript('OnEnter', button.preFlyoutOnEnter)
button:SetScript('OnLeave', button.preFlyoutOnLeave)
button.preFlyoutOnEnter = nil
button.preFlyoutOnLeave = nil
end
flyouts[slot] = nil
return
end
button.sticky = false
button.flyoutDirection = nil
local icon = false
local macro = GetActionText(slot)
local _, _, body = GetMacroInfo(GetMacroIndexByName(macro))
local s, e = strfind(body, '/flyout')
if s and s == 1 and e == 7 then
if not button.preFlyoutOnEnter then
button.preFlyoutOnEnter = button:GetScript('OnEnter')
button.preFlyoutOnLeave = button:GetScript('OnLeave')
end
-- Identify sticky menus.
if strfind(body, '%[sticky%]') then
body = strgsub(body, '%[sticky%]', '')
button.sticky = true
end
if strfind(body, '%[icon%]') then
icon = true
body = strgsub(body, '%[icon%]', '')
end
-- Identify direction override.
local _, _, dirValue = strfind(body, '%[direction:(%a+)%]')
if dirValue then
body = strgsub(body, '%[direction:%a+%]', '')
local dirMap = { up = 'TOP', down = 'BOTTOM', left = 'LEFT', right = 'RIGHT' }
button.flyoutDirection = dirMap[strlower(dirValue)]
end
body = strsub(body, e + 1)
if not button.flyoutActions then
button.flyoutActions = {}
end
strsplit(body, ';', button.flyoutActions)
if table.getn(button.flyoutActions) > 0 then
local cost
local action, type, texture, bag, bagSlot = GetFlyoutActionInfo(button.flyoutActions[1])
if type == 0 then
FlyoutScanner:SetOwner(WorldFrame, 'ANCHOR_NONE')
FlyoutScanner:SetSpell(action, 'spell')
_, _, cost = string.find(FlyoutScanner.manaText:GetText() or '', '^(%d+)')
end
flyouts[slot] = {
action = action,
type = type,
texture = icon and texture or false,
cost = cost and tonumber(cost) or 0,
bag = bag,
slot = bagSlot
}
button.flyoutAction = action
button.flyoutActionType = type
end
Flyout_UpdateFlyoutArrow(button)
button:SetScript('OnLeave', FlyoutBarButton_OnLeave)
button:SetScript('OnEnter', FlyoutBarButton_OnEnter)
end
end
end
local function HandleEvent()
if event == 'VARIABLES_LOADED' then
if not Flyout_Config or (Flyout_Config['REVISION'] == nil or Flyout_Config['REVISION'] ~= revision) then
Flyout_Config = {}
end
-- Initialize defaults if not present.
for key, value in pairs(FLYOUT_DEFAULT_CONFIG) do
if not Flyout_Config[key] then
Flyout_Config[key] = value
end
end
return
elseif event == 'PLAYER_ENTERING_WORLD' then
local scanner = CreateFrame('GameTooltip', 'FlyoutScanner')
scanner:SetOwner(WorldFrame, 'ANCHOR_NONE')
scanner.nameText = scanner:CreateFontString()
scanner.rankText = scanner:CreateFontString()
scanner.manaText = scanner:CreateFontString()
scanner:AddFontStrings(scanner.nameText, scanner.rankText)
scanner:AddFontStrings(scanner.manaText, scanner:CreateFontString())
elseif event == 'ACTIONBAR_SLOT_CHANGED' then
Flyout_Hide(true) -- Keep sticky menus open.
UpdateBarButton(arg1)
return
end
Flyout_Hide()
Flyout_UpdateBars()
end
local handler = CreateFrame('Frame')
handler:RegisterEvent('VARIABLES_LOADED')
handler:RegisterEvent('PLAYER_ENTERING_WORLD')
handler:RegisterEvent('ACTIONBAR_SLOT_CHANGED')
handler:RegisterEvent('ACTIONBAR_PAGE_CHANGED')
handler:SetScript('OnEvent', HandleEvent)
-- globals
function Flyout_OnClick(button)
if not button or not button.flyoutActionType or not button.flyoutAction or button.flyoutAction == 0 then
return
end
if arg1 == nil or arg1 == 'LeftButton' then
if button.flyoutActionType == 0 then
CastSpell(button.flyoutAction, 'spell')
elseif button.flyoutActionType == 1 then
Flyout_ExecuteMacro(button.flyoutAction)
elseif button.flyoutActionType == 2 then
UseContainerItem(GetBagPosition(button.flyoutAction))
end
Flyout_Hide(true)
elseif arg1 == 'RightButton' and button.flyoutParent then
local parent = button.flyoutParent
local oldAction = parent.flyoutActions[1]
local newAction = parent.flyoutActions[button:GetID()]
if oldAction ~= newAction then
local slot = ActionButton_GetPagedID(parent)
local macro = GetActionText(slot)
local name, icon, body, isLocal = GetMacroInfo(GetMacroIndexByName(macro))
local as, ae = string.find(body, oldAction, 1, true)
local bs, be = string.find(body, newAction, 1, true)
if as and bs then
body =
string.sub(body, 1, as - 1)
.. newAction
.. string.sub(body, ae + 1, bs - 1)
.. oldAction
.. string.sub(body, be + 1)
EditMacro(GetMacroIndexByName(macro), macro, icon, body, isLocal)
Flyout_Show(parent)
end
else
button:SetChecked(0)
end
end
end
function Flyout_ExecuteMacro(macro)
local _, _, body = GetMacroInfo(macro)
local commands = strsplit(body, '\n')
for i = 1, sizeof(commands) do
ChatFrameEditBox:SetText(commands[i])
ChatEdit_SendText(ChatFrameEditBox)
end
end
function Flyout_Hide(keepOpenIfSticky)
local i = 1
local button = _G['FlyoutButton' .. i]
while button do
i = i + 1
if not keepOpenIfSticky or (keepOpenIfSticky and not button.sticky) then
button:Hide()
button:GetNormalTexture():SetTexture(nil)
button:GetPushedTexture():SetTexture(nil)
end
-- Un-highlight if no longer needed.
if button.flyoutActionType ~= 0 or not IsCurrentCast(button.flyoutAction, 'spell') then
button:SetChecked(false)
end
button = _G['FlyoutButton' .. i]
end
-- Restore arrow to original strata (it was moved to FULLSCREEN in Flyout_Show())
if _G['FlyoutButton1'] and not _G['FlyoutButton1']:IsVisible() and _G['FlyoutButton1'].flyoutParent then
local arrow = _G[_G['FlyoutButton1'].flyoutParent:GetName() .. 'FlyoutArrow']
arrow:SetFrameStrata(arrow.flyoutOriginalStrata)
end
end
-- Reusable variables for FlyoutBarButton_UpdateCooldown().
local cooldownStart, cooldownDuration, cooldownEnable
local function FlyoutBarButton_UpdateCooldown(button, reset)
button = button or this
if button.flyoutActionType == 0 then
cooldownStart, cooldownDuration, cooldownEnable = GetSpellCooldown(button.flyoutAction, BOOKTYPE_SPELL)
if cooldownStart > 0 and cooldownDuration > 0 then
-- Start/Duration check is needed to get the shine animation.
CooldownFrame_SetTimer(button.cooldown, cooldownStart, cooldownDuration, cooldownEnable)
elseif reset then
-- When switching flyouts, need to hide cooldown if it shouldn't be visible.
button.cooldown:Hide()
end
else
button.cooldown:Hide()
end
end
function FlyoutBarButton_OnUpdate()
-- Update tooltip.
if GetMouseFocus() == this and (not this.lastUpdate or GetTime() - this.lastUpdate > 1) then
this:GetScript('OnEnter')()
this.lastUpdate = GetTime()
end
FlyoutBarButton_UpdateCooldown(this)
end
function Flyout_Show(button)
local direction = GetFlyoutDirection(button)
local size = Flyout_Config['BUTTON_SIZE']
local offset = size
-- Put arrow above the flyout buttons.
_G[button:GetName() .. 'FlyoutArrow']:SetFrameStrata('FULLSCREEN')
for i, n in button.flyoutActions do
local b = _G['FlyoutButton' .. i]
if not b then
b = CreateFrame('CheckButton', 'FlyoutButton' .. i, UIParent, 'FlyoutButtonTemplate')
b:SetID(i)
end
b.flyoutParent = button
b.sticky = button.sticky
local texture = nil
b.flyoutAction, b.flyoutActionType, texture = GetFlyoutActionInfo(n)
if texture then
b:ClearAllPoints()
b:SetWidth(size)
b:SetHeight(size)
b.cooldown:SetScale(size / b.cooldown:GetWidth()) -- Scale cooldown so it will stay centered on the button.
b:SetBackdropColor(Flyout_Config['BORDER_COLOR'][1], Flyout_Config['BORDER_COLOR'][2], Flyout_Config['BORDER_COLOR'][3])
b:Show()
b:GetNormalTexture():SetTexture(texture)
b:GetPushedTexture():SetTexture(texture) -- Without this, icons disappear on click.
-- Highlight professions and channeled casts.
if b.flyoutActionType == 0 and IsCurrentCast(b.flyoutAction, 'spell') then
b:SetChecked(true)
end
-- Force an instant update.
this.lastUpdate = nil
FlyoutBarButton_UpdateCooldown(b, true)
if direction == 'BOTTOM' then
b:SetPoint('BOTTOM', button, 0, -offset)
elseif direction == 'LEFT' then
b:SetPoint('LEFT', button, -offset, 0)
elseif direction == 'RIGHT' then
b:SetPoint('RIGHT', button, offset, 0)
else
b:SetPoint('TOP', button, 0, offset)
end
offset = offset + size
end
end
end
function Flyout_GetActionButton(action)
for i = 1, sizeof(bars) do
for j = 1, 12 do
local button = _G[bars[i] .. 'Button' .. j]
local slot = ActionButton_GetPagedID(button)
if slot == action then
return button
end
end
end
end
function Flyout_UpdateBars()
for i = 1, 120 do
UpdateBarButton(i)
end
end
function Flyout_UpdateFlyoutArrow(button)
if not button then return end
local direction = GetFlyoutDirection(button)
local arrow = _G[button:GetName() .. 'FlyoutArrow']
if not arrow then
arrow = CreateFrame('Frame', button:GetName() .. 'FlyoutArrow', button)
arrow:SetPoint('TOPLEFT', button)
arrow:SetPoint('BOTTOMRIGHT', button)
arrow.flyoutOriginalStrata = arrow:GetFrameStrata()
arrow.texture = arrow:CreateTexture(arrow:GetName() .. 'Texture', 'ARTWORK')
arrow.texture:SetTexture('Interface\\AddOns\\Flyout\\assets\\FlyoutButton')
end
arrow:Show()
arrow.texture:ClearAllPoints()
local arrowWideDimension = (button:GetWidth() or 36) * Flyout_Config['ARROW_SCALE']
local arrowShortDimension = arrowWideDimension * ARROW_RATIO
if direction == 'BOTTOM' then
arrow.texture:SetWidth(arrowWideDimension)
arrow.texture:SetHeight(arrowShortDimension)
arrow.texture:SetTexCoord(0, 0.565, 0.315, 0)
arrow.texture:SetPoint('BOTTOM', arrow, 0, -6)
elseif direction == 'LEFT' then
arrow.texture:SetWidth(arrowShortDimension)
arrow.texture:SetHeight(arrowWideDimension)
arrow.texture:SetTexCoord(0, 0.315, 0.375, 1)
arrow.texture:SetPoint('LEFT', arrow, -6, 0)
elseif direction == 'RIGHT' then
arrow.texture:SetWidth(arrowShortDimension)
arrow.texture:SetHeight(arrowWideDimension)
arrow.texture:SetTexCoord(0.315, 0, 0.375, 1)
arrow.texture:SetPoint('RIGHT', arrow, 6, 0)
else
arrow.texture:SetWidth(arrowWideDimension)
arrow.texture:SetHeight(arrowShortDimension)
arrow.texture:SetTexCoord(0, 0.565, 0, 0.315)
arrow.texture:SetPoint('TOP', arrow, 0, 6)
end
end
local original = {}
original.GetActionCooldown = GetActionCooldown
function GetActionCooldown(slot)
local action = flyouts[slot]
if action then
if action.type == 0 then
return GetSpellCooldown(action.action, 'spell')
elseif action.type == 2 then
return GetContainerItemCooldown(action.bag, action.slot)
end
end
return original.GetActionCooldown(slot)
end
original.GetActionTexture = GetActionTexture
function GetActionTexture(slot)
local action = flyouts[slot]
return action and action.texture or original.GetActionTexture(slot)
end
original.IsUsableAction = IsUsableAction
function IsUsableAction(slot)
local action = flyouts[slot]
if action then
if UnitMana('player') >= action.cost then
return true, false
else
return false, true
end
end
return original.IsUsableAction(slot, unit)
end
original.UseAction = UseAction
function UseAction(slot, checkCursor, onSelf)
original.UseAction(slot, checkCursor, onSelf)
Flyout_OnClick(Flyout_GetActionButton(slot))
Flyout_Hide()
end