-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuildNoteDisplay.lua
More file actions
416 lines (357 loc) · 13.4 KB
/
GuildNoteDisplay.lua
File metadata and controls
416 lines (357 loc) · 13.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
-- Initialisation
local appName, app = ... -- App name and app table
app.L = app.L or {} -- Localisation table
local L = app.L; -- Localisation table
app.api = {} -- Api table for our app
GuildNoteDisplay = app.api -- Api namespace
local api = app.api -- Api prefix for easier access
app.Name = "Guild Note Display" -- Do not localize
-- Event registration
local event = CreateFrame("Frame")
event:SetScript("OnEvent", function(self, event, ...)
if self[event] then
self[event](self, ...)
end
end)
event:RegisterEvent("ADDON_LOADED")
event:RegisterEvent("CHAT_MSG_SYSTEM")
-- Initial load
function app.Initialise()
-- Declare SavedVariables
if not GuildNoteDisplayDB then GuildNoteDisplayDB = {} end
-- Enable default user settings
if GuildNoteDisplayDB["colour_guild_note"] == nil then
GuildNoteDisplayDB["colour_guild_note"] = true
end
if GuildNoteDisplayDB["note_colour_table"] == nil then
GuildNoteDisplayDB["note_colour_table"] = {
r=1,
g=1,
b=1,
}
end
if GuildNoteDisplayDB["note_in_author_field"] == nil then
GuildNoteDisplayDB["note_in_author_field"] = false
end
if GuildNoteDisplayDB["normalise_special_characters"] == nil then
GuildNoteDisplayDB["normalise_special_characters"] = false
end
if GuildNoteDisplayDB["use_officer_note"] == nil then
GuildNoteDisplayDB["use_officer_note"] = false
end
if GuildNoteDisplayDB["display_party_raid_chat"] == nil then
GuildNoteDisplayDB["display_party_raid_chat"] = true
end
if GuildNoteDisplayDB["hide"] == nil then
GuildNoteDisplayDB["hide"] = false
end
end
-- Minimap icon
function app.MinimapIcon()
local LDB = LibStub("LibDataBroker-1.1")
local icon = LDB:NewDataObject(appName, {
type = "data source",
text = app.Name,
icon = "Interface\\AddOns\\GuildNoteDisplay\\GuildNoteDisplay.blp",
OnClick = function()
app.OpenSettings()
end,
})
app.MinimapIcon = LibStub("LibDBIcon-1.0")
app.MinimapIcon:Register(appName, icon, GuildNoteDisplayDB)
end
-- Addon is loaded
function event:ADDON_LOADED(addOnName, containsBindings)
if addOnName == appName then
app.Initialise()
app.Settings()
app.MinimapIcon()
app.AddMessageEventFilters()
end
end
-- Color picker
function app.ShowColorPicker(configTable)
local r, g, b, a = GuildNoteDisplayDB.note_colour_table.r, GuildNoteDisplayDB.note_colour_table.g, GuildNoteDisplayDB.note_colour_table.b, GuildNoteDisplayDB.note_colour_table.a;
local function OnColorChanged()
local newR, newG, newB = ColorPickerFrame:GetColorRGB();
local newA = ColorPickerFrame:GetColorAlpha();
GuildNoteDisplayDB.note_colour_table.r, GuildNoteDisplayDB.note_colour_table.g, GuildNoteDisplayDB.note_colour_table.b, GuildNoteDisplayDB.note_colour_table.a = newR, newG, newB, newA;
end
local function OnCancel()
GuildNoteDisplayDB.note_colour_table.r, GuildNoteDisplayDB.note_colour_table.g, GuildNoteDisplayDB.note_colour_table.b, GuildNoteDisplayDB.note_colour_table.a = r, g, b, a;
end
local options = {
swatchFunc = OnColorChanged,
opacityFunc = OnColorChanged,
cancelFunc = OnCancel,
hasOpacity = a ~= nil,
opacity = a,
r = r,
g = g,
b = b,
};
ColorPickerFrame:SetupColorPickerAndShow(options);
end
-- Open settings
function app.OpenSettings()
Settings.OpenToCategory(app.Category:GetID())
end
-- AddOn Compartment Click
function GuildNoteDisplay_Click(self, button)
app.OpenSettings()
end
-- Slash command to open settings
SLASH_GUILDNOTEDISPLAY1 = "/gnd";
SLASH_GUILDNOTEDISPLAY2 = "/guildnotedisplay";
SlashCmdList["GUILDNOTEDISPLAY"] = function(msg)
app.OpenSettings()
end
-- Normalize special characters
function app.NormalizeSpecialCharacters(str)
local substitutes = {}
substitutes["À"] = "A"
substitutes["Á"] = "A"
substitutes["Â"] = "A"
substitutes["Ã"] = "A"
substitutes["Ä"] = "A"
substitutes["Å"] = "A"
substitutes["Æ"] = "AE"
substitutes["Ç"] = "C"
substitutes["È"] = "E"
substitutes["É"] = "E"
substitutes["Ê"] = "E"
substitutes["Ë"] = "E"
substitutes["Ì"] = "I"
substitutes["Í"] = "I"
substitutes["Î"] = "I"
substitutes["Ï"] = "I"
substitutes["Ð"] = "D"
substitutes["Ñ"] = "N"
substitutes["Ò"] = "O"
substitutes["Ó"] = "O"
substitutes["Ô"] = "O"
substitutes["Õ"] = "O"
substitutes["Ö"] = "O"
substitutes["Ø"] = "O"
substitutes["Ù"] = "U"
substitutes["Ú"] = "U"
substitutes["Û"] = "U"
substitutes["Ü"] = "U"
substitutes["Ý"] = "Y"
substitutes["Þ"] = "P"
substitutes["ß"] = "s"
substitutes["à"] = "a"
substitutes["á"] = "a"
substitutes["â"] = "a"
substitutes["ã"] = "a"
substitutes["ä"] = "a"
substitutes["å"] = "a"
substitutes["æ"] = "ae"
substitutes["ç"] = "c"
substitutes["è"] = "e"
substitutes["é"] = "e"
substitutes["ê"] = "e"
substitutes["ë"] = "e"
substitutes["ì"] = "i"
substitutes["í"] = "i"
substitutes["î"] = "i"
substitutes["ï"] = "i"
substitutes["ð"] = "eth"
substitutes["ñ"] = "n"
substitutes["ò"] = "o"
substitutes["ó"] = "o"
substitutes["ô"] = "o"
substitutes["õ"] = "o"
substitutes["ö"] = "o"
substitutes["ø"] = "o"
substitutes["ù"] = "u"
substitutes["ú"] = "u"
substitutes["û"] = "u"
substitutes["ü"] = "u"
substitutes["ý"] = "y"
substitutes["þ"] = "p"
substitutes["ÿ"] = "y"
local normalisedString = ''
local normalisedString = str: gsub("[%z\1-\127\194-\244][\128-\191]*", substitutes)
return normalisedString
end
-- Normalises the guild note and compares it to the short name
function app.NormalizeAndCompare(guildNote, shortName)
if GuildNoteDisplayDB["normalise_special_characters"] then
guildNote = app.NormalizeSpecialCharacters(guildNote)
shortName = app.NormalizeSpecialCharacters(shortName)
end
return guildNote, shortName, guildNote and string.lower(shortName) ~= string.lower(guildNote)
end
-- Gets the guild note for a player by looping over the number of guild members
function app.FindGuildNoteForPlayer(nameWithRealm, useOfficerNote)
for i = 1, GetNumGuildMembers() do
local name, _, _, _, _, _, publicNote, officerNote = GetGuildRosterInfo(i)
if name == nameWithRealm and not useOfficerNote then
return publicNote
elseif name == nameWithRealm and useOfficerNote then
return officerNote
end
end
end
-- Formats the message with the guild note
function app.FormatMessageWithGuildNote(msg, author, guildNote)
local textColor = CreateColor(GuildNoteDisplayDB.note_colour_table.r, GuildNoteDisplayDB.note_colour_table.g, GuildNoteDisplayDB.note_colour_table.b, GuildNoteDisplayDB.note_colour_table.a)
if not GuildNoteDisplayDB["note_in_author_field"] then
if GuildNoteDisplayDB["colour_guild_note"] then
msg = "|c" .. textColor:GenerateHexColor() .. "(" .. guildNote .. ")|r " .. msg
else
msg = "(" .. guildNote .. ") " .. msg
end
else
author = Ambiguate(author, "guild") .. " (" .. guildNote .. ")"
end
return msg, author
end
-- Adds the guild note to the guild chat
function app.AddGuildNoteToGuildChat(self, event, msg, author, ...)
if event == "CHAT_MSG_GUILD" or event == "CHAT_MSG_OFFICER" then
local guildNote = app.FindGuildNoteForPlayer(author, GuildNoteDisplayDB["use_officer_note"])
local shortName = Ambiguate(author, "short")
local guildNoteForCompare, shortNameForCompare, isDifferent = app.NormalizeAndCompare(guildNote, shortName)
if guildNote and guildNote ~= "" and isDifferent then
msg, author = app.FormatMessageWithGuildNote(msg, author, guildNote)
end
return false, msg, author, ...
end
end
-- Adds the guild note to the party and raid chat
function app.AddGuildNoteToPartyRaidChat(self, event, msg, author, ...)
if GuildNoteDisplayDB["display_party_raid_chat"] and (event == "CHAT_MSG_PARTY" or event == "CHAT_MSG_PARTY_LEADER" or event == "CHAT_MSG_RAID" or event == "CHAT_MSG_RAID_LEADER") then
local guildNote = app.FindGuildNoteForPlayer(author, GuildNoteDisplayDB["use_officer_note"])
local shortName = Ambiguate(author, "short")
local guildNoteForCompare, shortNameForCompare, isDifferent = app.NormalizeAndCompare(guildNote, shortName)
if guildNote and guildNote ~= "" and isDifferent then
msg, author = app.FormatMessageWithGuildNote(msg, author, guildNote)
end
return false, msg, author, ...
end
end
-- Adds the guild note to the login and logout message
function app.AddGuildNoteToLoginLogoutMessage(self, event, msg, ...)
if event == "CHAT_MSG_SYSTEM" then
-- Get the name from the login or logout message
local subbedMessage = ERR_FRIEND_ONLINE_SS:gsub("%%s", "(%.+)"):gsub("%[", "%%["):gsub("%]","%%]")
local author = string.match(msg, subbedMessage)
if author == nil then
subbedMessage = ERR_FRIEND_OFFLINE_S:gsub("%%s", "(%.+)"):gsub("%[", "%%["):gsub("%]","%%]")
author = string.match(msg, subbedMessage)
end
if author ~= nil then
-- Author needs the realm name added to it
if not strfind(author, "-") then
author = author .. "-" .. GetNormalizedRealmName()
end
local guildNote = app.FindGuildNoteForPlayer(author, GuildNoteDisplayDB["use_officer_note"])
local shortName = Ambiguate(author, "short")
local guildNoteForCompare, shortNameForCompare, isDifferent = app.NormalizeAndCompare(guildNote, shortName)
if guildNote and guildNote ~= "" and isDifferent then
msg = app.FormatMessageWithGuildNote(msg, author, guildNote)
end
return false, msg, ...
end
end
end
-- Add message event filters
function app.AddMessageEventFilters()
-- Add the event filter for the guild chat messages
ChatFrame_AddMessageEventFilter("CHAT_MSG_GUILD", app.AddGuildNoteToGuildChat)
ChatFrame_AddMessageEventFilter("CHAT_MSG_OFFICER", app.AddGuildNoteToGuildChat)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", app.AddGuildNoteToLoginLogoutMessage)
if GuildNoteDisplayDB["display_party_raid_chat"] ~= nil and GuildNoteDisplayDB["display_party_raid_chat"] then
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY", app.AddGuildNoteToPartyRaidChat)
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY_LEADER", app.AddGuildNoteToPartyRaidChat)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID", app.AddGuildNoteToPartyRaidChat)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID_LEADER", app.AddGuildNoteToPartyRaidChat)
end
end
-- Settings
function app.Settings()
-- Settings page
local category, layout = Settings.RegisterVerticalLayoutCategory(app.Name)
Settings.RegisterAddOnCategory(category)
app.Category = category
layout:AddInitializer(CreateSettingsListSectionHeaderInitializer(C_AddOns.GetAddOnMetadata(appName, "Version")))
local CreateCheckbox = Settings.CreateCheckbox or Settings.CreateCheckBox
local function OnSettingChanged(_, setting, value)
local variable = setting:GetVariable()
if strsub(variable, 1, 17) == "GuildNoteDisplay_" then
variable = strsub(variable, 17);
end
end
local function RegisterSetting(variableKey, defaultValue, name)
local uniqueVariable = "GuildNoteDisplay_" .. variableKey;
local setting;
setting = Settings.RegisterAddOnSetting(category, uniqueVariable, variableKey, GuildNoteDisplayDB, type(defaultValue), name, defaultValue);
setting:SetValue(GuildNoteDisplayDB[variableKey]);
Settings.SetOnValueChangedCallback(uniqueVariable, OnSettingChanged);
return setting;
end
do -- checkbox
local variable = "colour_guild_note"
local name = L["Colour guild note"]
local tooltip = L["Colour guild note tooltip"]
local defaultValue = true
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
end
do -- color picker
local function OnButtonClick()
app.ShowColorPicker(GuildNoteDisplayDB.note_colour_table)
end
local initializer = CreateSettingsButtonInitializer(L["Guild note colour"], L["Click to pick a colour"], OnButtonClick, nil, true);
layout:AddInitializer(initializer)
end
do -- checkbox
local variable = "note_in_author_field"
local name = L["Note in name field"]
local tooltip = L["Note in name field tooltip"]
local defaultValue = false
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
end
do -- checkbox
local variable = "normalise_special_characters"
local name = L["Normalise special characters"]
local tooltip = L["Normalise special characters tooltip"]
local defaultValue = false
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
end
do -- checkbox
local variable = "use_officer_note"
local name = L["Use the officer note"]
local tooltip = L["Use the officer note tooltip"]
local defaultValue = false
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
end
do -- checkbox
local variable = "display_party_raid_chat"
local name = L["Display in party and raid chat"]
local tooltip = L["Display in party and raid chat tooltip"]
local defaultValue = true
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
end
do -- checkbox
local variable = "hide"
local name = L["Hide minimap icon"]
local tooltip = L["Hide minimap icon tooltip"]
local defaultValue = false
local setting = RegisterSetting(variable, defaultValue, name)
CreateCheckbox(category, setting, tooltip)
setting:SetValueChangedCallback(function()
if GuildNoteDisplayDB["hide"] == false then
app.MinimapIcon:Show(appName)
else
app.MinimapIcon:Hide(appName)
end
end)
end
end