```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 = {}
refined.refineData = nil
refined.equipData = {}
refined.lock = {0, 0, 0} -- 复选框状态
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
for x, y in pairs(EquipPos) do
refined.lockStatus[x] = {0, 0, 0} -- 默认复选框状态
local buttonName = "but_"..x
GUI:addOnClickEvent(refined.ui[buttonName], function()
refined.currentEquipPos = x
refined:createHighLight(refined.ui[buttonName], x)
SL:SendLuaNetMsg(2013, 13, 0, x, "装备洗练") -- 放入装备
-- 获取复选框状态
-- 逻辑与实际相反,打钩为false..1,未打钩为true..0
for i = 1 ,3 do
local lockState = "CheckBox_"..i
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:JsonEncode(refined.lock)
SL:SendLuaNetMsg(2013, 13, 2, x, lockJson)
end)
end
end)
end
-- 首次进入时,在第一个按钮上创建高亮
if refined.ui["but_0"] then
refined:createHighLight(refined.ui["but_0"], 0)
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
local highBright_Img = "highBright"..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:createHighLight(button, p3)
refined:clearHighLight()
local highBright = GUI:Image_Create(button, "highBright_"..p3, 0, 0, "res/public/1900000582.png")
GUI:setContentSize(highBright, 105, 32)
GUI:setLocalZOrder(highBright, 1)
refined.currentHighLight = highBright
return highBright
end
function refined:clearHighLight()
if refined.currentHighLight and not GUI:Win_IsNull(refined.currentHighLight) then
GUI:removeFromParent(refined.currentHighLight)
refined.currentHighLight = nil
end
end
SL:RegisterLuaNetMsg(1013, networkCB)
```
你的代码逻辑基本正确,实现了洗练界面的打开、装备选择、高亮显示和状态刷新功能。不过,根据浮生梦老师前端课程内容,这里有几个小建议:
1. **按钮状态设置**:你使用了`GUI:Button_setGrey()`来设置按钮的灰暗状态,这个函数在提供的资料中没有直接找到。根据课程内容,更常见的做法是使用`GUI:setTouchEnabled()`控制点击,以及通过`GUI:Button_loadTextureDisabled()`设置禁用时的图片(如资料1、2所示)。
2. **复选框状态逻辑**:你的注释说明“逻辑与实际相反,打钩为false..1,未打钩为true..0”,这个逻辑需要和后端保持一致。建议在代码中增加更明确的注释,说明`refined.lock[i] = 1`和`= 0`分别代表锁定和解锁。
3. **装备位置遍历**:`EquipPos`表中缺少位置9,这可能是设计如此,但建议确认一下是否所有装备位置都已覆盖。
4. **高亮图片路径**:你使用了`res/public/1900000582.png`,这个路径是正确的,但建议确保图片资源存在。
整体代码结构清晰,功能完整,继续加油!如果还有不明白的地方,随时可以问我。