Restrict frame size to follow grid (otherwise what's the point)

This commit is contained in:
2025-01-06 18:00:54 +01:00
parent cdd859ba50
commit e8e15444c9

View File

@@ -8,20 +8,24 @@ local addonname, shared = ...
---@field frame Frame ---@field frame Frame
---@field cellWidth number ---@field cellWidth number
---@field cellHeight number ---@field cellHeight number
---@field allowOverflow boolean
---@field columnCursors table<number, number> ---@field columnCursors table<number, number>
GridFrame = { GridFrame = {
---@param name string ---@param name string
---@param parent Frame ---@param parent Frame
---@param columns number ---@param columns number
---@param cellHeight number ---@param cellHeight number
---@param size {w: number, h:number} ---@param size {w: number, h:number}?
---@return GridFrame ---@return GridFrame
new = function(name, parent, columns, cellHeight, size) new = function(name, parent, columns, cellHeight, size)
local self = setmetatable({}, { local self = setmetatable({}, {
__index = GridFrame __index = GridFrame
}) })
self.frame = CreateFrame("Frame", name, parent) self.frame = CreateFrame("Frame", name, parent)
self.frame:SetSize(size.w, size.h) size = size or {}
if size.w then self.frame:SetWidth(size.w) end
if size.h then self.frame:SetHeight(size.h) end
self.allowOverflow = false
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",
@@ -38,6 +42,14 @@ GridFrame = {
for i = 1, columns do for i = 1, columns do
self.columnCursors[i] = 0 self.columnCursors[i] = 0
end end
local previousSetWidth = self.frame.SetWidth
self.frame.SetWidth = function(frame, width)
if not width then return end
previousSetWidth(frame, width)
self.cellWidth = self.frame:GetWidth() / self.columns
end
return self return self
end, end,
---@param self GridFrame ---@param self GridFrame
@@ -61,9 +73,17 @@ GridFrame = {
end end
end end
if not self.allowOverflow then
if bestMaxY + self.cellHeight * rowspan > self.frame:GetHeight() then
print("Frame is too tall")
return
end
end
if bestStartColumn then if bestStartColumn then
local x = (bestStartColumn - 1) * self.cellWidth local x = (bestStartColumn - 1) * self.cellWidth
local y = -bestMaxY local y = -bestMaxY
print("Adding frame to grid", x, y, self.cellWidth * colspan, 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)
@@ -80,9 +100,13 @@ GridFrame = {
local configFrame = GridFrame.new("HeimdallConfig", local configFrame = GridFrame.new("HeimdallConfig",
UIParent, UIParent,
6, 32, 12, 20,
{ w = 512, h = 512 + 256 }) { w = 512, h = 512 + 256 })
local innerFrame = GridFrame.new("HeimdallConfigInner",
configFrame.frame, 6, 20)
configFrame:Add(innerFrame.frame, 6, 6)
local colors = { local colors = {
{ 1, 0, 0, 1 }, { 1, 0, 0, 1 },
{ 0, 1, 0, 1 }, { 0, 1, 0, 1 },
@@ -92,7 +116,7 @@ local colors = {
{ 0, 1, 1, 1 }, { 0, 1, 1, 1 },
{ 1, 1, 1, 1 }, { 1, 1, 1, 1 },
} }
for i = 1, 100 do for i = 1, 20 do
local frame = CreateFrame("Frame", "HeimdallConfigFrame" .. i, UIParent) local frame = CreateFrame("Frame", "HeimdallConfigFrame" .. i, UIParent)
frame:SetBackdrop({ frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background", bgFile = "Interface/Tooltips/UI-Tooltip-Background",
@@ -101,7 +125,7 @@ for i = 1, 100 do
}) })
frame:SetBackdropColor(unpack(colors[i % #colors + 1])) frame:SetBackdropColor(unpack(colors[i % #colors + 1]))
frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
configFrame:Add(frame, 1, 12) innerFrame:Add(frame, 4, 2)
end end
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
shared.Config = {} shared.Config = {}
@@ -197,25 +221,26 @@ function shared.Config.Init()
--configFrame:SetBackdropColor(0, 0, 0, 0.8) --configFrame:SetBackdropColor(0, 0, 0, 0.8)
--configFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) --configFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
configFrame.frame:SetMovable(true) -- configFrame.frame:SetMovable(true)
configFrame.frame:EnableMouse(true) -- configFrame.frame:EnableMouse(true)
configFrame.frame:RegisterForDrag("LeftButton") -- configFrame.frame:RegisterForDrag("LeftButton")
configFrame.frame:SetScript("OnDragStart", function(self) -- configFrame.frame:SetScript("OnDragStart", function(self)
self:StartMoving() -- self:StartMoving()
end) -- end)
configFrame.frame:SetScript("OnDragStop", function(self) -- configFrame.frame:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing() -- self:StopMovingOrSizing()
end) -- end)
configFrame.frame:SetScript("OnShow", function(self) -- configFrame.frame:SetScript("OnShow", function(self)
self:SetScale(1) -- self:SetScale(1)
end) -- end)
--
-- local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
-- title:SetText("Heimdall Config")
-- configFrame:Add(title, 1, 12)
local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal") -- local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig",
title:SetText("Heimdall Config") -- configFrame.frame, 12, 20, { w: 0,h: 0 })
--configFrame:Add(title, 1, 12) -- configFrame:Add(spotterConfigFrame.frame, 12, 6)
-- local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig", configFrame.frame, 12, 12)
-- --configFrame:Add(spotterConfigFrame.frame, 12, 6)
-- spotterConfigFrame.frame:SetBackdrop({ -- spotterConfigFrame.frame: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",
@@ -229,8 +254,9 @@ function shared.Config.Init()
-- bottom = 4 -- bottom = 4
-- } -- }
-- }) -- })
-- spotterConfigFrame.frame:SetBackdropColor(0, 0, 0, 0.8) -- spotterConfigFrame.frame:SetBackdropColor(1, 0, 0, 0.8)
-- spotterConfigFrame.frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) -- spotterConfigFrame.frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
-- spotterConfigFrame:Show()
--local spotterEnableButton = BasicButton("HeimdallSpotterEnable", spotterConfigFrame, "Enable Spotter", --local spotterEnableButton = BasicButton("HeimdallSpotterEnable", spotterConfigFrame, "Enable Spotter",
-- function(state) -- function(state)