我想鼠标 点到哪个装备或者背包的哪个道具就展示他的IDX
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-15 19:37:36 |
👁️ 阅读: 12
要实现鼠标点击装备或道具时展示其IDX,可以使用`GUI:MoveWidget_Create`函数创建可移动控件,结合点击事件获取道具的`MakeIndex`。以下是完整实现方案:
```lua
-- 创建主界面
local parent = GUI:Win_Create("ItemShowMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建显示IDX的文本控件
local textShow = GUI:Text_Create(parent, "Text_ShowIDX", 100, 100, 16, "#ffffff", "点击道具查看IDX")
-- 创建可移动控件(用于装备框)
local function createMoveWidgetForEquip(pos)
local moveWidget = GUI:MoveWidget_Create(
parent,
"MoveWidget_Equip_" .. pos,
0, 0, 50, 50, -- 位置和大小根据实际布局调整
SL:GetMetaValue("ITEMFROMUI_ENUM").PALYER_EQUIP, -- 装备来源
{
pcDoubleCB = function(makeIndex, itemData)
-- 双击回调,获取道具的MakeIndex
if makeIndex and makeIndex > 0 then
local idxStr = tostring(makeIndex)
GUI:Text_SetText(textShow, "装备IDX: " .. idxStr)
SL:Print("装备IDX:", idxStr)
end
end
}
)
return moveWidget
end
-- 创建可移动控件(用于背包)
local function createMoveWidgetForBag(pos)
local moveWidget = GUI:MoveWidget_Create(
parent,
"MoveWidget_Bag_" .. pos,
0, 0, 50, 50, -- 位置和大小根据实际布局调整
SL:GetMetaValue("ITEMFROMUI_ENUM").BAG, -- 背包来源
{
pcDoubleCB = function(makeIndex, itemData)
-- 双击回调,获取道具的MakeIndex
if makeIndex and makeIndex > 0 then
local idxStr = tostring(makeIndex)
GUI:Text_SetText(textShow, "背包IDX: " .. idxStr)
SL:Print("背包IDX:", idxStr)
end
end
}
)
return moveWidget
end
-- 示例:为装备位置1创建可移动控件
local equipWidget1 = createMoveWidgetForEquip(1)
-- 示例:为背包位置1创建可移动控件
local bagWidget1 = createMoveWidgetForBag(1)
-- 也可以为其他来源创建,如仓库
local function createMoveWidgetForStorage(pos)
local moveWidget = GUI:MoveWidget_Create(
parent,
"MoveWidget_Storage_" .. pos,
0, 0, 50, 50,
SL:GetMetaValue("ITEMFROMUI_ENUM").STORAGE,
{
pcDoubleCB = function(makeIndex, itemData)
if makeIndex and makeIndex > 0 then
local idxStr = tostring(makeIndex)
GUI:Text_SetText(textShow, "仓库IDX: " .. idxStr)
SL:Print("仓库IDX:", idxStr)
end
end
}
)
return moveWidget
end
-- 获取指定位置背包物品的MakeIndex(备用方法)
local function getBagItemMakeIndex(pos)
local makeIndex = SL:GetMetaValue("BAG_MAKEINDEX_BY_POS", pos)
if makeIndex and makeIndex > 0 then
return makeIndex
end
return nil
end
-- 点击事件测试
local testBtn = GUI:Button_Create(parent, "btn_test", 200, 200, "res/public/1900000612.png")
GUI:Button_setTitleText(testBtn, "测试")
GUI:addOnClickEvent(testBtn, function()
-- 测试获取背包第一个位置的MakeIndex
local idx = getBagItemMakeIndex(1)
if idx then
GUI:Text_SetText(textShow, "背包1号位IDX: " .. tostring(idx))
else
GUI:Text_SetText(textShow, "背包1号位无物品")
end
end)
```
**关键点说明:**
1. **`GUI:MoveWidget_Create`**:创建可移动的控件,支持道具拖拽和双击事件
- `from`参数指定道具来源:`SL:GetMetaValue("ITEMFROMUI_ENUM").PALYER_EQUIP`(装备)、`SL:GetMetaValue("ITEMFROMUI_ENUM").BAG`(背包)
- `ext.pcDoubleCB`:双击回调函数,参数`makeIndex`就是道具的唯一ID
2. **道具来源枚举**:根据浮生梦老师前端课程内容,常见的来源包括:
- 玩家装备(`PALYER_EQUIP`)
- 背包(`BAG`)
- 仓库(`STORAGE`)
- 快捷栏(`QUICKUSE`)
3. **备用方法**:`SL:GetMetaValue("BAG_MAKEINDEX_BY_POS", pos)`可以直接获取背包指定位置的MakeIndex
**使用建议:**
- 将可移动控件放在实际的装备框或背包格子上方
- 调整控件位置和大小与原有UI对齐
- 可以通过`GUI:setVisible(moveWidget, false)`隐藏控件,只保留功能
- 单机点击使用`pcDoubleCB`回调,如需单击可考虑其他交互方式
这样点击任意装备或道具时,就会在文本控件中显示其唯一的MakeIndex了。
← 返回问答库