-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.lua
More file actions
3400 lines (3176 loc) · 141 KB
/
Copy pathUI.lua
File metadata and controls
3400 lines (3176 loc) · 141 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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- PETAL UI
--
-- This file deliberately owns presentation only. Runtime/evaluation code can
-- expose optional APS:GetRules/APS:SetRuleEnabled/APS:SetRulePets APIs; the
-- journal will use them when present and falls back to the original
-- PETALDB shape when it is not. Nothing in this file writes secure
-- attributes or performs a protected action.
local APS = _G.PETAL
if not APS then
return
end
local UI = {}
APS.UI = UI
local WHITE = "Interface\\Buttons\\WHITE8x8"
local QUESTION_ICON = "Interface\\Icons\\INV_Misc_QuestionMark"
local PAPER_WASH = "Interface\\AddOns\\PETAL\\Art\\paper-wash.tga"
local NAV_ICON_ROOT = "Interface\\AddOns\\PETAL\\Art\\ui-v2\\"
local WORDMARK = NAV_ICON_ROOT .. "petal-wordmark-clean.png"
local JOURNAL_SHELL = NAV_ICON_ROOT .. "journal-shell.png"
local NO_PET_ART = NAV_ICON_ROOT .. "pawprint-stamp.png"
local DEFAULT_CAT_SPECIES_ID = 40 -- Blizzard's Bombay Cat
local DEFAULT_MODEL_FACING = math.rad(45)
local PET_PREVIEW_ROTATION_SPEED = math.rad(15)
local TODAY_ROTATION_SPEED = math.rad(5)
-- Retail does not expose a battle-pet model bounding box. These conservative
-- distances keep the largest wide/tall meshes inside their visual stages.
local HERO_MODEL_FIT_DISTANCE = 1.65
local PREVIEW_MODEL_FIT_DISTANCE = 1.50
local CHANGE_PET_BINDING = "PETAL_CHANGE_PET"
local function rgba8(red, green, blue, alpha)
return {red / 255, green / 255, blue / 255, alpha == nil and 1 or alpha}
end
-- Muted sand, clay, ochre, and wine from the supplied light references.
-- These avoid paper-white surfaces while keeping dark text readable.
local COLOR = {
panel = rgba8(191, 168, 135), -- warm parchment beige
rail = rgba8(174, 139, 124), -- dusty clay
card = rgba8(201, 178, 146), -- raised sand
cardAlt = rgba8(184, 139, 120), -- rose clay
border = rgba8(97, 71, 59), -- #61473B umber
borderSoft = rgba8(145, 117, 76), -- #91754C muted ochre
text = rgba8(67, 0, 17), -- #430011 wine-black
muted = rgba8(92, 51, 61), -- dusty wine
faint = rgba8(119, 70, 82), -- quiet rosewood
sky = rgba8(141, 97, 34), -- #8D6122 ochre
skySoft = rgba8(194, 164, 119), -- soft golden sand
mint = rgba8(141, 68, 34), -- #8D4422 burnt clay
mintSoft = rgba8(188, 143, 125), -- muted terracotta
lilac = rgba8(128, 31, 55), -- #801F37 berry
lilacSoft = rgba8(166, 113, 130), -- dusty mauve
amber = rgba8(141, 97, 34), -- #8D6122 ochre
amberSoft = rgba8(194, 164, 119), -- soft golden sand
red = rgba8(128, 31, 55), -- #801F37 berry
redSoft = rgba8(166, 113, 130), -- dusty mauve
hoverSurface = rgba8(211, 157, 143), -- warm rose highlight
hoverBorder = rgba8(128, 31, 55), -- berry outline
white = rgba8(244, 231, 216),
tagAll = rgba8(78, 42, 64), -- dark wine, deliberately neutral
tagAllSoft = rgba8(101, 73, 91),
tagCity = rgba8(78, 170, 194), -- clear sky blue
tagCitySoft = rgba8(173, 215, 225),
tagDungeon = rgba8(138, 103, 194), -- lavender
tagDungeonSoft = rgba8(207, 190, 229),
tagRaid = rgba8(218, 151, 52), -- warm amber
tagRaidSoft = rgba8(237, 206, 153),
tagOpenWorld = rgba8(72, 163, 111), -- meadow green
tagOpenWorldSoft = rgba8(176, 215, 190),
tagNeighborhood = rgba8(196, 91, 129), -- flower pink
tagNeighborhoodSoft = rgba8(226, 177, 195),
tagText = rgba8(37, 31, 35),
}
-- Cool ink surfaces with distinct elevation, selection, and accent states.
local DARK_COLOR = {
panel = rgba8(18, 16, 25), -- deepest page surface
rail = rgba8(28, 27, 40), -- raised navigation plane
card = rgba8(35, 33, 46), -- normal interactive surface
cardAlt = rgba8(43, 39, 54), -- secondary control surface
border = rgba8(90, 83, 105), -- focused outline
borderSoft = rgba8(66, 62, 76), -- quiet card separation
text = rgba8(238, 233, 239),
muted = rgba8(180, 172, 184),
faint = rgba8(125, 117, 132),
sky = rgba8(80, 173, 205),
skySoft = rgba8(27, 54, 69),
mint = rgba8(80, 199, 151), -- selected pet outline
mintSoft = rgba8(22, 69, 55), -- selected pet surface
lilac = rgba8(160, 124, 224), -- selected navigation accent
lilacSoft = rgba8(57, 43, 82), -- selected navigation surface
amber = rgba8(230, 171, 77),
amberSoft = rgba8(76, 54, 24),
red = rgba8(210, 92, 119),
redSoft = rgba8(73, 33, 45),
hoverSurface = rgba8(52, 48, 64), -- neutral lift, distinct from selection
hoverBorder = rgba8(110, 100, 124),
white = rgba8(245, 241, 244),
tagAll = rgba8(104, 72, 118), -- brighter selected row action
tagAllSoft = rgba8(42, 31, 49),
tagCity = rgba8(81, 190, 215), -- luminous without turning neon
tagCitySoft = rgba8(21, 58, 70),
tagDungeon = rgba8(160, 124, 224),
tagDungeonSoft = rgba8(45, 34, 66),
tagRaid = rgba8(231, 171, 75),
tagRaidSoft = rgba8(70, 49, 20),
tagOpenWorld = rgba8(83, 188, 129),
tagOpenWorldSoft = rgba8(22, 61, 43),
tagNeighborhood = rgba8(218, 104, 146),
tagNeighborhoodSoft = rgba8(68, 32, 49),
tagText = rgba8(18, 17, 22),
}
local LIGHT_SHELL_TINT = rgba8(191, 168, 135)
local DARK_SHELL_TINT = rgba8(31, 28, 42)
local LIGHT_COLOR = {}
for name, color in pairs(COLOR) do LIGHT_COLOR[name] = {color[1], color[2], color[3], color[4]} end
local THEME_BACKDROPS, THEME_FONTS, THEME_TEXTURES, THEME_EDITBOXES = {}, {}, {}, {}
-- The UI exposes the existing rule pools as five simple place tags. Rule IDs
-- stay stable so old selections migrate without duplicating saved data.
local TAG_DEFINITIONS = {
{key = "city", ruleID = "cities", label = "City", width = 64, color = COLOR.tagCity, soft = COLOR.tagCitySoft},
{key = "dungeon", ruleID = "dungeons", label = "Dungeon", width = 82, color = COLOR.tagDungeon, soft = COLOR.tagDungeonSoft},
{key = "raid", ruleID = "raids", label = "Raid", width = 58, color = COLOR.tagRaid, soft = COLOR.tagRaidSoft},
{key = "openworld", ruleID = "solo", label = "Open world", width = 96, color = COLOR.tagOpenWorld, soft = COLOR.tagOpenWorldSoft},
{key = "neighborhood", ruleID = "neighborhood", label = "Neighborhood", width = 108, color = COLOR.tagNeighborhood, soft = COLOR.tagNeighborhoodSoft},
}
-- ALL is a row action, not a sixth condition. It is intentionally compact
-- so the five actual place toggles stay visible at every supported UI scale.
local ALL_TAG_DEFINITION = {key = "all", label = "ALL", width = 36, color = COLOR.tagAll, soft = COLOR.tagAllSoft}
local TAG_BY_KEY = {}
for _, definition in ipairs(TAG_DEFINITIONS) do
TAG_BY_KEY[definition.key] = definition
end
local DEFAULT_RULES = {
{id = "everywhere", label = "Everywhere", description = "Use selected pets anywhere.", accent = "lilac"},
{id = "dungeons", label = "Dungeons", description = "Use selected pets in dungeons.", accent = "sky"},
{id = "raids", label = "Raids", description = "Use selected pets in raids.", accent = "lilac"},
{id = "solo", label = "Solo", description = "Use selected pets while solo.", accent = "mint"},
{id = "cities", label = "Cities", description = "Use selected pets in cities.", accent = "sky"},
{id = "neighborhood", label = "Home area", description = "Use selected pets near home.", accent = "mint"},
{id = "outfit", label = "Outfit match", description = "Use a separate pet list for outfits.", accent = "lilac"},
}
local SIMPLE_RULE_COPY = {}
for _, rule in ipairs(DEFAULT_RULES) do
SIMPLE_RULE_COPY[rule.id] = rule
end
local function copyColor(color, alpha)
return color[1], color[2], color[3], alpha == nil and color[4] or alpha
end
local function setThemeTextureColor(texture, color, alpha)
if not texture or not texture.SetColorTexture then return end
texture._apsThemeColor = color
texture._apsThemeAlpha = alpha
if not texture._apsThemeTracked then
texture._apsThemeTracked = true
table.insert(THEME_TEXTURES, texture)
end
texture:SetColorTexture(copyColor(color, alpha))
end
local function applyShellPalette(shell, dark)
if not shell then return end
-- Both modes share the exact proven rounded alpha mask. Dark mode is a
-- code tint, so it cannot reintroduce a baked opaque contour or halo.
shell:SetTexture(JOURNAL_SHELL)
if shell.SetDesaturated then pcall(shell.SetDesaturated, shell, true) end
local tint = dark and DARK_SHELL_TINT or LIGHT_SHELL_TINT
if shell.SetVertexColor then shell:SetVertexColor(copyColor(tint, 1)) end
shell:SetAlpha(1)
end
local function safeCall(object, method, ...)
if not object or type(object[method]) ~= "function" then
return false, nil
end
local ok, a, b, c, d, e, f, g, h, i = pcall(object[method], object, ...)
if not ok then
return false, nil
end
return true, a, b, c, d, e, f, g, h, i
end
local function isSecret(value)
if type(APS.IsSecret) == "function" then
local ok, secret = pcall(APS.IsSecret, APS, value)
if ok then return secret == true end
end
if type(_G.issecretvalue) == "function" then
local ok, secret = pcall(_G.issecretvalue, value)
if ok then return secret == true end
end
return false
end
local function readableString(value)
return not isSecret(value) and type(value) == "string" and value or nil
end
local function readableValue(value)
return not isSecret(value) and value or nil
end
local function safeGlobal(name, ...)
local fn = _G[name]
if type(fn) ~= "function" then
return false, nil
end
local ok, a, b, c, d, e, f, g, h, i = pcall(fn, ...)
if not ok then
return false, nil
end
return true, a, b, c, d, e, f, g, h, i
end
local function setTextColor(fontString, color, alpha)
if fontString and fontString.SetTextColor then
fontString._apsThemeColor = color
fontString._apsThemeAlpha = alpha
if not fontString._apsThemeTracked then
fontString._apsThemeTracked = true
table.insert(THEME_FONTS, fontString)
end
fontString:SetTextColor(copyColor(color, alpha))
end
end
local function setBackdrop(frame, background, border, alpha, edgeSize)
if not frame then
return
end
if frame.SetBackdrop then
frame._apsThemeBackdrop = {background = background or COLOR.card, border = border or COLOR.border, alpha = alpha, edgeSize = edgeSize}
if not frame._apsThemeTracked then
frame._apsThemeTracked = true
table.insert(THEME_BACKDROPS, frame)
end
frame:SetBackdrop({
bgFile = WHITE,
edgeFile = WHITE,
tile = true,
tileSize = 8,
edgeSize = edgeSize or 1,
insets = {left = 1, right = 1, top = 1, bottom = 1},
})
frame:SetBackdropColor(copyColor(background or COLOR.card, alpha))
frame:SetBackdropBorderColor(copyColor(border or COLOR.border, alpha or 1))
return
end
if not frame._apsBackground then
frame._apsBackground = frame:CreateTexture(nil, "BACKGROUND")
frame._apsBackground:SetAllPoints(frame)
end
setThemeTextureColor(frame._apsBackground, background or COLOR.card, alpha)
end
function UI:ApplyTheme()
local dark = type(APS.GetSetting) == "function" and APS:GetSetting("darkMode", false) == true
local palette = dark and DARK_COLOR or LIGHT_COLOR
for name, source in pairs(palette) do
local target = COLOR[name]
if target then
target[1], target[2], target[3], target[4] = source[1], source[2], source[3], source[4]
end
end
for _, frame in ipairs(THEME_BACKDROPS) do
local style = frame and frame._apsThemeBackdrop
if style and frame.SetBackdropColor then
frame:SetBackdropColor(copyColor(style.background, style.alpha))
if frame.SetBackdropBorderColor then frame:SetBackdropBorderColor(copyColor(style.border, style.alpha or 1)) end
end
end
for _, fontString in ipairs(THEME_FONTS) do
if fontString and fontString.SetTextColor and fontString._apsThemeColor then
fontString:SetTextColor(copyColor(fontString._apsThemeColor, fontString._apsThemeAlpha))
end
end
for _, editBox in ipairs(THEME_EDITBOXES) do
if editBox and editBox.SetTextColor and editBox._apsThemeTextColor then
editBox:SetTextColor(copyColor(editBox._apsThemeTextColor, 1))
end
end
for _, texture in ipairs(THEME_TEXTURES) do
if texture and texture.SetColorTexture and texture._apsThemeColor then
texture:SetColorTexture(copyColor(texture._apsThemeColor, texture._apsThemeAlpha))
end
end
if not self.frame then return end
if self.frame.SetBackdrop then self.frame:SetBackdrop(nil) end
if self.frame.shell then
applyShellPalette(self.frame.shell, dark)
end
if self.frame.paper then self.frame.paper:SetAlpha(dark and 0.055 or 0.11) end
if self.help and self.help.paper then self.help.paper:SetAlpha(dark and 0.04 or 0.08) end
if self.pages then
self:RefreshAll(true)
for pageID, button in pairs(self.nav or {}) do self:StyleRailButton(button, pageID == self.currentPage) end
end
if self.help and self.help:IsShown() then self:ShowHelpTopic(self.helpTopic or "start") end
end
local function font(parent, template, size, color)
local fs = parent:CreateFontString(nil, "OVERLAY", template or "GameFontNormal")
if fs.SetFont then
local path = _G.STANDARD_TEXT_FONT
if not path and fs.GetFont then path = fs:GetFont() end
if path then fs:SetFont(path, size or 12, "") end
end
setTextColor(fs, color or COLOR.text)
if fs.SetShadowColor then fs:SetShadowColor(0, 0, 0, 0) end
if fs.SetShadowOffset then fs:SetShadowOffset(0, 0) end
return fs
end
local function makeLabel(parent, text, size, color)
local fs = font(parent, "GameFontNormal", size, color)
fs:SetText(text or "")
return fs
end
local function setModelFacing(model, facing)
if not model or type(facing) ~= "number" then return end
model._apsRotation = facing
if model.SetFacing then
pcall(model.SetFacing, model, facing)
elseif model.SetRotation then
pcall(model.SetRotation, model, facing)
end
end
local function setModelSilhouette(model, enabled)
if not model or type(model.SetLight) ~= "function" then return false end
if type(_G.CreateVector3D) ~= "function" or type(_G.CreateColor) ~= "function" then
return false
end
if not enabled then
local nativeLight = {
omnidirectional = false,
point = _G.CreateVector3D(-1, 0, -1),
ambientIntensity = 0.7,
ambientColor = _G.CreateColor(0.7, 0.7, 0.7),
diffuseIntensity = 1,
diffuseColor = _G.CreateColor(0.8, 0.8, 0.8),
}
-- SetLight(false) does not reliably clear the previous custom light
-- on Retail PlayerModel widgets. Install the neutral light explicitly
-- so a real summoned pet can never inherit the empty-state silhouette.
return pcall(model.SetLight, model, true, nativeLight)
end
local dark = type(APS.GetSetting) == "function" and APS:GetSetting("darkMode", false) == true
-- Pure black is clear on parchment. Dark mode uses a single cool-ink tone
-- so the same silhouette remains visible without restoring model color.
local shade = dark and 0.19 or 0
local light = {
omnidirectional = true,
point = _G.CreateVector3D(0, 0, 0),
ambientIntensity = 1,
ambientColor = _G.CreateColor(shade, shade, shade + (dark and 0.035 or 0)),
diffuseIntensity = 0,
diffuseColor = _G.CreateColor(0, 0, 0),
}
return pcall(model.SetLight, model, true, light)
end
local function resetModelRotation(model, automaticSpeed)
if not model then return end
model._apsAutoRotationSpeed = automaticSpeed
setModelFacing(model, DEFAULT_MODEL_FACING)
end
local function enableModelRotation(model)
if not model then return end
model:EnableMouse(true)
if model.EnableMouseMotion then pcall(model.EnableMouseMotion, model, true) end
model:RegisterForDrag("LeftButton")
model._apsRotation = 0
local function stopRotation(self)
self._apsRotating = nil
self._apsLastCursorX = nil
end
local function updateRotation(self, elapsed)
if not self._apsRotating then
local speed = self._apsAutoRotationSpeed
if type(speed) == "number" and speed ~= 0 then
setModelFacing(self, (self._apsRotation or DEFAULT_MODEL_FACING) - speed * (elapsed or 0))
end
return
end
if type(_G.IsMouseButtonDown) == "function" then
local downOK, isDown = pcall(_G.IsMouseButtonDown, "LeftButton")
if downOK and not isSecret(isDown) and isDown == false then
stopRotation(self)
return
end
end
local cursorOK, cursorX = safeGlobal("GetCursorPosition")
if not cursorOK or isSecret(cursorX) or type(cursorX) ~= "number" then
stopRotation(self)
return
end
local previous = self._apsLastCursorX or cursorX
local delta = cursorX - previous
self._apsLastCursorX = cursorX
if delta == 0 then return end
local facing = self._apsRotation or 0
if self.GetFacing then
local facingOK, current = pcall(self.GetFacing, self)
if facingOK and not isSecret(current) and type(current) == "number" then facing = current end
end
facing = facing + delta * 0.01
setModelFacing(self, facing)
end
model:SetScript("OnUpdate", updateRotation)
model:SetScript("OnModelLoaded", function(self)
local callback = self._apsModelLoadedCallback
if callback then callback(self) end
end)
model:SetScript("OnMouseDown", function(self, button)
if button ~= "LeftButton" then return end
stopRotation(self)
local cursorOK, cursorX = safeGlobal("GetCursorPosition")
if not cursorOK or isSecret(cursorX) or type(cursorX) ~= "number" then return end
self._apsLastCursorX = cursorX
self._apsRotating = true
end)
model:SetScript("OnMouseUp", stopRotation)
model:SetScript("OnDragStop", stopRotation)
model:SetScript("OnLeave", stopRotation)
model:SetScript("OnHide", stopRotation)
end
local function setPetModel(model, info, fitDistance, automaticSpeed, onLoaded, onFailed)
if not model or type(info) ~= "table" then return false end
local displayID = not isSecret(info.displayID) and tonumber(info.displayID) or nil
local creatureID = not isSecret(info.creatureID) and tonumber(info.creatureID) or nil
if not displayID and not creatureID then return false end
model._apsModelRequestToken = (model._apsModelRequestToken or 0) + 1
local token = model._apsModelRequestToken
model._apsModelLoaded = false
model._apsModelLoadedCallback = function(self)
if self._apsModelRequestToken ~= token then return end
self._apsModelLoaded = true
if self.SetCamDistanceScale and fitDistance then
pcall(self.SetCamDistanceScale, self, fitDistance)
end
resetModelRotation(self, automaticSpeed)
if onLoaded then onLoaded(self) end
end
local function tryMethod(methodName, value)
local method = model[methodName]
if type(method) ~= "function" or type(value) ~= "number" then return false end
local ok = pcall(method, model, value)
return ok
end
if model.ClearModel then pcall(model.ClearModel, model) end
model:Show()
resetModelRotation(model, automaticSpeed)
local attempted = tryMethod("SetDisplayInfo", displayID)
if not attempted then attempted = tryMethod("SetCreature", creatureID) end
if not attempted then
model._apsModelLoadedCallback = nil
if onFailed then onFailed(model) end
return false
end
-- A successful Lua call only means the request was accepted. Some battle
-- pet displays do not produce a model on the first request. Retry through
-- their creature ID, then the exact display ID, for every pet rather than
-- maintaining a list of exceptions.
if C_Timer and C_Timer.After then
C_Timer.After(0.35, function()
if model._apsModelRequestToken ~= token or model._apsModelLoaded then return end
if creatureID and model.SetCreature then
if model.ClearModel then pcall(model.ClearModel, model) end
tryMethod("SetCreature", creatureID)
elseif displayID then
tryMethod("SetDisplayInfo", displayID)
end
end)
C_Timer.After(1.25, function()
if model._apsModelRequestToken ~= token or model._apsModelLoaded then return end
if displayID and model.SetDisplayInfo then
if model.ClearModel then pcall(model.ClearModel, model) end
tryMethod("SetDisplayInfo", displayID)
end
end)
C_Timer.After(3, function()
if model._apsModelRequestToken ~= token or model._apsModelLoaded then return end
if onFailed then onFailed(model) end
end)
end
return true
end
local function setButtonFont(button, size, color)
local fs = button._apsText or (button.GetFontString and button:GetFontString())
if fs then
if size and fs.SetFont then
local path = _G.STANDARD_TEXT_FONT
if not path and fs.GetFont then path = fs:GetFont() end
if path then fs:SetFont(path, size, "") end
end
setTextColor(fs, color or COLOR.text)
if fs.SetShadowColor then fs:SetShadowColor(0, 0, 0, 0) end
if fs.SetShadowOffset then fs:SetShadowOffset(0, 0) end
end
end
local function makeButton(parent, text, width, height, tone)
local button = CreateFrame("Button", nil, parent, "BackdropTemplate")
button:SetSize(width or 100, height or 30)
setBackdrop(button, (tone and tone[1]) or COLOR.card, (tone and tone[2]) or COLOR.borderSoft, 1, 1)
local label = font(button, "GameFontNormal", 11, (tone and tone[3]) or COLOR.text)
label:SetPoint("LEFT", 7, 0)
label:SetPoint("RIGHT", -7, 0)
label:SetJustifyH("CENTER")
label:SetWordWrap(false)
if label.SetMaxLines then label:SetMaxLines(1) end
button._apsText = label
if button.SetFontString then button:SetFontString(label) end
button:SetText(text or "")
setButtonFont(button, 11, (tone and tone[3]) or COLOR.text)
button:SetScript("OnEnter", function(self)
if self:IsEnabled() then
self:SetBackdropColor(copyColor(COLOR.hoverSurface, 1))
self:SetBackdropBorderColor(copyColor(COLOR.hoverBorder, 0.72))
end
end)
button:SetScript("OnLeave", function(self)
if self._apsSelected then
self:SetBackdropColor(copyColor(COLOR.lilacSoft, 1))
self:SetBackdropBorderColor(copyColor(COLOR.lilac, 1))
else
setBackdrop(self, (tone and tone[1]) or COLOR.card, (tone and tone[2]) or COLOR.borderSoft, 1, 1)
end
end)
button:SetScript("OnDisable", function(self)
self:SetAlpha(0.45)
end)
button:SetScript("OnEnable", function(self)
self:SetAlpha(1)
end)
return button
end
local function addButtonIcon(button, texture, size)
if not button then return end
local icon = button:CreateTexture(nil, "ARTWORK", nil, 1)
icon:SetSize(size or 16, size or 16)
icon:SetPoint("LEFT", 9, 0)
icon:SetTexture(texture)
button._apsAdornmentIcon = icon
if button._apsText then
button._apsText:ClearAllPoints()
button._apsText:SetPoint("LEFT", icon, "RIGHT", 6, 0)
button._apsText:SetPoint("RIGHT", -8, 0)
end
return icon
end
local function addBottomAccent(button, color)
if not button then return end
local accent = button:CreateTexture(nil, "ARTWORK", nil, 2)
accent:SetPoint("BOTTOMLEFT", 1, 1)
accent:SetPoint("BOTTOMRIGHT", -1, 1)
accent:SetHeight(3)
setThemeTextureColor(accent, color or COLOR.lilac, 1)
button._apsBottomAccent = accent
button._apsAccentColor = color or COLOR.lilac
return accent
end
function UI:StyleRailButton(button, selected)
if not button then return end
button._apsSelected = selected and true or false
local accent = button._apsMarkerColor or COLOR.lilac
if button._apsSelected then
button:SetBackdropColor(copyColor(COLOR.lilacSoft, 1))
button:SetBackdropBorderColor(copyColor(COLOR.lilacSoft, 1))
if button._apsLabel then setTextColor(button._apsLabel, COLOR.text) end
if button._apsMarker then setThemeTextureColor(button._apsMarker, accent, 1) end
if button._apsIcon then button._apsIcon:SetAlpha(1) end
else
button:SetBackdropColor(copyColor(COLOR.rail, 1))
button:SetBackdropBorderColor(copyColor(COLOR.rail, 1))
if button._apsLabel then setTextColor(button._apsLabel, COLOR.muted) end
if button._apsMarker then setThemeTextureColor(button._apsMarker, COLOR.faint, 0.55) end
if button._apsIcon then button._apsIcon:SetAlpha(0.72) end
end
end
function UI:StyleFilterButton(button, selected)
if not button then return end
button._apsSelected = selected and true or false
local accent = button._apsAccentColor or COLOR.lilac
if selected then
button:SetBackdropColor(copyColor(COLOR.lilacSoft, 1))
button:SetBackdropBorderColor(copyColor(COLOR.lilac, 1))
setButtonFont(button, nil, COLOR.text)
if button._apsAdornmentIcon then button._apsAdornmentIcon:SetAlpha(1) end
if button._apsBottomAccent then setThemeTextureColor(button._apsBottomAccent, accent, 1) end
else
button:SetBackdropColor(copyColor(COLOR.cardAlt, 0.55))
button:SetBackdropBorderColor(copyColor(COLOR.borderSoft, 0.85))
setButtonFont(button, nil, COLOR.muted)
if button._apsAdornmentIcon then button._apsAdornmentIcon:SetAlpha(0.68) end
if button._apsBottomAccent then setThemeTextureColor(button._apsBottomAccent, accent, 0.45) end
end
end
local function styleEditBox(editBox)
if not editBox then return end
local fontObject = _G.GameFontHighlight or _G.GameFontNormal
if fontObject and editBox.SetFontObject then editBox:SetFontObject(fontObject) end
if editBox.SetFont then
local path = _G.STANDARD_TEXT_FONT
if not path and fontObject and fontObject.GetFont then path = fontObject:GetFont() end
if path then editBox:SetFont(path, 12, "") end
end
editBox._apsThemeTextColor = COLOR.text
if not editBox._apsThemeTextTracked then
editBox._apsThemeTextTracked = true
table.insert(THEME_EDITBOXES, editBox)
end
if editBox.SetTextColor then editBox:SetTextColor(copyColor(editBox._apsThemeTextColor, 1)) end
if editBox.SetShadowColor then editBox:SetShadowColor(0, 0, 0, 0) end
end
local function setButtonSelected(button, selected, accent)
button._apsSelected = selected and true or false
accent = accent or COLOR.lilac
if selected then
button:SetBackdropColor(copyColor(COLOR.lilacSoft, 1))
button:SetBackdropBorderColor(copyColor(accent, 1))
setButtonFont(button, nil, COLOR.text)
else
button:SetBackdropColor(copyColor(COLOR.card, 1))
button:SetBackdropBorderColor(copyColor(COLOR.borderSoft, 1))
setButtonFont(button, nil, COLOR.muted)
end
end
local function makeDivider(parent, x, y, width, color)
local divider = parent:CreateTexture(nil, "ARTWORK")
setThemeTextureColor(divider, color or COLOR.borderSoft, 1)
divider:SetPoint("TOPLEFT", x, y)
divider:SetSize(width, 1)
return divider
end
local function trim(value)
value = tostring(value or "")
return (value:gsub("^%s+", ""):gsub("%s+$", ""))
end
local function lower(value)
return string.lower(tostring(value or ""))
end
local function tableContains(list, value)
if type(list) ~= "table" then
return false
end
for _, item in ipairs(list) do
if tostring(item) == tostring(value) then
return true
end
end
return false
end
local function tableCopy(list)
local result = {}
if type(list) == "table" then
for index, value in ipairs(list) do
result[index] = value
end
end
return result
end
local function petSelectionList(selection)
if type(selection) ~= "table" then return {} end
if #selection > 0 then return tableCopy(selection) end
local result = {}
for petID, selected in pairs(selection) do
if selected == true and (type(petID) == "string" or type(petID) == "number") then
result[#result + 1] = petID
end
end
table.sort(result, function(a, b) return tostring(a) < tostring(b) end)
return result
end
function UI:EnsureDB()
if type(APS.GetDB) == "function" then
local ok, runtimeDB = pcall(APS.GetDB, APS)
if ok and type(runtimeDB) == "table" then
if type(runtimeDB.ui) ~= "table" then runtimeDB.ui = {} end
return runtimeDB
end
end
if type(_G.PETALDB) ~= "table" then
_G.PETALDB = {}
end
local db = _G.PETALDB
if db.enabled == nil then db.enabled = true end
if db.intervalMinutes == nil then db.intervalMinutes = 5 end
if db.dismissGraceMinutes == nil then db.dismissGraceMinutes = 10 end
if type(db.selectedPets) ~= "table" then db.selectedPets = {} end
if type(db.rules) ~= "table" then db.rules = {} end
if type(db.ui) ~= "table" then db.ui = {} end
return db
end
function UI:ReadRules()
local ok, rules = safeCall(APS, "GetRules")
if ok and type(rules) == "table" and #rules > 0 then
return rules
end
if ok and type(rules) == "table" then
local mapped = {}
for key, value in pairs(rules) do
if type(value) == "table" then
local copy = {}
for field, fieldValue in pairs(value) do copy[field] = fieldValue end
if not copy.id and not copy.key then copy.id = key end
table.insert(mapped, copy)
end
end
if #mapped > 0 then return mapped end
end
local db = self:EnsureDB()
local source = db.rules
local result = {}
local legacyPets = self:SelectedPetIDs(nil)
for index, fallback in ipairs(DEFAULT_RULES) do
local stored = source[fallback.id]
if type(stored) ~= "table" and type(source[index]) == "table" then
stored = source[index]
end
local rule = {
id = fallback.id,
key = fallback.id,
label = fallback.label,
title = fallback.label,
description = fallback.description,
accent = fallback.accent,
enabled = stored and stored.enabled ~= false or (not stored and fallback.id == "everywhere"),
petIDs = stored and (stored.petIDs or stored.pets or stored.selectedPets) or tableCopy(legacyPets),
mode = stored and (stored.mode or stored.selectionMode) or "selected",
}
result[index] = rule
end
return result
end
function UI:RuleID(rule)
if type(rule) ~= "table" then return nil end
return rule.id or rule.key or rule.context or rule.name
end
function UI:RuleTitle(rule)
if type(rule) ~= "table" then return "Rule" end
local preset = SIMPLE_RULE_COPY[self:RuleID(rule)]
if preset then return preset.label end
return rule.title or rule.label or rule.name or self:RuleID(rule) or "Rule"
end
function UI:RuleDescription(rule)
if type(rule) ~= "table" then return "" end
local preset = SIMPLE_RULE_COPY[self:RuleID(rule)]
if preset then return preset.description end
return rule.description or rule.subtitle or rule.summary or "Uses your selected pets."
end
function UI:RuleAccent(rule)
local accent = type(rule) == "table" and rule.accent
if type(accent) == "table" then return accent end
return COLOR[accent] or COLOR.sky
end
function UI:RulePetIDs(rule)
if type(rule) ~= "table" then return {} end
local ids = rule.petIDs or rule.pets or rule.selectedPets or rule.petPool or rule.pool
if type(ids) == "table" then
ids = ids.petIDs or ids.petIds or ids.pets or ids.selectedPets or ids
end
if type(ids) == "table" and #ids == 0 and type(rule.pool) == "table" then
local poolIDs = rule.pool.petIDs or rule.pool.petIds or rule.pool.pets or rule.pool.selectedPets
if type(poolIDs) == "table" then ids = poolIDs end
end
if type(ids) ~= "table" then return {} end
if #ids > 0 then return tableCopy(ids) end
local result = {}
for petID, enabled in pairs(ids) do
if enabled then table.insert(result, petID) end
end
return result
end
function UI:RuleMode(rule)
if type(rule) ~= "table" then return "selected" end
local mode = rule.mode or rule.selectionMode
if not mode and type(rule.pool) == "table" then
mode = rule.pool.mode or rule.pool.randomMode
if rule.pool.random == true and mode == "selected" then mode = "shuffle" end
end
mode = type(mode) == "string" and string.lower(mode) or "selected"
if mode == "random" or mode == "selectedrandom" then mode = "shuffle" end
if mode == "fullrandom" or mode == "full_random" or mode == "all" then mode = "full" end
if mode == "favorites" then mode = "favorite" end
return mode
end
function UI:RulesSignature(rules)
local parts = {}
for _, rule in ipairs(rules or {}) do
local ids = self:RulePetIDs(rule)
table.sort(ids, function(a, b) return tostring(a) < tostring(b) end)
local switches = rule.switchOnMomentChange == true
if type(rule.behavior) == "table" then switches = switches or rule.behavior.switchOnMomentChange == true end
table.insert(parts, tostring(self:RuleID(rule)) .. ":" .. (self:IsRuleEnabled(rule) and "1" or "0") .. ":" .. self:RuleMode(rule) .. ":" .. (switches and "1" or "0") .. ":" .. table.concat(ids, ","))
end
return table.concat(parts, "|")
end
function UI:IsRuleEnabled(rule)
return type(rule) == "table" and rule.enabled ~= false
end
function UI:PersistRuleField(rule, field, value)
local id = self:RuleID(rule)
if not id then return end
local method
if field == "enabled" then
method = "SetRuleEnabled"
elseif field == "petIDs" or field == "pets" then
method = "SetRulePets"
elseif field == "mode" then
method = "SetRuleMode"
elseif field == "switchOnMomentChange" then
method = "SetRuleSwitchOnMomentChange"
end
local handled = false
if method and type(APS[method]) == "function" then
local ok = pcall(APS[method], APS, id, value)
handled = ok
if not ok then
handled = pcall(APS[method], id, value)
end
end
if not handled then
local db = self:EnsureDB()
local target
for _, candidate in ipairs(db.rules or {}) do
if self:RuleID(candidate) == id then target = candidate break end
end
if not target and type(id) == "number" then target = db.rules[id] end
if not target then
db.rules = db.rules or {}
target = {id = id, name = id, enabled = false, order = #db.rules + 1}
db.rules[#db.rules + 1] = target
end
target[field] = type(value) == "table" and tableCopy(value) or value
end
if field == "enabled" then rule.enabled = value
elseif field == "petIDs" then rule.petIDs = tableCopy(value)
elseif field == "mode" then rule.mode = value
elseif field == "switchOnMomentChange" then
rule.switchOnMomentChange = value == true
rule.behavior = type(rule.behavior) == "table" and rule.behavior or {}
rule.behavior.switchOnMomentChange = value == true
end
if type(APS.RefreshRules) == "function" then
pcall(APS.RefreshRules, APS)
end
end
function UI:SelectedPetIDs(rule)
if rule then
return self:RulePetIDs(rule)
end
local rosterOK, roster = safeCall(APS, "GetSelectedRoster")
if rosterOK and type(roster) == "table" then
return petSelectionList(roster)
end
local apiOK, apiIDs = safeCall(APS, "GetSelectedPetIDs")
if apiOK and type(apiIDs) == "table" then
return tableCopy(apiIDs)
end
local db = self:EnsureDB()
local result = {}
for petID, enabled in pairs(db.selectedPets) do
if enabled then table.insert(result, petID) end
end
return result
end
function UI:IsPetSelected(petID, rule)
return tableContains(self:SelectedPetIDs(rule), petID)
end
function UI:SetPetSelected(petID, selected, rule)
if not petID then return end
local current = self:SelectedPetIDs(rule)
local nextList = {}
for _, id in ipairs(current) do
if tostring(id) ~= tostring(petID) then table.insert(nextList, id) end
end
if selected then table.insert(nextList, petID) end
if rule then
self:PersistRuleField(rule, "petIDs", nextList)
return
end
if type(APS.SetPetSelected) == "function" then
local ok = pcall(APS.SetPetSelected, APS, petID, selected and true or false)
if ok then return end
end
local db = self:EnsureDB()
db.selectedPets[petID] = selected and true or nil
if type(APS.SetSelectedPets) == "function" then
pcall(APS.SetSelectedPets, APS, db.selectedPets)
end
end
function UI:PetIDsForInfo(info)
if not info then return {} end
if type(info.petIDs) == "table" and #info.petIDs > 0 then return info.petIDs end
return info.petID and {info.petID} or {}
end
function UI:IsPetInfoSelected(info)
for _, petID in ipairs(self:PetIDsForInfo(info)) do
if self:IsPetSelected(petID, nil) then return true end
end
return false
end
function UI:SetPetInfoSelected(info, selected)
if not info then return end
if selected then
self:SetPetSelected(info.petID, true, nil)
return
end
-- A species may still have several GUIDs selected from an older build.
-- One click on its single visible card removes every copy and its tags.
for _, petID in ipairs(self:PetIDsForInfo(info)) do
if self:IsPetSelected(petID, nil) then self:SetPetSelected(petID, false, nil) end
end
end
function UI:SelectedPetSpeciesCount()
local count = 0
for _, info in ipairs(self.petCache or {}) do
if self:IsPetInfoSelected(info) then count = count + 1 end
end
return count
end
function UI:GetPetInfoTags(info)
local result = {}
for _, petID in ipairs(self:PetIDsForInfo(info)) do
local tags = self:GetPetTags(petID)
for _, definition in ipairs(TAG_DEFINITIONS) do
if tags[definition.key] == true then result[definition.key] = true end
end
end
return result
end
function UI:SetPetInfoTag(info, tagKey, enabled)
if not info then return false end
if enabled then return self:SetPetTag(info.petID, tagKey, true) end
local handled = false
for _, petID in ipairs(self:PetIDsForInfo(info)) do
if self:PetHasTag(petID, tagKey) then
handled = self:SetPetTag(petID, tagKey, false) or handled
end
end
if not handled then handled = self:SetPetTag(info.petID, tagKey, false) end