local MainLeftTop = {}
local UI_config = {
{name = "shijieditu", x = -720, y = -7, res = "res\\yidalu\\dingbuanniu/000000.png", cmd = {1234, 6001}},
{name = "gonglue", x = -320, y = -7, res = "res\\yidalu\\dingbuanniu/000008.png", cmd = {123
要将向右收缩改为向上收缩,需要修改 `collapseButtons` 函数中的位置计算逻辑。以下是完整的修改后代码:
```lua
local MainLeftTop = {}
local UI_config = {
{name = "shijieditu", x = -720, y = -7, res = "res\\yidalu\\dingbuanniu/000000.png", cmd = {1234, 6001}},
{name = "gonglue", x = -320, y = -7, res = "res\\yidalu\\dingbuanniu/000008.png", cmd = {1235, 6002}},
{ name ="fuli", x =-420, y = -7, res = "res\\yidalu\\dingbuanniu/000006.png" ,cmd = {1236,5003}},
{ name ="huod", x =-520 , y = -7, res = "res\\yidalu\\dingbuanniu/000004.png" ,cmd = {1237,5004}},
{ name ="shenhun", x =-620 , y = -7, res = "res\\yidalu\\dingbuanniu/000002.png" ,cmd = {1238,5005}},
{name = "aaa", x = -920, y = -100, res = "res\\yidalu\\dingbuanniu/000000.png", cmd = {1239, 6006}},
{name = "ssss", x = -820, y = -100, res = "res\\yidalu\\dingbuanniu/000008.png", cmd = {1240, 6007}},
{ name ="cccc", x =-720, y = -100, res = "res\\yidalu\\dingbuanniu/000006.png" ,cmd = {1241,5008}},
{ name ="xxxx", x =-620 , y = -100, res = "res\\yidalu\\dingbuanniu/000004.png" ,cmd = {1242,5009}},
{ name ="eeee", x =-520 , y = -100, res = "res\\yidalu\\dingbuanniu/000002.png" ,cmd = {1243,5010}},
{ name ="www", x =-420 , y = -100, res = "res\\yidalu\\dingbuanniu/000004.png" ,cmd = {1244,5011}},
{ name ="ddd", x =-320 , y = -100, res = "res\\yidalu\\dingbuanniu/000002.png" ,cmd = {1245,5012}},
}
-- 存储按钮原始位置
local buttonOriginalPositions = {}
-- 存储按钮对象
local buttonObjects = {}
-- 当前状态:true为展开,false为收起
local isExpanded = true
function MainLeftTop.main()
SL:ScheduleOnce(function()
local parent = GUI:Attach_RightTop()
SL:Print("parent 值: ", parent)
if GUI:Win_IsNull(parent) then
SL:Print("错误: GUI:Attach_RightTop() 返回空值!")
return
end
local w = SL:GetMetaValue("SCREEN_WIDTH") or 800
local h = SL:GetMetaValue("SCREEN_HEIGHT") or 600
SL:Print("屏幕尺寸: ", w, "x", h)
local oldNode = GUI:getChildByName(parent, "shenyusuo")
if not GUI:Win_IsNull(oldNode) then
GUI:removeFromParent(oldNode)
end
local topNode = GUI:Widget_Create(parent, "shenyusuo", 0, 0, w, 100)
SL:Print("topNode 创建: ", topNode)
-- 创建所有按钮并记录位置
for k, v in ipairs(UI_config) do
local posX = v.x
if type(v.x) == "number" and v.x >= 0 and v.x <= 1 then
posX = w * v.x
end
SL:Print("创建按钮: ", v.name, " 位置X: ", posX, " 位置Y: ", v.y)
local oldBtn = GUI:getChildByName(topNode, v.name)
if not GUI:Win_IsNull(oldBtn) then
GUI:removeFromParent(oldBtn)
end
local btn = GUI:Button_Create(topNode, v.name, posX, v.y, v.res)
if not GUI:Win_IsNull(btn) then
GUI:setAnchorPoint(btn, 1, 1)
-- 记录原始位置
buttonOriginalPositions[v.name] = {x = posX, y = v.y}
buttonObjects[v.name] = btn
GUI:addOnClickEvent(btn, function()
if v.cmd and #v.cmd >= 2 then
SL:SendLuaNetMsg(v.cmd[1], v.cmd[2], 0, 0, "")
end
SL:Require("GUILayout/NPC/"..UI_config[k].name..".lua", true)
end)
else
SL:Print("创建按钮失败: ", v.name)
end
end
local oldBtn = GUI:getChildByName(parent, "SUOFANG")
if not GUI:Win_IsNull(oldBtn) then
GUI:removeFromParent(oldBtn)
end
local btn = GUI:Button_Create(parent, "SUOFANG", -300, -90, "res\\yidalu\\dingbuanniu/000011.png")
if not GUI:Win_IsNull(btn) then
GUI:setTag(btn, 1)
-- 获取缩放按钮位置
local btnPos = GUI:getPosition(btn)
local btnX = btnPos.x
local btnY = btnPos.y
GUI:addOnClickEvent(btn, function()
local currentTag = GUI:getTag(btn)
local newTexture = (currentTag == 1) and "res\\yidalu\\dingbuanniu/000010.png" or "res\\yidalu\\dingbuanniu/000011.png"
GUI:Button_loadTextureNormal(btn, newTexture)
GUI:setTag(btn, (currentTag == 1) and 0 or 1)
-- 切换展开/收起状态
isExpanded = not isExpanded
if isExpanded then
-- 展开动画:回到原始位置并显示
MainLeftTop:expandButtons(btnX, btnY)
else
-- 收起动画:移动到缩放按钮上方(向上排列)并隐藏
MainLeftTop:collapseButtons(btnX, btnY)
end
end)
end
end, 1.0)
end
-- 收起动画:将按钮移动到缩放按钮上方(向上排列)
function MainLeftTop:collapseButtons(btnX, btnY)
local startY = btnY + 100 -- 缩放按钮上方100像素
local xPos = btnX - 10 -- 稍微向左偏移
-- 所有按钮都需要收起
local allButtons = {}
for _, v in ipairs(UI_config) do
table.insert(allButtons, v.name)
end
-- 为每个按钮创建动画
for i, btnName in ipairs(allButtons) do
local btn = buttonObjects[btnName]
if btn and not GUI:Win_IsNull(btn) then
-- 计算目标位置:在缩放按钮上方排列
local targetY = startY - (i-1) * 60 -- 每个按钮间隔60像素,向上排列
-- 特殊处理"aaa"、"ssss"、"cccc":停留在缩放按钮旁边
if btnName == "aaa" then
local targetX = btnX - 160
targetY = btnY + 100
-- 创建移动动画
local actionSequence = GUI:ActionSequence(
GUI:ActionMoveTo(0.3, targetX, targetY)
)
GUI:runAction(btn, actionSequence)
elseif btnName == "ssss" then
local targetX = btnX - 80
targetY = btnY + 100
local actionSequence = GUI:ActionSequence(
GUI:ActionMoveTo(0.3, targetX, targetY)
)
GUI:runAction(btn, actionSequence)
elseif btnName == "cccc" then
local targetX = btnX - 5
targetY = btnY + 100
local actionSequence = GUI:ActionSequence(
GUI:ActionMoveTo(0.3, targetX, targetY)
)
GUI:runAction(btn, actionSequence)
else
-- 其他按钮向上移动并隐藏
local targetX = xPos
local actionSequence = GUI:ActionSequence(
GUI:ActionMoveTo(0.3, targetX, targetY),
GUI:CallFunc(function()
GUI:setVisible(btn, false) -- 移动完成后隐藏按钮
end)
)
GUI:runAction(btn, actionSequence)
end
end
end
end
-- 展开动画:将按钮移回原始位置并显示
function MainLeftTop:expandButtons(btnX, btnY)
-- 所有按钮都回到原始位置
for k, v in ipairs(UI_config) do
local btn = buttonObjects[v.name]
if btn and not GUI:Win_IsNull(btn) then
local originalPos = buttonOriginalPositions[v.name]
if originalPos then
-- 先显示按钮,然后移动
GUI:setVisible(btn, true) -- 先显示按钮
local actionSequence = GUI:ActionSequence(
GUI:ActionMoveTo(0.3, originalPos.x, originalPos.y)
)
GUI:runAction(btn, actionSequence)
end
end
end
end
local function init()
SL:ScheduleOnce(function()
MainLeftTop.main()
end, 0.5)
end
SL:RegisterLUAEvent("LUA_EVENT_ENTER_WORLD", "MainLeftTop", init)
return MainLeftTop
```
主要修改内容:
1. **第111行注释**:将"向右排列"改为"向上排列"
2. **第113-114行**:`startY = btnY + 100` 改为缩放按钮上方100像素,`xPos = btnX - 10` 稍微向左偏移
3. **第124行**:`targetY = startY - (i-1) * 60` 改为向上排列,每个按钮间隔60像素
4. **第136、143、150行**:保留了特殊按钮的位置处理逻辑
5. **第157行**:其他按钮使用 `targetX = xPos` 和计算出的 `targetY` 向上移动
这样修改后,按钮在收起时会向上排列在缩放按钮上方,而不是向右排列。