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