根据浮生梦老师前端课程内容,装备基础属性的显示主要涉及属性数据的解析和格式化输出。以下是关键步骤:
1. **获取装备基础属性数据**:
```lua
local itemCfg = SL:GetMetaValue("ITEM_DATA", item.Index)
local attList = {}
if itemCfg and itemCfg.attribute then
local tAttribute = string.split(itemCfg.attribute or "", "|")
for i, v in ipairs(tAttribute) do
if v and v ~= "" and string.len(v) > 0 then
local tAttribute2 = string.split(v or "", "#")
table.insert(attList, {
id = tonumber(tAttribute2[2]) or 3,
value = tonumber(tAttribute2[3]) or 0
})
end
end
end
```
2. **属性显示格式化**:
```lua
local function getAttOriginId(id)
return id >= 10000 and math.floor(id / 10000) or id
end
local function getAddShow(id, value)
if tonumber(value) and tonumber(value) < 0 then
return ""
end
if id == 1 or id == 2 or id == 13 or id == 14 or id == 15 or id == 16 or id == 17 or id == 18 or id == 19 or id == 20 or id == 38 or id == 39 then
return "+"
end
return ""
end
```
3. **属性显示队列处理**:
```lua
local stringAtt = GUIFunction:GetAttDataShow(attList, nil, true)
local basicAttrShow = {}
local yuansuAttrShow = {}
for id, v in pairs(stringAtt) do
v.id = id
local originId = getAttOriginId(id)
local attConfig = SL:GetMetaValue("ATTR_CONFIG", originId)
v.sort = attConfig and attConfig.sort or originId + 1000
if attConfig and attConfig.ys == 1 then
table.insert(yuansuAttrShow, v)
else
table.insert(basicAttrShow, v)
end
end
-- 排序显示
table.sort(basicAttrShow, function(a, b)
return a.sort < b.sort
end)
```
4. **最终显示输出**:
```lua
for _, v in pairs(basicAttrShow) do
local name = string.gsub(v.name, " ", "")
name = string.gsub(name, " ", "")
local value = getAddShow(v.id, v.value) .. v.value
local nameLen, chineseLen = SL:GetUTF8ByteLen(name)
local newLen = math.max(attrAlignment - nameLen - chineseLen * attrCoefficient + SL:GetUTF8ByteLen(value), 0)
local lenStr = string.format("%%%ds", newLen)
value = string.format(lenStr, value)
-- 这里可以创建Text控件显示属性
end
```
注意:属性ID 10-23对应不同的属性类型,需要根据`ATTR_CONFIG`配置获取具体的属性名称和显示格式。