diff --git a/media/textures/pixel_edge.tga b/media/textures/pixel_edge.tga new file mode 100644 index 0000000..81a9423 Binary files /dev/null and b/media/textures/pixel_edge.tga differ diff --git a/media/textures/square_white.tga b/media/textures/square_white.tga new file mode 100644 index 0000000..b4ba3df Binary files /dev/null and b/media/textures/square_white.tga differ diff --git a/src/types/indicators/squareindicator.lua b/src/types/indicators/squareindicator.lua index ffea271..c6b283a 100644 --- a/src/types/indicators/squareindicator.lua +++ b/src/types/indicators/squareindicator.lua @@ -17,6 +17,8 @@ local omi = select(2, ...) local types = omi.GetModule("types") +local squarePool = CreateFramePool("Frame", nil, "BackdropTemplate") + ---SquareIndicator is an indicator that displays a colored square texture ---@class SquareIndicator: Indicator local SquareIndicator = types.CreateClass("SquareIndicator", Indicator) @@ -36,33 +38,90 @@ end -- point Attachment point to the unitframe's overlay frame -- x, y x and y offset for the attachment point -- color Color of the square -function SquareIndicator:Init(unitframe, size, point, x, y, color) - local frame = unitframe.overlays - - local texture = frame:CreateTexture(nil, "ARTWORK") - self.texture = texture - texture:Hide() - texture:SetTexture("Interface\\Addons\\OmicronFrames\\media\\textures\\square_b") - texture:SetSize(size, size) - texture:SetPoint(point, x, y) - self:SetColor(color) - texture:SetVertexColor(unpack(color)) - texture:SetDrawLayer("ARTWORK") +---@param unitframe UnitFrame +---@param size number +---@param point string +---@param x number +---@param y number +---@param color number[] +---@param showStacks boolean +function SquareIndicator:Init(unitframe, size, point, x, y, color, showStacks) + self.unitframe = unitframe + self.frameParent = unitframe.overlays + self.size = size + self.point = point + self.x = x + self.y = y + self.color = color + self.showStacks = showStacks end --- Set the color of the Square Indicator -- color: a sequence table with 3 color channels {r, g, b} function SquareIndicator:SetColor(color) - self.texture:SetVertexColor(unpack(color)) + self.color = color + local frame = self.frame + if frame then + frame:SetBackdropColor(unpack(color)) + end end --- Show the square indicator. ----@param data table|nil +---@param data table function SquareIndicator:Show(data) - self.texture:Show() + local frame, stacks = self:GetFrame() + local numStacks = data.stacks or 0 + self.frame = frame + self.stacks = stacks + if self.showStacks and numStacks > 0 then + stacks:SetText(numStacks) + stacks:Show() + end + frame:SetBackdropColor(unpack(self.color)) + frame:Show() +end + +function SquareIndicator:GetFrame() + local frame, new = squarePool:Acquire() + local stacks + if new then + frame.backdropInfo = { + bgFile = "Interface\\Addons\\OmicronFrames\\media\\textures\\square_white", + edgeFile = "Interface\\Addons\\OmicronFrames\\media\\textures\\pixel_edge", + edgeSize = 2, + insets = {left=0, right=0, top=0, bottom=0}, + } + frame:ApplyBackdrop() + + stacks = frame:CreateFontString(nil, "OVERLAY") + frame.stacks = stacks + stacks:SetFont("Interface\\AddOns\\OmicronFrames\\media\\fonts\\roboto\\Roboto-Medium.ttf", 12, "OUTLINE") + stacks:SetPoint("CENTER") + stacks:SetTextColor(1, 1, 1) + end + + frame:SetSize(self.size, self.size) + frame:SetParent(self.frameParent) + frame:SetPoint(self.point, self.x, self.y) + frame:SetBackdropBorderColor(0, 0, 0, 1) + stacks = frame.stacks + return frame, stacks +end + +---@param data table +function SquareIndicator:Update(data) + local numStacks = data.stacks or 0 + if numStacks > 0 then + self.stacks:SetText(numStacks) + self.stacks:Show() + end end --- Hide the square indicator. function SquareIndicator:Hide() - self.texture:Hide() + local frame = self.frame + self.stacks:Hide() + self.frame = nil + self.stacks = nil + squarePool:Release(frame) end