-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlyoutDef.lua
More file actions
executable file
·307 lines (260 loc) · 10.2 KB
/
FlyoutDef.lua
File metadata and controls
executable file
·307 lines (260 loc) · 10.2 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
-- FlyoutDef
-- a flyout menu definition
-- data for a single flyout object, its spells/pets/macros/items/etc. and methods for manipulating that data
-- is currently a collection if parallel lists, each containing one param for each button in the menu
-- instead, should be one collection/list of button objects, each containing all params for each button. ENCAPSULATION FTW!
-------------------------------------------------------------------------------
-- Module Loading
-------------------------------------------------------------------------------
---@type Ufo
local ADDON_NAME, Ufo = ...
Ufo.Wormhole() -- Lua voodoo magic that replaces the current Global namespace with the Ufo object
local zebug = Zebug:new(Z_VOLUME_GLOBAL_OVERRIDE or Zebug.INFO)
---@class FlyoutDef : UfoMixIn
---@field id string A unique, immutable, permanent identifier. This is not it's index in any array.
---@field name string
---@field ufoType string
---@field icon string user chosen icon
---@field fallbackIcon string in absence of the icon field, this is used when the Bliz UI fails to load all the necessary icons on login (usually pets and toys)
---@field btns table
---@field lastMod number timestamp of the last significant modification
FlyoutDef = {
ufoType = "FlyoutDef",
}
UfoMixIn:mixInto(FlyoutDef)
-------------------------------------------------------------------------------
-- Methods
-------------------------------------------------------------------------------
-- coerce the incoming table into a FlyoutDef instance
---@return FlyoutDef
---@param self FlyoutDef
function FlyoutDef:oneOfUs(self)
zebug.trace:setMethodName("oneOfUs"):print("self",self)
if self.ufoType == FlyoutDef.ufoType then
return self
end
-- create a table to store stuff that we do NOT want persisted out to SAVED_VARIABLES
-- and attach methods to get and put that data
local privateCache = {
alreadyCoercedMyButtons = false,
}
function privateCache:setAlreadyCoercedMyButtons() privateCache.alreadyCoercedMyButtons = true end
function privateCache:cacheUsableFlyoutDef(usableFlyoutDef) privateCache.cachedUsableFlyoutDef = usableFlyoutDef end
function privateCache:cacheHasItem(hasIt) privateCache._hasItem = hasIt end
function privateCache:cacheHasMacro(hasIt) privateCache._hasMacro = hasIt end
function privateCache:cacheHasSpell(hasIt) privateCache._hasSpell = hasIt end
-- tie the privateData table to the FlyoutDef class definition
setmetatable(privateCache, { __index = FlyoutDef })
-- tie the "self" instance to the privateData table (which in turn is tied to the class)
setmetatable(self, { __index = privateCache })
self:installMyToString()
-- on any modification always update the lastMod
-- well, this causes the initial load to silently fail and UFO does nothing until a reload
-- maybe because no defs get loaded SAVED_VARIABLES ?
self:setModStamp()
return self
end
local flyoutIndex = 1
---@return FlyoutDef
function FlyoutDef:new()
local self = { btns = {} } -- I tried putting self.btns = {} into oneOfUs() but then they failed to persist out to SAVED_VARIABLES :-/
self.name = flyoutIndex
flyoutIndex = flyoutIndex + 1
return FlyoutDef:oneOfUs(self)
end
function FlyoutDef:toString()
if self == Cursor then
return "nil"
else
local icon = self:getIcon() or self.fallbackIcon or DEFAULT_ICON_FULL
return string.format("<FlyDef: |T%s:0|t %s size=%d>", icon, nilStr(self.name), nilStr(self.btns and #(self.btns) or 0))
end
end
---@return string
function FlyoutDef:getName()
return self.name
end
function FlyoutDef:setModStamp()
self.lastMod = time()
end
function FlyoutDef:getModStamp()
return self.lastMod
end
-- UNUSED :-(
---@param time number a time ( as returned by a call to the time() function )
---@return boolean true if self has been modified more recently than the given time
function FlyoutDef:isModNewerThan(time)
return (time or 0) >= self:getModStamp()
end
function FlyoutDef:invalidateCache()
self:setModStamp()
self:cacheUsableFlyoutDef(nil)
self:cacheHasItem(nil)
self:cacheHasMacro(nil)
self:cacheHasSpell(nil)
end
function FlyoutDef:invalidateCacheOfUsableFlyoutDefOnly()
self:cacheUsableFlyoutDef(nil)
end
function FlyoutDef:newId()
return DB:nextN() ..":".. getIdForCurrentToon() ..":".. (time()-1687736964)
end
function FlyoutDef:howManyButtons()
return #self.btns
end
---@param callback function if the function returns true, then, that means it did something that requires the caches to be nuked
function FlyoutDef:forEachBtn(callback)
local i = Xedni:getFlyoutDef(self.id)
zebug.trace:owner(self):print("i", i, "self.btns",self.btns)
assert(self.btns, "This instance of FlyoutDef has no 'btns' field.")
self:ensureCoerced()
local invalidateCaches = false
---@param buttonDef ButtonDef
for j, buttonDef in ipairs(self.btns) do -- this must remain self.btns and NOT self:getAllButtonDefs() - otherwise infinite loop
zebug.trace:owner(self):print("i", i, "btn #", j, "buttonDef.name", buttonDef.name)
local killCache = callback(buttonDef, buttonDef, j, self) -- support both functions and methods (which expects 1st arg as self and 2nd arg as the actual arg)
if killCache then
zebug.trace:owner(self):print(i, "btn #", j, "buttonDef.name", buttonDef.name, "sent signal to INVALIDATE Caches")
buttonDef:invalidateCache()
invalidateCaches = true
end
end
if invalidateCaches then
self:invalidateCache()
end
end
---@param killTester function
function FlyoutDef:batchDeleteBtns(killTester)
local btns = self:getAllButtonDefs()
local modified = deleteFromArray(btns, killTester)
if modified then
self:invalidateCache()
end
end
function FlyoutDef:getAllButtonDefs()
zebug.trace:owner(self):print("self.alreadyCoercedMyButtons",self.alreadyCoercedMyButtons)
self:ensureCoerced()
return self.btns
end
function FlyoutDef:ensureCoerced()
if not self.alreadyCoercedMyButtons then
for i, buttonDef in ipairs(self.btns) do
ButtonDef:oneOfUs(buttonDef)
end
self:setAlreadyCoercedMyButtons()
end
end
---@return ButtonDef
function FlyoutDef:getButtonDef(i)
return self:getAllButtonDefs()[tonumber(i)]
end
---@param buttonDef ButtonDef
function FlyoutDef:addButton(buttonDef)
table.insert(self.btns, buttonDef)
self:invalidateCache()
end
---@param i number
---@param buttonDef ButtonDef
function FlyoutDef:replaceButton(i, buttonDef)
self.btns[tonumber(i)] = buttonDef
self:invalidateCache()
end
---@param i number
---@param buttonDef ButtonDef
function FlyoutDef:insertButton(index, buttonDef)
index = tonumber(index)
local n = index
local existingBtnDef = buttonDef
for i = index, #self.btns do
existingBtnDef = self.btns[i]
self.btns[i] = buttonDef
buttonDef = existingBtnDef
n = i+1
end
--if n <= MAX_FLYOUT_SIZE then
-- eh, let the btns's exist virtually even if there aren't enough UI btns to display them all
self.btns[n] = existingBtnDef
--end
self:invalidateCache()
end
-- removes a button an moves the rest down a notch
function FlyoutDef:removeButton(removeAtIndex)
local howManyButtons = self:howManyButtons()
self:forEachBtn(function(buttonDef, buttonDef, i)
if i >= removeAtIndex and i <= howManyButtons then
local nextBtnDef = self:getButtonDef(i + 1) -- lua is OK with index out of range
self:replaceButton(i, nextBtnDef)
end
end)
self:invalidateCache()
end
function FlyoutDef:getIcon(n)
if not n or n==1 then
-- do we have a defined, static icon?
if self.icon then
return self.icon
end
end
local btn = self:getButtonDef(n or 1)
if btn then
local icon = btn:getIcon()
if icon then
-- compensate for the Bliz UI bug where not all icons have been loaded at the moment of login
self.fallbackIcon = icon
end
return icon
end
return nil
end
---@return FlyoutDef
function FlyoutDef:filterOutUnusable()
if self.cachedUsableFlyoutDef then
zebug.trace:owner(self):print("returning cached usableFlyoutDef")
return self.cachedUsableFlyoutDef
end
zebug.trace:owner(self):print("filtering...")
local usableFlyoutDef = FlyoutDef:new()
---@param btn ButtonDef
for i, btn in ipairs(self:getAllButtonDefs()) do
if btn:isUsable() then
zebug.trace:owner(self):print("can use", btn:getName())
usableFlyoutDef:addButton(btn)
else
zebug.info:owner(self):print("CANNOT use", btn:getName())
end
end
usableFlyoutDef.name = self.name
usableFlyoutDef.icon = self.icon
usableFlyoutDef.fallbackIcon = self.fallbackIcon
usableFlyoutDef.setAlreadyCoercedMyButtons() -- because the source btns have already been coerced
self:cacheUsableFlyoutDef(usableFlyoutDef)
return usableFlyoutDef
end
function FlyoutDef:hasItem(event)
return self:hasFoo("_hasItem", self.cacheHasItem, ButtonType.ITEM, event)
end
function FlyoutDef:hasMacro(event)
return self:hasFoo("_hasMacro", self.cacheHasMacro, ButtonType.MACRO, event)
end
function FlyoutDef:hasSpell(event)
return self:hasFoo("_hasSpell", self.cacheHasSpell, ButtonType.SPELL, event)
end
function FlyoutDef:hasFoo(key, cacher, type, event)
zebug.trace:mark(Mark.FIRE):event(event):owner(self):print("key",key, "exists", self[key], "type", type)
if self[key] == nil then
cacher(self, false)
zebug.info:mark(Mark.INFO):event(event):owner(self):print("indexing for", key)
---@param btnDef ButtonDef
self:forEachBtn(function(btnDef)
if btnDef.type == type then
cacher(self, true)
zebug.info:event(event):owner(self):print(key, self[key], "btn", btnDef.name)
else
zebug.trace:event(event):owner(self):print("the",btnDef.name, "is not a",key )
end
end)
else
zebug.trace:event(event):owner(self):print("using cached",key, "with a val of", self[key])
end
return self[key]
end