Thread: Grid
View Single Post
08/28/14, 11:07 AM   #9
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by skyraker
To follow on with seeing if I understand ControlPool:

So once I have to Pool I essentially want to maintain a table of created objects that are in use and a table of objects that have been released.

I use AcquireObject() to get/create a new control, place it in the table I created, modify it to what want and display it.

When the grid is 'disabled', I release all the objects back to the pool for the next use.
Yes, it is exactly as you say. AcquireObject() returns first free object or if there is not free object, it will create a new one using the factory function in case of object pool - ZO_ObjectPool:New(factoryFunction, resetFunction) or from the template in case of control pool - ZO_ControlPool:New(templateName, parent, prefix).

As I did't want to use XML (you have to use XML to define control template), I have used just object pool instead of control pool in my Bloody Screen addon (v0.1). Here is how it works:
Warning: Spoiler


In your case you will have to do the same as me - create top level window of the same size as GuiRoot, set draw layer to overlay as you want to display grid on the top of other windows and then create lines.

top level window:
Lua Code:
  1. TLW = WINDOW_MANAGER:CreateTopLevelWindow()
  2. TLW:SetAnchorFill(GuiRoot)
  3. TLW:SetDrawLayer(DL_OVERLAY)
  4. TLW:SetMouseEnabled(false)

simple object pool for lines:
Lua Code:
  1. local function FactoryFunction(pool)
  2.    local line = WINDOW_MANAGER:CreateControl(nil, TLW, CT_TEXTURE)
  3.    line:SetColor(0,0,0,1) --black
  4.    return line
  5. end
  6.  
  7. local function ResetFunction(object)
  8.    object:SetHidden(true)
  9. end
  10.  
  11. local objectPool = ZO_ObjectPool:New(FactoryFunction, ResetFunction)
  12. --if you omit reset function, object pool will use default ZO_ObjectPool_DefaultResetControl function which works exactly the same as my ResetFunction - it will just hide released object.

And then create lots of lines:
Lua Code:
  1. local GRID_SIZE = 50
  2. local LINE_THICKNESS = 3
  3. local SCREEN_WIDTH, SCREEN_HEIGHT = GuiRoot:GetDimensions()
  4.  
  5. --horizontal lines
  6. for i = 1, zo_floor(SCREEN_WIDTH / GRID_SIZE) do
  7.    local line = objectPool:AcquireObject()
  8.    line:SetDimensions(SCREEN_WIDTH, LINE_THICKNESS)
  9.    line:SetAnchor(TOPLEFT, TLW, TOPLEFT, GRID_SIZE * i, 0)
  10.    line:SetHidden(false)
  11. end
  12.  
  13. --vertical lines
  14. for i = 1, zo_floor(SCREEN_HEIGHT / GRID_SIZE) do
  15.    local line = objectPool:AcquireObject()
  16.    line:SetDimensions(LINE_THICKNESS, SCREEN_HEIGHT)
  17.    line:SetAnchor(TOPLEFT, TLW, TOPLEFT, 0, GRID_SIZE * i)
  18.    line:SetHidden(false)
  19. end

Last edited by Garkin : 08/28/14 at 11:15 AM.
  Reply With Quote