forked from fxpw/Immersion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPools.lua
More file actions
357 lines (290 loc) · 9.8 KB
/
Copy pathPools.lua
File metadata and controls
357 lines (290 loc) · 9.8 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
local ObjectPoolMixin, _, L = {}, ...
local Mixin, CreateFromMixins = L.MixinNormal, L.CreateFromMixins
----------------------------------
-- Compat
----------------------------------
-- function Mixin(object, ...)
-- local mixins = {...};
-- for _,mixin in pairs(mixins) do
-- for k,v in next,mixin do
-- object[k] = v;
-- end
-- end
-- return object;
-- end
-- function CreateFromMixins(...)
-- return Mixin({}, ...)
-- end
function ObjectPoolMixin:OnLoad(creationFunc, resetterFunc)
self.creationFunc = creationFunc
self.resetterFunc = resetterFunc
self.activeObjects = {}
self.inactiveObjects = {}
self.numActiveObjects = 0
end
function ObjectPoolMixin:Acquire()
local numInactiveObjects = #self.inactiveObjects
if numInactiveObjects > 0 then
local obj = self.inactiveObjects[numInactiveObjects]
self.activeObjects[obj] = true
self.numActiveObjects = self.numActiveObjects + 1
self.inactiveObjects[numInactiveObjects] = nil
return obj, false
end
local newObj = self.creationFunc(self);
if self.resetterFunc and not self.disallowResetIfNew then
self.resetterFunc(self, newObj);
end
self.activeObjects[newObj] = true
self.numActiveObjects = self.numActiveObjects + 1
return newObj, true
end
function ObjectPoolMixin:Release(obj)
if self:IsActive(obj) then
self.inactiveObjects[#self.inactiveObjects + 1] = obj
self.activeObjects[obj] = nil
self.numActiveObjects = self.numActiveObjects - 1
if self.resetterFunc then
self.resetterFunc(self, obj)
end
return true
end
return false
end
function ObjectPoolMixin:ReleaseAll()
for obj in pairs(self.activeObjects) do
self:Release(obj)
end
end
function ObjectPoolMixin:SetResetDisallowedIfNew(disallowed)
self.disallowResetIfNew = disallowed;
end
function ObjectPoolMixin:EnumerateActive()
return pairs(self.activeObjects)
end
function ObjectPoolMixin:GetNextActive(current)
return (next(self.activeObjects, current))
end
function ObjectPoolMixin:IsActive(object)
return (self.activeObjects[object] ~= nil)
end
function ObjectPoolMixin:GetNumActive()
return self.numActiveObjects
end
function ObjectPoolMixin:EnumerateInactive()
return ipairs(self.inactiveObjects)
end
function L.CreateObjectPool(creationFunc, resetterFunc)
local objectPool = CreateFromMixins(ObjectPoolMixin)
objectPool:OnLoad(creationFunc, resetterFunc)
return objectPool
end
local FramePoolMixin = CreateFromMixins(ObjectPoolMixin)
local function FramePoolFactory(framePool)
local parentName, frameName = framePool.parent and framePool.parent:GetName()
if parentName then
framePool.createFrames = framePool.createFrames + 1
frameName = string.format("%sPoolFrame%s%d", parentName, framePool.frameTemplate, framePool.createFrames)
else
frameName = string.format("FramePoolFrame_%s_%s", framePool:GetNumActive(), time())
end
return CreateFrame(framePool.frameType, frameName, framePool.parent, framePool.frameTemplate)
end
local function ForbiddenFramePoolFactory(framePool)
return print(framePool.frameType, string.format("FramePoolFrame_%s_%s", framePool:GetNumActive(), time()), framePool.parent, framePool.frameTemplate)
end
function FramePoolMixin:OnLoad(frameType, parent, frameTemplate, resetterFunc, forbidden)
if forbidden then
ObjectPoolMixin.OnLoad(self, ForbiddenFramePoolFactory, resetterFunc)
else
ObjectPoolMixin.OnLoad(self, FramePoolFactory, resetterFunc)
end
self.createFrames = 0
self.frameType = frameType
self.parent = parent
self.frameTemplate = frameTemplate
end
function FramePoolMixin:GetTemplate()
return self.frameTemplate
end
local function FramePool_Hide(framePool, frame)
frame:Hide()
end
function FramePool_HideAndClearAnchors(framePool, frame)
frame:Hide()
frame:ClearAllPoints()
end
function L.CreateFramePool(frameType, parent, frameTemplate, resetterFunc, forbidden)
local framePool = CreateFromMixins(FramePoolMixin)
framePool:OnLoad(frameType, parent, frameTemplate, resetterFunc or FramePool_HideAndClearAnchors, forbidden)
return framePool
end
local TexturePoolMixin = CreateFromMixins(ObjectPoolMixin)
local function TexturePoolFactory(texturePool)
return texturePool.parent:CreateTexture(nil, texturePool.layer, texturePool.textureTemplate, texturePool.subLayer)
end
function TexturePoolMixin:OnLoad(parent, layer, subLayer, textureTemplate, resetterFunc)
ObjectPoolMixin.OnLoad(self, TexturePoolFactory, resetterFunc)
self.parent = parent
self.layer = layer
self.subLayer = subLayer
self.textureTemplate = textureTemplate
end
TexturePool_Hide = FramePool_Hide
local TexturePool_HideAndClearAnchors = FramePool_HideAndClearAnchors
function CreateTexturePool(parent, layer, subLayer, textureTemplate, resetterFunc)
local texturePool = CreateFromMixins(TexturePoolMixin)
texturePool:OnLoad(parent, layer, subLayer, textureTemplate, resetterFunc or TexturePool_HideAndClearAnchors)
return texturePool
end
local FontStringPoolMixin = CreateFromMixins(ObjectPoolMixin)
local function FontStringPoolFactory(fontStringPool)
return fontStringPool.parent:CreateFontString(nil, fontStringPool.layer, fontStringPool.fontStringTemplate, fontStringPool.subLayer)
end
function FontStringPoolMixin:OnLoad(parent, layer, subLayer, fontStringTemplate, resetterFunc)
ObjectPoolMixin.OnLoad(self, FontStringPoolFactory, resetterFunc)
self.parent = parent
self.layer = layer
self.subLayer = subLayer
self.fontStringTemplate = fontStringTemplate
end
FontStringPool_Hide = FramePool_Hide
FontStringPool_HideAndClearAnchors = FramePool_HideAndClearAnchors
function L.CreateFontStringPool(parent, layer, subLayer, fontStringTemplate, resetterFunc)
local fontStringPool = CreateFromMixins(FontStringPoolMixin)
fontStringPool:OnLoad(parent, layer, subLayer, fontStringTemplate, resetterFunc or FontStringPool_HideAndClearAnchors)
return fontStringPool
end
local ActorPoolMixin = CreateFromMixins(ObjectPoolMixin)
local function ActorPoolFactory(actorPool)
return actorPool.parent:CreateActor(nil, actorPool.actorTemplate)
end
function ActorPoolMixin:OnLoad(parent, actorTemplate, resetterFunc)
ObjectPoolMixin.OnLoad(self, ActorPoolFactory, resetterFunc)
self.parent = parent
self.actorTemplate = actorTemplate
end
ActorPool_Hide = FramePool_Hide
function ActorPool_HideAndClearModel(actorPool, actor)
actor:ClearModel()
actor:Hide()
end
function CreateActorPool(parent, actorTemplate, resetterFunc)
local actorPool = CreateFromMixins(ActorPoolMixin)
actorPool:OnLoad(parent, actorTemplate, resetterFunc or ActorPool_HideAndClearModel)
return actorPool
end
local FramePoolCollectionMixin = {}
function CreateFramePoolCollection()
local poolCollection = CreateFromMixins(FramePoolCollectionMixin)
poolCollection:OnLoad()
return poolCollection
end
function FramePoolCollectionMixin:OnLoad()
self.pools = {}
end
function FramePoolCollectionMixin:GetNumActive()
local numTotalActive = 0
for _, pool in pairs(self.pools) do
numTotalActive = numTotalActive + pool:GetNumActive()
end
return numTotalActive
end
function FramePoolCollectionMixin:GetOrCreatePool(frameType, parent, template, resetterFunc, forbidden)
local pool = self:GetPool(template)
if not pool then
pool = self:CreatePool(frameType, parent, template, resetterFunc, forbidden)
end
return pool
end
function FramePoolCollectionMixin:CreatePool(frameType, parent, template, resetterFunc, forbidden)
assert(self:GetPool(template) == nil)
local pool = L.CreateFramePool(frameType, parent, template, resetterFunc, forbidden)
self.pools[template] = pool
return pool
end
function FramePoolCollectionMixin:GetPool(template)
return self.pools[template]
end
function FramePoolCollectionMixin:Acquire(template)
local pool = self:GetPool(template)
assert(pool)
return pool:Acquire()
end
function FramePoolCollectionMixin:Release(object)
for _, pool in pairs(self.pools) do
if pool:Release(object) then
-- Found it! Just return
return
end
end
-- Huh, we didn't find that object
assert(false)
end
function FramePoolCollectionMixin:ReleaseAllByTemplate(template)
local pool = self:GetPool(template)
if pool then
pool:ReleaseAll()
end
end
function FramePoolCollectionMixin:ReleaseAll()
for key, pool in pairs(self.pools) do
pool:ReleaseAll()
end
end
function FramePoolCollectionMixin:EnumerateActiveByTemplate(template)
local pool = self:GetPool(template)
if pool then
return pool:EnumerateActive()
end
return nop
end
function FramePoolCollectionMixin:EnumerateActive()
local currentPoolKey, currentPool = next(self.pools, nil)
local currentObject = nil
return function()
if currentPool then
currentObject = currentPool:GetNextActive(currentObject)
while not currentObject do
currentPoolKey, currentPool = next(self.pools, currentPoolKey)
if currentPool then
currentObject = currentPool:GetNextActive()
else
break
end
end
end
return currentObject
end, nil
end
local FixedSizeFramePoolCollectionMixin = CreateFromMixins(FramePoolCollectionMixin)
function CreateFixedSizeFramePoolCollection()
local poolCollection = CreateFromMixins(FixedSizeFramePoolCollectionMixin)
poolCollection:OnLoad()
return poolCollection
end
function FixedSizeFramePoolCollectionMixin:OnLoad()
FramePoolCollectionMixin.OnLoad(self)
self.sizes = {}
end
function FixedSizeFramePoolCollectionMixin:CreatePool(frameType, parent, template, resetterFunc, forbidden, maxPoolSize, preallocate)
local pool = FramePoolCollectionMixin.CreatePool(self, frameType, parent, template, resetterFunc, forbidden)
if preallocate then
for i = 1, maxPoolSize do
pool:Acquire()
end
pool:ReleaseAll()
end
self.sizes[template] = maxPoolSize
return pool
end
function FixedSizeFramePoolCollectionMixin:Acquire(template)
local pool = self:GetPool(template)
assert(pool)
if pool:GetNumActive() < self.sizes[template] then
return pool:Acquire()
end
return nil
end
ImmersionAPI.CreateFramePool = L.CreateFramePool; -- for XML
ImmersionAPI.CreateFontStringPool = L.CreateFontStringPool; -- for XML