function ItemTips.CreateNameWidget(param)
if not param or not next(param) then
return
end
for k,v in pairs(param) do
SL:print(k,v)
end
local r_name = GUI:Text_Create(-1, "r_name", 0, 0, 16, "#ffffff", " ")
-- 定义七彩颜色
local colors = {
"#FF0000", -- 红色
"#FF7F00", -- 橙色
"#FFFF00", -- 黄色
"#00FF00", -- 绿色
"#0000FF", -- 蓝色
"#4B0082", -- 靛色
"#8B00FF" -- 紫色
}
-- 获取字符串的字符数(支持中文)
local function getCharCount(str)
local len = 0
local i = 1
while i <= #str do
local byte = string.byte(str, i)
if byte >= 0x80 then
i = i + 3 -- 中文字符占3个字节
else
i = i + 1 -- 英文字符占1个字节
end
len = len + 1
end
return len
end
-- 获取字符串的第n个字符(支持中文)
local function getCharAt(str, n)
local i = 1
local count = 0
while i <= #str do
local byte = string.byte(str, i)
if byte >= 0x80 then
if count + 1 == n then
return string.sub(str, i, i + 2) -- 返回中文字符
end
i = i + 3
else
if count + 1 == n then
return string.sub(str, i, i) -- 返回英文字符
end
i = i + 1
end
count = count + 1
end
return ""
end
-- 创建流光文本
local function createGradientText(parent, str, x, y, fontSize, defaultColor)
-- 获取文本长度(按字符数)
local textLength = getCharCount(str)
-- 计算每个字符的宽度
local charWidth = fontSize * 1.2 -- 假设每个字符的宽度为字体大小的1.2倍
-- 存储所有字符的Text控件
local charTexts = {}
-- 创建所有字符的Text控件
for i = 1, textLength do
local char = getCharAt(str, i)
local charText = GUI:Text_Create(parent, "char_" .. i, x + (i - 1) * charWidth, y, fontSize, defaultColor, char)
table.insert(charTexts, charText)
end
-- 动态流光效果
local currentColorIndex = 1
local function updateGradient()
-- 更新每个字符的颜色
for i = 1, textLength do
local charText = charTexts[i]
if charText and not tolua.isnull(charText) then -- 检查控件是否有效
local colorIndex = (currentColorIndex + i - 1) % #colors + 1
GUI:Text_setTextColor(charText, colors[colorIndex])
end
end
-- 更新颜色索引
currentColorIndex = currentColorIndex + 1
if currentColorIndex > #colors then
currentColorIndex = 1
end
end
-- 启动定时器,每隔0.1秒更新一次颜色
SL:Schedule(updateGradient, 0.1)
end
-- 示例调用
local str = param.tip_name
local parent = r_name
local x, y = 0, 0 -- 位置
local fontSize = 16 -- 字体大小
local defaultColor = "#ffffff" -- 默认颜色
createGradientText(parent, str, x, y, fontSize, defaultColor)
return r_name
end