我来帮你写一个添加红点的函数。根据你的代码,我需要为`item_show_clone`和第一个`button_obj`控件添加红点。
```lua
-- 红点管理函数
function npc:addRedPointToWidget(widget, redPointId, showState)
if not widget or GUI:Win_IsNull(widget) then
return
end
if showState then
-- 添加红点
ssrAddRedpoint(widget, nil, nil, 1, -4)
else
-- 移除红点
ssrDelRedpoint(widget, 1)
end
end
-- 判断红点是否显示的函数
function npc:shouldShowRedPoint(itemType, itemIndex)
-- 这里根据你的业务逻辑来判断红点是否显示
-- 示例:根据物品是否已收集来判断
-- 获取物品的收集状态(这里需要根据你的实际逻辑来写)
local isCollected = SL:GetMetaValue("ITEM_COLLECTED_STATUS", itemType, itemIndex)
-- 如果物品未收集,则显示红点
return not isCollected
end
-- 在switch_window_show函数中添加红点
function npc:switch_window_show(id)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", id))
if id == 1 then
GUI:setVisible(JM_show, true)
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local item_show = GUI:getChildByName(JM_show, "sj_1_item")
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
local children = GUI:getChildren(pssj_ListView)
-- 为第一个button_obj添加红点
local zc_ListView = GUI:getChildByName(npc.ui.Image_1, "ListView_zc")
local zc_children = GUI:getChildren(zc_ListView)
if zc_children and zc_children[1] then
local firstButton = zc_children[1]
local shouldShow = npc:shouldShowRedPoint("button", 1)
npc:addRedPointToWidget(firstButton, "first_button", shouldShow)
end
for k, v in ipairs(npc.fbsj_t) do
local sj_show = GUI:getChildByName(pssj_ListView, string.format("sj_show_" .. k))
local sj_name = GUI:getChildByName(sj_show, "sj_name")
local sj_name_str = GUI:Text_getString(sj_name)
GUI:Text_setString(sj_name, string.format(sj_name_str, v.xilie))
local sj_sx = GUI:getChildByName(sj_show, "sj_sx")
GUI:Text_setString(sj_sx, v.str)
local sj_jd = GUI:getChildByName(sj_show, "sj_jd")
local sj_jd_str = GUI:Text_getString(sj_jd)
GUI:Text_setString(sj_jd, string.format(sj_jd_str, 0, v.mun))
local x_js = v.mun == 6 and 98 or 70
local children = GUI:getChildren(sj_show)
for i, child in ipairs(children) do
if child ~= sj_name and child ~= sj_sx and child ~= sj_jd then
GUI:removeChildByName(sj_show, GUI:getName(child))
end
end
for kk, vv in ipairs(v) do
local item_show_clone = GUI:Clone(item_show)
GUI:setVisible(item_show_clone, true)
GUI:addChild(sj_show, item_show_clone)
GUI:setPosition(item_show_clone, 40 + x_js * (kk - 1), 50)
-- 为item_show_clone添加红点
local shouldShow = npc:shouldShowRedPoint("item", vv)
npc:addRedPointToWidget(item_show_clone, "item_" .. vv, shouldShow)
local shoulu = GUI:getChildByName(item_show_clone, "shoulu")
local itemData = {
index = SL:GetMetaValue("ITEM_INDEX_BY_NAME", vv),
look = true,
bgVisible = false,
count = 1,
color = 225
}
local item = GUI:ItemShow_Create(item_show_clone, "my_item", 0, 0, itemData)
GUI:ItemShow_setIconGrey(item, true)
GUI:setPosition(item, current_opermode == 2 and 0 or 15, current_opermode == 2 and 0 or 15)
GUI:addOnClickEvent(shoulu, function()
SL:print(k, kk)
GUIUtil:Lua_SubmitForm("装备回收", "yijianshoulututeng", "")
end)
end
for k, v in ipairs(npc.fbsj_t.arstr) do
local shuxing_show = GUI:getChildByName(JM_show, "sx_show_" .. k)
GUI:Text_setString(shuxing_show, v[1])
end
local Button_yjsl = GUI:getChildByName(JM_show, "Button_yjsl")
GUI:addOnClickEvent(Button_yjsl, function()
GUIUtil:Lua_SubmitForm("配饰图腾", "yijianshoulututeng", "")
end)
end
elseif id == 2 then
GUI:setVisible(JM_show, true)
local item_Panel_1 = GUI:getChildByName(JM_show, "item_Panel_1")
local ListView_fj = GUI:getChildByName(JM_show, "ListView_fj")
GUI:removeAllChildren(ListView_fj)
for i = 1, 10 do
local item_bj = GUI:Clone(item_Panel_1)
GUI:setVisible(item_bj, true)
GUI:addChild(ListView_fj, item_bj)
end
elseif id == 3 then
GUI:setVisible(JM_show, true)
elseif id == 4 then
GUI:setVisible(JM_show, true)
end
end
```
**函数说明:**
1. **`npc:addRedPointToWidget(widget, redPointId, showState)`**
- 功能:为控件添加或移除红点
- 参数:
- `widget`: 控件对象
- `redPointId`: 红点标识(用于区分不同红点)
- `showState`: 布尔值,true显示红点,false隐藏红点
2. **`npc:shouldShowRedPoint(itemType, itemIndex)`**
- 功能:判断红点是否应该显示
- 参数:
- `itemType`: 物品类型("button"或"item")
- `itemIndex`: 物品索引或名称
- 返回值:布尔值,true显示红点,false隐藏红点
**使用方式:**
1. 在`switch_window_show`函数中,我为第一个按钮和每个`item_show_clone`都添加了红点
2. 你需要根据实际业务逻辑修改`shouldShowRedPoint`函数的判断条件
3. 红点的位置可以通过`ssrAddRedpoint`的最后一个参数调整(示例中是-4)
**注意:**
- 红点函数`ssrAddRedpoint`和`ssrDelRedpoint`在知识库中已有示例
- 记得在适当的时候调用`npc:addRedPointToWidget()`来更新红点状态