-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapPins.lua
More file actions
929 lines (797 loc) · 24.1 KB
/
Copy pathMapPins.lua
File metadata and controls
929 lines (797 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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
local AddonName, MapPins = ...
local HBD = LibStub("HereBeDragons-2.0")
local HBDPins = LibStub("HereBeDragons-Pins-2.0")
local addon = CreateFrame("Frame")
MapPins.addon = addon
local PIN_SIZE = 18
local CONTEXT_MENU_WIDTH = 140
local CONTEXT_MENU_INSET = 6
local CONTEXT_MENU_ROW_HEIGHT = 18
local CONTEXT_MENU_ICON_SIZE = 14
local CONTEXT_MENU_NOTE_HEIGHT = 18
local CONTEXT_MENU_SEPARATOR_HEIGHT = 8
local CONTEXT_MENU_CURSOR_OFFSET_X = 8
local CONTEXT_MENU_CURSOR_OFFSET_Y = -8
local PIN_TOOLTIP_CURSOR_OFFSET_Y = 10
local CONTEXT_SUBMENU_INSET = 6
local CONTEXT_SUBMENU_ICON_SIZE = 18
local CONTEXT_SUBMENU_ICON_SPACING = 4
local CONTEXT_SUBMENU_COLUMNS = 6
local MINIMAP_SHOW_IN_PARENT_ZONE = true
local MINIMAP_FLOAT_ON_EDGE = true
local DEFAULT_PIN_ICON = "green"
local ICON_DEFINITIONS = {
{ key = "green", text = "Green", basic = true },
{ key = "yellow", text = "Yellow", basic = true },
{ key = "red", text = "Red", basic = true },
{ key = "blue", text = "Blue", basic = true },
{ key = "pink", text = "Pink", basic = true },
{ key = "available", text = "Available" },
{ key = "complete", text = "Complete" },
{ key = "eventquest", text = "Event Quest" },
{ key = "eventquest_complete", text = "Event Quest Complete" },
{ key = "pvpquest", text = "PVP Quest" },
{ key = "pvpquest_complete", text = "PVP Quest Complete" },
{ key = "repeatable", text = "Repeatable" },
{ key = "repeatable_complete", text = "Repeatable Complete" },
{ key = "tooltip_available", text = "Tooltip Available" },
{ key = "tooltip_complete", text = "Tooltip Complete" },
{ key = "grave", text = "Grave" },
{ key = "loot", text = "Loot" },
{ key = "moonwell", text = "Moonwell" },
{ key = "object", text = "Object" },
{ key = "reagents", text = "Reagents" },
{ key = "slay", text = "Slay" },
{ key = "sod_rune", text = "Rune" },
{ key = "weaponmaster", text = "Weaponmaster" },
}
local PIN_TEXTURES = {}
local MENU_FRAME_NAME = "MapPinsContextMenu"
local AZEROTH_MAP_ID = 947
local worldPinFramesByID = {}
local minimapPinFramesByID = {}
local menuOwnerPinID
local BASIC_PIN_CONTEXT_MENU_ITEMS = {}
local ADDITIONAL_PIN_CONTEXT_MENU_ITEMS = {}
local PIN_CONTEXT_MENU_ITEMS = {
{
text = "Change icon",
isTitle = true,
},
{
text = "More",
hasSubmenu = true,
},
{
isSeparator = true,
},
{
label = "Note",
isNoteInput = true,
},
{
isSeparator = true,
},
{
text = "Remove pin",
func = function(pinID)
addon:RemovePin(pinID)
end,
},
}
local function CreateIconMenuItem(iconDefinition)
local iconKey = iconDefinition.key
return {
text = iconDefinition.text,
icon = iconKey,
func = function(pinID)
addon:SetPinIcon(pinID, iconKey)
end,
}
end
for _, iconDefinition in ipairs(ICON_DEFINITIONS) do
PIN_TEXTURES[iconDefinition.key] = "Interface\\AddOns\\MapPins\\icons\\" .. iconDefinition.key .. ".png"
local menuItem = CreateIconMenuItem(iconDefinition)
if iconDefinition.basic then
table.insert(BASIC_PIN_CONTEXT_MENU_ITEMS, menuItem)
else
table.insert(ADDITIONAL_PIN_CONTEXT_MENU_ITEMS, menuItem)
end
end
for index = #PIN_CONTEXT_MENU_ITEMS, 1, -1 do
if PIN_CONTEXT_MENU_ITEMS[index].hasSubmenu then
for basicIndex = #BASIC_PIN_CONTEXT_MENU_ITEMS, 1, -1 do
table.insert(PIN_CONTEXT_MENU_ITEMS, index, BASIC_PIN_CONTEXT_MENU_ITEMS[basicIndex])
end
break
end
end
local LEGACY_PIN_ICON_KEYS = {
default = DEFAULT_PIN_ICON,
}
local function Clamp(value, minValue, maxValue)
if value < minValue then
return minValue
end
if value > maxValue then
return maxValue
end
return value
end
local function RoundCoord(value)
return tonumber(string.format("%.4f", value))
end
local function NormalizePinIconKey(iconKey)
if LEGACY_PIN_ICON_KEYS[iconKey] then
iconKey = LEGACY_PIN_ICON_KEYS[iconKey]
end
if not PIN_TEXTURES[iconKey] then
iconKey = DEFAULT_PIN_ICON
end
return iconKey
end
local function NormalizePinNote(note)
if type(note) ~= "string" then
return nil
end
note = note:gsub("^%s+", ""):gsub("%s+$", "")
if note == "" then
return nil
end
return note
end
local function EnsureSavedVariables()
if not MapPinsDB and WMPTAB then
MapPinsDB = WMPTAB
end
MapPinsDB = MapPinsDB or {}
MapPinsDB.pins = MapPinsDB.pins or {}
MapPinsDB.nextPinID = MapPinsDB.nextPinID or 1
end
local function GetStoredPins(mapID)
EnsureSavedVariables()
if not MapPinsDB.pins[mapID] then
MapPinsDB.pins[mapID] = {}
end
return MapPinsDB.pins[mapID]
end
local function AllocatePinID()
local pinID = MapPinsDB.nextPinID
MapPinsDB.nextPinID = pinID + 1
return pinID
end
local function GetDisplayedMapID()
if WorldMapFrame and WorldMapFrame.GetMapID then
return WorldMapFrame:GetMapID()
end
return nil
end
local function GetNormalizedMapCursorPosition()
if not WorldMapFrame then
return nil, nil
end
if WorldMapFrame.GetNormalizedCursorPosition then
return WorldMapFrame:GetNormalizedCursorPosition()
end
if WorldMapFrame.ScrollContainer and WorldMapFrame.ScrollContainer.GetNormalizedCursorPosition then
return WorldMapFrame.ScrollContainer:GetNormalizedCursorPosition()
end
return nil, nil
end
local function GetMapInfo(mapID)
if not mapID then
return nil
end
return C_Map.GetMapInfo(mapID)
end
local function IsZoneMapType(mapType)
return mapType == Enum.UIMapType.Zone or mapType == Enum.UIMapType.Dungeon or mapType == Enum.UIMapType.Micro
end
local function IterateCandidateMaps(mapID, results)
results = results or {}
local children = C_Map.GetMapChildrenInfo(mapID)
if not children then
return results
end
for _, childInfo in ipairs(children) do
local childMapID = childInfo.mapID
if childMapID then
table.insert(results, childMapID)
IterateCandidateMaps(childMapID, results)
end
end
return results
end
local function GetMapWorldArea(mapID)
if C_Map.GetMapWorldSize then
local width, height = C_Map.GetMapWorldSize(mapID)
if width and height then
return width * height
end
end
local mapData = HBD.mapData[mapID]
if mapData and mapData[1] and mapData[2] then
return mapData[1] * mapData[2]
end
return math.huge
end
local function ResolveClickTargetMap(displayedMapID, x, y)
local mapInfo = GetMapInfo(displayedMapID)
if not mapInfo then
return nil, nil, nil
end
if IsZoneMapType(mapInfo.mapType) then
return displayedMapID, x, y
end
local worldX, worldY, instanceID
if displayedMapID == AZEROTH_MAP_ID then
local bestMapID
local bestX
local bestY
local bestArea
for candidateMapID, mapData in pairs(HBD.mapData) do
if mapData then
local candidateInfo = GetMapInfo(candidateMapID)
if candidateInfo and IsZoneMapType(candidateInfo.mapType) then
local candidateX, candidateY = HBD:TranslateZoneCoordinates(x, y, AZEROTH_MAP_ID, candidateMapID)
if candidateX and candidateY then
local area = GetMapWorldArea(candidateMapID)
if not bestArea or area < bestArea then
bestMapID = candidateMapID
bestX = candidateX
bestY = candidateY
bestArea = area
end
end
end
end
end
return bestMapID, bestX, bestY
else
worldX, worldY, instanceID = HBD:GetWorldCoordinatesFromZone(x, y, displayedMapID)
end
if not worldX or not worldY or not instanceID then
return nil, nil, nil
end
local candidates = IterateCandidateMaps(displayedMapID, {})
local bestMapID
local bestX
local bestY
local bestArea
for _, candidateMapID in ipairs(candidates) do
local candidateInfo = GetMapInfo(candidateMapID)
if candidateInfo and IsZoneMapType(candidateInfo.mapType) then
local candidateX, candidateY = HBD:GetZoneCoordinatesFromWorldInstance(worldX, worldY, instanceID, candidateMapID)
if candidateX and candidateY then
local area = GetMapWorldArea(candidateMapID)
if not bestArea or area < bestArea then
bestMapID = candidateMapID
bestX = candidateX
bestY = candidateY
bestArea = area
end
end
end
end
if bestMapID then
return bestMapID, bestX, bestY
end
return displayedMapID, x, y
end
local function CreateContextMenuSubmenu(parent, frameTemplate)
if #ADDITIONAL_PIN_CONTEXT_MENU_ITEMS == 0 then
return nil
end
local submenu = CreateFrame("Frame", nil, UIParent, frameTemplate)
--submenu:SetFrameStrata("TOOLTIP")
submenu:SetClampedToScreen(true)
submenu:EnableMouse(true)
submenu:SetToplevel(true)
submenu:Hide()
submenu:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 16,
insets = {
left = 4,
right = 4,
top = 4,
bottom = 4,
},
})
submenu:SetBackdropColor(0.06, 0.06, 0.06, 0.95)
submenu:SetBackdropBorderColor(0.6, 0.6, 0.6, 1)
local columns = math.min(CONTEXT_SUBMENU_COLUMNS, #ADDITIONAL_PIN_CONTEXT_MENU_ITEMS)
local rows = math.ceil(#ADDITIONAL_PIN_CONTEXT_MENU_ITEMS / columns)
local width = (CONTEXT_SUBMENU_INSET * 2) + (columns * CONTEXT_SUBMENU_ICON_SIZE) + ((columns - 1) * CONTEXT_SUBMENU_ICON_SPACING)
local height = (CONTEXT_SUBMENU_INSET * 2) + (rows * CONTEXT_SUBMENU_ICON_SIZE) + ((rows - 1) * CONTEXT_SUBMENU_ICON_SPACING)
submenu:SetSize(width, height)
for index, item in ipairs(ADDITIONAL_PIN_CONTEXT_MENU_ITEMS) do
local column = (index - 1) % columns
local row = math.floor((index - 1) / columns)
local button = CreateFrame("Button", nil, submenu)
button:SetSize(CONTEXT_SUBMENU_ICON_SIZE, CONTEXT_SUBMENU_ICON_SIZE)
button:SetPoint(
"TOPLEFT",
submenu,
"TOPLEFT",
CONTEXT_SUBMENU_INSET + (column * (CONTEXT_SUBMENU_ICON_SIZE + CONTEXT_SUBMENU_ICON_SPACING)),
-CONTEXT_SUBMENU_INSET - (row * (CONTEXT_SUBMENU_ICON_SIZE + CONTEXT_SUBMENU_ICON_SPACING))
)
button:RegisterForClicks("LeftButtonUp")
button.highlight = button:CreateTexture(nil, "HIGHLIGHT")
button.highlight:SetAllPoints()
button.highlight:SetColorTexture(1, 1, 1, 0.12)
button.icon = button:CreateTexture(nil, "ARTWORK")
button.icon:SetAllPoints()
button.icon:SetTexture(PIN_TEXTURES[item.icon])
button:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetText(item.text)
GameTooltip:Show()
end)
button:SetScript("OnLeave", function()
GameTooltip:Hide()
C_Timer.After(0, function()
if parent.moreRow and parent.moreIconsSubmenu and parent.moreIconsSubmenu:IsShown() and not parent.moreRow:IsMouseOver() and not parent.moreIconsSubmenu:IsMouseOver() then
parent.moreIconsSubmenu:Hide()
end
end)
end)
button:SetScript("OnClick", function()
local selectedPinID = menuOwnerPinID
if parent.SaveNote then
parent:SaveNote()
end
if item.func then
item.func(selectedPinID)
end
parent:Hide()
end)
end
submenu:SetScript("OnHide", function()
GameTooltip:Hide()
end)
return submenu
end
local function CreatePinContextMenu()
if _G[MENU_FRAME_NAME] then
return _G[MENU_FRAME_NAME]
end
local frameTemplate
if BackdropTemplateMixin then
frameTemplate = "BackdropTemplate"
end
local menuFrame = CreateFrame("Frame", MENU_FRAME_NAME, UIParent, frameTemplate)
menuFrame:SetFrameStrata("TOOLTIP")
menuFrame:SetClampedToScreen(true)
menuFrame:EnableMouse(true)
menuFrame:SetToplevel(true)
menuFrame:Hide()
menuFrame:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 16,
insets = {
left = 4,
right = 4,
top = 4,
bottom = 4,
},
})
menuFrame:SetBackdropColor(0.06, 0.06, 0.06, 0.95)
menuFrame:SetBackdropBorderColor(0.6, 0.6, 0.6, 1)
menuFrame.rows = {}
menuFrame.noteInput = nil
menuFrame.noteLabel = nil
menuFrame.moreRow = nil
menuFrame.moreIconsSubmenu = CreateContextMenuSubmenu(menuFrame, frameTemplate)
menuFrame.HideMoreIconsSubmenu = function(self)
if self.moreIconsSubmenu and self.moreIconsSubmenu:IsShown() then
self.moreIconsSubmenu:Hide()
end
end
menuFrame.ShowMoreIconsSubmenu = function(self)
if not self.moreIconsSubmenu or not self.moreRow then
return
end
self.moreIconsSubmenu:ClearAllPoints()
self.moreIconsSubmenu:SetPoint("TOPLEFT", self.moreRow, "TOPRIGHT", 2, 0)
self.moreIconsSubmenu:Show()
self.moreIconsSubmenu:Raise()
end
local currentY = -CONTEXT_MENU_INSET
for _, item in ipairs(PIN_CONTEXT_MENU_ITEMS) do
if item.isSeparator then
local separator = menuFrame:CreateTexture(nil, "ARTWORK")
separator:SetColorTexture(0.35, 0.35, 0.35, 1)
separator:SetHeight(1)
separator:SetPoint("TOPLEFT", menuFrame, "TOPLEFT", CONTEXT_MENU_INSET, currentY - (CONTEXT_MENU_SEPARATOR_HEIGHT / 2))
separator:SetPoint("TOPRIGHT", menuFrame, "TOPRIGHT", -CONTEXT_MENU_INSET, currentY - (CONTEXT_MENU_SEPARATOR_HEIGHT / 2))
currentY = currentY - CONTEXT_MENU_SEPARATOR_HEIGHT
elseif item.isNoteInput then
local noteLabel = menuFrame:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
noteLabel:SetPoint("TOPLEFT", menuFrame, "TOPLEFT", CONTEXT_MENU_INSET + 2, currentY)
noteLabel:SetPoint("TOPRIGHT", menuFrame, "TOPRIGHT", -CONTEXT_MENU_INSET - 2, currentY)
noteLabel:SetJustifyH("LEFT")
noteLabel:SetText(item.label)
currentY = currentY - CONTEXT_MENU_ROW_HEIGHT
local noteInputTemplate
if BackdropTemplateMixin then
noteInputTemplate = "BackdropTemplate"
end
local noteInput = CreateFrame("EditBox", nil, menuFrame, noteInputTemplate)
noteInput:SetPoint("TOPLEFT", menuFrame, "TOPLEFT", CONTEXT_MENU_INSET, currentY)
noteInput:SetSize(CONTEXT_MENU_WIDTH - (CONTEXT_MENU_INSET * 2), CONTEXT_MENU_NOTE_HEIGHT)
noteInput:SetAutoFocus(false)
noteInput:SetFontObject(GameFontHighlightSmall)
noteInput:SetTextInsets(4, 4, 0, 0)
noteInput:SetMaxLetters(120)
noteInput:SetBackdrop({
bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 12,
insets = {
left = 3,
right = 3,
top = 3,
bottom = 3,
},
})
noteInput:SetBackdropColor(0.02, 0.02, 0.02, 0.95)
noteInput:SetBackdropBorderColor(0.45, 0.45, 0.45, 1)
noteInput:SetScript("OnEscapePressed", function(self)
self:ClearFocus()
end)
noteInput:SetScript("OnEnterPressed", function(self)
self:ClearFocus()
end)
menuFrame.noteLabel = noteLabel
menuFrame.noteInput = noteInput
currentY = currentY - CONTEXT_MENU_NOTE_HEIGHT - 2
else
local row = CreateFrame("Button", nil, menuFrame)
row:SetPoint("TOPLEFT", menuFrame, "TOPLEFT", CONTEXT_MENU_INSET, currentY)
row:SetSize(CONTEXT_MENU_WIDTH - (CONTEXT_MENU_INSET * 2), CONTEXT_MENU_ROW_HEIGHT)
row:RegisterForClicks("LeftButtonUp")
row.highlight = row:CreateTexture(nil, "HIGHLIGHT")
row.highlight:SetAllPoints()
row.highlight:SetColorTexture(1, 1, 1, 0.08)
if item.icon then
row.icon = row:CreateTexture(nil, "ARTWORK")
row.icon:SetSize(CONTEXT_MENU_ICON_SIZE, CONTEXT_MENU_ICON_SIZE)
row.icon:SetPoint("LEFT", row, "LEFT", 2, 0)
row.icon:SetTexture(PIN_TEXTURES[item.icon] or PIN_TEXTURES[DEFAULT_PIN_ICON])
end
row.text = row:CreateFontString(nil, "ARTWORK", item.isTitle and "GameFontNormalSmall" or "GameFontHighlightSmall")
row.text:SetJustifyH("LEFT")
row.text:SetJustifyV("MIDDLE")
if item.hasSubmenu then
row.text:SetPoint("LEFT", row, "LEFT", 2, 0)
row.text:SetPoint("RIGHT", row, "RIGHT", -14, 0)
elseif row.icon then
row.text:SetPoint("LEFT", row.icon, "RIGHT", 6, 0)
row.text:SetPoint("RIGHT", row, "RIGHT", -2, 0)
else
row.text:SetPoint("LEFT", row, "LEFT", 2, 0)
row.text:SetPoint("RIGHT", row, "RIGHT", -2, 0)
end
row.text:SetText(item.text)
if item.hasSubmenu then
row.arrow = row:CreateTexture(nil, "ARTWORK")
row.arrow:SetSize(12, 12)
row.arrow:SetPoint("RIGHT", row, "RIGHT", -2, 0)
row.arrow:SetTexture("Interface\\MONEYFRAME\\Arrow-Right-Up")
end
if item.isTitle then
row.text:SetTextColor(1, 0.82, 0, 1)
row:Disable()
row.highlight:Hide()
elseif item.hasSubmenu then
menuFrame.moreRow = row
row:SetScript("OnEnter", function()
menuFrame:ShowMoreIconsSubmenu()
end)
row:SetScript("OnLeave", function()
C_Timer.After(0, function()
if menuFrame.moreRow and menuFrame.moreIconsSubmenu and menuFrame.moreIconsSubmenu:IsShown() and not menuFrame.moreRow:IsMouseOver() and not menuFrame.moreIconsSubmenu:IsMouseOver() then
menuFrame.moreIconsSubmenu:Hide()
end
end)
end)
row:SetScript("OnClick", function()
if menuFrame.moreIconsSubmenu and menuFrame.moreIconsSubmenu:IsShown() then
menuFrame.moreIconsSubmenu:Hide()
else
menuFrame:ShowMoreIconsSubmenu()
end
end)
else
row:SetScript("OnEnter", function()
menuFrame:HideMoreIconsSubmenu()
end)
row:SetScript("OnClick", function()
local selectedPinID = menuOwnerPinID
if menuFrame.SaveNote then
menuFrame:SaveNote()
end
if item.func then
item.func(selectedPinID)
end
menuFrame:Hide()
end)
end
table.insert(menuFrame.rows, row)
currentY = currentY - CONTEXT_MENU_ROW_HEIGHT
end
end
menuFrame.SaveNote = function(self)
if not self.noteInput or not menuOwnerPinID then
return
end
addon:SetPinNote(menuOwnerPinID, self.noteInput:GetText())
end
if menuFrame.noteInput then
menuFrame.noteInput:SetScript("OnEditFocusLost", function()
menuFrame:SaveNote()
end)
end
menuFrame:SetSize(CONTEXT_MENU_WIDTH, -currentY + CONTEXT_MENU_INSET)
menuFrame:SetScript("OnShow", function(self)
local frameName = self:GetName()
if frameName and UISpecialFrames then
for _, specialFrameName in ipairs(UISpecialFrames) do
if specialFrameName == frameName then
return
end
end
table.insert(UISpecialFrames, frameName)
end
end)
menuFrame:SetScript("OnHide", function()
menuFrame:HideMoreIconsSubmenu()
if menuFrame.noteInput then
menuFrame.noteInput:ClearFocus()
end
menuOwnerPinID = nil
end)
return menuFrame
end
local function ShowPinContextMenu(pinID)
local menuFrame = CreatePinContextMenu()
if not menuFrame then
return
end
menuOwnerPinID = pinID
local pin = addon:FindPin(pinID)
if menuFrame.noteInput then
menuFrame.noteInput:SetText(pin and pin.note or "")
menuFrame.noteInput:HighlightText(0, 0)
end
local scale = UIParent:GetEffectiveScale()
local cursorX, cursorY = GetCursorPosition()
cursorX = cursorX / scale
cursorY = cursorY / scale
local x = Clamp(cursorX + CONTEXT_MENU_CURSOR_OFFSET_X, 0, UIParent:GetWidth() - menuFrame:GetWidth())
local y = Clamp(cursorY + CONTEXT_MENU_CURSOR_OFFSET_Y, menuFrame:GetHeight(), UIParent:GetHeight())
menuFrame:ClearAllPoints()
menuFrame:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", x, y)
menuFrame:Show()
menuFrame:Raise()
if GameTooltip and GameTooltip:IsOwned(menuFrame) then
GameTooltip:Hide()
end
end
local function HidePinContextMenu()
local menuFrame = _G[MENU_FRAME_NAME]
if menuFrame and menuFrame:IsShown() then
if menuFrame.SaveNote then
menuFrame:SaveNote()
end
menuFrame:Hide()
end
end
local function GetTextureForPin(pin)
local iconKey = NormalizePinIconKey(pin and pin.icon or DEFAULT_PIN_ICON)
return PIN_TEXTURES[iconKey]
end
local function ReleasePinFrame(frameStore, pinID, isMinimap)
local frame = frameStore[pinID]
if not frame then
return
end
if isMinimap then
HBDPins:RemoveMinimapIcon(AddonName, frame)
else
HBDPins:RemoveWorldMapIcon(AddonName, frame)
end
frameStore[pinID] = nil
frame.pinID = nil
frame:Hide()
end
local function CreatePinFrame()
local frame = CreateFrame("Button", nil, UIParent)
frame:SetSize(PIN_SIZE, PIN_SIZE)
frame:RegisterForClicks("RightButtonUp")
frame.texture = frame:CreateTexture(nil, "OVERLAY")
frame.texture:SetAllPoints()
frame:SetScript("OnEnter", function(self)
local pin = addon:FindPin(self.pinID)
local note = pin and NormalizePinNote(pin.note)
if not note then
return
end
local scale = UIParent:GetEffectiveScale()
local cursorX, cursorY = GetCursorPosition()
cursorX = cursorX / scale
cursorY = cursorY / scale
GameTooltip:SetOwner(UIParent, "ANCHOR_NONE")
GameTooltip:ClearAllPoints()
GameTooltip:SetPoint("BOTTOM", UIParent, "BOTTOMLEFT", cursorX, cursorY + PIN_TOOLTIP_CURSOR_OFFSET_Y)
GameTooltip:AddLine(note)
GameTooltip:Show()
end)
frame:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
frame:SetScript("OnClick", function(self, button)
if button ~= "RightButton" then
return
end
ShowPinContextMenu(self.pinID)
end)
return frame
end
local function AcquirePinFrame(frameStore, pin)
local frame = frameStore[pin.id]
if frame then
return frame
end
frame = CreatePinFrame()
frameStore[pin.id] = frame
frame.pinID = pin.id
return frame
end
function addon:RefreshPinFrame(pin)
local worldFrame = AcquirePinFrame(worldPinFramesByID, pin)
worldFrame.pinID = pin.id
worldFrame.texture:SetTexture(GetTextureForPin(pin))
worldFrame:Show()
HBDPins:RemoveWorldMapIcon(AddonName, worldFrame)
HBDPins:AddWorldMapIconMap(AddonName, worldFrame, pin.mapID, pin.x, pin.y, HBD_PINS_WORLDMAP_SHOW_CONTINENT)
local minimapFrame = AcquirePinFrame(minimapPinFramesByID, pin)
minimapFrame.pinID = pin.id
minimapFrame.texture:SetTexture(GetTextureForPin(pin))
minimapFrame:Show()
HBDPins:RemoveMinimapIcon(AddonName, minimapFrame)
HBDPins:AddMinimapIconMap(AddonName, minimapFrame, pin.mapID, pin.x, pin.y, MINIMAP_SHOW_IN_PARENT_ZONE, MINIMAP_FLOAT_ON_EDGE)
end
function addon:RefreshAllPins()
HBDPins:RemoveAllMinimapIcons(AddonName)
HBDPins:RemoveAllWorldMapIcons(AddonName)
local activePinIDs = {}
for mapID, pins in pairs(MapPinsDB.pins) do
for _, pin in ipairs(pins) do
pin.mapID = mapID
activePinIDs[pin.id] = true
self:RefreshPinFrame(pin)
end
end
for pinID in pairs(worldPinFramesByID) do
if not activePinIDs[pinID] then
ReleasePinFrame(worldPinFramesByID, pinID, false)
end
end
for pinID in pairs(minimapPinFramesByID) do
if not activePinIDs[pinID] then
ReleasePinFrame(minimapPinFramesByID, pinID, true)
end
end
end
function addon:FindPin(pinID)
for mapID, pins in pairs(MapPinsDB.pins) do
for index, pin in ipairs(pins) do
if pin.id == pinID then
pin.mapID = mapID
return pin, pins, index
end
end
end
return nil, nil, nil
end
function addon:RemovePin(pinID)
if not pinID then
return
end
local _, pins, index = self:FindPin(pinID)
if not pins or not index then
return
end
table.remove(pins, index)
self:RefreshAllPins()
end
function addon:SetPinIcon(pinID, iconKey)
local pin = self:FindPin(pinID)
if not pin then
return
end
pin.icon = NormalizePinIconKey(iconKey)
self:RefreshAllPins()
end
function addon:SetPinNote(pinID, note)
local pin = self:FindPin(pinID)
if not pin then
return
end
pin.note = NormalizePinNote(note)
end
function addon:AddPin(mapID, x, y)
local pins = GetStoredPins(mapID)
local pin = {
id = AllocatePinID(),
x = RoundCoord(Clamp(x, 0, 1)),
y = RoundCoord(Clamp(y, 0, 1)),
icon = DEFAULT_PIN_ICON,
note = nil,
mapID = mapID,
}
table.insert(pins, pin)
self:RefreshAllPins()
end
function addon:HandleModifiedMapClick()
HidePinContextMenu()
if not WorldMapFrame or not WorldMapFrame:IsShown() then
return
end
local displayedMapID = GetDisplayedMapID()
if not displayedMapID then
return
end
local cursorX, cursorY = GetNormalizedMapCursorPosition()
if not cursorX or not cursorY or cursorX < 0 or cursorX > 1 or cursorY < 0 or cursorY > 1 then
return
end
local targetMapID, targetX, targetY = ResolveClickTargetMap(displayedMapID, cursorX, cursorY)
if not targetMapID or not targetX or not targetY then
return
end
self:AddPin(targetMapID, targetX, targetY)
end
function addon:InstallClickHandler()
if self.clickHandlerInstalled then
return
end
local scrollContainer = WorldMapFrame and WorldMapFrame.ScrollContainer
if not scrollContainer then
return
end
scrollContainer:HookScript("OnMouseUp", function(_, button)
if button ~= "RightButton" then
HidePinContextMenu()
end
if button == "LeftButton" and IsControlKeyDown() then
addon:HandleModifiedMapClick()
end
end)
self.clickHandlerInstalled = true
end
function addon:Initialize()
EnsureSavedVariables()
self:InstallClickHandler()
if WorldMapFrame then
WorldMapFrame:HookScript("OnHide", HidePinContextMenu)
end
self:RefreshAllPins()
end
addon:SetScript("OnEvent", function(_, event)
if event == "PLAYER_LOGIN" then
addon:Initialize()
end
end)
addon:RegisterEvent("PLAYER_LOGIN")