COCOS按钮优先级处理。cocos studio 的widget 和cocos组件 的问题

--


--
-- 还在为cocos组件跟cocos studio的widget触摸优先度而烦恼吗?这个类可以帮到你
-- transNode可以把cocos组件(node,layer,sprite等)的触摸方式变成跟widget同样,就可以通过显示前后来决定触摸优先度啦
-- UIPushButton用transNode也行,transUIPushBtn也可以
TransLuaEventNodeToWidgetFactory={}


local function checkVisible(node)
if node:isVisible() == false then
return false
elseif node.getParent and node:getParent() then
return checkVisible(node:getParent())
end
return true
end
function TransLuaEventNodeToWidgetFactory.transNode(luaNode,isSwallowTouches)
local isSwallowTouches = isSwallowTouches
if isSwallowTouches == nil and luaNode.isTouchSwallowEnabled then
isSwallowTouches = luaNode:isTouchSwallowEnabled()
end
if isSwallowTouches == nil then
isSwallowTouches = true
end

if luaNode._scriptEventListeners_==nil or luaNode._scriptEventListeners_[cc.NODE_TOUCH_EVENT]==nil then
return
end

luaNode:setTouchEnabled(false)
luaNode._touchEnabled = false
function luaNode.onTouchBegan (touch, unusedEvent)
local event = {name="began",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaNode._touchEnabled and checkVisible(luaNode) and luaNode:getCascadeBoundingBox():containsPoint(touch:getLocation())
-- print(luaNode:convertToWorldSpace(cc.p(luaNode:getPosition())).x)
-- print(touch:getLocation().x)
-- print(luaNode:convertToWorldSpace(cc.p(luaNode:getPosition())).y)
-- print(touch:getLocation().y)
if inArea then
luaNode:EventDispatcher(cc.NODE_TOUCH_EVENT,event)
end
return inArea
end

function luaNode.onTouchMoved (touch, unusedEvent)
local event = {name="moved",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaNode._touchEnabled --and luaNode:getCascadeBoundingBox():containsPoint(touch:getLocation())
if inArea then
luaNode:EventDispatcher(cc.NODE_TOUCH_EVENT,event)
end
end

function luaNode.onTouchEnded (touch, unusedEvent)
local event = {name="ended",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaNode._touchEnabled --and luaNode:getCascadeBoundingBox():containsPoint(touch:getLocation())
if inArea then
luaNode:EventDispatcher(cc.NODE_TOUCH_EVENT,event)
-- else
-- event.name="cancelled"
-- luaNode:EventDispatcher(cc.NODE_TOUCH

end
end

function luaNode.onTouchCancelled (touch, unusedEvent)
local event = {name="cancelled",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
if luaNode._touchEnabled then
luaNode:EventDispatcher(cc.NODE_TOUCH_EVENT,event)
end
end

function luaNode:setTouchEnabled(enable)
if enable == self._touchEnabled then
return
end
local _eventDispatcher = cc.Director:getInstance():getEventDispatcher()
self._touchEnabled = enable
if (self._touchEnabled) then
self._touchListener = cc.EventListenerTouchOneByOne:create()
self._touchListener:setSwallowTouches(isSwallowTouches)
self._touchListener:registerScriptHandler(self.onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
self._touchListener:registerScriptHandler(self.onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)
self._touchListener:registerScriptHandler(self.onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
self._touchListener:registerScriptHandler(self.onTouchCanceld,cc.Handler.EVENT_TOUCH_CANCELLED)
_eventDispatcher:addEventListenerWithSceneGraphPriority(self._touchListener,self)
else
_eventDispatcher:removeEventListener(self._touchListener)
end
end

luaNode:setTouchEnabled(true)
return luaNode


function TransLuaEventNodeToWidgetFactory.transUIPushBtn(luaBtn,isSwallowTouches)
luaBtn._touchEnabled = false
if isSwallowTouches == nil then
isSwallowTouches = true
end
function luaBtn.onTouchBegan (touch, unusedEvent)
local event = {name="began",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}

local inArea = luaBtn._touchEnabled and checkVisible(luaBtn) and luaBtn:checkTouchInSprite_(touch:getLocation())
if inArea then
luaBtn:onTouch_(event)
end
return inArea
end

function luaBtn.onTouchMoved (touch, unusedEvent)
local event = {name="moved",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaBtn._touchEnabled and luaBtn:checkTouchInSprite_(touch:getLocation())
if inArea then
luaBtn:onTouch_(event)
end
end

function luaBtn.onTouchEnded (touch, unusedEvent)
local event = {name="ended",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaBtn._touchEnabled and luaBtn:checkTouchInSprite_(touch:getLocation())
if inArea then
luaBtn:onTouch_(event)
else
event.name="cancelled"
luaBtn:onTouch_(event)
end
end


function luaBtn.onTouchCancelled (touch, unusedEvent)
local event = {name="cancelled",
x=touch:getLocation().x,
y=touch:getLocation().y,
prevX=touch:getPreviousLocation().x,
prevY=touch:getPreviousLocation().y,
}
local inArea = luaBtn._touchEnabled and luaBtn:checkTouchInSprite_(touch:getLocation())
if inArea then
luaBtn:onTouch_(event)
end
end


function luaBtn:setButtonEnabled(enable)
if enable == luaBtn._touchEnabled then
return
end

local _eventDispatcher = cc.Director:getInstance():getEventDispatcher()
luaBtn._touchEnabled = enable

if (self._touchEnabled) then
self._touchListener = cc.EventListenerTouchOneByOne:create()
self._touchListener:setSwallowTouches(isSwallowTouches)
self._touchListener:registerScriptHandler(self.onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)

self._touchListener:registerScriptHandler(self.onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)
self._touchListener:registerScriptHandler(self.onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
self._touchListener:registerScriptHandler(self.onTouchCancelled,cc.Handler.EVENT_TOUCH_CANCELLED)


_eventDispatcher:addEventListenerWithSceneGraphPriority(self._touchListener,self)
else
_eventDispatcher:removeEventListener(self._touchListener)
end
end
luaBtn:setButtonEnabled(true)
return luaBtn
end
return TransLuaEventNodeToWidgetFactory


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值