Quantcast
WoWInterface - View Single Post - How to properly use CreateFramePool() ?
View Single Post
08-05-17, 10:54 AM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 428
This is entirely implemented in Lua so you can just read the source code.

Lua Code:
  1. ObjectPoolMixin = {};
  2.  
  3. function ObjectPoolMixin:OnLoad(creationFunc, resetterFunc)
  4.     self.creationFunc = creationFunc;
  5.     self.resetterFunc = resetterFunc;
  6.  
  7.     self.activeObjects = {};
  8.     self.inactiveObjects = {};
  9.  
  10.     self.numActiveObjects = 0;
  11. end
  12.  
  13. function ObjectPoolMixin:Acquire()
  14.     local numInactiveObjects = #self.inactiveObjects;
  15.     if numInactiveObjects > 0 then
  16.         local obj = self.inactiveObjects[numInactiveObjects];
  17.         self.activeObjects[obj] = true;
  18.         self.numActiveObjects = self.numActiveObjects + 1;
  19.         self.inactiveObjects[numInactiveObjects] = nil;
  20.         return obj, false;
  21.     end
  22.  
  23.     local newObj = self.creationFunc(self);
  24.     if self.resetterFunc then
  25.         self.resetterFunc(self, newObj);
  26.     end
  27.     self.activeObjects[newObj] = true;
  28.     self.numActiveObjects = self.numActiveObjects + 1;
  29.     return newObj, true;
  30. end
  31.  
  32. function ObjectPoolMixin:Release(obj)
  33.     assert(self.activeObjects[obj]);
  34.  
  35.     self.inactiveObjects[#self.inactiveObjects + 1] = obj;
  36.     self.activeObjects[obj] = nil;
  37.     self.numActiveObjects = self.numActiveObjects - 1;
  38.     if self.resetterFunc then
  39.         self.resetterFunc(self, obj);
  40.     end
  41. end
  42.  
  43. function ObjectPoolMixin:ReleaseAll()
  44.     for obj in pairs(self.activeObjects) do
  45.         self:Release(obj);
  46.     end
  47. end
  48.  
  49. function ObjectPoolMixin:EnumerateActive()
  50.     return pairs(self.activeObjects);
  51. end
  52.  
  53. function ObjectPoolMixin:GetNextActive(current)
  54.     return (next(self.activeObjects, current));
  55. end
  56.  
  57. function ObjectPoolMixin:GetNumActive()
  58.     return self.numActiveObjects;
  59. end
  60.  
  61. function ObjectPoolMixin:EnumerateInactive()
  62.     return ipairs(self.inactiveObjects);
  63. end
  64.  
  65. function CreateObjectPool(creationFunc, resetterFunc)
  66.     local objectPool = CreateFromMixins(ObjectPoolMixin);
  67.     objectPool:OnLoad(creationFunc, resetterFunc);
  68.     return objectPool;
  69. end
  70.  
  71. FramePoolMixin = Mixin({}, ObjectPoolMixin);
  72.  
  73. local function FramePoolFactory(framePool)
  74.     return CreateFrame(framePool.frameType, nil, framePool.parent, framePool.frameTemplate);
  75. end
  76.  
  77. function FramePoolMixin:OnLoad(frameType, parent, frameTemplate, resetterFunc)
  78.     ObjectPoolMixin.OnLoad(self, FramePoolFactory, resetterFunc);
  79.     self.frameType = frameType;
  80.     self.parent = parent;
  81.     self.frameTemplate = frameTemplate;
  82. end
  83.  
  84. function FramePool_Hide(framePool, frame)
  85.     frame:Hide();
  86. end
  87.  
  88. function FramePool_HideAndClearAnchors(framePool, frame)
  89.     frame:Hide();
  90.     frame:ClearAllPoints();
  91. end
  92.  
  93. function CreateFramePool(frameType, parent, frameTemplate, resetterFunc)
  94.     local framePool = CreateFromMixins(FramePoolMixin);
  95.     framePool:OnLoad(frameType, parent, frameTemplate, resetterFunc or FramePool_HideAndClearAnchors);
  96.     return framePool;
  97. end
__________________
  Reply With Quote