Files
wow-Heimdall/Modules/Config.lua
2025-01-07 00:20:27 +01:00

656 lines
24 KiB
Lua

local addonname, shared = ...
---@cast shared HeimdallShared
---@cast addonname string
---@param str string
---@return table<string, boolean>
local function StringToMap(str, deliminer)
if not str then return {} end
local map = {}
local parts = { strsplit(deliminer, str) }
for _, line in ipairs(parts) do
line = strtrim(line)
if line ~= "" then
map[line] = true
end
end
return map
end
---@param str string
---@return string[]
local function StringToArray(str, deliminer)
if not str then return {} end
local ret = {}
local array = { strsplit(deliminer, str) }
for i, line in ipairs(array) do
line = strtrim(line)
if line ~= "" then
ret[i] = line
end
end
return ret
end
---@param map table<any, any>
---@param deliminer string
---@return string
local function MapKeyToString(map, deliminer)
local str = ""
for k, _ in pairs(map) do
str = str .. k .. deliminer
end
return str
end
---@param map table<any, any>
---@param deliminer string
---@return string
local function MapValueToString(map, deliminer)
local str = ""
for _, v in pairs(map) do
str = str .. tostring(v) .. deliminer
end
return str
end
---@class GridFrame:Frame
---@field name string
---@field columns number
---@field frame Frame
---@field cellWidth number
---@field cellHeight number
---@field columnHeights table<number, number>
GridFrame = {
---@param name string
---@param parent Frame
---@param columns number
---@param cellHeight number
---@param size {w: number, h:number}?
---@return GridFrame
new = function(name, parent, columns, cellHeight, size)
local self = setmetatable({}, {
__index = GridFrame
})
self.frame = CreateFrame("Frame", name, parent)
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", parent, "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)
self.columns = columns
self.cellWidth = self.frame:GetWidth() / columns
self.cellHeight = cellHeight
self.columnHeights = {}
for i = 1, columns do
self.columnHeights[i] = 0
end
return self
end,
---@param self GridFrame
---@param frame Frame
---@param rowspan number
---@param colspan number
Add = function(self, frame, rowspan, colspan)
colspan = math.min(colspan, self.columns)
local bestColumn = nil
local bestRow = 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.columnHeights[c])
end
if currentMaxY < bestRow then
bestRow = currentMaxY
bestColumn = startColumn
end
end
if bestColumn then
frame:SetParent(self.frame)
frame.gridData = {
row = bestRow,
column = bestColumn,
colspan = colspan,
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 = bestColumn, bestColumn + colspan - 1 do
self.columnHeights[c] = self.columnHeights[c] + rowspan
end
else
print("No available space in the grid.")
end
end,
Recalculate = function(self)
local children = { self.frame:GetChildren() }
for _, child in pairs(children) do
if child.gridData then
child:SetPos()
-- else
-- print("Child has no grid data", child)
end
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
if tallestRow > 0 then
self.cellHeight = height / tallestRow
end
self:Recalculate()
end,
SetPoint = function(self, point, relativeTo, relativePoint, offsetX, offsetY)
self.frame:SetPoint(point, relativeTo, relativePoint, offsetX, offsetY)
self:Recalculate()
end,
SetParent = function(self, parent)
self.frame:SetParent(parent)
self:Recalculate()
end
}
---@class StaticGridFrame
---@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>>
StaticGridFrame = {
---@param name string
---@param parent Frame
---@param rows number
---@param columns number
---@param size {w: number, h:number}?
---@return StaticGridFrame
new = function(name, parent, rows, columns, size)
local self = setmetatable({}, {
__index = StaticGridFrame
})
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 StaticGridFrame
---@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.colspan = colspan
frame.rowspan = rowspan
print("Setting width", self.cellWidth * colspan)
frame:SetWidth(self.cellWidth * colspan)
print("Setting height", self.cellHeight * rowspan)
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,
MakeMovable = function(self)
self.frame:SetMovable(true)
self.frame:EnableMouse(true)
self.frame:RegisterForDrag("LeftButton")
self.frame:SetScript("OnDragStart", function(self)
self:StartMoving()
end)
self.frame:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
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 = width / self.columns
self:Recalculate()
end,
SetHeight = function(self, height)
self.frame:SetHeight(height)
self.cellHeight = height / self.rows
self:Recalculate()
end,
SetPoint = function(self, point, relativeTo, relativePoint, offsetX, offsetY)
self.frame:SetPoint(point, relativeTo, relativePoint, offsetX, offsetY)
self:Recalculate()
end,
SetParent = function(self, parent)
self.frame:SetParent(parent)
self:Recalculate()
end
}
local configFrame = StaticGridFrame.new("HeimdallConfig",
UIParent,
40, 12,
{ w = 1024 + 512, h = 1024 })
configFrame:MakeMovable()
--configFrame.frame:SetScript("OnKeyUp", function(self, key)
-- if key == "ESCAPE" then
-- self:Hide()
-- end
--end)
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 },
}
--HeimdallTestFrame = GridFrame.new("HeimdallPartyFrame", UIParent, 12, 20, { w = 1024, h = 1024 })
--for i = 1, 10 do
-- local frame = CreateFrame("Frame", "HeimdallPartyFrame" .. 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)
-- HeimdallTestFrame:Add(frame, 4, 2)
--end
--HeimdallTestFrame:SetHeight(128)
--HeimdallTestFrame:SetWidth(512)
--configFrame:Add(HeimdallTestFrame, 20, 12)
---@diagnostic disable-next-line: missing-fields
shared.Config = {}
function shared.Config.Init()
local buttonColors = {
enabled = { 0, 1, 0, 1 },
disabled = { 1, 0, 0, 1 }
}
---@param name string
---@param parent Frame
---@param onClick fun(): boolean
local function BasicButton(name, parent, text, onClick)
local button = CreateFrame("Frame", name, parent)
button:SetScript("OnMouseDown", function()
local res = onClick()
local color = res and buttonColors.enabled or buttonColors.disabled
button:SetBackdropColor(unpack(color))
end)
button:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true,
tileSize = 2,
edgeSize = 12,
insets = {
left = 2,
right = 2,
top = 2,
bottom = 2
}
})
button.UpdateColor = function(self, state)
local color = state and buttonColors.enabled or buttonColors.disabled
self:SetBackdropColor(unpack(color))
end
--spotterEnableButton:SetChecked(Heimdall_Data.config.spotter.enabled)
local spotterEnableButtonLabel = button:CreateFontString(nil, "OVERLAY", "GameFontNormal")
spotterEnableButtonLabel:SetText(text)
spotterEnableButtonLabel:SetAllPoints(button)
return button
end
---@param name string
---@param parent Frame
---@param text string
---@param onDone fun(editBox: Frame)
local function CreateBasicSmallEditBox(name, parent, text, initialValue, onDone)
local container = GridFrame.new(name, parent, 1, 1)
local editBoxFrame = CreateFrame("Frame", name .. "EditBoxFrame", container.frame)
local editBox = CreateFrame("EditBox", name .. "EditBox", editBoxFrame)
local textFrame = CreateFrame("Frame", name .. "TextFrame", container.frame)
local textElement = textFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
textElement:SetText(text)
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 = 12,
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:SetTextInsets(6, 6, 0, 0)
editBox:SetScript("OnEnterPressed", function() editBox:ClearFocus() end)
editBox:SetScript("OnEscapePressed", function() editBox:ClearFocus() end)
editBox:SetScript("OnEditFocusLost", function() onDone(editBox) end)
editBox:SetText(initialValue)
container:Add(textFrame, 1, 1)
container:Add(editBox, 2, 1)
textElement:SetPoint("TOPLEFT", textFrame, "TOPLEFT", 2, -2)
return container
end
---@param name string
---@param parent Frame
---@param text string
---@param onDone fun(editBox: Frame)
local function CreateBasicBigEditBox(name, parent, text, initialValue, onDone)
local container = GridFrame.new(name, parent, 1, 100)
local editBoxFrame = CreateFrame("Frame", name .. "EditBoxFrame", container.frame)
local editBox = CreateFrame("EditBox", name .. "EditBox", editBoxFrame)
local textFrame = CreateFrame("Frame", name .. "TextFrame", container.frame)
local textElement = textFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
textElement:SetText(text)
editBoxFrame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true,
tileSize = 2,
edgeSize = 12,
insets = {
left = 2,
right = 2,
top = 2,
bottom = 2
}
})
editBoxFrame:SetBackdropColor(0.8, 0.8, 0.8, 1)
editBoxFrame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
editBox:SetAllPoints(editBoxFrame)
editBox:SetAutoFocus(false)
editBox:SetFontObject("GameFontNormal")
editBox:SetText(initialValue)
editBox:SetTextInsets(6, 6, 6, 6)
editBox:SetMultiLine(true)
editBox:SetSize(280, 100)
editBox:SetMaxLetters(100000)
local oldSetHeight = editBox.SetHeight
editBox.SetHeight = function(self, height)
oldSetHeight(self, height)
print("Set height", height)
end
editBox:SetScript("OnEscapePressed", function() editBox:ClearFocus() end)
editBox:SetScript("OnEditFocusLost", function() onDone(editBox) end)
container:Add(textFrame, 1, 1)
container:Add(editBoxFrame, 7, 1)
textElement:SetPoint("TOPLEFT", textFrame, "TOPLEFT", 2, -2)
return container
end
local title = configFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetText("Heimdall Config")
configFrame:Add(title, 1, 12)
do
local spotterConfigFrame = GridFrame.new("HeimdallSpotterConfig",
UIParent, 12, 20)
configFrame:Add(spotterConfigFrame, 4, 3)
local title = spotterConfigFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetText("Spotter")
spotterConfigFrame:Add(title, 1, 12)
local enableButton = BasicButton("HeimdallSpotterConfigEnableButton",
spotterConfigFrame.frame, "Enabled", function()
Heimdall_Data.config.spotter.enabled = not Heimdall_Data.config.spotter.enabled
return Heimdall_Data.config.spotter.enabled
end)
enableButton:UpdateColor(Heimdall_Data.config.spotter.enabled)
spotterConfigFrame:Add(enableButton, 1, 6)
local everyoneButton = BasicButton("HeimdallSpotterConfigEveryoneButton",
spotterConfigFrame.frame, "Everyone", function()
Heimdall_Data.config.spotter.everyone = not Heimdall_Data.config.spotter.everyone
return Heimdall_Data.config.spotter.everyone
end)
everyoneButton:UpdateColor(Heimdall_Data.config.spotter.everyone)
spotterConfigFrame:Add(everyoneButton, 1, 6)
local hostileButton = BasicButton("HeimdallSpotterConfigHostileButton",
spotterConfigFrame.frame, "Hostile", function()
Heimdall_Data.config.spotter.hostile = not Heimdall_Data.config.spotter.hostile
return Heimdall_Data.config.spotter.hostile
end)
hostileButton:UpdateColor(Heimdall_Data.config.spotter.hostile)
spotterConfigFrame:Add(hostileButton, 1, 4)
local allianceButton = BasicButton("HeimdallSpotterConfigAllianceButton",
spotterConfigFrame.frame, "Alliance", function()
Heimdall_Data.config.spotter.alliance = not Heimdall_Data.config.spotter.alliance
return Heimdall_Data.config.spotter.alliance
end)
allianceButton:UpdateColor(Heimdall_Data.config.spotter.alliance)
spotterConfigFrame:Add(allianceButton, 1, 4)
local stinkyButton = BasicButton("HeimdallSpotterConfigStinkyButton",
spotterConfigFrame.frame, "Stinky", function()
Heimdall_Data.config.spotter.stinky = not Heimdall_Data.config.spotter.stinky
return Heimdall_Data.config.spotter.stinky
end)
stinkyButton:UpdateColor(Heimdall_Data.config.spotter.stinky)
spotterConfigFrame:Add(stinkyButton, 1, 4)
local notifyChannel = CreateBasicSmallEditBox("HeimdallSpotterConfigNotifyChannel",
spotterConfigFrame.frame, "Notify Channel",
Heimdall_Data.config.spotter.notifyChannel,
function(self)
local text = self:GetText()
if string.match(text, "%S+") then
Heimdall_Data.config.spotter.notifyChannel = text
print("Notify channel set to", tostring(text))
else
print("Invalid channel name", tostring(text))
self:SetText(Heimdall_Data.config.spotter.notifyChannel)
end
end)
spotterConfigFrame:Add(notifyChannel, 2, 4)
local zoneOverride = CreateBasicSmallEditBox("HeimdallSpotterConfigZoneOverride",
spotterConfigFrame.frame, "Zone Override",
Heimdall_Data.config.spotter.zoneOverride,
function(self)
local text = self:GetText()
if string.match(text, "%S+") then
Heimdall_Data.config.spotter.zoneOverride = text
print("Zone override set to", tostring(text))
else
print("Invalid zone override", tostring(text))
self:SetText(Heimdall_Data.config.spotter.zoneOverride)
end
end)
spotterConfigFrame:Add(zoneOverride, 2, 4)
local throttleTime = CreateBasicSmallEditBox("HeimdallSpotterConfigThrottleTime",
spotterConfigFrame.frame, "Throttle Time",
Heimdall_Data.config.spotter.throttleTime,
function(self)
local text = self:GetText()
if string.match(text, "%d+") then
Heimdall_Data.config.spotter.throttleTime = tonumber(text)
else
print("Invalid throttle time", tostring(text))
self:SetText(Heimdall_Data.config.spotter.throttleTime)
end
end)
spotterConfigFrame:Add(throttleTime, 2, 4)
end
do
local whoerConfigFrame = GridFrame.new("HeimdallWhoerConfig",
UIParent, 12, 20)
configFrame:Add(whoerConfigFrame, 8, 3)
local title = whoerConfigFrame.frame:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetText("Whoer")
whoerConfigFrame:Add(title, 1, 12)
local enableButton = BasicButton("HeimdallWhoerConfigEnableButton",
whoerConfigFrame.frame, "Enabled", function()
Heimdall_Data.config.who.enabled = not Heimdall_Data.config.who.enabled
return Heimdall_Data.config.who.enabled
end)
enableButton:UpdateColor(Heimdall_Data.config.who.enabled)
whoerConfigFrame:Add(enableButton, 1, 6)
local doWhisperButton = BasicButton("HeimdallWhoerConfigDoWhisperButton",
whoerConfigFrame.frame, "Do Whisper", function()
Heimdall_Data.config.who.doWhisper = not Heimdall_Data.config.who.doWhisper
return Heimdall_Data.config.who.doWhisper
end)
doWhisperButton:UpdateColor(Heimdall_Data.config.who.doWhisper)
whoerConfigFrame:Add(doWhisperButton, 1, 6)
local notifyChannel = CreateBasicSmallEditBox("HeimdallWhoerConfigNotifyChannel",
whoerConfigFrame.frame, "Notify Channel",
Heimdall_Data.config.who.notifyChannel,
function(self)
local text = self:GetText()
if string.match(text, "%S+") then
Heimdall_Data.config.who.notifyChannel = text
print("Notify channel set to", tostring(text))
else
print("Invalid channel name", tostring(text))
self:SetText(Heimdall_Data.config.who.notifyChannel)
end
end)
whoerConfigFrame:Add(notifyChannel, 2, 6)
local ttl = CreateBasicSmallEditBox("HeimdallWhoerConfigTTL",
whoerConfigFrame.frame, "TTL",
Heimdall_Data.config.who.ttl,
function(self)
local text = self:GetText()
if string.match(text, "%d+") then
Heimdall_Data.config.who.ttl = tonumber(text)
print("TTL set to", tostring(text))
else
print("Invalid TTL", tostring(text))
self:SetText(Heimdall_Data.config.who.ttl)
end
end)
whoerConfigFrame:Add(ttl, 2, 6)
local ignored = CreateBasicBigEditBox("HeimdallWhoerConfigIgnored",
whoerConfigFrame.frame, "Ignored",
MapKeyToString(Heimdall_Data.config.who.ignored or {}, "\n"),
function(self)
local ignored = StringToMap(self:GetText(), "\n")
Heimdall_Data.config.who.ignored = ignored
end)
whoerConfigFrame:Add(ignored, 6, 6)
local zoneNotifyFor = CreateBasicBigEditBox("HeimdallWhoerConfigZoneNotifyFor",
whoerConfigFrame.frame, "Zone Notify For",
MapKeyToString(Heimdall_Data.config.who.zoneNotifyFor or {}, "\n"),
function(self)
local zoneNotifyFor = StringToMap(self:GetText(), "\n")
Heimdall_Data.config.who.zoneNotifyFor = zoneNotifyFor
end)
whoerConfigFrame:Add(zoneNotifyFor, 6, 6)
end
print("Heimdall - Config loaded")
end
SlashCmdList["HEIMDALL_CONFIG"] = function()
configFrame:Show()
end
SLASH_HEIMDALL_CONFIG1 = "/heimdall_config"
SLASH_HEIMDALL_CONFIG2 = "/hc"