-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.lua
More file actions
144 lines (126 loc) · 4.53 KB
/
Copy pathCommon.lua
File metadata and controls
144 lines (126 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---@class CUF
local CUF = select(2, ...)
---@class CUF.API
local API = CUF.API
--- Returns a frame by either its unit name or frame name.
---
--- This function searches for the frame using the given unit name (e.g., "player")
--- or the full frame name (e.g., "CUF_Player"). If no valid frame is found, it returns `nil`.
---
--- Example usage:
--- ```
--- local frame = API:GetUnitFrame("player")
--- local frame = API:GetUnitFrame("CUF_Player")
--- ```
---@param name string Unit or frame name
---@return CUFUnitButton? unitFrame corresponding unit frame or nil if not found
---@return string? lowerCaseUnitName lower case unit name
function API:GetUnitFrame(name)
if not API:ValidateParms("GetUnitFrame", { { name, "string", "name" } }) then return end
local unitFrame = CUF.unitButtons[name]
if not unitFrame then
unitFrame = _G[name]
if not unitFrame then
CUF:Warn("Cannot find valid unit frame for '" .. name .. "'.")
return
end
end
return unitFrame, unitFrame._baseUnit
end
--- Returns all unit frames as an indexed table.
---
--- This function retrieves all unit frames and returns them in a table where the keys
--- are unit names and the values are the corresponding unit frames.
---
--- Example usage:
--- ```
--- local frames = API:GetAllUnitFrames()
--- ```
--- @return table<string, CUFUnitButton> unitFrames table mapping unit names to their respective frames
function API:GetAllUnitFrames()
return CUF.unitButtons
end
------------------------------------------------------------
-- MARK: Validation
-- Private functions that the user should not call directly.
------------------------------------------------------------
---@param point FramePoint
---@return true|string
local function IsValidPoint(point)
local isValidPoint = type(point) == "string" and
(point == "TOPLEFT"
or point == "TOPRIGHT"
or point == "BOTTOMLEFT"
or point == "BOTTOMRIGHT"
or point == "CENTER"
or point == "TOP"
or point == "BOTTOM"
or point == "LEFT"
or point == "RIGHT")
if not isValidPoint then
return "FramePoint"
end
return isValidPoint
end
---@param frame Frame
---@return true|string
local function IsValidFrame(frame)
local isValidFrame = type(frame) == "table" and
frame.GetName ~= nil
if not isValidFrame then
return "Frame"
end
return isValidFrame
end
--- Validates the types of the given parameters.
---@param functionName string The name of the function being called.
---@param params table A table of parameters to check, with each entry being {value, expectedType, paramName}.
---@return boolean isValid all types are valid.
function API:ValidateParms(functionName, params)
local errorMsg
for _, param in ipairs(params) do
local value, expectedType, paramName = unpack(param)
local maybeErr
if type(value) == "nil" then
maybeErr = string.format("Missing param '%s'.", paramName)
elseif type(expectedType) == "function" then
local isValid = expectedType(value)
if type(isValid) == "string" then
maybeErr = string.format("Invalid type for param '%s'. Expected type '%s'.",
paramName,
isValid)
end
elseif expectedType == "Frame" then
local isValid = IsValidFrame(value)
if type(isValid) == "string" then
maybeErr = string.format("Invalid type for param '%s'. Expected type '%s'.",
paramName,
isValid)
end
elseif expectedType == "FramePoint" then
local isValid = IsValidPoint(value)
if type(isValid) == "string" then
maybeErr = string.format("Invalid type for param '%s'. Expected type '%s'.",
paramName,
isValid)
end
elseif type(value) ~= expectedType then
maybeErr = string.format("Invalid type for param '%s'. Expected type '%s', got '%s'.",
paramName,
expectedType,
type(value))
end
if type(maybeErr) == "string" then
if not errorMsg then
errorMsg = maybeErr
else
errorMsg = errorMsg .. "\n" .. maybeErr
end
end
end
if errorMsg then
CUF:Warn("Error in API function '" .. functionName .. "'" .. ":\n" .. errorMsg)
return false
end
return true
end