forked from m00nyONE/LibCustomIcons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.lua
More file actions
140 lines (123 loc) · 5.73 KB
/
api.lua
File metadata and controls
140 lines (123 loc) · 5.73 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
-- SPDX-FileCopyrightText: 2025 m00nyONE
-- SPDX-License-Identifier: Artistic-2.0
local lib_name = "LibCustomIcons"
local lib = _G[lib_name]
local s = lib.GetStaticTable()
local a = lib.GetAnimatedTable()
--[[ doc.lua begin ]]
local currentFolder = "misc" .. tostring(os.date("%Y") - 2018)
--- @alias texturePath string
--- @alias textureCoordsLeft number
--- @alias textureCoordsRight number
--- @alias textureCoordsTop number
--- @alias textureCoordsBottom number
--- @alias columns number
--- @alias rows number
--- @alias fps number
--- Retrieves the current folder where the icons are stored.
--- @return string folderName The current folder name where new icons should be put in
function lib.GetCurrentFolder()
return currentFolder
end
--- Checks whether a static icon exists for the given username.
--- @param username string The player's account name (e.g., "@m00nyONE").
--- @return boolean hasStatic `true` if a custom static icons exists, `false` otherwise.
function lib.HasStatic(username)
return s[username] ~= nil
end
--- Checks whether an animated icon exists for the given username.
--- @param username string The player's account name (e.g., "@m00nyONE").
--- @return boolean hasAnimated `true` if a custom animated icons exists, `false` otherwise.
function lib.HasAnimated(username)
return a[username] ~= nil
end
--- Checks if a custom icon (either static or animated) exists for the given username.
--- @param username string The player's account name (e.g., "@m00nyONE").
--- @return boolean hasIcon `true` if a custom static icons exists, `false` otherwise.
function lib.HasIcon(username)
return lib.HasStatic(username) or lib.HasAnimated(username)
end
--- Retrieves the texturePath of the static icon for the user or nil if none exists.
--- @param username string The player's account name (e.g., "@m00nyONE").
--- @return texturePath, textureCoordsLeft, textureCoordsRight, textureCoordsTop, textureCoordsBottom
function lib.GetStatic(username)
if type(s[username]) == "table" then -- compiled icon
local texture, left, right, top, bottom, width, height = unpack(s[username])
return texture, left/width, right/width, top/height, bottom/height
end
return s[username], 0, 1, 0, 1
end
--- Retrieves the texturePath and animation parameters of the animated icon for the user or nil if none exists.
--- @param username string The player's account name (e.g., "@m00nyONE").
--- @return texturePath, textureCoordsLeft, textureCoordsRight, textureCoordsTop, textureCoordsBottom, columns, rows, fps
function lib.GetAnimated(username)
-- TODO: uncomment when animated icons get merged textures
--local anim = a[username]
--if not anim then
-- return nil, 0, 1, 0, 1, nil, nil, nil
--end
--if #anim > 4 then -- compiled animation
-- local texturePath, columns, rows, fps, left, right, top, bottom, width, height = unpack(anim)
-- return texturePath, left/width, right/width, top/height, bottom/height, columns, rows, fps
--end
--
--return anim[1], 0, 1, 0, 1, anim[2], anim[3], anim[4]
return a[username]
end
-- cached Clones of the internal tables for the GetAll* function. As these tables should always be readOnly and do nothing if edited, there is no need for them to be cloned each time they're requested
local cachedStaticIconsTableClone = nil
local cachedAnimatedIconsTableClone = nil
--- Retrieves all registered static icons from the internal table as a deep copy.
--- Editing the returning table has no effect to the internal one that is used to retrieve actual icons.
--- @return table<string,string> staticTable mapping `@accountname` to `texturePath` for all static icons
function lib.GetAllStatic()
if not cachedStaticIconsTableClone then
cachedStaticIconsTableClone = ZO_DeepTableCopy(s)
end
return cachedStaticIconsTableClone
end
--- Retrieves all registered animated icons from the internal table as a deep copy.
--- Editing the returning table has no effect to the internal one that is used to retrieve actual icons.
--- @return table<string,table> animTable mapping `@accountname` to `{texturePath, width, height, fps}` for all animated icons
function lib.GetAllAnimated()
if not cachedAnimatedIconsTableClone then
cachedAnimatedIconsTableClone = ZO_DeepTableCopy(a)
end
return cachedAnimatedIconsTableClone
end
-- The number of static and animated icons is fixed at runtime (icons are registered once and not modified later). To optimize performance, counts are calculated only once when first requested and then cached for future calls.
local cachedStaticIconCount = 0
local cachedAnimatedIconCount = 0
local cachedTotalIconCount = 0
--- Returns the number of registered static icons.
--- The result is cached after the first computation.
--- @return number count The number of static icons
function lib.GetStaticCount()
if cachedStaticIconCount == 0 then
for _ in pairs(s) do
cachedStaticIconCount = cachedStaticIconCount + 1
end
end
return cachedStaticIconCount
end
--- Returns the number of registered animated icons.
--- The result is cached after the first computation.
--- @return number count The number of animated icons
function lib.GetAnimatedCount()
if cachedAnimatedIconCount == 0 then
for _ in pairs(a) do
cachedAnimatedIconCount = cachedAnimatedIconCount + 1
end
end
return cachedAnimatedIconCount
end
--- Returns the total number of registered icons (static + animated).
--- The result is cached after the first computation.
--- @return number count The total number of icons
function lib.GetIconCount()
if cachedTotalIconCount == 0 then
cachedTotalIconCount = lib.GetStaticCount() + lib.GetAnimatedCount()
end
return cachedTotalIconCount
end
--[[ doc.lua end ]]