-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPVPTabTarget.lua
More file actions
1007 lines (862 loc) · 28.4 KB
/
PVPTabTarget.lua
File metadata and controls
1007 lines (862 loc) · 28.4 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
-- ============================================================================
-- PVPTabTarget - Smart TAB Targeting for WoW
-- Refactored with AceAddon-3.0, AceDB-3.0, AceEvent-3.0, AceConsole-3.0,
-- AceTimer-3.0, AceLocale-3.0 & AceDBOptions-3.0
-- ============================================================================
local AddonName, ns = ...
-- Locale
local L = LibStub("AceLocale-3.0"):GetLocale("PVPTabTarget")
-- Create addon with Ace mixins
local PVPTabTarget = LibStub("AceAddon-3.0"):NewAddon("PVPTabTarget",
"AceConsole-3.0",
"AceEvent-3.0",
"AceTimer-3.0"
)
ns.Addon = PVPTabTarget
-- ============================================================================
-- LOCALIZED WOW API (Performance)
-- ============================================================================
local issecurevariable = issecurevariable
local InCombatLockdown = InCombatLockdown
local IsInInstance = IsInInstance
local GetCurrentBindingSet = GetCurrentBindingSet
local GetZonePVPInfo = C_PvP.GetZonePVPInfo
local GetBindingKey = GetBindingKey
local GetBindingAction = GetBindingAction
local SetBinding = SetBinding
local SaveBindings = SaveBindings
local ipairs = ipairs
local pairs = pairs
-- ============================================================================
-- CONSTANTS
-- ============================================================================
-- Targeting Modes
local MODE_PLAYERS_ONLY = "PLAYERS"
local MODE_ALL_ENEMIES = "ALL"
-- Zone Types
local ZONE_PVP = "PVP"
local ZONE_PVE = "PVE"
local ZONE_WORLD = "WORLD"
-- Binding Commands
local ACTION_TARGET_NEXT_ALL = "TARGETNEARESTENEMY"
local ACTION_TARGET_NEXT_PLAYERS = "TARGETNEARESTENEMYPLAYER"
local ACTION_TARGET_PREVIOUS_ALL = "TARGETPREVIOUSENEMY"
local ACTION_TARGET_PREVIOUS_PLAYERS = "TARGETPREVIOUSENEMYPLAYER"
-- UI Color Codes
local COLORS = {
ADDON = "|cFF74D06C",
SUCCESS = "|cFF74D06C",
WARNING = "|cFFFFD700",
DANGER = "|cFFFF6B6B",
INFO = "|cFF69B4FF",
MUTED = "|cFF888888",
WHITE = "|cFFFFFFFF",
}
-- Friendly Display Names (using locale)
local ZONE_NAMES = {
[ZONE_PVP] = function() return L["zone_pvp"] end,
[ZONE_PVE] = function() return L["zone_pve"] end,
[ZONE_WORLD] = function() return L["zone_world"] end,
}
local MODE_NAMES = {
[MODE_PLAYERS_ONLY] = function() return L["mode_players_only"] end,
[MODE_ALL_ENEMIES] = function() return L["mode_all_enemies"] end,
}
-- Icon Choices for Players Only mode (PvP themed)
local PLAYERS_ONLY_ICONS = {
["pvp_1"] = "Interface\\Icons\\Achievement_Arena_2v2_7",
["pvp_2"] = "Interface\\Icons\\Achievement_Arena_3v3_7",
["pvp_3"] = "Interface\\Icons\\Achievement_PVP_H_01",
["pvp_4"] = "Interface\\Icons\\Achievement_PVP_A_01",
["pvp_5"] = "Interface\\Icons\\Ability_Rogue_Shadowstrike",
["pvp_6"] = "Interface\\Icons\\Achievement_BG_KillXEnemies_GeneralsRoom",
["pvp_7"] = "Interface\\Icons\\INV_Misc_Head_Human_01",
}
local ALL_ENEMIES_ICONS = {
["pve_1"] = "Interface\\Icons\\INV_Misc_Head_Dragon_01",
["pve_2"] = "Interface\\Icons\\INV_Misc_Head_Dragon_Black",
["pve_3"] = "Interface\\Icons\\Ability_Hunter_SniperShot",
["pve_4"] = "Interface\\Icons\\Achievement_PVP_H_01",
["pve_5"] = "Interface\\Icons\\Achievement_PVP_A_01",
}
-- ============================================================================
-- ICON HELPERS
-- ============================================================================
local function GetPlayersOnlyIconValues()
local values = {}
for key, path in pairs(PLAYERS_ONLY_ICONS) do
values[key] = "|T" .. path .. ":16:16|t Icon " .. key:match("%d+")
end
return values
end
local function GetAllEnemiesIconValues()
local values = {}
for key, path in pairs(ALL_ENEMIES_ICONS) do
values[key] = "|T" .. path .. ":16:16|t Icon " .. key:match("%d+")
end
return values
end
local function GetModeIcon(mode)
if mode == MODE_PLAYERS_ONLY then
local iconKey = PVPTabTarget.db and PVPTabTarget.db.profile.PlayersOnlyIcon or "pvp_1"
return PLAYERS_ONLY_ICONS[iconKey] or PLAYERS_ONLY_ICONS["pvp_1"]
else
local iconKey = PVPTabTarget.db and PVPTabTarget.db.profile.AllEnemiesIcon or "pve_1"
return ALL_ENEMIES_ICONS[iconKey] or ALL_ENEMIES_ICONS["pve_1"]
end
end
-- ============================================================================
-- ACEDB DEFAULTS
-- ============================================================================
local defaults = {
profile = {
-- Behavior
SilentMode = false,
ShowMinimap = true,
-- Targeting modes per zone
PVPMode = MODE_PLAYERS_ONLY,
PVEMode = MODE_ALL_ENEMIES,
WorldMode = MODE_ALL_ENEMIES,
-- Icons for each mode
PlayersOnlyIcon = "pvp_1",
AllEnemiesIcon = "pve_1",
-- Custom keybinds
TargetKeyDisabled = false,
PreviousTargetKeyDisabled = false,
-- LibDBIcon minimap position (nested table expected by LibDBIcon)
minimap = {
hide = false,
minimapPos = 225,
},
},
}
-- ============================================================================
-- ADDON STATE (non-persistent)
-- ============================================================================
PVPTabTarget.BindingFailed = false
PVPTabTarget.PendingApply = false
PVPTabTarget.CurrentZoneType = ZONE_WORLD
PVPTabTarget.CurrentTargetMode = MODE_ALL_ENEMIES
PVPTabTarget.TemporaryOverride = false
PVPTabTarget.TemporaryMode = nil
-- ============================================================================
-- DISPLAY HELPERS
-- ============================================================================
local function FormatMode(mode, includeIcon)
local icon = includeIcon and ("|T" .. GetModeIcon(mode) .. ":14:14|t ") or ""
local name = MODE_NAMES[mode] and MODE_NAMES[mode]() or mode
if mode == MODE_PLAYERS_ONLY then
return icon .. COLORS.DANGER .. name .. "|r"
end
return icon .. COLORS.SUCCESS .. name .. "|r"
end
local function GetModeDropdownValues()
return {
[MODE_PLAYERS_ONLY] = "|T" .. GetModeIcon(MODE_PLAYERS_ONLY) .. ":14:14|t " ..
COLORS.DANGER .. L["mode_players_only"] .. "|r",
[MODE_ALL_ENEMIES] = "|T" .. GetModeIcon(MODE_ALL_ENEMIES) .. ":14:14|t " ..
COLORS.SUCCESS .. L["mode_all_enemies"] .. "|r",
}
end
local function FormatZone(zoneType)
local zoneName = ZONE_NAMES[zoneType] and ZONE_NAMES[zoneType]() or zoneType
if zoneType == ZONE_PVP then
return COLORS.DANGER .. zoneName .. "|r"
elseif zoneType == ZONE_PVE then
return COLORS.WARNING .. zoneName .. "|r"
end
return COLORS.SUCCESS .. zoneName .. "|r"
end
local function GetStatusText()
local zoneText = FormatZone(PVPTabTarget.CurrentZoneType)
local modeText = FormatMode(PVPTabTarget.CurrentTargetMode, true)
local status = L["current_zone"] .. ": " .. zoneText
if PVPTabTarget.TemporaryOverride then
status = status .. " " .. COLORS.WARNING .. "[" .. L["temporary_override"] .. "]|r"
end
status = status .. "\n" .. L["targeting"] .. ": " .. modeText
if PVPTabTarget.PendingApply then
status = status .. "\n" .. COLORS.WARNING .. L["pending_combat"] .. "|r"
end
return status
end
-- ============================================================================
-- ZONE DETECTION
-- ============================================================================
local function GetZoneTypeString()
local pvpType = GetZonePVPInfo()
local _, instanceType = IsInInstance()
if instanceType == "arena" or instanceType == "pvp" or pvpType == "combat" then
return ZONE_PVP
end
if instanceType == "party" or instanceType == "raid" or instanceType == "scenario" then
return ZONE_PVE
end
return ZONE_WORLD
end
local function GetModeForZone(zoneType)
if PVPTabTarget.TemporaryOverride and PVPTabTarget.TemporaryMode then
return PVPTabTarget.TemporaryMode
end
local p = PVPTabTarget.db.profile
if zoneType == ZONE_PVP then
return p.PVPMode or MODE_PLAYERS_ONLY
elseif zoneType == ZONE_PVE then
return p.PVEMode or MODE_ALL_ENEMIES
else
return p.WorldMode or MODE_ALL_ENEMIES
end
end
-- ============================================================================
-- BINDING HELPERS
-- ============================================================================
local function GetFirstBoundKey(action)
local key1, key2 = GetBindingKey(action)
return key1 or key2
end
local function CollectBoundKeys(primaryAction, secondaryAction)
local keys = {}
local seen = {}
local function AddActionKeys(action)
local key1, key2 = GetBindingKey(action)
if key1 and not seen[key1] then
seen[key1] = true
keys[#keys + 1] = key1
end
if key2 and not seen[key2] then
seen[key2] = true
keys[#keys + 1] = key2
end
end
AddActionKeys(primaryAction)
if secondaryAction then
AddActionKeys(secondaryAction)
end
return keys
end
local function ResolveKeybind(customKey, isDisabled, preferredAction, alternateAction)
if customKey and customKey ~= "" then
return customKey
end
if isDisabled then
return nil
end
local existingKey = GetFirstBoundKey(preferredAction)
if not existingKey and alternateAction then
existingKey = GetFirstBoundKey(alternateAction)
end
if existingKey then
return existingKey
end
return nil
end
local function GetResolvedTargetKey()
local p = PVPTabTarget.db.profile
return ResolveKeybind(
p.TargetKey,
p.TargetKeyDisabled,
ACTION_TARGET_NEXT_ALL,
ACTION_TARGET_NEXT_PLAYERS
)
end
local function GetResolvedPreviousTargetKey()
local p = PVPTabTarget.db.profile
return ResolveKeybind(
p.PreviousTargetKey,
p.PreviousTargetKeyDisabled,
ACTION_TARGET_PREVIOUS_ALL,
ACTION_TARGET_PREVIOUS_PLAYERS
)
end
-- ============================================================================
-- MINIMAP BUTTON
-- ============================================================================
local function UpdateMinimapIcon()
local LDB = LibStub("LibDataBroker-1.1", true)
if not LDB then return end
local dataObj = LDB:GetDataObjectByName("PVPTabTarget")
if not dataObj then return end
dataObj.icon = GetModeIcon(PVPTabTarget.CurrentTargetMode)
local iconLib = LibStub("LibDBIcon-1.0", true)
if iconLib then
local button = iconLib:GetMinimapButton("PVPTabTarget")
if button and button.icon then
button.icon:SetTexture(dataObj.icon)
end
if button and button:IsMouseOver() then
local onLeave = button:GetScript("OnLeave")
local onEnter = button:GetScript("OnEnter")
if onLeave then
onLeave(button)
end
if onEnter then
onEnter(button)
end
end
end
end
local function ToggleTargetMode()
local newMode
if PVPTabTarget.TemporaryOverride then
newMode = (PVPTabTarget.TemporaryMode == MODE_PLAYERS_ONLY)
and MODE_ALL_ENEMIES or MODE_PLAYERS_ONLY
else
PVPTabTarget.TemporaryOverride = true
newMode = (PVPTabTarget.CurrentTargetMode == MODE_PLAYERS_ONLY)
and MODE_ALL_ENEMIES or MODE_PLAYERS_ONLY
end
PVPTabTarget.TemporaryMode = newMode
PVPTabTarget:ApplyBindings(true)
end
function PVPTabTarget:SetupMinimapButton()
local LDB = LibStub("LibDataBroker-1.1", true)
local iconLib = LibStub("LibDBIcon-1.0", true)
if not LDB or not iconLib then return end
local dataObj = LDB:NewDataObject("PVPTabTarget", {
type = "launcher",
text = "PVPTabTarget",
icon = GetModeIcon(self.CurrentTargetMode or MODE_ALL_ENEMIES),
OnClick = function(_, button)
if button == "LeftButton" then
ToggleTargetMode()
elseif button == "RightButton" then
self:OpenSettings()
end
end,
OnTooltipShow = function(tooltip)
tooltip:AddLine(COLORS.ADDON .. "PVPTabTarget|r")
tooltip:AddLine(" ")
tooltip:AddLine(GetStatusText())
tooltip:AddLine(" ")
tooltip:AddDoubleLine(
L["tooltip_next_target"], GetResolvedTargetKey() or NOT_BOUND,
1, 1, 1, 0.7, 0.9, 1
)
tooltip:AddDoubleLine(
L["tooltip_prev_target"], GetResolvedPreviousTargetKey() or NOT_BOUND,
1, 1, 1, 0.7, 0.9, 1
)
tooltip:AddLine(" ")
tooltip:AddLine(COLORS.INFO .. L["minimap_left_click"] .. "|r - " .. L["minimap_toggle_mode"])
tooltip:AddLine(COLORS.INFO .. L["minimap_right_click"] .. "|r - " .. L["minimap_open_settings"])
if self.TemporaryOverride then
tooltip:AddLine(" ")
tooltip:AddLine(COLORS.MUTED .. L["override_resets"] .. "|r")
end
end,
})
iconLib:Register("PVPTabTarget", dataObj, self.db.profile.minimap)
end
-- ============================================================================
-- SETTINGS PANEL
-- ============================================================================
function PVPTabTarget:OpenSettings()
if self.optionsCategory and self.optionsCategory.name then
Settings.OpenToCategory(self.optionsCategory.name)
else
self:Print(COLORS.DANGER .. L["msg_settings_unavailable"] .. "|r")
end
end
function PVPTabTarget:HandleKeybindChange(profileKey, disabledKey, otherResolvedKey, otherLabel, value)
if value ~= "" and value == otherResolvedKey then
self:Print(COLORS.WARNING .. L["msg_key_conflict"]:format(value, otherLabel) .. "|r")
LibStub("AceConfigRegistry-3.0"):NotifyChange("PVPTabTarget")
return
end
if value ~= "" then
self.db.profile[profileKey] = value
self.db.profile[disabledKey] = false
else
self.db.profile[profileKey] = nil
self.db.profile[disabledKey] = true
end
self:ApplyBindings()
end
-- ============================================================================
-- ACE OPTIONS TABLE - Tabbed Layout
-- ============================================================================
function PVPTabTarget:GetOptionsTable()
local options = {
type = "group",
name = COLORS.ADDON .. "PVPTabTarget|r",
childGroups = "tab",
args = {
-- ============================================================
-- STATUS (always visible at top, before tabs)
-- ============================================================
statusHeader = {
type = "header",
name = "",
order = 1,
},
statusDisplay = {
type = "description",
name = function()
return "\n" .. GetStatusText() .. "\n"
end,
fontSize = "medium",
order = 2,
},
-- ============================================================
-- TAB 1: TARGETING
-- ============================================================
targeting = {
type = "group",
name = L["tab_targeting"],
order = 10,
args = {
modeDescription = {
type = "description",
name = L["targeting_desc"] .. "\n\n" ..
COLORS.DANGER .. L["mode_players_only"] .. "|r - " .. L["mode_players_desc"] .. "\n" ..
COLORS.SUCCESS .. L["mode_all_enemies"] .. "|r - " .. L["mode_all_desc"] .. "\n",
fontSize = "medium",
order = 1,
},
spacer1 = { type = "description", name = " ", order = 9 },
PVPMode = {
type = "select",
name = COLORS.DANGER .. L["pvp_zones_label"] .. "|r",
desc = L["pvp_zones_desc"],
values = function() return GetModeDropdownValues() end,
width = "full",
order = 10,
set = function(_, val)
self.db.profile.PVPMode = val
self:ApplyBindings()
end,
get = function() return self.db.profile.PVPMode end,
},
PVEMode = {
type = "select",
name = COLORS.WARNING .. L["pve_zones_label"] .. "|r",
desc = L["pve_zones_desc"],
values = function() return GetModeDropdownValues() end,
width = "full",
order = 11,
set = function(_, val)
self.db.profile.PVEMode = val
self:ApplyBindings()
end,
get = function() return self.db.profile.PVEMode end,
},
WorldMode = {
type = "select",
name = COLORS.SUCCESS .. L["world_zones_label"] .. "|r",
desc = L["world_zones_desc"],
values = function() return GetModeDropdownValues() end,
width = "full",
order = 12,
set = function(_, val)
self.db.profile.WorldMode = val
self:ApplyBindings()
end,
get = function() return self.db.profile.WorldMode end,
},
},
},
-- ============================================================
-- TAB 2: KEYBINDS
-- ============================================================
keybinds = {
type = "group",
name = L["tab_keybinds"],
order = 20,
args = {
keybindDescription = {
type = "description",
name = L["keybind_desc"] .. "\n",
fontSize = "medium",
order = 1,
},
TargetKey = {
type = "keybinding",
name = L["next_enemy"],
desc = L["next_enemy_desc"],
width = "full",
order = 10,
set = function(_, val)
self:HandleKeybindChange(
"TargetKey",
"TargetKeyDisabled",
GetResolvedPreviousTargetKey(),
L["prev_enemy"],
val
)
end,
get = function() return GetResolvedTargetKey() end,
},
PreviousTargetKey = {
type = "keybinding",
name = L["prev_enemy"],
desc = L["prev_enemy_desc"],
width = "full",
order = 11,
set = function(_, val)
self:HandleKeybindChange(
"PreviousTargetKey",
"PreviousTargetKeyDisabled",
GetResolvedTargetKey(),
L["next_enemy"],
val
)
end,
get = function() return GetResolvedPreviousTargetKey() end,
},
spacer1 = { type = "description", name = " ", order = 19 },
},
},
-- ============================================================
-- TAB 3: APPEARANCE
-- ============================================================
appearance = {
type = "group",
name = L["tab_appearance"],
order = 30,
args = {
-- Icons section
iconHeader = {
type = "header",
name = L["header_icons"],
order = 1,
},
iconDescription = {
type = "description",
name = L["icon_desc"] .. "\n",
fontSize = "medium",
order = 2,
},
PlayersOnlyIcon = {
type = "select",
name = COLORS.DANGER .. L["players_icon_label"] .. "|r",
desc = L["players_icon_desc"],
values = function() return GetPlayersOnlyIconValues() end,
width = "full",
order = 10,
set = function(_, val)
self.db.profile.PlayersOnlyIcon = val
UpdateMinimapIcon()
LibStub("AceConfigRegistry-3.0"):NotifyChange("PVPTabTarget")
end,
get = function() return self.db.profile.PlayersOnlyIcon end,
},
AllEnemiesIcon = {
type = "select",
name = COLORS.SUCCESS .. L["all_icon_label"] .. "|r",
desc = L["all_icon_desc"],
values = function() return GetAllEnemiesIconValues() end,
width = "full",
order = 11,
set = function(_, val)
self.db.profile.AllEnemiesIcon = val
UpdateMinimapIcon()
LibStub("AceConfigRegistry-3.0"):NotifyChange("PVPTabTarget")
end,
get = function() return self.db.profile.AllEnemiesIcon end,
},
-- Display section
displayHeader = {
type = "header",
name = L["header_display"],
order = 20,
},
ShowMinimap = {
type = "toggle",
name = L["show_minimap"],
desc = L["show_minimap_desc"],
width = "full",
order = 21,
set = function(_, val)
self.db.profile.ShowMinimap = val
self.db.profile.minimap.hide = not val
local iconLib = LibStub("LibDBIcon-1.0", true)
if iconLib then
if val then
iconLib:Show("PVPTabTarget")
else
iconLib:Hide("PVPTabTarget")
end
end
end,
get = function() return self.db.profile.ShowMinimap end,
},
SilentMode = {
type = "toggle",
name = L["silent_mode"],
desc = L["silent_mode_desc"],
width = "full",
order = 22,
set = function(_, val) self.db.profile.SilentMode = val end,
get = function() return self.db.profile.SilentMode end,
},
-- Help section
helpHeader = {
type = "header",
name = L["header_help"],
order = 30,
},
helpText = {
type = "description",
name = "\n" ..
COLORS.INFO .. L["help_slash"] .. "|r\n" ..
" " .. L["help_slash_desc"] .. "\n\n" ..
COLORS.INFO .. L["help_minimap"] .. "|r\n" ..
" " .. L["help_left_click"] .. "\n" ..
" " .. L["help_right_click"] .. "\n\n" ..
COLORS.MUTED .. L["help_temp_info"] .. "|r\n",
fontSize = "medium",
order = 31,
},
},
},
},
}
return options
end
-- ============================================================================
-- OPTIONS REGISTRATION
-- ============================================================================
function PVPTabTarget:SetupOptions()
local options = self:GetOptionsTable()
-- Inject AceDBOptions profile management as a tab
local profileOptions = LibStub("AceDBOptions-3.0"):GetOptionsTable(self.db)
profileOptions.order = 40
profileOptions.name = L["tab_profiles"]
options.args.profiles = profileOptions
-- Register with AceConfig
LibStub("AceConfigRegistry-3.0"):RegisterOptionsTable("PVPTabTarget", options)
-- Add to Blizzard settings panel
self.optionsCategory = LibStub("AceConfigDialog-3.0"):AddToBlizOptions(
"PVPTabTarget", "PVPTabTarget"
)
end
-- ============================================================================
-- BINDING LOGIC
-- ============================================================================
local function ClearStaleBindings(desiredKey, primaryAction, secondaryAction)
local keys = CollectBoundKeys(primaryAction, secondaryAction)
local changed = false
local success = true
for _, key in ipairs(keys) do
if key ~= desiredKey then
success = SetBinding(key) and success
changed = true
end
end
return success, changed
end
function PVPTabTarget:ApplyBindings(isTemporaryToggle, suppressMessage)
local bindSet = GetCurrentBindingSet()
if bindSet ~= 1 and bindSet ~= 2 then return end
-- Update state even in combat so the tooltip and icon reflect queued changes.
self.CurrentZoneType = GetZoneTypeString()
self.CurrentTargetMode = GetModeForZone(self.CurrentZoneType)
if InCombatLockdown() then
local wasPending = self.PendingApply
self.BindingFailed = true
self.PendingApply = true
UpdateMinimapIcon()
LibStub("AceConfigRegistry-3.0"):NotifyChange("PVPTabTarget")
if not wasPending and not suppressMessage and not self.db.profile.SilentMode then
self:Print(COLORS.WARNING .. L["msg_pending_combat"] .. "|r")
end
return
end
local p = self.db.profile
-- Resolve keybinds
local targetKey = GetResolvedTargetKey()
local previousKey = GetResolvedPreviousTargetKey()
-- Determine actions
local targetAction, previousAction
if self.CurrentTargetMode == MODE_PLAYERS_ONLY then
targetAction = ACTION_TARGET_NEXT_PLAYERS
previousAction = ACTION_TARGET_PREVIOUS_PLAYERS
else
targetAction = ACTION_TARGET_NEXT_ALL
previousAction = ACTION_TARGET_PREVIOUS_ALL
end
-- Reconcile bindings
local success = true
local changed = false
local targetClearSuccess, targetClearChanged = ClearStaleBindings(
targetKey,
ACTION_TARGET_NEXT_ALL,
ACTION_TARGET_NEXT_PLAYERS
)
success = targetClearSuccess and success
changed = targetClearChanged or changed
local previousClearSuccess, previousClearChanged = ClearStaleBindings(
previousKey,
ACTION_TARGET_PREVIOUS_ALL,
ACTION_TARGET_PREVIOUS_PLAYERS
)
success = previousClearSuccess and success
changed = previousClearChanged or changed
if success and targetKey and GetBindingAction(targetKey) ~= targetAction then
success = SetBinding(targetKey, targetAction)
changed = true
end
if success and previousKey and GetBindingAction(previousKey) ~= previousAction then
success = SetBinding(previousKey, previousAction)
changed = true
end
if success then
if changed then
SaveBindings(bindSet)
end
self.BindingFailed = false
self.PendingApply = false
UpdateMinimapIcon()
LibStub("AceConfigRegistry-3.0"):NotifyChange("PVPTabTarget")
if changed and not suppressMessage and not p.SilentMode then
local modeText = FormatMode(self.CurrentTargetMode)
local zoneText = FormatZone(self.CurrentZoneType)
if self.TemporaryOverride then
self:Print(COLORS.WARNING .. L["msg_override"] .. "|r " .. modeText)
else
self:Print(zoneText .. " → " .. modeText)
end
end
else
self.BindingFailed = true
self.PendingApply = false
end
end
function PVPTabTarget:NormalizeBindingSettings()
local p = self.db.profile
local legacyFallbackEnabled = p.UseDefaultKeys ~= false and p.DefaultKey ~= false
if p.TargetKey == "" then
p.TargetKey = nil
end
if p.PreviousTargetKey == "" then
p.PreviousTargetKey = nil
end
-- Older versions stored TAB and SHIFT-TAB as fallback values. Clear those
-- legacy defaults so the addon only tracks explicit or real game bindings.
if legacyFallbackEnabled and p.TargetKey == "TAB" then
p.TargetKey = nil
end
if legacyFallbackEnabled and p.PreviousTargetKey == "SHIFT-TAB" then
p.PreviousTargetKey = nil
end
if p.TargetKey then
p.TargetKeyDisabled = false
end
if p.PreviousTargetKey then
p.PreviousTargetKeyDisabled = false
end
p.UseDefaultKeys = nil
p.DefaultKey = nil
end
function PVPTabTarget:RefreshDisabledBindingFlags()
local p = self.db.profile
if p.TargetKeyDisabled and #CollectBoundKeys(ACTION_TARGET_NEXT_ALL, ACTION_TARGET_NEXT_PLAYERS) > 0 then
p.TargetKeyDisabled = false
end
if p.PreviousTargetKeyDisabled and #CollectBoundKeys(ACTION_TARGET_PREVIOUS_ALL, ACTION_TARGET_PREVIOUS_PLAYERS) > 0 then
p.PreviousTargetKeyDisabled = false
end
end
-- ============================================================================
-- MIGRATION - Import old PVPTabTargetSettings into AceDB
-- ============================================================================
function PVPTabTarget:MigrateOldSettings()
if not PVPTabTargetSettings then return end
local old = PVPTabTargetSettings
local p = self.db.profile
-- Map old keys to new profile keys
local migrations = {
"UseDefaultKeys", "SilentMode", "ShowMinimap",
"PVPMode", "PVEMode", "WorldMode",
"PlayersOnlyIcon", "AllEnemiesIcon",
"TargetKey", "PreviousTargetKey",
}
local migrated = false
for _, key in ipairs(migrations) do
if old[key] ~= nil then
p[key] = old[key]
migrated = true
end
end
-- Handle renamed key
if old.DefaultKey ~= nil then
p.UseDefaultKeys = old.DefaultKey
migrated = true
end
-- Migrate minimap position
if old.minimapPos then
p.minimap.minimapPos = old.minimapPos
migrated = true
end
-- Sync ShowMinimap with minimap.hide
p.minimap.hide = not p.ShowMinimap
if migrated then
-- Clear old saved variable so migration only happens once
PVPTabTargetSettings = nil
self:Print(COLORS.INFO .. "Settings migrated to the new profile system." .. "|r")
end
end
-- ============================================================================
-- PROFILE CHANGE CALLBACK
-- ============================================================================
function PVPTabTarget:OnProfileChanged()
self:NormalizeBindingSettings()
self:ApplyBindings()
local iconLib = LibStub("LibDBIcon-1.0", true)
if iconLib then
if self.db.profile.ShowMinimap then
iconLib:Show("PVPTabTarget")
else
iconLib:Hide("PVPTabTarget")
end
end
UpdateMinimapIcon()
end
-- ============================================================================
-- ACE ADDON LIFECYCLE
-- ============================================================================
function PVPTabTarget:OnInitialize()
-- Create AceDB database
self.db = LibStub("AceDB-3.0"):New("PVPTabTargetDB", defaults, true)
-- Migrate from legacy saved variable
self:MigrateOldSettings()
self:NormalizeBindingSettings()
-- Register AceDB profile callbacks
self.db.RegisterCallback(self, "OnProfileChanged", "OnProfileChanged")
self.db.RegisterCallback(self, "OnProfileCopied", "OnProfileChanged")
self.db.RegisterCallback(self, "OnProfileReset", "OnProfileChanged")
-- Setup GUI and commands
self:SetupOptions()
self:SetupMinimapButton()
-- Register slash commands via AceConsole
self:RegisterChatCommand("pvptab", "OpenSettings")
self:RegisterChatCommand("ptt", "OpenSettings")
end
function PVPTabTarget:OnEnable()
-- Register events via AceEvent - handler methods match event names
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:RegisterEvent("ZONE_CHANGED_NEW_AREA")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("UPDATE_BINDINGS")
end
-- ============================================================================
-- EVENT HANDLERS (AceEvent auto-dispatches to methods matching event names)
-- ============================================================================
function PVPTabTarget:PLAYER_ENTERING_WORLD()
-- Delay initial binding to ensure everything is loaded
self:ScheduleTimer("ApplyBindings", 1)
end
function PVPTabTarget:ZONE_CHANGED_NEW_AREA()
self.TemporaryOverride = false
self.TemporaryMode = nil
self:ApplyBindings()
end
function PVPTabTarget:PLAYER_REGEN_ENABLED()
if self.BindingFailed then
self:ApplyBindings()