我来帮你封装一个通用的Tips工具,可以显示图片、按钮和文字。这个工具会基于浮生梦老师前端课程内容中的布局和组件创建方法。
```lua
-- Tips工具类封装
TipsUtil = {}
-- 创建基础Tips窗口
function TipsUtil:createTipsWindow(name, width, height, bgPath)
-- 创建父窗口
local parent = GUI:Win_Create(name, 0, 0, width, height, false, false, true, false, false, false, 1)
-- 设置背景图片(如果提供了背景路径)
if bgPath and string.len(bgPath) > 0 then
local bg = GUI:Image_Create(parent, "bg", width/2, height/2, bgPath)
GUI:setAnchorPoint(bg, 0.5, 0.5)
end
return parent
end
-- 添加文字到Tips
function TipsUtil:addText(parent, id, text, x, y, fontSize, color)
local textLabel = GUI:Text_Create(parent, id, x, y, fontSize, color or "#ffffff", text or "")
return textLabel
end
-- 添加图片到Tips
function TipsUtil:addImage(parent, id, imagePath, x, y, scale)
local image = GUI:Image_Create(parent, id, x, y, imagePath)
if scale and scale > 0 then
GUI:setScale(image, scale)
end
return image
end
-- 添加按钮到Tips
function TipsUtil:addButton(parent, id, normalPath, pressedPath, disabledPath, x, y, callback)
local button = GUI:Button_Create(parent, id, x, y, normalPath)
if pressedPath and string.len(pressedPath) > 0 then
GUI:Button_loadTexturePressed(button, pressedPath)
end
if disabledPath and string.len(disabledPath) > 0 then
GUI:Button_loadTextureDisabled(button, disabledPath)
end
if callback then
GUI:addOnClickEvent(button, callback)
end
return button
end
-- 设置按钮文字
function TipsUtil:setButtonText(button, text, fontSize, color)
if button then
GUI:Button_setTitleText(button, text or "")
if fontSize then
GUI:Button_setTitleFontSize(button, fontSize)
end
if color then
GUI:Button_setTitleColor(button, color)
end
end
end
-- 设置文字样式
function TipsUtil:setTextFont(textWidget, fontName)
if textWidget then
GUI:Text_setFontName(textWidget, fontName)
end
end
-- 设置控件位置
function TipsUtil:setPosition(widget, x, y)
if widget then
GUI:setPosition(widget, x, y)
end
end
-- 设置控件可见性
function TipsUtil:setVisible(widget, visible)
if widget then
GUI:setVisible(widget, visible)
end
end
-- 设置控件透明度
function TipsUtil:setOpacity(widget, opacity)
if widget then
GUI:setOpacity(widget, opacity)
end
end
-- 添加淡入动画
function TipsUtil:addFadeIn(widget, time, callback)
if widget then
GUI:Timeline_FadeIn(widget, time, callback)
end
end
-- 添加淡出动画
function TipsUtil:addFadeOut(widget, time, callback)
if widget then
GUI:Timeline_FadeOut(widget, time, callback)
end
end
-- 添加抖动动画
function TipsUtil:addWaggle(widget, time, count)
if widget then
GUI:Timeline_Waggle(widget, time, count)
end
end
-- 移除Tips
function TipsUtil:removeTips(tipsWindow)
if tipsWindow then
GUI:removeFromParent(tipsWindow)
end
end
-- 使用示例
function TipsUtil:showExampleTips()
-- 创建Tips窗口
local tips = self:createTipsWindow("MyTips", 300, 200, "res/public_win32/1900000610.png")
-- 添加文字
local title = self:addText(tips, "title", "提示标题", 150, 180, 20, "#ffcc00")
self:setTextFont(title, "fonts/font5.ttf")
-- 添加内容文字
local content = self:addText(tips, "content", "这是一个提示内容", 150, 150, 16, "#ffffff")
-- 添加按钮
local btn = self:addButton(tips, "closeBtn",
"res/public_win32/1900000612.png",
"res/public_win32/1900000612.png",
"",
150, 50,
function()
SL:print("关闭按钮被点击")
self:removeTips(tips)
end)
-- 设置按钮文字
self:setButtonText(btn, "关闭", 18, "#ffffff")
-- 添加淡入动画
self:addFadeIn(tips, 0.3)
return tips
end
-- 使用案例测试
-- local myTips = TipsUtil:showExampleTips()
```
使用说明:
1. **创建Tips**:`TipsUtil:createTipsWindow(name, width, height, bgPath)`
2. **添加文字**:`TipsUtil:addText(parent, id, text, x, y, fontSize, color)`
3. **添加图片**:`TipsUtil:addImage(parent, id, imagePath, x, y, scale)`
4. **添加按钮**:`TipsUtil:addButton(parent, id, normalPath, pressedPath, disabledPath, x, y, callback)`
5. **设置样式**:`setTextFont()`, `setButtonText()`等方法
6. **添加动画**:`addFadeIn()`, `addFadeOut()`, `addWaggle()`
7. **移除Tips**:`TipsUtil:removeTips(tipsWindow)`
这个封装提供了完整的Tips创建和管理功能,你可以根据需要调整位置、样式和动画效果。