local function pushDescItem(desc, descTag)
if not desc or not ItemTips.contentPanel then
return
end
local movePosY = 0
if desc and next(desc) then
local contentSz = GUI:getContentSize(ItemTips.contentPanel)
for
这段代码是浮生梦老师前端课程中处理物品描述显示的典型实现。我来逐行解析:
1. **函数定义和参数检查**:
```lua
local function pushDescItem(desc, descTag)
if not desc or not ItemTips.contentPanel then
return
end
```
- `desc`:描述数据表
- `descTag`:描述标签类型(1表示顶部描述)
- 如果描述为空或内容面板不存在,直接返回
2. **变量初始化**:
```lua
local movePosY = 0
if desc and next(desc) then
local contentSz = GUI:getContentSize(ItemTips.contentPanel)
```
- `movePosY`:Y轴位置调整值
- `contentSz`:获取内容面板的尺寸
3. **遍历描述数据**:
```lua
for i, v in ipairs(desc) do
local isadd = true
if descTag == 1 then
if v.y and v.y ~= 0 then
isadd = false
v.y = contentSz.height - v.y
v.isTop = true
table.insert(topDesY, v)
end
end
```
- `isadd`:标记是否添加到面板
- 当`descTag==1`(顶部描述)且`v.y`不为0时:
- 设置`isadd=false`(不直接添加)
- 计算顶部坐标:`contentSz.height - v.y`
- 标记`isTop=true`
- 存入`topDesY`表(顶部描述专用)
4. **处理可添加项**:
```lua
if isadd then
if v.isTop then
v.y = contentSz.height - v.y
GUI:setContentSize(ItemTips.contentPanel, contentSz)
end
```
- 如果是顶部描述项,重新计算Y坐标
5. **处理换行**:
```lua
if v.newLine then
local lineH = v.height or 0
lineH = lineH > 0 and lineH or _DefaultSpace
local linePanel = ItemTips.CreateIntervalPanel(ItemTips.contentPanel, lineH, true)
GUI:setPosition(linePanel, v.x or 0, v.y or 0)
ItemTips.PushItem(ItemTips.contentPanel, linePanel)
```
- 创建间隔面板作为换行
- `_DefaultSpace`:默认间距常量
6. **处理文本**:
```lua
elseif v.text then
rich_att_num = rich_att_num + 1
local textSize = _setFontSize or _DefaultFSize
if v.fontsize and v.fontsize > 0 then
textSize = v.fontsize
end
local rich_att = GUI:RichTextFCOLOR_Create(ItemTips.contentPanel, string.format("rich_att_%s", rich_att_num), math.floor(v.x or 0), math.floor(v.y or 0), v.text, ItemTips._isItem and 420 or (_TotalWidth - 20), textSize, "#FFFFFF", vspace, nil, fontPath)
ItemTips.PushItem(ItemTips.contentPanel, rich_att)
if SL:GetMetaValue("WINPLAYMODE") then
movePosY = movePosY - 1
end
```
- 创建彩色富文本控件
- `GUI:RichTextFCOLOR_Create()`参数:
- `parent`:父节点
- `ID`:唯一标识
- `x,y`:坐标
- `text`:文本内容
- `width`:宽度(物品描述420,其他自适应)
- `fontSize`:字体大小
- `color`:颜色
- `vspace`:垂直间距
- `fontPath`:字体路径
- PC模式下调整Y坐标
7. **处理图片**:
```lua
elseif v.res then
desc_img_num = desc_img_num + 1
local img = ItemTips.GetNodeImage(ItemTips.contentPanel, v, desc_img_num)
if img then
ItemTips.PushItem(ItemTips.contentPanel, img)
if v.isSFX and SL:GetMetaValue("WINPLAYMODE") then
local imgSz = GUI:getContentSize(img)
local posX = GUI:getPositionX(img)
local posY = GUI:getPositionY(img)
GUI:setPosition(img, math.floor(posX), math.floor(posY - imgSz.height / 2 + movePosY))
end
end
```
- 处理描述中的图片资源
- 如果是特效图片且PC模式,调整位置居中显示
**关键点**:
1. 顶部描述单独处理,不直接添加到面板
2. 支持文本、图片、换行三种描述类型
3. PC和移动端适配处理
4. 坐标系统采用从左上角开始的坐标系
这个函数展示了浮生梦老师课程中描述的模块化处理和坐标转换技巧。