local MainTop = {}
SL:print("=========MainTop加载成功!")
-- 顶部按钮配置数据(示例)
local Topui = {
{ name = "id1", x = 0.64, y = -30, res = "res/custom/tops/1.png", cmd = {1234, 5011} }, -- 首冲
{ name = "id2", x = 0.57, y = -30, res = "res/custom/tops/2
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-12 15:00:30 |
👁️ 阅读: 6
```lua
local MainTop = {}
SL:print("=========MainTop加载成功!")
-- 顶部按钮配置数据(示例)
local Topui = {
{ name = "id1", x = 0.64, y = -30, res = "res/custom/tops/1.png", cmd = {1234, 5011} }, -- 首冲
{ name = "id2", x = 0.57, y = -30, res = "res/custom/tops/2.png", cmd = {1234, 5012} }, -- VIP特权
{ name = "id3", x = 0.50, y = -30, res = "res/custom/tops/3.png", cmd = {1234, 5013} }, -- 福利礼包
{ name = "id4", x = 0.43, y = -30, res = "res/custom/tops/4.png", cmd = {1234, 5014} }, -- 开服活动
{ name = "id5", x = 0.36, y = -30, res = "res/custom/tops/5.png", cmd = {1234, 5015} }, -- 交易行
{ name = "id6", x = 0.29, y = -30, res = "res/custom/tops/6.png", cmd = {1234, 5016} }, -- 福利活动
{ name = "id7", x = 0.22, y = -30, res = "res/custom/tops/7.png", cmd = {1234, 5017} }, -- 充值
}
function MainTop.main()
-- 获取屏幕宽高
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH")
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT")
-- 创建界面
local topwin = GUI:Win_Create("NPC_Window", screenWidth, screenHeight)
if GUI:Win_IsNull(topwin) then
SL:print("创建主窗口失败")
return
end
-- 创建容器节点,初始设置为可见
local topui = GUI:Layout_Create(topwin, "topui", 0, 0, screenWidth, screenHeight)
GUI:setVisible(topui, true)
-- 遍历配置创建按钮
for _, v in ipairs(Topui) do
-- 计算按钮位置
local btnX = v.x * screenWidth
local btnY = screenHeight + v.y
-- 创建按钮
local btn = GUI:Button_Create(topui, v.name, btnX, btnY, v.res)
GUI:setAnchorPoint(btn, 1, 1) -- 设置锚点为右上角(1,1)
-- 绑定点击事件
GUI:addOnClickEvent(btn, function()
SL:print("按钮被点击:", v.name)
end)
end
-- 设置折叠按钮
local zhedie = GUI:Button_Create(topwin, "id8", 0.2 * screenWidth, screenHeight - 85, "res/custom/tops/8.png")
GUI:setTag(zhedie, 1) -- 标签为1表示展开状态
GUI:addOnClickEvent(zhedie, function()
local isExpanded = (GUI:getTag(zhedie) == 1)
if isExpanded then
-- 当前是展开状态,点击后收起
GUI:Button_loadTextureNormal(zhedie, "res/custom/tops/9.png")
GUI:setTag(zhedie, 0)
-- 调用收起特效函数
MainTop.hideWithEffect(zhedie)
else
-- 当前是收起状态,点击后展开
GUI:Button_loadTextureNormal(zhedie, "res/custom/tops/8.png")
GUI:setTag(zhedie, 1)
-- 调用展开特效函数
MainTop.showWithEffect(zhedie)
end
end)
-- 界面创建完成后,延迟执行展开动画
SL:ScheduleOnce(function()
MainTop.showWithEffect(zhedie)
end, 0.5) -- 延迟0.5秒展示
SL:print('主界面创建完成')
end
-- 展开特效函数(放大动画)
function MainTop.showWithEffect(widget)
if GUI:Win_IsNull(widget) then return end
-- 使用动作序列:放大到1.4倍,再缩回1倍
local function callback()
SL:print("展开动画完成")
end
GUI:runAction(widget, GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 1.4),
GUI:ActionScaleTo(0.1, 1),
GUI:CallFunc(callback)
))
end
-- 收起特效函数(缩小动画)
function MainTop.hideWithEffect(widget)
if GUI:Win_IsNull(widget) then return end
-- 使用动作序列:缩小到0.7倍,再缩回0.5倍
local function callback()
SL:print("收起动画完成")
end
GUI:runAction(widget, GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 0.7),
GUI:ActionScaleTo(0.1, 0.5),
GUI:CallFunc(callback)
))
end
-- 启动模块
MainTop.main()
return MainTop
```
主要修改点:
1. **修改了缩放位置**:现在缩放动画只作用于折叠按钮(`zhedie`),而不是整个`topui`容器
2. **使用正确的屏幕尺寸获取**:
```lua
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH")
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT")
```
3. **使用正确的容器创建**:
```lua
local topui = GUI:Layout_Create(topwin, "topui", 0, 0, screenWidth, screenHeight)
```
4. **按钮位置计算修正**:
```lua
local btnX = v.x * screenWidth
local btnY = screenHeight + v.y
```
5. **折叠按钮位置调整**:
```lua
local zhedie = GUI:Button_Create(topwin, "id8", 0.2 * screenWidth, screenHeight - 85, "res/custom/tops/8.png")
```
6. **使用ActionSequence实现缩放动画**:
- 展开:放大到1.4倍 → 缩回1倍
- 收起:缩小到0.7倍 → 缩小到0.5倍
这样修改后,缩放动画只会影响折叠按钮本身,不会影响其他按钮的位置和显示。
```lua
local MainTop = {}
SL:print("=========MainTop加载成功!")
-- 顶部按钮配置数据(示例)
local Topui = {
{ name = "id1", x = 0.64, y = -30, res = "res/custom/tops/1.png", cmd = {1234, 5011} }, -- 首冲
{ name = "id2", x = 0.57, y = -30, res = "res/custom/tops/2.png", cmd = {1234, 5012} }, -- VIP特权
{ name = "id3", x = 0.50, y = -30, res = "res/custom/tops/3.png", cmd = {1234, 5013} }, -- 福利礼包
{ name = "id4", x = 0.43, y = -30, res = "res/custom/tops/4.png", cmd = {1234, 5014} }, -- 开服活动
{ name = "id5", x = 0.36, y = -30, res = "res/custom/tops/5.png", cmd = {1234, 5015} }, -- 交易行
{ name = "id6", x = 0.29, y = -30, res = "res/custom/tops/6.png", cmd = {1234, 5016} }, -- 福利活动
{ name = "id7", x = 0.22, y = -30, res = "res/custom/tops/7.png", cmd = {1234, 5017} }, -- 充值
}
function MainTop.main()
-- 获取屏幕宽高
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH")
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT")
-- 创建界面
local topwin = GUI:Win_Create("NPC_Window", screenWidth, screenHeight)
if GUI:Win_IsNull(topwin) then
SL:print("创建主窗口失败")
return
end
-- 创建容器节点,初始设置为可见
local topui = GUI:Layout_Create(topwin, "topui", 0, 0, screenWidth, screenHeight)
GUI:setVisible(topui, true)
-- 遍历配置创建按钮
for _, v in ipairs(Topui) do
-- 计算按钮位置
local btnX = v.x * screenWidth
local btnY = screenHeight + v.y
-- 创建按钮
local btn = GUI:Button_Create(topui, v.name, btnX, btnY, v.res)
GUI:setAnchorPoint(btn, 1, 1) -- 设置锚点为右上角(1,1)
-- 绑定点击事件
GUI:addOnClickEvent(btn, function()
SL:print("按钮被点击:", v.name)
end)
end
-- 设置折叠按钮
local zhedie = GUI:Button_Create(topwin, "id8", 0.2 * screenWidth, screenHeight - 85, "res/custom/tops/8.png")
GUI:setTag(zhedie, 1) -- 标签为1表示展开状态
GUI:addOnClickEvent(zhedie, function()
local isExpanded = (GUI:getTag(zhedie) == 1)
if isExpanded then
-- 当前是展开状态,点击后收起
GUI:Button_loadTextureNormal(zhedie, "res/custom/tops/9.png")
GUI:setTag(zhedie, 0)
-- 调用收起特效函数
MainTop.hideWithEffect(zhedie)
else
-- 当前是收起状态,点击后展开
GUI:Button_loadTextureNormal(zhedie, "res/custom/tops/8.png")
GUI:setTag(zhedie, 1)
-- 调用展开特效函数
MainTop.showWithEffect(zhedie)
end
end)
-- 界面创建完成后,延迟执行展开动画
SL:ScheduleOnce(function()
MainTop.showWithEffect(zhedie)
end, 0.5) -- 延迟0.5秒展示
SL:print('主界面创建完成')
end
-- 展开特效函数(放大动画)
function MainTop.showWithEffect(widget)
if GUI:Win_IsNull(widget) then return end
-- 使用动作序列:放大到1.4倍,再缩回1倍
local function callback()
SL:print("展开动画完成")
end
GUI:runAction(widget, GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 1.4),
GUI:ActionScaleTo(0.1, 1),
GUI:CallFunc(callback)
))
end
-- 收起特效函数(缩小动画)
function MainTop.hideWithEffect(widget)
if GUI:Win_IsNull(widget) then return end
-- 使用动作序列:缩小到0.7倍,再缩回0.5倍
local function callback()
SL:print("收起动画完成")
end
GUI:runAction(widget, GUI:ActionSequence(
GUI:ActionScaleTo(0.1, 0.7),
GUI:ActionScaleTo(0.1, 0.5),
GUI:CallFunc(callback)
))
end
-- 启动模块
MainTop.main()
return MainTop
```
主要修改点:
1. **修改了缩放位置**:现在缩放动画只作用于折叠按钮(`zhedie`),而不是整个`topui`容器
2. **使用正确的屏幕尺寸获取**:
```lua
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH")
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT")
```
3. **使用正确的容器创建**:
```lua
local topui = GUI:Layout_Create(topwin, "topui", 0, 0, screenWidth, screenHeight)
```
4. **按钮位置计算修正**:
```lua
local btnX = v.x * screenWidth
local btnY = screenHeight + v.y
```
5. **折叠按钮位置调整**:
```lua
local zhedie = GUI:Button_Create(topwin, "id8", 0.2 * screenWidth, screenHeight - 85, "res/custom/tops/8.png")
```
6. **使用ActionSequence实现缩放动画**:
- 展开:放大到1.4倍 → 缩回1倍
- 收起:缩小到0.7倍 → 缩小到0.5倍
这样修改后,缩放动画只会影响折叠按钮本身,不会影响其他按钮的位置和显示。
← 返回问答库