dedupe some nameplate logic
This commit is contained in:
parent
d9297245a7
commit
5406acc987
1 changed files with 48 additions and 60 deletions
|
@ -6,66 +6,10 @@ local g = require("scripts.libs.gradient")
|
|||
|
||||
---@param name string
|
||||
---@param gradient Gradient
|
||||
---@param nameplates Nameplate | Nameplate[] where to use the animated name
|
||||
---@param count number number of gradients to be present in the span
|
||||
---@param phase_shift_rate number how fast to shift the gradient
|
||||
---@returns fun():void # function to unregister
|
||||
local function animate_gradient_name(name, gradient, nameplates, count, phase_shift_rate)
|
||||
if type(nameplates) ~= "table" then
|
||||
nameplates = {nameplates}
|
||||
end
|
||||
|
||||
local string_width = client.getTextWidth(name) / count;
|
||||
local string_width_chars = {};
|
||||
for i = 1, #name do
|
||||
string_width_chars[i] = client.getTextWidth(name:sub(i, i))
|
||||
end
|
||||
|
||||
---@type ColoredText[]
|
||||
local result = {}
|
||||
|
||||
local phase_shift = 0;
|
||||
local function render()
|
||||
if client.isPaused() then
|
||||
return
|
||||
end
|
||||
|
||||
local acc = 0;
|
||||
for i = 1, #name do
|
||||
local offset = acc + string_width_chars[i];
|
||||
local color = gradient:at((offset / string_width) + phase_shift);
|
||||
acc = offset
|
||||
|
||||
result[i] = {
|
||||
text = name:sub(i, i),
|
||||
color = color:toHex(true, false)
|
||||
}
|
||||
end
|
||||
|
||||
phase_shift = phase_shift + phase_shift_rate
|
||||
|
||||
local json = toJson(result);
|
||||
for _, nameplate in pairs(nameplates) do
|
||||
nameplate:setText(json)
|
||||
end
|
||||
end
|
||||
|
||||
events.render:register(render)
|
||||
return function ()
|
||||
events.render:remove(render)
|
||||
end
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@param gradient Gradient
|
||||
---@param nameplates Nameplate | Nameplate[] where to use the static name
|
||||
---@param count number number of gradients to be present in the span
|
||||
---@param phase_shift number shift of the gradient
|
||||
local function static_gradient_name(name, gradient, nameplates, count, phase_shift)
|
||||
if type(nameplates) ~= "table" then
|
||||
nameplates = {nameplates}
|
||||
end
|
||||
|
||||
---@returns string
|
||||
local function render_gradient_name(name, gradient, count, phase_shift)
|
||||
local string_width = client.getTextWidth(name) / count;
|
||||
local string_width_chars = {};
|
||||
for i = 1, #name do
|
||||
|
@ -87,9 +31,53 @@ local function static_gradient_name(name, gradient, nameplates, count, phase_shi
|
|||
}
|
||||
end
|
||||
|
||||
local json = toJson(result);
|
||||
return toJson(result)
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@param gradient Gradient
|
||||
---@param nameplates Nameplate | Nameplate[] where to use the animated name
|
||||
---@param count number number of gradients to be present in the span
|
||||
---@param phase_shift_rate number how fast to shift the gradient
|
||||
---@returns fun():void # function to unregister
|
||||
local function animate_gradient_name(name, gradient, nameplates, count, phase_shift_rate)
|
||||
if type(nameplates) ~= "table" then
|
||||
nameplates = {nameplates}
|
||||
end
|
||||
|
||||
local phase_shift = 0;
|
||||
local function render()
|
||||
if client.isPaused() then
|
||||
return
|
||||
end
|
||||
|
||||
phase_shift = phase_shift + phase_shift_rate
|
||||
|
||||
local result = render_gradient_name(name, gradient, count, phase_shift)
|
||||
for _, nameplate in pairs(nameplates) do
|
||||
nameplate:setText(result)
|
||||
end
|
||||
end
|
||||
|
||||
events.render:register(render)
|
||||
return function ()
|
||||
events.render:remove(render)
|
||||
end
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@param gradient Gradient
|
||||
---@param nameplates Nameplate | Nameplate[] where to use the static name
|
||||
---@param count number number of gradients to be present in the span
|
||||
---@param phase_shift number shift of the gradient
|
||||
local function static_gradient_name(name, gradient, nameplates, count, phase_shift)
|
||||
if type(nameplates) ~= "table" then
|
||||
nameplates = {nameplates}
|
||||
end
|
||||
|
||||
local result = render_gradient_name(name, gradient, count, phase_shift);
|
||||
for _, nameplate in pairs(nameplates) do
|
||||
nameplate:setText(json)
|
||||
nameplate:setText(result)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue