看到你的代码,复选框状态目前是共享的,你需要为每个装备位置单独保存状态。我来帮你修改:
```lua
-- 在refined表中添加lockStatus来保存每个位置的复选框状态
refined.lockStatus = {}
-- 在openWin函数中初始化lockStatus
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)
-- 恢复当前装备位置的复选框状态
refined:restoreCheckBoxState(x)
-- 获取复选框状态
local lockData = {}
for i = 1, 3 do
local lockState = "CheckBox_"..i
local lockState_i = GUI:CheckBox_isSelected(refined.ui[lockState])
lockData[y] = lockData[y] or {}
if lockState_i == true then
lockData[y][i] = 1
else
lockData[y][i] = 0
end
end
local lockJson = SL:JsonEncode(lockData)
SL:SendLuaNetMsg(2013, 13, 0, x, lockJson)
end)
end
-- 添加复选框事件来保存状态
for i = 1, 3 do
local checkBoxName = "CheckBox_"..i
GUI:CheckBox_addOnEvent(refined.ui[checkBoxName], function()
refined:saveCheckBoxState()
end)
end
-- 首次进入时,在第一个按钮上创建高亮
if refined.ui["but_0"] then
refined:createHighLight(refined.ui["but_0"], 0)
refined:restoreCheckBoxState(0) -- 恢复第一个位置的复选框状态
end
end
-- 新增函数:保存当前装备位置的复选框状态
function refined:saveCheckBoxState()
local pos = refined.currentEquipPos
if not refined.lockStatus[pos] then
refined.lockStatus[pos] = {0, 0, 0}
end
for i = 1, 3 do
local checkBoxName = "CheckBox_"..i
local isSelected = GUI:CheckBox_isSelected(refined.ui[checkBoxName])
-- 注意:CheckBox_isSelected返回true表示选中,false表示未选中
refined.lockStatus[pos][i] = isSelected and 1 or 0
end
SL:print("保存位置" .. pos .. "的复选框状态: " ..
tostring(refined.lockStatus[pos][1]) .. ", " ..
tostring(refined.lockStatus[pos][2]) .. ", " ..
tostring(refined.lockStatus[pos][3]))
end
-- 新增函数:恢复指定装备位置的复选框状态
function refined:restoreCheckBoxState(pos)
if not refined.lockStatus[pos] then
refined.lockStatus[pos] = {0, 0, 0}
return
end
for i = 1, 3 do
local checkBoxName = "CheckBox_"..i
local state = refined.lockStatus[pos][i]
-- 设置复选框状态,1表示选中,0表示未选中
GUI:CheckBox_setSelected(refined.ui[checkBoxName], state == 1)
end
SL:print("恢复位置" .. pos .. "的复选框状态: " ..
tostring(refined.lockStatus[pos][1]) .. ", " ..
tostring(refined.lockStatus[pos][2]) .. ", " ..
tostring(refined.lockStatus[pos][3]))
end
-- 在refreshWin函数中,当装备位置改变时也需要恢复复选框状态
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
-- 装备位置改变时恢复复选框状态
refined.currentEquipPos = p3
refined:restoreCheckBoxState(p3)
end
end
```
主要修改点:
1. **添加lockStatus表**:为每个装备位置(0-11)保存独立的复选框状态
2. **保存状态**:当复选框状态改变时,自动保存到当前装备位置的lockStatus中
3. **恢复状态**:切换装备位置时,从lockStatus中恢复对应的复选框状态
4. **初始化**:每个位置初始状态都是{0, 0, 0}(未选中)
这样每个装备位置的复选框状态就完全独立了,切换装备位置时会显示对应的锁定状态。