Rework grid to have infinite rows

This commit is contained in:
2025-01-06 17:35:59 +01:00
parent fc3732fd0c
commit ab85b3712a

View File

@@ -4,27 +4,24 @@ local addonname, shared = ...
---@class GridFrame ---@class GridFrame
---@field name string ---@field name string
---@field rows number
---@field columns number ---@field columns number
---@field frame Frame ---@field frame Frame
---@field cellWidth number ---@field cellWidth number
---@field cellHeight number ---@field cellHeight number
---@field occupancy table<number, table<number, boolean>> ---@field columnCursors table<number, number>
GridFrame = { GridFrame = {
---@param name string ---@param name string
---@param parent Frame ---@param parent Frame
---@param rows number
---@param columns number ---@param columns number
---@param size {w: number, h:number}? ---@param cellHeight number
---@param size {w: number, h:number}
---@return GridFrame ---@return GridFrame
new = function(name, parent, rows, columns, size) new = function(name, parent, columns, cellHeight, size)
local self = setmetatable({}, { local self = setmetatable({}, {
__index = GridFrame __index = GridFrame
}) })
size = size or {}
self.frame = CreateFrame("Frame", name, parent) self.frame = CreateFrame("Frame", name, parent)
self.frame:SetWidth(columns * 128) self.frame:SetSize(size.w, size.h)
self.frame:SetHeight(rows * 128)
self.frame:SetPoint("CENTER", UIParent, "CENTER") self.frame:SetPoint("CENTER", UIParent, "CENTER")
self.frame:SetBackdrop({ self.frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background", bgFile = "Interface/Tooltips/UI-Tooltip-Background",
@@ -33,19 +30,13 @@ GridFrame = {
}) })
self.frame:SetBackdropColor(0, 0, 0, 0.8) self.frame:SetBackdropColor(0, 0, 0, 0.8)
self.frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) self.frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
if size.w then self.frame:SetWidth(size.w) end
if size.h then self.frame:SetHeight(size.h) end
self.rows = rows
self.columns = columns self.columns = columns
self.cellWidth = self.frame:GetWidth() / columns self.cellWidth = self.frame:GetWidth() / columns
self.cellHeight = self.frame:GetHeight() / rows self.cellHeight = cellHeight
self.occupancy = {} self.columnCursors = {}
for row = 1, rows do for i = 1, columns do
self.occupancy[row] = {} self.columnCursors[i] = 0
for column = 1, columns do
self.occupancy[row][column] = false
end
end end
return self return self
end, end,
@@ -54,45 +45,64 @@ GridFrame = {
---@param rowspan number ---@param rowspan number
---@param colspan number ---@param colspan number
Add = function(self, frame, rowspan, colspan) Add = function(self, frame, rowspan, colspan)
for row = 1, self.rows do colspan = math.min(colspan, self.columns)
for col = 1, self.columns do
if not self.occupancy[row][col] then local bestStartColumn = nil
local canPlace = true local bestMaxY = math.huge
for r = row, row + rowspan - 1 do
for c = col, col + colspan - 1 do for startColumn = 1, self.columns - colspan + 1 do
if not self.occupancy[r] or self.occupancy[r][c] then local currentMaxY = 0
canPlace = false for c = startColumn, startColumn + colspan - 1 do
break currentMaxY = math.max(currentMaxY, self.columnCursors[c])
end end
if not canPlace then break end if currentMaxY < bestMaxY then
bestMaxY = currentMaxY
bestStartColumn = startColumn
end end
end end
if canPlace then
local x = (col - 1) * self.cellWidth if bestStartColumn then
local y = -(row - 1) * self.cellHeight local x = (bestStartColumn - 1) * self.cellWidth
print(string.format("frame:SetWidth(%d)", self.cellWidth * colspan)) local y = -bestMaxY
print(string.format("frame:SetHeight(%d)", self.cellHeight * rowspan))
frame:SetWidth(self.cellWidth * colspan) frame:SetWidth(self.cellWidth * colspan)
frame:SetHeight(self.cellHeight * rowspan) frame:SetHeight(self.cellHeight * rowspan)
frame:SetPoint("TOPLEFT", self.frame, "TOPLEFT", x, y) frame:SetPoint("TOPLEFT", self.frame, "TOPLEFT", x, y)
frame:SetParent(self.frame)
for r = row, row + rowspan - 1 do for c = bestStartColumn, bestStartColumn + colspan - 1 do
for c = col, col + colspan - 1 do self.columnCursors[c] = self.columnCursors[c] + self.cellHeight * rowspan
self.occupancy[r][c] = true
end
end
return
end
end
end
end end
else
print("No available space in the grid.") print("No available space in the grid.")
end end
end
} }
local configFrame = GridFrame.new("HeimdallConfig", UIParent, 60, 12, { w = 512, h = 512 + 256 }) local configFrame = GridFrame.new("HeimdallConfig",
UIParent,
6, 32,
{ w = 512, h = 512 + 256 })
local colors = {
{ 1, 0, 0, 1 },
{ 0, 1, 0, 1 },
{ 0, 0, 1, 1 },
{ 1, 1, 0, 1 },
{ 1, 0, 1, 1 },
{ 0, 1, 1, 1 },
{ 1, 1, 1, 1 },
}
for i = 1, 100 do
local frame = CreateFrame("Frame", "HeimdallConfigFrame" .. i, UIParent)
frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
tileSize = 64,
tile = true
})
frame:SetBackdropColor(unpack(colors[i % #colors + 1]))
frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
configFrame:Add(frame, 1, 12)
end
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
shared.Config = {} shared.Config = {}
function shared.Config.Init() function shared.Config.Init()
@@ -202,26 +212,25 @@ function shared.Config.Init()
local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal") local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetText("Heimdall Config") title:SetText("Heimdall Config")
configFrame:Add(title, 1, 12) --configFrame:Add(title, 1, 12)
local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig", configFrame.frame, 12, 12) -- local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig", configFrame.frame, 12, 12)
configFrame:Add(spotterConfigFrame.frame, 12, 6) -- --configFrame:Add(spotterConfigFrame.frame, 12, 6)
spotterConfigFrame:SetPoint("TOPLEFT", configFrame, "TOPLEFT", 8, -24) -- spotterConfigFrame.frame:SetBackdrop({
spotterConfigFrame:SetBackdrop({ -- bgFile = "Interface/Tooltips/UI-Tooltip-Background",
bgFile = "Interface/Tooltips/UI-Tooltip-Background", -- edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border", -- tile = true,
tile = true, -- tileSize = 4,
tileSize = 4, -- edgeSize = 4,
edgeSize = 4, -- insets = {
insets = { -- left = 4,
left = 4, -- right = 4,
right = 4, -- top = 4,
top = 4, -- bottom = 4
bottom = 4 -- }
} -- })
}) -- spotterConfigFrame.frame:SetBackdropColor(0, 0, 0, 0.8)
spotterConfigFrame:SetBackdropColor(0, 0, 0, 0.8) -- spotterConfigFrame.frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
spotterConfigFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
--local spotterEnableButton = BasicButton("HeimdallSpotterEnable", spotterConfigFrame, "Enable Spotter", --local spotterEnableButton = BasicButton("HeimdallSpotterEnable", spotterConfigFrame, "Enable Spotter",
-- function(state) -- function(state)