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