Skip to content

Commit

Permalink
Fixes and code updates.
Browse files Browse the repository at this point in the history
Start adding checks to everything. This is an ongoing process. We also fixed 2 reported bugs.
  • Loading branch information
Kkthnx committed Nov 26, 2024
1 parent 6d9c97c commit 1a0f842
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 172 deletions.
45 changes: 14 additions & 31 deletions KkthnxUI/Core/API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,12 @@ end

-- Kill Function
local function Kill(object)
-- Check if the object has an "UnregisterAllEvents" method
if object.UnregisterAllEvents then
-- Unregister all events for the object
object:UnregisterAllEvents()
-- Set the object's parent to K.UIFrameHider (likely a hidden frame used for hiding objects)
object:SetParent(K.UIFrameHider)
else
-- If the object does not have an "UnregisterAllEvents" method, set its "Show" method to its "Hide" method
object.Show = object.Hide
end
-- Hide the object
object:Hide()
end

Expand Down Expand Up @@ -547,51 +542,40 @@ end
local function addapi(object)
local mt = getmetatable(object).__index

if not object.CreateBorder then
if not mt.CreateBorder then
mt.CreateBorder = CreateBorder
end

if not object.CreateBackdrop then
if not mt.CreateBackdrop then
mt.CreateBackdrop = CreateBackdrop
end

if not object.CreateShadow then
if not mt.CreateShadow then
mt.CreateShadow = CreateShadow
end

if not object.Kill then
if not mt.Kill then
mt.Kill = Kill
end

if not object.SkinButton then
if not mt.SkinButton then
mt.SkinButton = SkinButton
end

if not object.StripTextures then
if not mt.StripTextures then
mt.StripTextures = StripTextures
end

if not object.StyleButton then
if not mt.StyleButton then
mt.StyleButton = StyleButton
end

if not object.SkinCloseButton then
if not mt.SkinCloseButton then
mt.SkinCloseButton = SkinCloseButton
end

if not object.SkinCheckBox then
if not mt.SkinCheckBox then
mt.SkinCheckBox = SkinCheckBox
end

if not object.SkinEditBox then
if not mt.SkinEditBox then
mt.SkinEditBox = SkinEditBox
end

if not object.SkinScrollBar then
if not mt.SkinScrollBar then
mt.SkinScrollBar = SkinScrollBar
end

if not object.HideBackdrop then
if not mt.HideBackdrop then
mt.HideBackdrop = HideBackdrop
end
end
Expand All @@ -606,10 +590,9 @@ addapi(object:CreateMaskTexture())

object = EnumerateFrames()
while object do
local objType = object:GetObjectType()
if not object:IsForbidden() and not handled[objType] then
if not object:IsForbidden() and not handled[object:GetObjectType()] then
addapi(object)
handled[objType] = true
handled[object:GetObjectType()] = true
end

object = EnumerateFrames(object)
Expand Down
17 changes: 10 additions & 7 deletions KkthnxUI/Core/Border.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ local borderSizeDefault = 10
local getBorderSize = (borderStyle == "KkthnxUI") and borderSizeKkthnx or borderSizeDefault

local function getTile(border, w, h)
return (w + 2 * border.__offset) / getBorderSize
local borderSize = getBorderSize
return (w + 2 * border.__offset) / borderSize
end

local function setTextureCoordinates(border, tile)
Expand All @@ -34,6 +35,10 @@ end

local function onSizeChanged(self, w, h)
local border = objectToWidgets[self]
if not border then
return
end

local tile = getTile(border, w, h)
setTextureCoordinates(border, tile)
end
Expand Down Expand Up @@ -91,9 +96,8 @@ function Module:SetSize(size)
end

function Module:Hide()
local len = #borderSections
for i = 1, len do
self[borderSections[i].name]:Hide()
for _, section in ipairs(borderSections) do
self[section.name]:Hide()
end
end

Expand All @@ -116,9 +120,8 @@ function Module:GetVertexColor()
end

function Module:SetVertexColor(r, g, b, a)
local len = #borderSections
for i = 1, len do
self[borderSections[i].name]:SetVertexColor(r, g, b, a)
for _, section in ipairs(borderSections) do
self[section.name]:SetVertexColor(r, g, b, a)
end
end

Expand Down
102 changes: 66 additions & 36 deletions KkthnxUI/Core/Functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ local tonumber = tonumber
local type = type
local unpack = unpack

local CLASS_ICON_TCOORDS = CLASS_ICON_TCOORDS
local C_Map_GetWorldPosFromMapPos = C_Map.GetWorldPosFromMapPos
local CreateVector2D = CreateVector2D
local ENCHANTED_TOOLTIP_LINE = ENCHANTED_TOOLTIP_LINE
Expand Down Expand Up @@ -62,13 +61,17 @@ do
end

function K.Round(number, idp)
-- Set the default number of decimal places to 0 if none is specified
if type(number) ~= "number" then
return
end

if idp ~= nil and type(idp) ~= "number" then
return
end

idp = idp or 0
local mult = 10 ^ idp
-- Round the number to the specified number of decimal places
-- by first multiplying it by 10 to the power of idp,
-- then rounding it to the nearest whole number using math.floor,
-- and finally dividing it by 10 to the power of idp

return math.floor(number * mult + 0.5) / mult
end
end
Expand Down Expand Up @@ -152,37 +155,37 @@ end

-- Table-related Functions
do
function K.CopyTable(source, target)
-- Loop through all key-value pairs in the source table
function K.CopyTable(source, target, seen)
target = target or {}
seen = seen or {}

if seen[source] then
return seen[source]
end

seen[source] = target

for key, value in pairs(source) do
-- If the value is a table, copy its contents recursively
if type(value) == "table" then
-- If there's no key in the target table, create it
if not target[key] then
target[key] = {}
end
-- Copy the contents of the sub-table
for k in pairs(value) do
target[key][k] = value[k]
end
target[key] = K.CopyTable(value, target[key] or {}, seen)
else
-- If the value is not a table, simply copy it
target[key] = value
end
end

return target
end

function K.SplitList(list, variable, cleanup)
-- Wipe the table if cleanup is true
variable = variable or ""

if cleanup then
table_wipe(list)
wipe(list)
end

for word in string.gmatch(variable, "%S+") do
-- Convert word to number if it is numeric
word = tonumber(word) or word
-- Add word to the list
table.insert(list, word)
for word in gmatch(variable, "%S+") do
word = tonumber(word) or word -- Convert to number if possible
list[word] = true
end
end
end
Expand Down Expand Up @@ -298,6 +301,11 @@ do
end

function K.CheckAddOnState(addon)
if type(addon) ~= "string" then
return false
end

-- Perform the check
return K.AddOns[string_lower(addon)] or false
end

Expand Down Expand Up @@ -711,10 +719,18 @@ end
do
function K.CreateMoverFrame(self, parent, saved)
local frame = parent or self
if not (frame and type(frame) == "table" and frame.SetMovable) then
return -- Exit if `frame` is invalid
end

frame:SetMovable(true)
frame:SetUserPlaced(true)
frame:SetClampedToScreen(true)

if not (self and type(self) == "table" and self.EnableMouse) then
return -- Exit if `self` is invalid
end

self:EnableMouse(true)
self:RegisterForDrag("LeftButton")
self:SetScript("OnDragStart", function()
Expand All @@ -728,15 +744,25 @@ do
end

local orig, _, tar, x, y = frame:GetPoint()
KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"][frame:GetName()] = { orig, "UIParent", tar, x, y }
if KkthnxUIDB.Variables and KkthnxUIDB.Variables[K.Realm] and KkthnxUIDB.Variables[K.Realm][K.Name] then
KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"] = KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"] or {}
KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"][frame:GetName()] = { orig, "UIParent", tar, x, y }
end
end)
end

function K.RestoreMoverFrame(self)
if not (self and type(self) == "table" and self.GetName) then
return -- Exit if `self` is invalid
end

local name = self:GetName()
if name and KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"][name] then
self:ClearAllPoints()
self:SetPoint(unpack(KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"][name]))
if name and KkthnxUIDB.Variables and KkthnxUIDB.Variables[K.Realm] and KkthnxUIDB.Variables[K.Realm][K.Name] then
local anchorData = KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"] and KkthnxUIDB.Variables[K.Realm][K.Name]["TempAnchor"][name]
if anchorData then
self:ClearAllPoints()
self:SetPoint(unpack(anchorData))
end
end
end

Expand Down Expand Up @@ -776,6 +802,10 @@ end
-- Interface Option Functions
do
function K.HideInterfaceOption(self)
if not self then
return
end

self:SetAlpha(0)
self:SetScale(0.0001)
end
Expand Down Expand Up @@ -832,10 +862,7 @@ end

-- Map Position and Money Formatting Functions
do
-- Maps rectangles for storing positional information
local mapRects = {}

-- Temporary 2D vector for calculations
local tempVec2D = CreateVector2D(0, 0)
function K.GetPlayerMapPos(mapID)
if not mapID then
Expand All @@ -857,15 +884,18 @@ do

mapRect = { pos1, pos2 }
mapRect[2]:Subtract(mapRect[1])

mapRects[mapID] = mapRect
end
tempVec2D:Subtract(mapRect[1])

tempVec2D:Subtract(mapRect[1])
return tempVec2D.y / mapRect[2].y, tempVec2D.x / mapRect[2].x
end

function K.FormatMoney(amount)
if type(amount) ~= "number" then
return "Invalid amount" -- Handle non-numeric input gracefully
end

local coppername = "|cffeda55fc|r"
local goldname = "|cffffd700g|r"
local silvername = "|cffc7c7cfs|r"
Expand All @@ -876,8 +906,8 @@ do
local copper = math_floor(mod(value, 100))

if gold > 0 then
-- stylua: ignore
return string_format("%s%s %02d%s %02d%s", BreakUpLargeNumbers(gold), goldname, silver, silvername, copper, coppername)
-- stylua: ignore
return string_format("%s%s %02d%s %02d%s", BreakUpLargeNumbers(gold), goldname, silver, silvername, copper, coppername)
elseif silver > 0 then
return string_format("%d%s %02d%s", silver, silvername, copper, coppername)
else
Expand Down
3 changes: 0 additions & 3 deletions KkthnxUI/Core/Install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ local Module = K:NewModule("Installer")
-- Frame and UI Elements
local CreateFrame = CreateFrame
local UIParent = UIParent
local UIErrorsFrame = UIErrorsFrame

-- Chat Functions and Variables
local ChangeChatColor = ChangeChatColor
local ChatConfig_UpdateChatSettings = ChatConfig_UpdateChatSettings
local ChatFrame_AddMessageGroup = ChatFrame_AddMessageGroup
local ChatFrame_RemoveAllMessageGroups = ChatFrame_RemoveAllMessageGroups
Expand All @@ -21,7 +19,6 @@ local FCF_ResetChatWindows = FCF_ResetChatWindows
local FCF_SetChatWindowFontSize = FCF_SetChatWindowFontSize
local FCF_SetLocked = FCF_SetLocked
local FCF_SetWindowName = FCF_SetWindowName
local ToggleChatColorNamesByClassGroup = ToggleChatColorNamesByClassGroup

-- Game and System Settings
local InCombatLockdown = InCombatLockdown
Expand Down
Loading

0 comments on commit 1a0f842

Please sign in to comment.