Implement a grid frame of sorts

This commit is contained in:
2025-01-06 16:57:51 +01:00
parent e425fa9209
commit 4652c60e64

View File

@@ -2,199 +2,286 @@ local addonname, shared = ...
---@cast shared HeimdallShared ---@cast shared HeimdallShared
---@cast addonname string ---@cast addonname string
---@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>>
GridFrame = {
---@param name string
---@param parent Frame
---@param rows number
---@param columns number
---@param size {w: number, h:number}?
---@return GridFrame
new = function(name, parent, rows, columns, 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:SetPoint("CENTER", UIParent, "CENTER")
self.frame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
tileSize = 64,
tile = true
})
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
end
return self
end,
---@param self GridFrame
---@param frame Frame
---@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
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
end
end
print("No available space in the grid.")
end
}
local configFrame = CreateFrame("Frame", "HeimdallConfig", UIParent) local configFrame = CreateFrame("Frame", "HeimdallConfig", UIParent)
---@diagnostic disable-next-line: missing-fields ---@diagnostic disable-next-line: missing-fields
shared.Config = {} shared.Config = {}
function shared.Config.Init() function shared.Config.Init()
local function PutBelow(a, b, offset) --local function PutBelow(a, b, offset)
local x = 0 -- local x = 0
local y = -24 -- local y = -24
if offset and offset.x then -- if offset and offset.x then
x = x + offset.x -- x = x + offset.x
end -- end
if offset and offset.y then -- if offset and offset.y then
y = y + offset.y -- y = y + offset.y
end -- end
a:SetPoint("LEFT", b, "LEFT", x, y) -- a:SetPoint("LEFT", b, "LEFT", x, y)
end --end
local function PutRight(a, b, offset) --local function PutRight(a, b, offset)
local x = 128 -- local x = 128
local y = 0 -- local y = 0
if offset and offset.x then -- if offset and offset.x then
x = x + offset.x -- x = x + offset.x
end -- end
if offset and offset.y then -- if offset and offset.y then
y = y + offset.y -- y = y + offset.y
end -- end
a:SetPoint("LEFT", b, "LEFT", x, y) -- a:SetPoint("LEFT", b, "LEFT", x, y)
end --end
---@param name string -----@param name string
---@param parent Frame -----@param parent Frame
---@param text string -----@param text string
---@param onClick fun(state: boolean) -----@param onClick fun(state: boolean)
local function BasicButton(name, parent, text, onClick) --local function BasicButton(name, parent, text, onClick)
local button = CreateFrame("CheckButton", name, parent, "UICheckButtonTemplate") -- local button = CreateFrame("CheckButton", name, parent, "UICheckButtonTemplate")
button.text = button:CreateFontString(nil, "OVERLAY", "GameFontNormal") -- button.text = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
button.text:SetText(text) -- button.text:SetText(text)
button.text:SetPoint("LEFT", button, "RIGHT", 0, 0) -- button.text:SetPoint("LEFT", button, "RIGHT", 0, 0)
button:SetSize(24, 24) -- button:SetSize(24, 24)
button:SetScript("OnClick", function() -- button:SetScript("OnClick", function()
onClick(button:GetChecked()) -- onClick(button:GetChecked())
end) -- end)
button.PutBelow = PutBelow -- button.PutBelow = PutBelow
button.PutRight = PutRight -- button.PutRight = PutRight
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 editBox = CreateFrame("EditBox", name, parent)
editBox.text = editBox:CreateFontString(nil, "OVERLAY", "GameFontNormal") -- editBox.text = editBox:CreateFontString(nil, "OVERLAY", "GameFontNormal")
editBox.text:SetText(text) -- editBox.text:SetText(text)
editBox.text:SetPoint("TOPLEFT", editBox, "TOPLEFT", 0, 8 + 4) -- editBox.text:SetPoint("TOPLEFT", editBox, "TOPLEFT", 0, 8 + 4)
editBox:SetNumeric(true) -- editBox:SetNumeric(true)
editBox:SetSize(128 - 8, 24) -- editBox:SetSize(128 - 8, 24)
editBox:SetAutoFocus(false) -- editBox:SetAutoFocus(false)
editBox:SetFontObject("GameFontNormal") -- editBox:SetFontObject("GameFontNormal")
editBox:SetText(Heimdall_Data.config.spotter.throttleTime) -- editBox:SetText(Heimdall_Data.config.spotter.throttleTime)
editBox:SetBackdrop({ -- editBox: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 = 2, -- tileSize = 2,
edgeSize = 2, -- edgeSize = 2,
insets = { -- insets = {
left = 2, -- left = 2,
right = 2, -- right = 2,
top = 2, -- top = 2,
bottom = 2 -- bottom = 2
} -- }
}) -- })
editBox:SetBackdropColor(0.8, 0.8, 0.8, 1) -- editBox:SetBackdropColor(0.8, 0.8, 0.8, 1)
editBox:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) -- editBox:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
editBox:SetScript("OnEnterPressed", function() -- editBox:SetScript("OnEnterPressed", function()
onDone(editBox:GetText()) -- onDone(editBox:GetText())
end) -- end)
editBox:SetScript("OnEscapePressed", function() -- editBox:SetScript("OnEscapePressed", function()
editBox:ClearFocus() -- editBox:ClearFocus()
end) -- end)
editBox:SetScript("OnEditFocusLost", function() -- editBox:SetScript("OnEditFocusLost", function()
onDone(editBox:GetText()) -- onDone(editBox:GetText())
end) -- end)
editBox.PutBelow = function(a, b, offset) -- editBox.PutBelow = function(a, b, offset)
offset = offset or {} -- offset = offset or {}
offset.x = (offset.x or 0) + 0 -- offset.x = (offset.x or 0) + 0
offset.y = (offset.y or 0) - 12 -- offset.y = (offset.y or 0) - 12
PutBelow(a, b, offset) -- PutBelow(a, b, offset)
end -- end
editBox.PutRight = function(a, b, offset) -- editBox.PutRight = function(a, b, offset)
offset = offset or {} -- offset = offset or {}
offset.x = (offset.x or 0) + 0 -- offset.x = (offset.x or 0) + 0
offset.y = (offset.y or 0) - 8 -- offset.y = (offset.y or 0) - 8
PutRight(a, b, offset) -- PutRight(a, b, offset)
end -- end
return editBox -- return editBox
end --end
configFrame:SetSize(256 + 256, 512) --configFrame:SetSize(256 + 256, 512)
configFrame:SetPoint("CENTER") --configFrame:SetPoint("CENTER")
configFrame:SetFrameStrata("HIGH") --configFrame:SetFrameStrata("HIGH")
configFrame:EnableMouse(true) --configFrame:EnableMouse(true)
configFrame:SetMovable(true) --configFrame:SetMovable(true)
configFrame:SetResizable(false) --configFrame:SetResizable(false)
configFrame:SetBackdrop({ --configFrame: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
} -- }
}) --})
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:SetMovable(true) --configFrame:SetMovable(true)
configFrame:EnableMouse(true) --configFrame:EnableMouse(true)
configFrame:RegisterForDrag("LeftButton") --configFrame:RegisterForDrag("LeftButton")
configFrame:SetScript("OnDragStart", function(self) --configFrame:SetScript("OnDragStart", function(self)
self:StartMoving() -- self:StartMoving()
end) --end)
configFrame:SetScript("OnDragStop", function(self) --configFrame:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing() -- self:StopMovingOrSizing()
end) --end)
configFrame:SetScript("OnShow", function(self) --configFrame:SetScript("OnShow", function(self)
self:SetScale(1) -- self:SetScale(1)
end) --end)
local title = configFrame:CreateFontString(nil, "ARTWORK", "GameFontNormal") --local title = configFrame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetText("Heimdall Config") --title:SetText("Heimdall Config")
title:SetPoint("TOP", configFrame, "TOP", 0, -8) --title:SetPoint("TOP", configFrame, "TOP", 0, -8)
local spotterConfigFrame = CreateFrame("Frame", "HeimdallSpotterConfig", configFrame) --local spotterConfigFrame = CreateFrame("Frame", "HeimdallSpotterConfig", configFrame)
spotterConfigFrame:SetSize(256, 128) --spotterConfigFrame:SetSize(256, 128)
spotterConfigFrame:SetPoint("TOPLEFT", configFrame, "TOPLEFT", 8, -24) --spotterConfigFrame:SetPoint("TOPLEFT", configFrame, "TOPLEFT", 8, -24)
spotterConfigFrame: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:SetBackdropColor(0, 0, 0, 0.8) --spotterConfigFrame:SetBackdropColor(0, 0, 0, 0.8)
spotterConfigFrame: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)
Heimdall_Data.config.spotter.enabled = state -- Heimdall_Data.config.spotter.enabled = state
end) -- end)
spotterEnableButton:SetPoint("TOPLEFT", spotterConfigFrame, "TOPLEFT", 4, -4) --spotterEnableButton:SetPoint("TOPLEFT", spotterConfigFrame, "TOPLEFT", 4, -4)
spotterEnableButton:SetChecked(Heimdall_Data.config.spotter.enabled) --spotterEnableButton:SetChecked(Heimdall_Data.config.spotter.enabled)
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)
spotterEveryoneButton:PutRight(spotterEnableButton) --spotterEveryoneButton:PutRight(spotterEnableButton)
spotterEveryoneButton:SetChecked(Heimdall_Data.config.spotter.everyone) --spotterEveryoneButton:SetChecked(Heimdall_Data.config.spotter.everyone)
local spotterHostileButton = BasicButton("HeimdallSpotterHostile", spotterConfigFrame, "Hostile", function(state) --local spotterHostileButton = BasicButton("HeimdallSpotterHostile", spotterConfigFrame, "Hostile", function(state)
Heimdall_Data.config.spotter.hostile = state -- Heimdall_Data.config.spotter.hostile = state
end) --end)
spotterHostileButton:PutBelow(spotterEnableButton) --spotterHostileButton:PutBelow(spotterEnableButton)
spotterHostileButton:SetChecked(Heimdall_Data.config.spotter.hostile) --spotterHostileButton:SetChecked(Heimdall_Data.config.spotter.hostile)
local spotterAllianceButton = BasicButton("HeimdallSpotterAlliance", spotterConfigFrame, "Alliance", function(state) --local spotterAllianceButton = BasicButton("HeimdallSpotterAlliance", spotterConfigFrame, "Alliance", function(state)
Heimdall_Data.config.spotter.alliance = state -- Heimdall_Data.config.spotter.alliance = state
end) --end)
spotterAllianceButton:PutRight(spotterHostileButton) --spotterAllianceButton:PutRight(spotterHostileButton)
spotterAllianceButton:SetChecked(Heimdall_Data.config.spotter.alliance) --spotterAllianceButton:SetChecked(Heimdall_Data.config.spotter.alliance)
local spotterStinkyButton = BasicButton("HeimdallSpotterStinky", spotterConfigFrame, "Stinky", function(state) --local spotterStinkyButton = BasicButton("HeimdallSpotterStinky", spotterConfigFrame, "Stinky", function(state)
Heimdall_Data.config.spotter.stinky = state -- Heimdall_Data.config.spotter.stinky = state
end) --end)
spotterStinkyButton:PutBelow(spotterHostileButton) --spotterStinkyButton:PutBelow(spotterHostileButton)
spotterStinkyButton:SetChecked(Heimdall_Data.config.spotter.stinky) --spotterStinkyButton:SetChecked(Heimdall_Data.config.spotter.stinky)
local spotterThrottleBox = CreateBasicSmallEditBox("HeimdallSpotterThrottle", spotterConfigFrame, "Throttle Time", --local spotterThrottleBox = CreateBasicSmallEditBox("HeimdallSpotterThrottle", spotterConfigFrame, "Throttle Time",
function(text) -- function(text)
Heimdall_Data.config.spotter.throttleTime = tonumber(text) -- Heimdall_Data.config.spotter.throttleTime = tonumber(text)
end) -- end)
spotterThrottleBox:PutRight(spotterStinkyButton) --spotterThrottleBox:PutRight(spotterStinkyButton)
-- ---@type table -- ---@type table
-- local spotterEnableButton = CreateFrame("Button", "HeimdallSpotterEnable", configFrame, "UIPanelButtonTemplate") -- local spotterEnableButton = CreateFrame("Button", "HeimdallSpotterEnable", configFrame, "UIPanelButtonTemplate")
@@ -233,7 +320,7 @@ function shared.Config.Init()
-- largeEditBox:ClearFocus() -- largeEditBox:ClearFocus()
-- end) -- end)
configFrame:Show() --configFrame:Hide()
print("Heimdall - Config loaded") print("Heimdall - Config loaded")
end end