看了你的代码,主要问题在于高亮逻辑的处理时机和按钮状态管理。根据浮生梦老师前端课程内容,我帮你优化一下:
```lua
SL:print("打开洗练界面")
local refined = {}
EquipPos = {[0] = "衣服",[1] = "武器",[2] = "勋章",[3] = "头盔",[4] = "项链",[5] = "手镯(右)" ,
[6] = "手镯(左)",[7] = "戒指(右)",[8] = "戒指(左)",[10] = "腰带",[11] = "靴子"}
refined.currentAttributes = {
{type = 0, name = "无属性", value = 0, color = "#ffffff"},
{type = 0, name = "无属性", value = 0, color = "#ffffff"},
{type = 0, name = "无属性", value = 0, color = "#ffffff"}
}
refined.lockStatus = {false, false, false}
refined.refineData = nil
refined.equipData = {}
refined.currentHighLight = nil -- 当前高亮对象
refined.highLightMap = {} -- 存储所有按钮的高亮对象
local function networkCB(msgID, p1 ,p2 ,p3 ,msgData)
SL:print("===== 收到后端消息 =====")
SL:print("消息ID: " .. tostring(msgID))
SL:print("参数1: " .. tostring(p1))
SL:print("参数2: " .. tostring(p2))
SL:print("参数3: " .. tostring(p3))
SL:print("字符串: " .. tostring(msgData))
SL:print("=======================")
if msgID == 1013 and p1 == 13 then
if p2 == 0 then
refined:openWin()
end
if p2 == 1 then
refined:refreshWin(p3, msgData)
end
end
end
function refined:openWin()
-- 创建界面
local win_refined = GUI:Win_Create("win_refined", 0, 0, 0, 0)
GUI:LoadExport(win_refined, "refined")
refined.ui = GUI:ui_delegate(win_refined)
-- 关闭按钮
GUI:addOnClickEvent(refined.ui.esc, function()
GUI:Win_Close(win_refined)
end)
-- 请求数据
SL:SendLuaNetMsg(2013, 13, 0, 0, "装备洗练")
-- 初始化所有按钮
refined.currentEquipPos = 0 -- 默认选中衣服位置0
refined.highLightMap = {} -- 清空高亮映射
-- 为每个装备位置创建按钮事件
for pos, name in pairs(EquipPos) do
local buttonName = "but_"..pos
if refined.ui[buttonName] then
-- 创建高亮对象但默认隐藏
local highLight = GUI:Image_Create(refined.ui[buttonName], "highBright_"..pos, 0, 0, "res/public/1900000582.png")
GUI:setContentSize(highLight, 105, 32)
GUI:setLocalZOrder(highLight, 1)
GUI:setVisible(highLight, false) -- 默认隐藏
-- 存储到映射表
refined.highLightMap[pos] = highLight
-- 按钮点击事件
GUI:addOnClickEvent(refined.ui[buttonName], function()
-- 更新当前选中位置
refined.currentEquipPos = pos
-- 更新所有高亮状态:隐藏所有,只显示当前
for p, hl in pairs(refined.highLightMap) do
if not GUI:Win_IsNull(hl) then
GUI:setVisible(hl, p == pos)
end
end
-- 发送网络消息
SL:SendLuaNetMsg(2013, 13, 0, pos, "装备洗练")
end)
end
end
-- 复选框状态处理
refined.lock = {0, 0, 0}
for i = 1, 3 do
local lockState = "CheckBox_"..i
if refined.ui[lockState] then
GUI:addOnClickEvent(refined.ui[lockState], function()
local lockState_i = GUI:CheckBox_isSelected(refined.ui[lockState])
if lockState_i == true then
refined.lock[i] = 1
else
refined.lock[i] = 0
end
local lockJson = SL:JsonDecode(refined.lock)
SL:SendLuaNetMsg(2013, 13, 2, refined.currentEquipPos, lockJson)
end)
end
end
-- 首次进入时显示第一个按钮的高亮
if refined.highLightMap[0] and not GUI:Win_IsNull(refined.highLightMap[0]) then
GUI:setVisible(refined.highLightMap[0], true)
end
end
function refined:refreshWin(p3, msgData)
refined.eqInfo = SL:JsonDecode(msgData)
for k, v in pairs(refined.eqInfo) do
if k <= 11 then
local buttonName = "but_"..v.pos
if refined.ui[buttonName] then
GUI:Button_setTitleText(refined.ui[buttonName], v.name or "未装备")
local hasEquip = not string.find(v.name, "未装备")
-- 设置按钮状态
GUI:setTouchEnabled(refined.ui[buttonName], hasEquip) -- 有装备可点击,无装备不可点击
GUI:Button_setGrey(refined.ui[buttonName], not hasEquip) -- 有装备高亮,无装备灰暗
end
end
end
if refined.eqInfo[12] then
local buttonName = "but_"..p3
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
end
end
-- 清理函数(可选,用于界面关闭时)
function refined:clearAllHighLights()
for pos, highLight in pairs(refined.highLightMap) do
if not GUI:Win_IsNull(highLight) then
GUI:removeFromParent(highLight)
end
end
refined.highLightMap = {}
refined.currentHighLight = nil
end
SL:RegisterLuaNetMsg(1013, networkCB)
```
**主要改进点:**
1. **高亮对象管理**:使用`refined.highLightMap`表存储所有按钮的高亮对象,便于统一管理
2. **状态切换**:点击按钮时遍历所有高亮对象,只显示当前选中的,其他隐藏
3. **默认高亮**:界面打开时自动显示位置0(衣服)的高亮
4. **对象判空**:使用`GUI:Win_IsNull()`判断控件是否有效
这样就能实现:进入界面时第一个按钮高亮,点击其他按钮时,新按钮高亮,之前的高亮自动消失。