-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibDBCompartment-1.0.lua
369 lines (277 loc) · 11.9 KB
/
LibDBCompartment-1.0.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
-- SPDX-License-Identifier: Unlicense
local LibDBCompartment = LibStub:NewLibrary("LibDBCompartment-1.0", 5);
if not LibDBCompartment then
return;
end
local LibDataBroker = LibStub:GetLibrary("LibDataBroker-1.1");
--
-- Button management functions
--
function LibDBCompartment:GetDataObject(name)
return self.objects[name];
end
function LibDBCompartment:GetDropDownButtonInfo(name)
return self.buttons[name];
end
function LibDBCompartment:GetNameByDataObject(dataObject)
return self.names[dataObject];
end
function LibDBCompartment:GetNameByDropDownButtonInfo(buttonInfo)
return self.names[buttonInfo];
end
function LibDBCompartment:IsRegistered(name)
return self:GetDropDownButtonInfo(name) ~= nil;
end
function LibDBCompartment:Register(name, dataObject)
assert(type(name) == "string", "bad argument #1 to 'Register': expected string");
assert(not self:IsRegistered(name), "bad argument #1 to 'Register': name is already registered");
assert(LibDataBroker:GetNameByDataObject(dataObject), "bad argument #2 to 'Register': invalid data object");
local buttonInfo = {};
self.buttons[name] = buttonInfo;
self.objects[name] = dataObject;
self.names[buttonInfo] = name;
self.names[dataObject] = name;
self:Refresh(name);
self:Show(name);
end
function LibDBCompartment:Refresh(name)
local buttonInfo = assert(self:GetDropDownButtonInfo(name), "bad argument #1 to 'Refresh': name not registered");
local dataObject = self:GetDataObject(name);
-- The following fields are dynamic and are always replaced when this
-- function is called based on object attributes, or if a refresh is
-- triggered through object attribute modifications.
buttonInfo.text = self:GetDataObjectLabel(dataObject);
buttonInfo.icon = self:GetDataObjectIcon(dataObject);
-- The following fields are only set if not already present. Callers may
-- obtain a reference to the info table via GetDropDownButtonInfo and
-- replace these if desired.
if not buttonInfo.func then
buttonInfo.func = function(...) self:OnDropDownButtonClick(dataObject, ...); end;
end
if not buttonInfo.funcOnEnter then
buttonInfo.funcOnEnter = function(...) self:OnDropDownButtonEnter(dataObject, ...); end;
end
if not buttonInfo.funcOnLeave then
buttonInfo.funcOnLeave = function(...) self:OnDropDownButtonLeave(dataObject, ...); end;
end
if buttonInfo.notCheckable == nil then
buttonInfo.notCheckable = true;
end
if buttonInfo.registerForAnyClick == nil then
buttonInfo.registerForAnyClick = true;
end
end
--
-- Button visibility functions
--
-- These functions will add or remove your registered compartment button from
-- the addon compartment dropdown. Unlike LibDBIcon, calling these functions
-- does *not* persist any state in saved variables.
--
function LibDBCompartment:Show(name)
assert(self:IsRegistered(name), "bad argument #1 to 'Show': name not registered");
self:SetShown(name, true);
end
function LibDBCompartment:Hide(name)
assert(self:IsRegistered(name), "bad argument #1 to 'Hide': name not registered");
self:SetShown(name, false);
end
function LibDBCompartment:IsShown(name)
if not AddonCompartmentFrame then
return false;
end
local buttonInfo = self:GetDropDownButtonInfo(name);
local index = tIndexOf(AddonCompartmentFrame.registeredAddons, buttonInfo);
return index ~= nil;
end
function LibDBCompartment:SetShown(name, shown)
local buttonInfo = assert(self:GetDropDownButtonInfo(name), "bad argument #1 to 'SetShown': name not registered");
if not AddonCompartmentFrame then
return;
end
local index = tIndexOf(AddonCompartmentFrame.registeredAddons, buttonInfo);
if shown and not index then
AddonCompartmentFrame:RegisterAddon(buttonInfo);
elseif not shown and index then
table.remove(AddonCompartmentFrame.registeredAddons, index);
AddonCompartmentFrame:UpdateDisplay();
end
end
--
-- Tooltip anchor functions
--
-- These functions are provided for any minimap addons that may be relocating
-- the addon compartment to a location that's incompatible with our internal
-- tooltip.
--
-- These work with Blizzards' AnchorMixin objects; see SharedXML\AnchorUtil.lua.
--
-- The special token "$button" can be set as the "relativeTo" frame to hint to
-- the library that a tooltip should be anchored to the dropdown button.
function LibDBCompartment:GetTooltipAnchor()
return self.tooltipAnchor;
end
function LibDBCompartment:SetTooltipAnchor(anchor)
assert(type(anchor) == "table", "bad argument #1 to 'SetTooltipAnchor': expected table");
assert(type(anchor.Get) == "function", "bad argument #1 to 'SetTooltipAnchor': invalid 'Get' field on anchor");
self.tooltipAnchor = anchor;
end
--
-- Internal functions
--
-- "Cor blimey mate, what are ye doing in me pockets?"
--
function LibDBCompartment:OnLoad()
if not self.buttons then
self.buttons = {}; -- { ["name"] = <uidropdownmenu button info>, ... }
end
if not self.objects then
self.objects = {}; -- { ["name"] = <ldb object>, ... }
end
if not self.names then
self.names = {}; -- { [<any>] = "name" };
end
if not self.tooltip then
self.tooltip = CreateFrame("GameTooltip", "LibDBCompartmentTooltip", UIParent, "GameTooltipTemplate");
end
if not self.tooltipAnchor then
self.tooltipAnchor = AnchorUtil.CreateAnchor("TOPRIGHT", "$button", "TOPLEFT", -20, 0);
end
LibDataBroker.RegisterCallback(self, "LibDataBroker_AttributeChanged__label", "OnDataObjectAttributeChanged");
LibDataBroker.RegisterCallback(self, "LibDataBroker_AttributeChanged__icon", "OnDataObjectAttributeChanged");
LibDataBroker.RegisterCallback(self, "LibDataBroker_AttributeChanged__tocname", "OnDataObjectAttributeChanged");
end
function LibDBCompartment:OnDataObjectAttributeChanged(_, _, _, _, dataObject)
local name = self:GetNameByDataObject(dataObject);
if name then
self:Refresh(name);
end
end
function LibDBCompartment:OnDropDownButtonClick(dataObject, button, menuInputData, _, _, mouseButtonName)
local OnClick = dataObject.OnAddonCompartmentClick or dataObject.OnClick;
if type(menuInputData) == "table" then
mouseButtonName = menuInputData.buttonName;
end
if OnClick then
OnClick(button, mouseButtonName);
end
end
function LibDBCompartment:OnDropDownButtonEnter(dataObject, button)
local OnEnter = dataObject.OnAddonCompartmentEnter or dataObject.OnEnter;
local OnTooltipShow = dataObject.OnAddonCompartmentTooltipShow or dataObject.OnTooltipShow;
if OnTooltipShow then
self.tooltip:SetOwner(button, "ANCHOR_NONE");
self.tooltip:ClearAllPoints();
self.tooltip:SetPoint(self:GetTooltipAnchorPoint(button));
OnTooltipShow(self.tooltip);
self.tooltip:Show();
elseif OnEnter then
OnEnter(button);
end
end
function LibDBCompartment:OnDropDownButtonLeave(dataObject, button)
local OnLeave = dataObject.OnAddonCompartmentLeave or dataObject.OnLeave;
self.tooltip:Hide();
if OnLeave then
OnLeave(button);
end
end
function LibDBCompartment:GetDataObjectLabel(dataObject)
local GetAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata;
local label;
if type(dataObject.label) == "string" then
label = dataObject.label;
elseif type(dataObject.tocname) == "string" then
label = GetAddOnMetadata(dataObject.tocname, "Title");
end
-- Two fallbacks; use the data object name first, and if that is for
-- whatever reason not a string use whatever name the caller registered
-- into us with. This is to ensure that one bad entry doesn't brick the
-- sort comparator in patch 10.1.
if type(label) ~= "string" then
label = LibDataBroker:GetNameByDataObject(dataObject);
end
if type(label) ~= "string" then
label = self.names[dataObject];
end
return label;
end
function LibDBCompartment:GetDataObjectIcon(dataObject)
local GetAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata;
local icon;
if dataObject.icon then
icon = dataObject.icon;
elseif type(dataObject.tocname) == "string" then
local tocname = dataObject.tocname;
icon = GetAddOnMetadata(tocname, "IconTexture") or GetAddOnMetadata(tocname, "IconAtlas");
end
return icon;
end
function LibDBCompartment:GetTooltipAnchorPoint(button)
local point, relativeTo, relativePoint, offsetX, offsetY = self.tooltipAnchor:Get();
if relativeTo == "$button" then
relativeTo = button;
end
return point, relativeTo, relativePoint, offsetX, offsetY;
end
LibDBCompartment:OnLoad();
--@do-not-package@
local function LibDBCompartment_SetupTestObjects()
-- Test with a manuallly set label, icon, and basic tooltip.
local TestObject1 = LibDataBroker:NewDataObject("LibDBCompartmentTest1");
TestObject1.label = "LibDBCompartment: Basic Test";
TestObject1.icon = [[Interface\Icons\Ambush]];
TestObject1.OnClick = print;
TestObject1.OnTooltipShow = function(tooltip)
tooltip:SetText("Hello!");
end;
LibDBCompartment:Register("LibDBCompartmentTest1", TestObject1);
-- Test with a complex tooltip and TOC-based name/icon inference.
local TestObject2 = LibDataBroker:NewDataObject("LibDBCompartmentTest2");
TestObject2.tocname = "LibDBCompartment";
TestObject2.OnEnter = function(button)
GameTooltip:SetOwner(button, "ANCHOR_BOTTOMLEFT");
GameTooltip:SetText("Hello!");
GameTooltip:Show();
end;
TestObject2.OnLeave = function(_)
GameTooltip:Hide();
end;
LibDBCompartment:Register("LibDBCompartmentTest2", TestObject2);
-- Test with a blank object; this should show no icon and default to
-- the name of the data object.
local TestObject3 = LibDataBroker:NewDataObject("LibDBCompartment: Blank Object Test");
LibDBCompartment:Register("LibDBCompartmentTest3", TestObject3);
-- Visibility toggle test; selecting this will toggle visiblity of the
-- previous object.
local TestObject4 = LibDataBroker:NewDataObject("LibDBCompartmentTest4");
TestObject4.label = "LibDBCompartment: Visibility Toggle Test";
TestObject4.OnClick = function()
LibDBCompartment:SetShown("LibDBCompartmentTest3", not LibDBCompartment:IsShown("LibDBCompartmentTest3"));
end;
LibDBCompartment:Register("LibDBCompartmentTest4", TestObject4);
-- Tooltip re-anchor test; selecting this will make built-in tooltips
-- anchor to the center of the screen.
local TestObject5 = LibDataBroker:NewDataObject("LibDBCompartmentTest5");
TestObject5.label = "LibDBCompartment: Tooltip Anchor Test";
TestObject5.OnClick = function()
LibDBCompartment:SetTooltipAnchor(AnchorUtil.CreateAnchor("CENTER", UIParent, "CENTER", 0, 0));
end;
LibDBCompartment:Register("LibDBCompartmentTest5", TestObject5);
-- Function override test; prefer "OnAddonCompartment<X>" function flavors
-- where provided.
local TestObject6 = LibDataBroker:NewDataObject("LibDBCompartmentTest6");
TestObject6.label = "LibDBCompartment: Function Override Test";
TestObject6.OnAddonCompartmentTooltipShow = function() print("Called OnAddonCompartmentTooltipShow"); end;
TestObject6.OnAddonCompartmentLeave = function() print("Called OnAddonCompartmentLeave"); end;
TestObject6.OnAddonCompartmentClick = function() print("Called OnAddonCompartmentClick"); end;
TestObject6.OnTooltipShow = function() error("Called OnTooltipShow"); end;
TestObject6.OnLeave = function() error("Called OnLeave"); end;
TestObject6.OnClick = function() error("Called OnClick"); end;
LibDBCompartment:Register("LibDBCompartmentTest6", TestObject6);
end
if (...) == "LibDBCompartment" then
SLASH_LDBC1 = "/ldbc";
SlashCmdList["LDBC"] = LibDBCompartment_SetupTestObjects;
end
--@end-do-not-package@