lua中Mask掩码的写法

 

local MaskUtil = {}

function MaskUtil.Value(maskBit)
    return 2 ^ (maskBit)
end

---是否包含某个mask值
function MaskUtil.HasValue(allMasks, mask)
    return allMasks % (mask + mask) >= mask;
end

--- 添加mask
function MaskUtil.Set(allMasks, ...)
    local bits = {...}
    for _, p in ipairs(bits) do
        allMasks = MaskUtil.HasValue(allMasks, p) and allMasks or allMasks + p
    end
    return allMasks
end

--- 移除mask
function MaskUtil.Remove(allMasks, ...)
    local bits = {...}
    for _, v in ipairs(bits) do
        allMasks = MaskUtil.HasValue(allMasks, v) and (allMasks - v) or allMasks
    end
    return allMasks
end

 

用法

FontStyle = {
    Bold = MaskUtil.Value(0),
    Italic = MaskUtil.Value(1),
    Outline = MaskUtil.Value(2),
}


function DrawFont(fontStyle)
    if MaskUtil.HasValue(fontStyle, FontStyle.Italic) then
        --绘制斜体
    end

    if MaskUtil.HasValue(fontStyle, FontStyle.Bold) then
        --加粗绘制
    end

    if MaskUtil.HasValue(fontStyle, FontStyle.Outline) then
        --加上外描边
    end
end

 

posted @ 2025-07-08 23:07  yanghui01  阅读(4)  评论(0)    收藏  举报