forked from Ermad/angry-assignments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.lua
2722 lines (2397 loc) · 80.4 KB
/
Core.lua
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
local AngryAssign = LibStub("AceAddon-3.0"):NewAddon("AngryAssignments", "AceConsole-3.0", "AceEvent-3.0", "AceComm-3.0", "AceTimer-3.0")
local AceGUI = LibStub("AceGUI-3.0")
local libS = LibStub("AceSerializer-3.0")
local libC = LibStub("LibCompress")
local lwin = LibStub("LibWindow-1.1")
local libCE = libC:GetAddonEncodeTable()
local LSM = LibStub("LibSharedMedia-3.0")
BINDING_HEADER_AngryAssign = "Angry Assignments"
BINDING_NAME_AngryAssign_WINDOW = "Toggle Window"
BINDING_NAME_AngryAssign_LOCK = "Toggle Lock"
BINDING_NAME_AngryAssign_DISPLAY = "Toggle Display"
BINDING_NAME_AngryAssign_SHOW_DISPLAY = "Show Display"
BINDING_NAME_AngryAssign_HIDE_DISPLAY = "Hide Display"
BINDING_NAME_AngryAssign_OUTPUT = "Output Assignment to Chat"
local AngryAssign_Version = '@project-version@'
local AngryAssign_Timestamp = '@project-date-integer@'
local isClassicVanilla = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
local isClassicTBC = WOW_PROJECT_ID == WOW_PROJECT_BURNING_CRUSADE_CLASSIC
local isClassicWrath = WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC
local isClassic = isClassicVanilla or isClassicTBC or isClassicWrath
local protocolVersion = 1
local comPrefix = "AnAss"..protocolVersion
local updateFrequency = 2
local pageLastUpdate = {}
local pageTimerId = {}
local displayLastUpdate = nil
local displayTimerId = nil
local versionLastUpdate = nil
local versionTimerId = nil
local guildOfficerNames = nil
-- Used for version tracking
local warnedOOD = false
local versionList = {}
local comStarted = false
local warnedPermission = false
local currentGroup = nil
-- Pages Saved Variable Format
-- AngryAssign_Pages = {
-- [Id] = { Id = 1231, Updated = time(), UpdateId = self:Hash(name, contents), Name = "Name", Contents = "...", Backup = "...", CategoryId = 123 },
-- ...
-- }
-- AngryAssign_Categories = {
-- [Id] = { Id = 1231, Name = "Name", CategoryId = 123 },
-- ...
-- }
--
-- Format for our addon communication
--
-- { "PAGE", [Id], [Last Update Timestamp], [Name], [Contents], [Last Update Unique Id] }
-- Sent when a page is updated. Id is a random unique value. Unique Id is hash of page contents. Uses RAID.
--
-- { "REQUEST_PAGE", [Id] }
-- Asks to be sent PAGE with given Id. Response is a throttled PAGE. Uses WHISPER to raid leader.
--
-- { "DISPLAY", [Id], [Last Update Timestamp], [Last Update Unique Id] }
-- Raid leader / promoted sends out when new page is to be displayed. Uses RAID.
--
-- { "REQUEST_DISPLAY" }
-- Asks to be sent DISPLAY. Response is a throttled DISPLAY. Uses WHISPER to raid leader.
--
-- { "VER_QUERY" }
-- { "VERSION", [Version], [Project Timestamp], [Valid Raid] }
-- Constants for dealing with our addon communication
local COMMAND = 1
local PAGE_Id = 2
local PAGE_Updated = 3
local PAGE_Name = 4
local PAGE_Contents = 5
local PAGE_UpdateId = 6
local REQUEST_PAGE_Id = 2
local DISPLAY_Id = 2
local DISPLAY_Updated = 3
local DISPLAY_UpdateId = 4
local VERSION_Version = 2
local VERSION_Timestamp = 3
local VERSION_ValidRaid = 4
-----------------------
-- Utility Functions --
-----------------------
local EasyMenu = EasyMenu
if not EasyMenu then
local function EasyMenu_Initialize( frame, level, menuList )
for index = 1, #menuList do
local value = menuList[index]
if (value.text) then
value.index = index
UIDropDownMenu_AddButton( value, level )
end
end
end
function EasyMenu(menuList, menuFrame, anchor, x, y, displayMode, autoHideDelay )
if ( displayMode == "MENU" ) then
menuFrame.displayMode = displayMode
end
UIDropDownMenu_Initialize(menuFrame, EasyMenu_Initialize, displayMode, nil, menuList)
ToggleDropDownMenu(1, nil, menuFrame, anchor, x, y, menuList, nil, autoHideDelay)
end
end
local GetSpellLink = C_Spell and C_Spell.GetSpellLink or GetSpellLink;
local GetItemInfo = GetItemInfo or C_Item.GetItemInfo
local GetSpellInfo = GetSpellInfo or function(spellID)
if not spellID then
return nil
end
local spellInfo = C_Spell.GetSpellInfo(spellID)
if spellInfo then
return spellInfo.name, nil, spellInfo.iconID, spellInfo.castTime, spellInfo.minRange, spellInfo.maxRange, spellInfo.spellID, spellInfo.originalIconID
end
end
local function selectedLastValue(input)
local a = select(-1, strsplit("", input or ""))
return tonumber(a)
end
local function tReverse(tbl)
for i=1, math.floor(#tbl / 2) do
tbl[i], tbl[#tbl - i + 1] = tbl[#tbl - i + 1], tbl[i]
end
end
local _player_realm = nil
local function EnsureUnitFullName(unit)
if not _player_realm then _player_realm = select(2, UnitFullName('player')) end
if unit and not unit:find('-') then
unit = unit..'-'.._player_realm
end
return unit
end
local function EnsureUnitShortName(unit)
if not _player_realm then _player_realm = select(2, UnitFullName('player')) end
local name, realm = strsplit("-", unit, 2)
if not realm or realm == _player_realm then
return name
else
return unit
end
end
local function PlayerFullName()
if not _player_realm then _player_realm = select(2, UnitFullName('player')) end
return UnitName('player')..'-'.._player_realm
end
local function RGBToHex(r, g, b, a)
r = math.ceil(255 * r)
g = math.ceil(255 * g)
b = math.ceil(255 * b)
if a == nil then
return string.format("%02x%02x%02x", r, g, b)
else
a = math.ceil(255 * a)
return string.format("%02x%02x%02x%02x", r, g, b, a)
end
end
local function HexToRGB(hex)
if string.len(hex) == 8 then
return tonumber("0x"..hex:sub(1,2)) / 255, tonumber("0x"..hex:sub(3,4)) / 255, tonumber("0x"..hex:sub(5,6)) / 255, tonumber("0x"..hex:sub(7,8)) / 255
else
return tonumber("0x"..hex:sub(1,2)) / 255, tonumber("0x"..hex:sub(3,4)) / 255, tonumber("0x"..hex:sub(5,6)) / 255
end
end
-------------------------
-- Addon Communication --
-------------------------
function AngryAssign:ReceiveMessage(prefix, data, channel, sender)
if prefix ~= comPrefix then return end
local one = libCE:Decode(data) -- Decode the compressed data
local two, message = libC:Decompress(one) -- Decompress the decoded data
if not two then error("Error decompressing: " .. message); return end
local success, final = libS:Deserialize(two) -- Deserialize the decompressed data
if not success then error("Error deserializing " .. final); return end
self:ProcessMessage( sender, final )
end
function AngryAssign:SendOutMessage(data, channel, target)
local one = libS:Serialize( data )
local two = libC:CompressHuffman(one)
local final = libCE:Encode(two)
if not channel then
if IsInGroup(LE_PARTY_CATEGORY_INSTANCE) or IsInRaid(LE_PARTY_CATEGORY_INSTANCE) then
channel = "INSTANCE_CHAT"
elseif IsInRaid(LE_PARTY_CATEGORY_HOME) then
channel = "RAID"
elseif IsInGroup(LE_PARTY_CATEGORY_HOME) then
channel = "PARTY"
end
end
if not channel then return end
-- self:Print("Sending "..data[COMMAND].." over "..channel.." to "..tostring(target))
self:SendCommMessage(comPrefix, final, channel, target, "NORMAL")
return true
end
function AngryAssign:ProcessMessage(sender, data)
local cmd = data[COMMAND]
sender = EnsureUnitFullName(sender)
-- self:Print("Received "..data[COMMAND].." from "..sender)
if cmd == "PAGE" then
if sender == PlayerFullName() then return end
if not self:PermissionCheck(sender) then
self:PermissionCheckFailError(sender)
return
end
local contents_updated = true
local id = data[PAGE_Id]
local page = AngryAssign_Pages[id]
if page then
if data[PAGE_UpdateId] and page.UpdateId == data[PAGE_UpdateId] then return end -- The version received is same as the one we already have
contents_updated = page.Contents ~= data[PAGE_Contents]
page.Name = data[PAGE_Name]
page.Contents = data[PAGE_Contents]
page.Updated = data[PAGE_Updated]
page.UpdateId = data[PAGE_UpdateId] or self:Hash(page.Name, page.Contents)
if self:SelectedId() == id then
self:SelectedUpdated(sender)
self:UpdateSelected()
end
else
AngryAssign_Pages[id] = { Id = id, Updated = data[PAGE_Updated], UpdateId = data[PAGE_UpdateId], Name = data[PAGE_Name], Contents = data[PAGE_Contents] }
end
if AngryAssign_State.displayed == id then
self:UpdateDisplayed()
self:ShowDisplay()
if contents_updated then self:DisplayUpdateNotification() end
end
self:UpdateTree()
elseif cmd == "DISPLAY" then
if sender == PlayerFullName() then return end
if not self:PermissionCheck(sender) then
if data[DISPLAY_Id] then self:PermissionCheckFailError(sender) end
return
end
local id = data[DISPLAY_Id]
local updated = data[DISPLAY_Updated]
local updateId = data[DISPLAY_UpdateId]
local page = AngryAssign_Pages[id]
local sameVersion = (updateId and page and updateId == page.UpdateId) or (not updateId and page and updated == page.Updated)
if id and not sameVersion then
self:SendRequestPage(id, sender)
end
if AngryAssign_State.displayed ~= id then
AngryAssign_State.displayed = id
self:UpdateTree()
self:UpdateDisplayed()
self:ShowDisplay()
if id then self:DisplayUpdateNotification() end
end
elseif cmd == "REQUEST_DISPLAY" then
if sender == PlayerFullName() then return end
if not self:IsPlayerRaidLeader() then return end
self:SendDisplay( AngryAssign_State.displayed )
elseif cmd == "REQUEST_PAGE" then
if sender == PlayerFullName() then return end
self:SendPage( data[REQUEST_PAGE_Id] )
elseif cmd == "VER_QUERY" then
self:SendVersion()
elseif cmd == "VERSION" then
local ver, timestamp
ver = tostring(data[VERSION_Version])
timestamp = tonumber(data[VERSION_Timestamp])
local localTimestamp = "dev"
local localIsClassic = 0
if AngryAssign_Timestamp:sub(1,1) ~= "@" then
localTimestamp = tonumber(AngryAssign_Timestamp)
if AngryAssign_Version:sub(-3) == "tbc" then
localIsClassic = 2
elseif AngryAssign_Version:sub(-1) == "c" then
localIsClassic = 1
end
end
local remoteIsClassic = 0
if ver:sub(-3) == "tbc" then
remoteIsClassic = 2
elseif ver:sub(-1) == "c" then
remoteIsClassic = 1
end
local localStr = tostring(localTimestamp)
local remoteStr = tostring(timestamp)
if (localStr ~= "dev" and localStr:len() ~= 14) or (remoteStr ~= "dev" and remoteStr:len() ~= 14) then
if localStr ~= "dev" then localTimestamp = tonumber(localStr:sub(1,8)) end
if remoteStr ~= "dev" then timestamp = tonumber(remoteStr:sub(1,8)) end
end
if localTimestamp ~= "dev" and timestamp ~= "dev" and timestamp > localTimestamp and localIsClassic == remoteIsClassic and not warnedOOD then
self:Print("Your version of Angry Assignments is out of date! Download the latest version from curse.com.")
warnedOOD = true
end
versionList[ sender ] = { valid = data[VERSION_ValidRaid], version = ver }
end
end
function AngryAssign:PermissionCheckFailError(sender)
if not warnedPermission then
self:Print( RED_FONT_COLOR_CODE .. "You have received a page update from "..Ambiguate(sender, "none").." that was rejected due to insufficient permissions. If you wish to see this page, please adjust your permission settings.|r" )
warnedPermission = true
end
end
function AngryAssign:SendPage(id, force)
local lastUpdate = pageLastUpdate[id]
local timerId = pageTimerId[id]
local curTime = time()
if lastUpdate and (curTime - lastUpdate <= updateFrequency) then
if not timerId then
if force then
self:SendPageMessage(id)
else
pageTimerId[id] = self:ScheduleTimer("SendPageMessage", updateFrequency - (curTime - lastUpdate), id)
end
elseif force then
self:CancelTimer( timerId )
self:SendPageMessage(id)
end
else
self:SendPageMessage(id)
end
end
function AngryAssign:SendPageMessage(id)
pageLastUpdate[id] = time()
pageTimerId[id] = nil
local page = AngryAssign_Pages[ id ]
if not page then error("Can't send page, does not exist"); return end
if not page.UpdateId then page.UpdateId = self:Hash(page.Name, page.Contents) end
self:SendOutMessage({ "PAGE", [PAGE_Id] = page.Id, [PAGE_Updated] = page.Updated, [PAGE_Name] = page.Name, [PAGE_Contents] = page.Contents, [PAGE_UpdateId] = page.UpdateId })
end
function AngryAssign:SendDisplay(id, force)
local curTime = time()
if displayLastUpdate and (curTime - displayLastUpdate <= updateFrequency) then
if not displayTimerId then
if force then
self:SendDisplayMessage(id)
else
displayTimerId = self:ScheduleTimer("SendDisplayMessage", updateFrequency - (curTime - displayLastUpdate), id)
end
elseif force then
self:CancelTimer( displayTimerId )
self:SendDisplayMessage(id)
end
else
self:SendDisplayMessage(id)
end
end
function AngryAssign:SendDisplayMessage(id)
displayLastUpdate = time()
displayTimerId = nil
local page = AngryAssign_Pages[ id ]
if not page then
self:SendOutMessage({ "DISPLAY", [DISPLAY_Id] = nil, [DISPLAY_Updated] = nil, [DISPLAY_UpdateId] = nil })
else
if not page.UpdateId then page.UpdateId = self:Hash(page.Name, page.Contents) end
self:SendOutMessage({ "DISPLAY", [DISPLAY_Id] = page.Id, [DISPLAY_Updated] = page.Updated, [DISPLAY_UpdateId] = page.UpdateId })
end
end
function AngryAssign:SendRequestDisplay()
if (IsInRaid() or IsInGroup()) then
local to = self:GetRaidLeader(true)
if to then self:SendOutMessage({ "REQUEST_DISPLAY" }, "WHISPER", to) end
end
end
function AngryAssign:SendVersion(force)
local curTime = time()
if versionLastUpdate and (curTime - versionLastUpdate <= updateFrequency) then
if not versionTimerId then
if force then
self:SendVersionMessage(id)
else
versionTimerId = self:ScheduleTimer("SendVersionMessage", updateFrequency - (curTime - versionLastUpdate), id)
end
elseif force then
self:CancelTimer( versionTimerId )
self:SendVersionMessage()
end
else
self:SendVersionMessage()
end
end
function AngryAssign:SendVersionMessage()
versionLastUpdate = time()
versionTimerId = nil
local revToSend
local timestampToSend
local verToSend
if AngryAssign_Version:sub(1,1) == "@" then verToSend = "dev" else verToSend = AngryAssign_Version end
if AngryAssign_Timestamp:sub(1,1) == "@" then timestampToSend = "dev" else timestampToSend = tonumber(AngryAssign_Timestamp) end
self:SendOutMessage({ "VERSION", [VERSION_Version] = verToSend, [VERSION_Timestamp] = timestampToSend, [VERSION_ValidRaid] = self:IsValidRaid() })
end
function AngryAssign:SendVerQuery()
self:SendOutMessage({ "VER_QUERY" })
end
function AngryAssign:SendRequestPage(id, to)
if (IsInRaid() or IsInGroup()) or to then
if not to then to = self:GetRaidLeader(true) end
if to then self:SendOutMessage({ "REQUEST_PAGE", [REQUEST_PAGE_Id] = id }, "WHISPER", to) end
end
end
function AngryAssign:GetRaidLeader(online_only)
if (IsInRaid() or IsInGroup()) then
for i = 1, GetNumGroupMembers() do
local name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(i)
if rank == 2 then
if (not online_only) or online then
return EnsureUnitFullName(name)
else
return nil
end
end
end
end
return nil
end
function AngryAssign:GetCurrentGroup()
local player = PlayerFullName()
if (IsInRaid() or IsInGroup()) then
for i = 1, GetNumGroupMembers() do
local name, _, subgroup = GetRaidRosterInfo(i)
if EnsureUnitFullName(name) == player then
return subgroup
end
end
end
return nil
end
function AngryAssign:VersionCheckOutput()
local missing_addon = {}
local invalid_raid = {}
local different_version = {}
local up_to_date = {}
local ver = AngryAssign_Version
if ver:sub(1,1) == "@" then ver = "dev" end
if (IsInRaid() or IsInGroup()) then
for i = 1, GetNumGroupMembers() do
local name, _, _, _, _, _, _, online = GetRaidRosterInfo(i)
local fullname = EnsureUnitFullName(name)
if online then
if not versionList[ fullname ] then
tinsert(missing_addon, name)
elseif versionList[ fullname ].valid == false or versionList[ fullname ].valid == nil then
tinsert(invalid_raid, name)
elseif ver ~= versionList[ fullname ].version then
tinsert(different_version, string.format("%s - %s", name, versionList[ fullname ].version) )
else
tinsert(up_to_date, name)
end
end
end
end
self:Print("Version check results:")
if #up_to_date > 0 then
print(LIGHTYELLOW_FONT_COLOR_CODE.."Same version:|r "..table.concat(up_to_date, ", "))
end
if #different_version > 0 then
print(LIGHTYELLOW_FONT_COLOR_CODE.."Different version:|r "..table.concat(different_version, ", "))
end
if #invalid_raid > 0 then
print(LIGHTYELLOW_FONT_COLOR_CODE.."Not allowing changes:|r "..table.concat(invalid_raid, ", "))
end
if #missing_addon > 0 then
print(LIGHTYELLOW_FONT_COLOR_CODE.."Missing addon:|r "..table.concat(missing_addon, ", "))
end
end
--------------------------
-- Editing Pages Window --
--------------------------
function AngryAssign_ToggleWindow()
if not AngryAssign.window then AngryAssign:CreateWindow() end
if AngryAssign.window:IsShown() then
AngryAssign.window:Hide()
else
AngryAssign.window:Show()
end
end
function AngryAssign_ToggleLock()
AngryAssign:ToggleLock()
end
local function AngryAssign_AddPage(widget, event, value)
local popup_name = "AngryAssign_AddPage"
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
local text = self.editBox:GetText()
if text ~= "" then AngryAssign:CreatePage(text) end
end,
EditBoxOnEnterPressed = function(self)
local text = self:GetParent().editBox:GetText()
if text ~= "" then AngryAssign:CreatePage(text) end
self:GetParent():Hide()
end,
text = "New page name:",
hasEditBox = true,
whileDead = true,
EditBoxOnEscapePressed = function(self) self:GetParent():Hide() end,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopup_Show(popup_name)
end
local function AngryAssign_RenamePage(pageId)
local page = AngryAssign:Get(pageId)
if not page then return end
local popup_name = "AngryAssign_RenamePage_"..page.Id
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
local text = self.editBox:GetText()
AngryAssign:RenamePage(page.Id, text)
end,
EditBoxOnEnterPressed = function(self)
local text = self:GetParent().editBox:GetText()
AngryAssign:RenamePage(page.Id, text)
self:GetParent():Hide()
end,
OnShow = function(self)
self.editBox:SetText(page.Name)
end,
whileDead = true,
hasEditBox = true,
EditBoxOnEscapePressed = function(self) self:GetParent():Hide() end,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopupDialogs[popup_name].text = 'Rename page "'.. page.Name ..'" to:'
StaticPopup_Show(popup_name)
end
local function AngryAssign_DeletePage(pageId)
local page = AngryAssign:Get(pageId)
if not page then return end
local popup_name = "AngryAssign_DeletePage_"..page.Id
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
AngryAssign:DeletePage(page.Id)
end,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopupDialogs[popup_name].text = 'Are you sure you want to delete page "'.. page.Name ..'"?'
StaticPopup_Show(popup_name)
end
local function AngryAssign_AddCategory(widget, event, value)
local popup_name = "AngryAssign_AddCategory"
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
local text = self.editBox:GetText()
if text ~= "" then AngryAssign:CreateCategory(text) end
end,
EditBoxOnEnterPressed = function(self)
local text = self:GetParent().editBox:GetText()
if text ~= "" then AngryAssign:CreateCategory(text) end
self:GetParent():Hide()
end,
text = "New category name:",
hasEditBox = true,
whileDead = true,
EditBoxOnEscapePressed = function(self) self:GetParent():Hide() end,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopup_Show(popup_name)
end
local function AngryAssign_RenameCategory(catId)
local cat = AngryAssign:GetCat(catId)
if not cat then return end
local popup_name = "AngryAssign_RenameCategory_"..cat.Id
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
local text = self.editBox:GetText()
AngryAssign:RenameCategory(cat.Id, text)
end,
EditBoxOnEnterPressed = function(self)
local text = self:GetParent().editBox:GetText()
AngryAssign:RenameCategory(cat.Id, text)
self:GetParent():Hide()
end,
OnShow = function(self)
self.editBox:SetText(cat.Name)
end,
whileDead = true,
hasEditBox = true,
EditBoxOnEscapePressed = function(self) self:GetParent():Hide() end,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopupDialogs[popup_name].text = 'Rename category "'.. cat.Name ..'" to:'
StaticPopup_Show(popup_name)
end
local function AngryAssign_DeleteCategory(catId)
local cat = AngryAssign:GetCat(catId)
if not cat then return end
local popup_name = "AngryAssign_DeleteCategory_"..cat.Id
if StaticPopupDialogs[popup_name] == nil then
StaticPopupDialogs[popup_name] = {
button1 = OKAY,
button2 = CANCEL,
OnAccept = function(self)
AngryAssign:DeleteCategory(cat.Id)
end,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3
}
end
StaticPopupDialogs[popup_name].text = 'Are you sure you want to delete category "'.. cat.Name ..'"?'
StaticPopup_Show(popup_name)
end
local function AngryAssign_AssignCategory(frame, entryId, catId)
HideDropDownMenu(1)
AngryAssign:AssignCategory(entryId, catId)
end
local function AngryAssign_RevertPage(widget, event, value)
if not AngryAssign.window then return end
AngryAssign:UpdateSelected(true)
end
function AngryAssign:DisplayPageByName( name )
for id, page in pairs(AngryAssign_Pages) do
if page.Name == name then
return self:DisplayPage( id )
end
end
return false
end
function AngryAssign:DisplayPage( id )
if not self:PermissionCheck() then return end
self:TouchPage( id )
self:SendPage( id, true )
self:SendDisplay( id, true )
if AngryAssign_State.displayed ~= id then
AngryAssign_State.displayed = id
AngryAssign:UpdateDisplayed()
AngryAssign:ShowDisplay()
AngryAssign:UpdateTree()
AngryAssign:DisplayUpdateNotification()
end
return true
end
local function AngryAssign_DisplayPage(widget, event, value)
if not AngryAssign:PermissionCheck() then return end
local id = AngryAssign:SelectedId()
AngryAssign:DisplayPage( id )
end
local function AngryAssign_ClearPage(widget, event, value)
if not AngryAssign:PermissionCheck() then return end
AngryAssign:ClearDisplayed()
AngryAssign:SendDisplay( nil, true )
end
local function AngryAssign_TextChanged(widget, event, value)
AngryAssign.window.button_revert:SetDisabled(false)
AngryAssign.window.button_restore:SetDisabled(false)
AngryAssign.window.button_display:SetDisabled(true)
AngryAssign.window.button_output:SetDisabled(true)
end
local function AngryAssign_TextEntered(widget, event, value)
AngryAssign:UpdateContents(AngryAssign:SelectedId(), value)
end
local function AngryAssign_RestorePage(widget, event, value)
if not AngryAssign.window then return end
local page = AngryAssign_Pages[AngryAssign:SelectedId()]
if not page or not page.Backup then return end
AngryAssign.window.text:SetText( page.Backup )
AngryAssign.window.text.button:Enable()
AngryAssign_TextChanged(widget, event, value)
end
local function AngryAssign_CategoryMenuList(entryId, parentId)
local categories = {}
local checkedId
if entryId > 0 then
local page = AngryAssign_Pages[entryId]
checkedId = page.CategoryId
else
local cat = AngryAssign_Categories[-entryId]
checkedId = cat.CategoryId
end
for _, cat in pairs(AngryAssign_Categories) do
if cat.Id ~= -entryId and (parentId or not cat.CategoryId) and (not parentId or cat.CategoryId == parentId) then
local subMenu = AngryAssign_CategoryMenuList(entryId, cat.Id)
table.insert(categories, { text = cat.Name, value = cat.Id, menuList = subMenu, hasArrow = (subMenu ~= nil), checked = (checkedId == cat.Id), func = AngryAssign_AssignCategory, arg1 = entryId, arg2 = cat.Id })
end
end
table.sort(categories, function(a,b) return a.text < b.text end)
if #categories > 0 then
return categories
end
end
local PagesDropDownList
function AngryAssign_PageMenu(pageId)
local page = AngryAssign_Pages[pageId]
if not page then return end
if not PagesDropDownList then
PagesDropDownList = {
{ notCheckable = true, isTitle = true },
{ text = "Rename", notCheckable = true, func = function(frame, pageId) AngryAssign_RenamePage(pageId) end },
{ text = "Delete", notCheckable = true, func = function(frame, pageId) AngryAssign_DeletePage(pageId) end },
{ text = "Category", notCheckable = true, hasArrow = true },
}
end
local permission = AngryAssign:PermissionCheck()
PagesDropDownList[1].text = page.Name
PagesDropDownList[2].arg1 = pageId
PagesDropDownList[2].disabled = not permission
PagesDropDownList[3].arg1 = pageId
local categories = AngryAssign_CategoryMenuList(pageId)
if categories ~= nil then
PagesDropDownList[4].menuList = categories
PagesDropDownList[4].disabled = false
else
PagesDropDownList[4].menuList = {}
PagesDropDownList[4].disabled = true
end
return PagesDropDownList
end
local CategoriesDropDownList
local function AngryAssign_CategoryMenu(catId)
local cat = AngryAssign_Categories[catId]
if not cat then return end
if not CategoriesDropDownList then
CategoriesDropDownList = {
{ notCheckable = true, isTitle = true },
{ text = "Rename", notCheckable = true, func = function(frame, pageId) AngryAssign_RenameCategory(pageId) end },
{ text = "Delete", notCheckable = true, func = function(frame, pageId) AngryAssign_DeleteCategory(pageId) end },
{ text = "Category", notCheckable = true, hasArrow = true },
}
end
CategoriesDropDownList[1].text = cat.Name
CategoriesDropDownList[2].arg1 = catId
CategoriesDropDownList[3].arg1 = catId
local categories = AngryAssign_CategoryMenuList(-catId)
if categories ~= nil then
CategoriesDropDownList[4].menuList = categories
CategoriesDropDownList[4].disabled = false
else
CategoriesDropDownList[4].menuList = {}
CategoriesDropDownList[4].disabled = true
end
return CategoriesDropDownList
end
local AngryAssign_DropDown
local function AngryAssign_TreeClick(widget, event, value, selected, button)
HideDropDownMenu(1)
local selectedId = selectedLastValue(value)
if selectedId < 0 then
if button == "RightButton" then
if not AngryAssign_DropDown then
AngryAssign_DropDown = CreateFrame("Frame", "AngryAssignMenuFrame", UIParent, "UIDropDownMenuTemplate")
end
EasyMenu(AngryAssign_CategoryMenu(-selectedId), AngryAssign_DropDown, "cursor", 0 , 0, "MENU")
else
local status = (widget.status or widget.localstatus).groups
status[value] = not status[value]
widget:RefreshTree()
end
return false
else
if button == "RightButton" then
if not AngryAssign_DropDown then
AngryAssign_DropDown = CreateFrame("Frame", "AngryAssignMenuFrame", UIParent, "UIDropDownMenuTemplate")
end
EasyMenu(AngryAssign_PageMenu(selectedId), AngryAssign_DropDown, "cursor", 0 , 0, "MENU")
return false
end
end
end
function AngryAssign:CreateWindow()
local window = AceGUI:Create("Frame")
window:SetTitle("Angry Assignments")
window:SetStatusText("")
window:SetLayout("Flow")
if AngryAssign:GetConfig('scale') then window.frame:SetScale( AngryAssign:GetConfig('scale') ) end
window:SetStatusTable(AngryAssign_State.window)
window:Hide()
AngryAssign.window = window
AngryAssign_Window = window.frame
if window.frame.SetResizeBounds then -- WoW 10.0
window.frame:SetResizeBounds(700, 400)
else
window.frame:SetMinResize(700, 400)
end
window.frame:SetFrameStrata("HIGH")
window.frame:SetFrameLevel(1)
window.frame:SetClampedToScreen(true)
tinsert(UISpecialFrames, "AngryAssign_Window")
local tree = AceGUI:Create("AngryTreeGroup")
tree:SetTree( self:GetTree() )
tree:SelectByValue(1)
tree:SetStatusTable(AngryAssign_State.tree)
tree:SetFullWidth(true)
tree:SetFullHeight(true)
tree:SetLayout("Flow")
tree:SetCallback("OnGroupSelected", function(widget, event, value) AngryAssign:UpdateSelected(true) end)
tree:SetCallback("OnClick", AngryAssign_TreeClick)
window:AddChild(tree)
window.tree = tree
local text = AceGUI:Create("MultiLineEditBox")
text:SetLabel(nil)
text:SetFullWidth(true)
text:SetFullHeight(true)
text:SetCallback("OnTextChanged", AngryAssign_TextChanged)
text:SetCallback("OnEnterPressed", AngryAssign_TextEntered)
tree:AddChild(text)
window.text = text
text.button:SetWidth(75)
local buttontext = text.button:GetFontString()
buttontext:ClearAllPoints()
buttontext:SetPoint("TOPLEFT", text.button, "TOPLEFT", 15, -1)
buttontext:SetPoint("BOTTOMRIGHT", text.button, "BOTTOMRIGHT", -15, 1)
tree:PauseLayout()
local button_display = AceGUI:Create("Button")
button_display:SetText("Send and Display")
button_display:SetWidth(140)
button_display:SetHeight(22)
button_display:ClearAllPoints()
button_display:SetPoint("BOTTOMRIGHT", text.frame, "BOTTOMRIGHT", 0, 4)
button_display:SetCallback("OnClick", AngryAssign_DisplayPage)
tree:AddChild(button_display)
window.button_display = button_display
local button_revert = AceGUI:Create("Button")
button_revert:SetText("Revert")
button_revert:SetWidth(80)
button_revert:SetHeight(22)
button_revert:ClearAllPoints()
button_revert:SetDisabled(true)
button_revert:SetPoint("BOTTOMLEFT", text.button, "BOTTOMRIGHT", 6, 0)
button_revert:SetCallback("OnClick", AngryAssign_RevertPage)
tree:AddChild(button_revert)
window.button_revert = button_revert
local button_restore = AceGUI:Create("Button")
button_restore:SetText("Restore")
button_restore:SetWidth(80)
button_restore:SetHeight(22)
button_restore:ClearAllPoints()
button_restore:SetPoint("LEFT", button_revert.frame, "RIGHT", 6, 0)
button_restore:SetCallback("OnClick", AngryAssign_RestorePage)
tree:AddChild(button_restore)
window.button_restore = button_restore
local button_output = AceGUI:Create("Button")
button_output:SetText("Output")
button_output:SetWidth(80)
button_output:SetHeight(22)
button_output:ClearAllPoints()
button_output:SetPoint("BOTTOMLEFT", button_restore.frame, "BOTTOMRIGHT", 6, 0)
button_output:SetCallback("OnClick", AngryAssign_OutputDisplayed)
tree:AddChild(button_output)
window.button_output = button_output
window:PauseLayout()
local button_add = AceGUI:Create("Button")
button_add:SetText("Add")
button_add:SetWidth(80)
button_add:SetHeight(19)
button_add:ClearAllPoints()
button_add:SetPoint("BOTTOMLEFT", window.frame, "BOTTOMLEFT", 17, 18)