lua界面属性设置_skybeauty_新浪博客

local BaseUI = {}

--[[--
给指定object添加一些基本特性
]]
function BaseUI.extend(object)
    --[[--
    初始化基本UI,例如坐标,锚点
    ]]
    function object:initBaseUI(conf)
        -- 设置坐标
        if conf.x and conf.y then
            local vx = conf.x
            local vy = conf.y
            if  conf.x < 1 and conf.x > 0 then
                vx = visibleSize.width *  conf.x
            end
            if conf.y < 1  and conf.y  > 0 then
                vy = visibleSize.height *  conf.y
            end

            -- 仅限于顶层元素
            local hAlign = conf.hAlign or "left"  -- 水平
            local vAlign = conf.vAlign or "bottom"          -- 垂直
            if hAlign == "right" then
                vx = visibleSize.width - vx
            end

            if vAlign == "top" then
                vy =   visibleSize.height - vy
            end

            object.displayObj:setPosition(ccp(vx, vy))
        end

        -- 设置缩放
        if conf.scale then
            object.displayObj:setScale(conf.scale)
        end

        -- 设置可见性
        if conf.visible ~= nil then
            object.displayObj:setVisible(conf.visible)
        end

        -- 设置描点
        if conf.anchorPoint ~= nil then
            object.displayObj:setAnchorPoint(ccp(conf.anchorPoint.x, conf.anchorPoint.y))
        end


        -- 吞噬事件
        if conf.swallowTouch then
            local function onTouch(eventType, x, y)
                return true
            end

            object.displayObj:setTouchEnabled(true)
            object.displayObj:registerScriptTouchHandler(onTouch,false,true)
        end

    end

    --[[--
    获取显示对象
    ]]
    function object:getDisplayObj()
        return object.displayObj
    end

    --[[--
    添加Child
    ]]
    function object:addChild(obj, isDisplayObj)
        if isDisplayObj then
            object.displayObj:addChild(obj)
        else
            object.displayObj:addChild(obj:getDisplayObj())
        end
    end

    --[[
    添加到父节点
    ]]
    function object:addToParent(parent, isDisplayObj)
        if isDisplayObj then
            parent:addChild(object.displayObj)
        else
            parent:addChild(object)
        end
    end

    --[[--
    清理
    ]]
    function object:clear()
        pcall(object.displayObj.removeFromParentAndCleanup, object.displayObj, true)
    end
end

--[[--
按照图片创建精灵
pic 图片配置
]]
function BaseUI.createSpriteFromPic(pic)
    if pic.frame ~= nil and pic.frame == true then
        local frame = CCSpriteFrameCache:sharedSpriteFrameCache():spriteFrameByName(pic.path)
        return CCSprite:createWithSpriteFrame(frame)
    else
        return CCSprite:create(tool.resPath(pic.path))
    end
end

--[[--
按照图片创建Sprite9
pic 图片配置
]]
function BaseUI.createSprite9FromPic(pic, middleRect, preferedSize)
    local sprite9
    if pic.frame ~= nil and pic.frame == true then
        sprite9 = CCScale9Sprite:createWithSpriteFrameName(pic.path, middleRect)
    else
        sprite9 = CCScale9Sprite:create(pic)
    end
    sprite9:setPreferredSize(preferedSize)
    return sprite9
end

return BaseUI
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值