根据你提供的代码,要实现复选框状态的保存和恢复,需要在几个关键位置进行处理。主要思路是:将锁定状态存储到装备的变量中,并在每次打开界面或切换装备时读取这些状态。
以下是需要修改的部分:
**1. 前端修改 - 在 `refined:openWin()` 函数中初始化复选框状态**
```lua
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
local buttonName = "but_"..x
GUI:addOnClickEvent(refined.ui[buttonName], function()
refined.currentEquipPos = x
-- 切换装备时,从服务端获取该装备的锁定状态
SL:SendLuaNetMsg(2013, 13, 0, x, "获取锁定状态")
end)
end
end
```
**2. 前端修改 - 在 `refined:refreshWin()` 函数中请求锁定状态**
```lua
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.eqInfo[12].hasEquip == false then
if refined.ui[buttonName] then
GUI:Button_setBright(refined.ui[buttonName], false)
end
end
if refined.ui.ItemShow_1 then
GUI:ItemShow_updateItem(refined.ui.ItemShow_1, refined.eqInfo[12].idx or 0)
end
-- 显示当前装备后,请求该装备的锁定状态
SL:SendLuaNetMsg(2013, 13, 2, p3, "请求锁定状态")
end
end
```
**3. 前端修改 - 新增处理锁定状态响应的函数**
```lua
function refined:refreshEquip(msgData)
local lockTbl = SL:JsonDecode(msgData)
-- 根据服务端返回的锁定状态设置复选框
for k = 1, 3 do
local CheckBox = "CheckBox_"..k
if refined.ui[CheckBox] then
if lockTbl[k] == 1 then
GUI:CheckBox_setSelected(refined.ui[CheckBox], true)
refined.lockStatus[k] = true
else
GUI:CheckBox_setSelected(refined.ui[CheckBox], false)
refined.lockStatus[k] = false
end
end
end
-- 洗练按钮点击事件
GUI:addOnClickEvent(refined.ui.refined_but, function()
GUI:delayTouchEnabled(refined.ui.refined_but, 0.7)
-- 获取当前复选框状态
local currentLockStatus = {}
for k = 1, 3 do
local CheckBox = "CheckBox_"..k
if refined.ui[CheckBox] then
currentLockStatus[k] = GUI:CheckBox_isSelected(refined.ui[CheckBox])
end
end
-- 发送锁定状态到服务端保存
local lockJson = SL:JsonEncode(currentLockStatus)
SL:SendLuaNetMsg(2013, 13, 1, refined.currentEquipPos, lockJson)
end)
end
```
**4. 后端修改 - 在洗练函数中保存锁定状态**
```lua
function npc:refined(actor, param3, str)
local player_ID = getbaseinfo(actor, 2)
local itemObj = linkbodyitem(actor, param3)
local itemOnlyID = getiteminfo(actor, itemObj, 1)
release_print("====== 开始洗练 ======")
release_print("玩家ID: "..player_ID..", 装备位置: "..param3..", 装备唯一ID: "..itemOnlyID)
-- 从全局表获取数据
local data = npc:getRefineData(actor, param3)
if not data then
sendmsg(actor, 1, '{"Msg":"<font color=\'#ff0000\'>★★★ 获取装备数据失败!★★★</font>","Type":9}')
return
end
-- 解析锁定状态
local lockStatus = json2tbl(str) or {}
local lockStatusTbl = {}
local lockCount = 0
-- 保存锁定状态到装备变量
for i = 1, 3 do
if lockStatus[i] == true then
lockCount = lockCount + 1
setitemparam(actor, -2, i, "1", itemObj) -- 保存锁定状态到装备变量
lockStatusTbl[i] = 1
else
setitemparam(actor, -2, i, "0", itemObj) -- 保存未锁定状态到装备变量
lockStatusTbl[i] = 0
end
end
-- 发送锁定状态到前端
local lockStatusJson = tbl2json(lockStatusTbl)
sendluamsg(actor, 1013, 13, 2, param3, lockStatusJson)
-- ... 其他洗练逻辑保持不变 ...
end
```
**5. 后端修改 - 新增获取锁定状态的函数**
```lua
-- 获取锁定状态
function npc:iniLock(actor, param3)
local player_ID = getbaseinfo(actor, 2)
local equipPos = tonumber(param3)
if not equipPos then
return false
end
local itemObj = linkbodyitem(actor, equipPos)
if not itemObj then
return false
end
-- 从装备变量中读取锁定状态
local lockStatusTbl = {}
for i = 1, 3 do
local lockValue = getitemintparam(actor, -2, i, itemObj)
lockStatusTbl[i] = tonumber(lockValue) or 0
end
-- 发送锁定状态到前端
local lockStatusJson = tbl2json(lockStatusTbl)
sendluamsg(actor, 1013, 13, 2, equipPos, lockStatusJson)
return true
end
```
**6. 后端修改 - 网络协议处理**
```lua
-- 在网络协议处理部分添加锁定状态的请求
if msgID == 2013 and param1 == 13 then
if param2 == 0 then
release_print("打开洗练界面",param1,data[param1],msgID)
include("QuestDiary/npc/"..data[param1]..".lua"):main(actor, param3)
end
if param2 == 1 then
release_print("执行洗练操作",param1,data[param1],msgID,param3)
include("QuestDiary/npc/"..data[param1]..".lua"):refined(actor, param3, str)
end
if param2 == 2 then
release_print("获取锁定状态",param1,data[param1],msgID,param3)
include("QuestDiary/npc/"..data[param1]..".lua"):iniLock(actor, param3)
end
end
```
**关键点说明:**
1. **状态存储**:使用 `setitemparam(actor, -2, i, "1", itemObj)` 将每个复选框的锁定状态(1表示锁定,0表示未锁定)保存到装备的变量中
2. **状态读取**:使用 `getitemintparam(actor, -2, i, itemObj)` 从装备变量中读取锁定状态
3. **状态同步**:每次切换装备或打开界面时,都会从服务端获取该装备的锁定状态并更新前端复选框
4. **状态保存**:每次点击洗练按钮时,都会将当前的复选框状态发送到服务端保存
这样修改后,复选框的锁定状态会永久保存在装备的变量中,即使界面关闭或切换其他装备再返回,状态也会保持不变。