Hi Guys,
I wanted to implement a grid toggle, I started with the vertical lines and tried it out.
Here the code, it is quite simple:
Lua Code:
LybrialAnchors = LybrialUI:NewModule("Lybrial Anchors");
local LybrialAnchors = LybrialAnchors;
LybrialAnchors.grid = nil;
-- Initialize Addon
function LybrialAnchors:OnInitialize()
end
-- Get Options
function LybrialAnchors:GetOptions()
return {
toggleGrid = {
type = "toggle",
order = 1,
name = "Toggle Grid",
set = function(_, value)
self:ToggleGrid(value);
end,
width = "full"
},
};
end
function LybrialAnchors:ToggleGrid(toggle)
if (toggle) then
if (not self.grid) then
print("Create Grid");
self:CreateGrid();
else
print("Show Grid");
self.grid:Show();
end
else
print("Hide Grid");
self.grid:Hide();
end
end
function LybrialAnchors:CreateGrid()
if (not self.grid) then
self.grid = CreateFrame("Frame", "Grid", UIParent);
self.grid:SetFrameStrata("BACKGROUND");
end
local steps = 128;
local width, height = UIParent:GetSize();
local widthStep = (width / steps);
self.grid:SetPoint("CENTER", UIParent);
self.grid:SetSize(width, height);
self.grid:Show();
self:CreateVerticalLines(steps, widthStep);
end
function LybrialAnchors:CreateVerticalLines(steps, widthStep)
for i = 0, steps do
local region = self.grid:CreateTexture();
region:SetColorTexture(0, 0, 0);
region:SetDrawLayer("BACKGROUND", 0);
region:ClearAllPoints();
region:SetPoint("TOPLEFT", self.grid, "TOPLEFT", (i * widthStep), 0);
region:SetPoint("BOTTOMRIGHT", self.grid, "BOTTOMLEFT", (i * widthStep), 0);
end
end
Let me click the toggle input 5 times. The output is:
Code:
Create Grid
Hide Grid
Show Grid
Hide Grid
Show Grid
So as you can see the first time I enable the toggle the grid is getting created. After that the grid frame
is only showing or hiding. So it should work exactly as I want it to work.
But the problem is: On each `self.grid:Show()` the memory usage of my addon is increasing.
I toggled 50 times and my addon memory usage was increased by 25 MB.
I dont understand why a `Show()` on an already existing `Frame` is increasing memory usage.
Anything anyone can tell me about that?