cocostudio 事件穿透(coco2dx 3.2)

因为经常需要让事件穿透到下一层的scrollview.可在cocostudio 中默认并没有开启事件穿透.查看原代码看到直接setSwallowTouches为true了.

对UIWidget做出如下的处理



在UIWidget.h中加入三个成员变量用与控制事件穿透


//UIWidget.h中的改动

public:
/**
	 * Sets whether the widget swallow touches
	 * the default value is true
	 * @param needSwallow true if the widget is need to swallow touches
	 * @param always false if the widget will swallow touches only when hitTest success
	 */
	void setSwallowTouches(bool needSwallow,bool always = true);
private:	
bool _swallowTouches;//控制是否需要穿透
bool _penetrateTouchesAlways;//用于决定是否总是穿透.
bool _hadMoveOutHitArea;//用于判断是否在移动过程中移出点击区域

//UIWidget.cpp中的改动
void Widget::setSwallowTouches(bool needSwallow,bool always/* = true*/)
{
	if (_touchListener)
	{
		_touchListener->setSwallowTouches(needSwallow);
	}
	_swallowTouches = needSwallow;
	_penetrateTouchesAlways = always;
}

bool Widget::onTouchBegan(Touch *touch,Event *unusedEvent)
{
    _hitted = false;
		
    if (isVisible() && isEnabled() && isAncestorsEnabled() && isAncestorsVisible(this) )
    {
        _touchBeganPosition = touch->getLocation();
        if(hitTest(_touchBeganPosition) && isClippingParentContainsPoint(_touchBeganPosition))
        {
            _hitted = true;
        }
    }
    if (!_hitted)
    {
        return false;
    }
    setHighlighted(true);
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->interceptTouchEvent(TouchEventType::BEGAN,this,touch);
    }
    pushDownEvent();

// 	if (!_penetrateTouchesAlways)
// 	{
// 		_touchListener->setSwallowTouches(true);
// 		log("beginning~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
// 	}
	_hadMoveOutHitArea = false;
    return true;
}

void Widget::onTouchMoved(Touch *touch,Event *unusedEvent)
{
    _touchMovePosition = touch->getLocation();
	if (hitTest(_touchMovePosition))
	{
		setHighlighted(true);
		if (!_swallowTouches && !_penetrateTouchesAlways && !_hadMoveOutHitArea)
			_touchListener->setSwallowTouches(true);
	}
	else
	{
		setHighlighted(false);
		if (!_swallowTouches && !_penetrateTouchesAlways)
		{
			_touchListener->setSwallowTouches(false);
			_hadMoveOutHitArea = true;
		}
	}
    
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->interceptTouchEvent(TouchEventType::MOVED,touch);
    }
    moveEvent();
}

void Widget::onTouchEnded(Touch *touch,Event *unusedEvent)
{
    bool highlight = _highlight;
    
    setHighlighted(false);
    
	if (!_swallowTouches && !_penetrateTouchesAlways)
	{
		_touchListener->setSwallowTouches(false);
		if (_hadMoveOutHitArea)
			return;
	}

    _touchEndPosition = touch->getLocation();
    
    Widget* widgetParent = getWidgetParent();
    if (widgetParent)
    {
        widgetParent->interceptTouchEvent(TouchEventType::ENDED,touch);
    }
    
    if (highlight)
    {
        releaseUpEvent();
    }
    else
    {
        cancelUpEvent();
    }
}
通过以上的修改,如果需要在extentions::scrollview中加入 widget.把widget->setSwallowTouches(false,false);就能得到一个很顺的效果

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...