今天突然发现用2.0时代重绘方式处理不规则按钮响应的已经无效了。
原因3.0时代异步方式重绘了,
怎么高效的过滤透明区域响应?
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;
}
if(_realTouchRange)
{
Node target = static_cast<Node>(unusedEvent->getCurrentTarget());
std::string strField = target->getName();
//Vec2 touchPoint = target->convertTouchToNodeSpaceAR(touch);
Vec2 touchPoint = target ->convertToNodeSpace(touch->getLocation());
Size s = target ->getContentSize();
RenderTexture* renderTexture = RenderTexture::create(target->getContentSize().width, target->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
uint8_t data;
//RenderTexture* renderTexture = RenderTexture::create(1* CC_CONTENT_SCALE_FACTOR(),1 * CC_CONTENT_SCALE_FACTOR(), Texture2D::PixelFormat::RGBA8888);
renderTexture->beginWithClear(0,0,0,0);
//只保存渲染一个像素的数据
Vec2 oldPos = target->getPosition();
Vec2 oldAnchor = target->getAnchorPoint();
target->setAnchorPoint(Vec2::ZERO);
//target->setPosition(Vec2(-touchPoint.x * CC_CONTENT_SCALE_FACTOR(), -touchPoint.y * CC_CONTENT_SCALE_FACTOR()));
target->visit();
target->setAnchorPoint(oldAnchor);
target->setPosition(oldPos);
renderTexture->end();
Renderer* renderer = Director::getInstance()->getRenderer();
//Director::getInstance()->getRenderer()->render();
renderer->render();
Image * pImage = renderTexture->newImage();
//Color4B c = pImage->getColor4B(point.x, point.y);
//获取像素数据
unsigned char* data_ = pImage->getData();
unsigned int *pixel = (unsigned int *)data_;
Color4B c;
//R通道
c.r = *pixel & 0xff;
//G通道
c.g = (*pixel >> 8) & 0xff;
//B通过
c.b = (*pixel >> 16) & 0xff;
//Alpha通道,我们有用的就是Alpha
c.a = (pixel >> 24) & 0xff;
if(c.a < 50)
{
return false;
}
}
setHighlighted(true);
Widget widgetParent = getWidgetParent();
if (widgetParent)
{
widgetParent->interceptTouchEvent(TouchEventType::BEGAN, this, touch);
}
pushDownEvent();
return true;
}