Back to Home

ESO Lua File v101043

libraries/zo_templates/objectpooltemplates.lua

[◄ back to folders ]
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
--[[
Animation Pool
--]]
ZO_AnimationPool = ZO_ObjectPool:Subclass()
do
    local function AnimationReset(timeline)
        timeline:Stop()
    end
    function ZO_AnimationPool:Initialize(templateName)
        local function AnimationFactory()
            return ANIMATION_MANAGER:CreateTimelineFromVirtual(templateName)
        end
        ZO_ObjectPool.Initialize(self, AnimationFactory, AnimationReset)
    end
end
--[[
Control Pool
--]]
ZO_ControlPool = ZO_ObjectPool:Subclass()
do
    local function ControlFactory(pool)
        return ZO_ObjectPool_CreateNamedControl(pool.name, pool.templateName, pool, pool.parent)
    end
    local function ControlReset(control)
        control:SetHidden(true)
        control:ClearAnchors()
    end
    function ZO_ControlPool:Initialize(templateName, parent, overrideName)
        ZO_ObjectPool.Initialize(self, ControlFactory, ControlReset)
        local controlName = overrideName or templateName
        parent = parent or GuiRoot
        if parent ~= GuiRoot then
            controlName = parent:GetName() .. controlName
        end
        self.name = controlName
        self.parent = parent
        self.templateName = templateName
    end
end
-- Begin ZO_ObjectPool Overrides --
function ZO_ControlPool:AcquireObject(objectKey)
    local control, key = ZO_ObjectPool.AcquireObject(self, objectKey)
    if control then
        control:SetHidden(false)
    end
    return control, key
end
-- End ZO_ObjectPool Overrides --
--[[
Entry Data Pool
--]]
ZO_EntryDataPool = ZO_ObjectPool:Subclass()
function ZO_EntryDataPool:Initialize(entryDataObjectClass, factoryFunction, resetFunction)
    factoryFunction = factoryFunction or function()
        -- EntryData classes typically take dataSource in their constructors,
        -- so we can't use ZO_ObjectPool's default factory object behavior, which constructs with the pool and key
        return self.entryDataObjectClass:New()
    end
    resetFunction = resetFunction or function(entryData)
        entryData:SetDataSource(nil)
    end
    ZO_ObjectPool.Initialize(self, factoryFunction, resetFunction)
    self.entryDataObjectClass = entryDataObjectClass
end
-- Begin ZO_ObjectPool Overrides --
function ZO_EntryDataPool:AcquireObject(objectKey, dataSource)
    local entryData, key = ZO_ObjectPool.AcquireObject(self, objectKey)
    if entryData and dataSource then
        entryData:SetDataSource(dataSource)
    end
    return entryData, key
end
function ZO_EntryDataPool:ResetObject(entryData)
    ZO_ObjectPool.ResetObject(self, entryData)
    entryData:Reset()
end
-- End ZO_ObjectPool Overrides --
--[[
Meta Pool
]]
--
ZO_MetaPool = ZO_Object:Subclass()
function ZO_MetaPool:New(sourcePool)
    local pool = ZO_Object.New(self)
    pool.sourcePool = sourcePool
    pool.activeObjects = {}
    return pool
end
function ZO_MetaPool:AcquireObject()
    local object, key = self.sourcePool:AcquireObject()
    self.activeObjects[key] = object
    if self.customAcquireBehavior then
        self.customAcquireBehavior(object)
    end
    return object, key
end
function ZO_MetaPool:GetActiveObject(objectKey)
    return self.activeObjects[objectKey]
end
function ZO_MetaPool:GetActiveObjectCount()
    return NonContiguousCount(self.activeObjects)
end
function ZO_MetaPool:HasActiveObjects()
    return next(self.activeObjects) ~= nil
end
function ZO_MetaPool:GetActiveObjects()
    return self.activeObjects
end
function ZO_MetaPool:ActiveObjectIterator(filterFunctions)
    return ZO_FilteredNonContiguousTableIterator(self.activeObjects, filterFunctions)
end
function ZO_MetaPool:ReleaseAllObjects()
    for key, object in pairs(self.activeObjects) do
        if self.customResetBehavior then
            self.customResetBehavior(object)
        end
        self.sourcePool:ReleaseObject(key)
    end
    ZO_ClearTable(self.activeObjects)
end
function ZO_MetaPool:ReleaseObject(objectKey)
    local object = self.activeObjects[objectKey]
    if self.customResetBehavior then
        self.customResetBehavior(object)
    end
    self.sourcePool:ReleaseObject(objectKey)
    self.activeObjects[objectKey] = nil
end
end
function ZO_MetaPool:SetCustomResetBehavior(customeResetBehavior)
    self.customResetBehavior = customeResetBehavior
end
--[[
Pooled Object Abstract Class
Helper class for when you want the objects you'll be putting into a pool to be able to release themselves easily
rather than making the owner of the pool release everything all at once or manually track down objects to release
--]]
ZO_PooledObject = ZO_InitializingObject:Subclass()
function ZO_PooledObject:SetPoolAndKey(pool, poolKey)
    self.pool = pool
    self.poolKey = poolKey
end
function ZO_PooledObject:ReleaseObject()
    self.pool:ReleaseObject(self.poolKey)
end