Make GridFrame automatically adjust to SetWidth and SetHeight

This commit is contained in:
2025-01-06 21:26:52 +01:00
parent 4594dff4a6
commit d44aa04e44

View File

@@ -9,7 +9,7 @@ local addonname, shared = ...
---@field cellWidth number ---@field cellWidth number
---@field cellHeight number ---@field cellHeight number
---@field allowOverflow boolean ---@field allowOverflow boolean
---@field columnCursors table<number, number> ---@field columnHeights table<number, number>
GridFrame = { GridFrame = {
---@param name string ---@param name string
---@param parent Frame ---@param parent Frame
@@ -26,7 +26,7 @@ GridFrame = {
if size.w then self.frame:SetWidth(size.w) end if size.w then self.frame:SetWidth(size.w) end
if size.h then self.frame:SetHeight(size.h) end if size.h then self.frame:SetHeight(size.h) end
self.allowOverflow = false self.allowOverflow = false
self.frame:SetPoint("CENTER", UIParent, "CENTER") self.frame:SetPoint("CENTER", parent, "CENTER")
self.frame:SetBackdrop({ self.frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background", bgFile = "Interface/Tooltips/UI-Tooltip-Background",
tileSize = 64, tileSize = 64,
@@ -38,18 +38,10 @@ GridFrame = {
self.columns = columns self.columns = columns
self.cellWidth = self.frame:GetWidth() / columns self.cellWidth = self.frame:GetWidth() / columns
self.cellHeight = cellHeight self.cellHeight = cellHeight
self.columnCursors = {} self.columnHeights = {}
for i = 1, columns do for i = 1, columns do
self.columnCursors[i] = 0 self.columnHeights[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
@@ -59,41 +51,71 @@ GridFrame = {
Add = function(self, frame, rowspan, colspan) Add = function(self, frame, rowspan, colspan)
colspan = math.min(colspan, self.columns) colspan = math.min(colspan, self.columns)
local bestStartColumn = nil local bestColumn = nil
local bestMaxY = math.huge local bestRow = math.huge
for startColumn = 1, self.columns - colspan + 1 do for startColumn = 1, self.columns - colspan + 1 do
local currentMaxY = 0 local currentMaxY = 0
for c = startColumn, startColumn + colspan - 1 do for c = startColumn, startColumn + colspan - 1 do
currentMaxY = math.max(currentMaxY, self.columnCursors[c]) currentMaxY = math.max(currentMaxY, self.columnHeights[c])
end end
if currentMaxY < bestMaxY then if currentMaxY < bestRow then
bestMaxY = currentMaxY bestRow = currentMaxY
bestStartColumn = startColumn bestColumn = startColumn
end end
end end
if not self.allowOverflow then if not self.allowOverflow then
if bestMaxY + self.cellHeight * rowspan > self.frame:GetHeight() then if bestRow + self.cellHeight * rowspan > self.frame:GetHeight() then
print("Frame is too tall") print("Frame is too tall")
return return
end end
end end
if bestStartColumn then if bestColumn then
local x = (bestStartColumn - 1) * self.cellWidth frame:SetParent(self.frame)
local y = -bestMaxY frame.gridData = {
print("Adding frame to grid", x, y, self.cellWidth * colspan, self.cellHeight * rowspan) row = bestRow,
frame:SetWidth(self.cellWidth * colspan) column = bestColumn,
frame:SetHeight(self.cellHeight * rowspan) colspan = colspan,
frame:SetPoint("TOPLEFT", self.frame, "TOPLEFT", x, y) rowspan = rowspan,
parent = self
}
frame.SetPos = function(self)
if not self.gridData then return end
local parent = self.gridData.parent
local x = (self.gridData.column - 1) * parent.cellWidth
local y = -(self.gridData.row * parent.cellHeight)
self:SetPoint("TOPLEFT", parent.frame, "TOPLEFT", x, y)
self:SetWidth(parent.cellWidth * self.gridData.colspan)
self:SetHeight(parent.cellHeight * self.gridData.rowspan)
end
frame.SetPos(frame)
for c = bestStartColumn, bestStartColumn + colspan - 1 do for c = bestColumn, bestColumn + colspan - 1 do
self.columnCursors[c] = self.columnCursors[c] + self.cellHeight * rowspan self.columnHeights[c] = self.columnHeights[c] + rowspan
end end
else else
print("No available space in the grid.") print("No available space in the grid.")
end end
end,
Recalculate = function(self)
local children = { self.frame:GetChildren() }
for _, child in pairs(children) do child:SetPos() end
end,
SetWidth = function(self, width)
self.frame:SetWidth(width)
self.cellWidth = width / self.columns
self:Recalculate()
end,
SetHeight = function(self, height)
self.frame:SetHeight(height)
local tallestRow = 0
for _, height in pairs(self.columnHeights) do
tallestRow = math.max(tallestRow, height)
end
self.cellHeight = height / tallestRow
self:Recalculate()
end end
} }
@@ -165,6 +187,8 @@ StaticGridFrame = {
if canPlace then if canPlace then
local x = (col - 1) * self.cellWidth local x = (col - 1) * self.cellWidth
local y = -(row - 1) * self.cellHeight local y = -(row - 1) * self.cellHeight
frame.colspan = colspan
frame.rowspan = 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)
@@ -191,7 +215,30 @@ StaticGridFrame = {
self.frame:SetScript("OnDragStop", function(self) self.frame:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing() self:StopMovingOrSizing()
end) end)
end,
Recalculate = function(self)
for _, frame in pairs(self.frame:GetChildren()) do
if frame.colspan then
frame:SetWidth(self.cellWidth * frame.colspan)
end
if frame.rowspan then
frame:SetHeight(self.cellHeight * frame.rowspan)
end
end
end,
SetWidth = function(self, width)
self.frame:SetWidth(width)
self.cellWidth = self.frame:GetWidth() / self.columns
self:Recalculate()
end,
SetHeight = function(self, height)
self.frame:SetHeight(height)
print("Setting height", height)
self.cellHeight = self.frame:GetHeight() / self.rows
print("Cell height", self.cellHeight)
self:Recalculate()
end end
} }
local configFrame = StaticGridFrame.new("HeimdallConfig", local configFrame = StaticGridFrame.new("HeimdallConfig",
@@ -199,13 +246,13 @@ local configFrame = StaticGridFrame.new("HeimdallConfig",
40, 12, 40, 12,
{ w = 1024 + 512, h = 1024 }) { w = 1024 + 512, h = 1024 })
configFrame:MakeMovable() configFrame:MakeMovable()
configFrame.frame:SetScript("OnKeyUp", function(self, key) --configFrame.frame:SetScript("OnKeyUp", function(self, key)
if key == "ESCAPE" then -- if key == "ESCAPE" then
self:Hide() -- self:Hide()
end -- end
end) --end)
local colors = { local colors = {
{ 1, 0, 0, 1 }, { 1, 0, 0, 1 },
{ 0, 1, 0, 1 }, { 0, 1, 0, 1 },
{ 0, 0, 1, 1 }, { 0, 0, 1, 1 },
@@ -214,17 +261,20 @@ local colors = {
{ 0, 1, 1, 1 }, { 0, 1, 1, 1 },
{ 1, 1, 1, 1 }, { 1, 1, 1, 1 },
} }
--for i = 1, 200 do HeimdallTestFrame = GridFrame.new("HeimdallPartyFrame", UIParent, 12, 20, { w = 1024, h = 1024 })
-- local frame = CreateFrame("Frame", "HeimdallConfigFrame" .. i, UIParent) for i = 1, 10 do
-- frame:SetBackdrop({ local frame = CreateFrame("Frame", "HeimdallPartyFrame" .. i, UIParent)
-- bgFile = "Interface/Tooltips/UI-Tooltip-Background", frame:SetBackdrop({
-- tileSize = 64, bgFile = "Interface/Tooltips/UI-Tooltip-Background",
-- tile = true tileSize = 64,
-- }) tile = true
-- frame:SetBackdropColor(unpack(colors[i % #colors + 1])) })
-- frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) frame:SetBackdropColor(unpack(colors[i % #colors + 1]))
-- configFrame:Add(frame, 4, 2) frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
--end HeimdallTestFrame:Add(frame, 4, 2)
end
HeimdallTestFrame:SetHeight(128)
HeimdallTestFrame:SetWidth(512)
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
shared.Config = {} shared.Config = {}
function shared.Config.Init() function shared.Config.Init()
@@ -263,59 +313,47 @@ function shared.Config.Init()
spotterEnableButtonLabel:SetAllPoints(button) spotterEnableButtonLabel:SetAllPoints(button)
return button return button
end end
-----@param name string ---@param name string
-----@param parent Frame ---@param parent Frame
-----@param text string ---@param text string
-----@param onDone fun(text: string) ---@param onDone fun(text: string)
--local function CreateBasicSmallEditBox(name, parent, text, onDone) local function CreateBasicSmallEditBox(name, parent, text, onDone)
-- local editBox = CreateFrame("EditBox", name, parent) local container = GridFrame.new(name, parent, 1, 1)
-- editBox.text = editBox:CreateFontString(nil, "OVERLAY", "GameFontNormal") local editBox = CreateFrame("EditBox", name .. "EditBox", container.frame)
-- editBox.text:SetText(text) local editBoxtext = editBox:CreateFontString(nil, "OVERLAY", "GameFontNormal")
-- editBox.text:SetPoint("TOPLEFT", editBox, "TOPLEFT", 0, 8 + 4) editBoxtext:SetText(text)
-- editBox:SetNumeric(true) editBoxtext:SetPoint("TOPLEFT", container.frame, "TOPLEFT", 0, 0)
-- editBox:SetSize(128 - 8, 24)
-- editBox:SetAutoFocus(false)
-- editBox:SetFontObject("GameFontNormal")
-- editBox:SetText(Heimdall_Data.config.spotter.throttleTime)
-- editBox:SetBackdrop({
-- bgFile = "Interface/Tooltips/UI-Tooltip-Background",
-- edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
-- tile = true,
-- tileSize = 2,
-- edgeSize = 2,
-- insets = {
-- left = 2,
-- right = 2,
-- top = 2,
-- bottom = 2
-- }
-- })
-- editBox:SetBackdropColor(0.8, 0.8, 0.8, 1)
-- editBox:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
-- editBox:SetScript("OnEnterPressed", function()
-- onDone(editBox:GetText())
-- end)
-- editBox:SetScript("OnEscapePressed", function()
-- editBox:ClearFocus()
-- end)
-- editBox:SetScript("OnEditFocusLost", function()
-- onDone(editBox:GetText())
-- end)
-- editBox.PutBelow = function(a, b, offset) editBox:SetPoint("TOPLEFT", container.frame, "TOPLEFT", 0, -8)
-- offset = offset or {} editBox:SetAutoFocus(false)
-- offset.x = (offset.x or 0) + 0 editBox:SetFontObject("GameFontNormal")
-- offset.y = (offset.y or 0) - 12 editBox:SetText(Heimdall_Data.config.spotter.throttleTime)
-- PutBelow(a, b, offset) editBox:SetBackdrop({
-- end bgFile = "Interface/Tooltips/UI-Tooltip-Background",
-- editBox.PutRight = function(a, b, offset) edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
-- offset = offset or {} tile = true,
-- offset.x = (offset.x or 0) + 0 tileSize = 2,
-- offset.y = (offset.y or 0) - 8 edgeSize = 2,
-- PutRight(a, b, offset) insets = {
-- end left = 2,
-- return editBox right = 2,
-- end top = 2,
bottom = 2
}
})
editBox:SetBackdropColor(0.8, 0.8, 0.8, 1)
editBox:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
editBox:SetScript("OnEnterPressed", function()
onDone(editBox:GetText())
end)
editBox:SetScript("OnEscapePressed", function()
editBox:ClearFocus()
end)
editBox:SetScript("OnEditFocusLost", function()
onDone(editBox:GetText())
end)
return container
end
-- --
local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal") local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
@@ -325,6 +363,9 @@ function shared.Config.Init()
local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig", local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig",
configFrame.frame, 12, 20) configFrame.frame, 12, 20)
configFrame:Add(spotterConfigFrame.frame, 6, 3) configFrame:Add(spotterConfigFrame.frame, 6, 3)
local spotterTitle = spotterConfigFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
spotterTitle:SetText("Spotter")
spotterConfigFrame:Add(spotterTitle, 1, 12)
local spotterEnableButton = BasicButton("HeimdallSpotterConfigEnableButton", local spotterEnableButton = BasicButton("HeimdallSpotterConfigEnableButton",
spotterConfigFrame.frame, "Enable Spotter", function() spotterConfigFrame.frame, "Enable Spotter", function()
@@ -333,6 +374,14 @@ function shared.Config.Init()
end) end)
spotterConfigFrame:Add(spotterEnableButton, 1, 3) spotterConfigFrame:Add(spotterEnableButton, 1, 3)
configFrame.frame:Hide()
-- local testEditBox = CreateBasicSmallEditBox("HeimdallSpotterConfigTestEditBox",
-- UIParent, "Test", function(text)
-- print(text)
-- end)
-- testEditBox:SetHeight(200)
-- spotterConfigFrame:Add(testEditBox.frame, 2, 6)
--local spotterEveryoneButton = BasicButton("HeimdallSpotterEveryone", spotterConfigFrame, "Everyone", function(state) --local spotterEveryoneButton = BasicButton("HeimdallSpotterEveryone", spotterConfigFrame, "Everyone", function(state)
-- Heimdall_Data.config.spotter.everyone = state -- Heimdall_Data.config.spotter.everyone = state
--end) --end)