Cocos2dx widget button transparent area Filter

Source: Internet
Author: User

Viagra encountered a proposition:

Button transparent area filtering. When you click a building button or flower, you have to find some ways to filter out the transparent area.

This makes the clicks ineffective.

I started searching for some ideas for a while.

There are a lot of code posts on the network.

Http://blog.csdn.net/lwuit/article/details/40658347

The Code is as follows:

bool CCMenu::CheckAlphaPoint(CCMenuItem* pChild, const CCPoint& point){    CCSize winSize = CCDirector::sharedDirector()->getWinSize();    CCNode* selectSprite = ((CCMenuItemSprite*)pChild)->getSelectedImage();        CCRenderTexture *renderer = CCRenderTexture::create(winSize.width, winSize.height);    renderer->begin();        bool visible = selectSprite->isVisible();    if (visible) {        selectSprite->visit();    }    else    {        selectSprite->setVisible(true);        selectSprite->visit();        selectSprite->setVisible(false);    }        GLubyte pixelColors[4];    #if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)    glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);#else    glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &pixelColors[0]);#endif        int alpha = pixelColors[0];    CCLOG("----alpha %d", alpha);        renderer->end();        if (alpha <= 30)    {       return true;    }    else    {        return false;    }    }

The above code is indeed in the test project directly resume ccsprite live menuitem can be executed.


As the UI tool advances. We chose cocostudio widgets. It's convenient for you and me.

But I can, however, paste the above Code and try again.


Some comrades have abandoned the exploration of knowledge principles.

The program is bitter. If you encounter such a problem, do you have to study it further?

After multiple scrutiny and drawing speculation.

Later I found the root cause of the problem:

Ccrendertexture * after Renderer is rendered, the color value above the position cannot be 0. 00000 why is it 0?
Is visit () good? Various questions

bool Widget::onTouchBegan(CCTouch *touch, CCEvent *unused_event){    _touchStartPos = touch->getLocation();    _hitted = isEnabled()            & isTouchEnabled()            & hitTest(_touchStartPos)    & clippingParentAreaContainPoint(_touchStartPos);        if (!_hitted)    {        return false;    }        // add yww alpha check    if (!AlphaTouchCheck(_touchStartPos))    {        return false;    }        setFocused(true);    Widget* widgetParent = getWidgetParent();    if (widgetParent)    {        widgetParent->checkChildInfo(0,this,_touchStartPos);    }    pushDownEvent();    return !_touchPassedEnabled;}

The above is the key detection logic.


The modified code is as follows. The principle is very simple. In the widget, the position of the ccnode node is 0 relative to the parent node, so the position starts from 0 to 0 during visit.

Modify the rendering node location. Convert to screen coordinates and then obtain the transparency value of the currently clicked Pixel based on touch coordinates.


// yww get alpha touch event checkbool Button::AlphaTouchCheck(const CCPoint &point){    bool isTouchClaimed = false;        if (getAlphaTouchEnable())    {            // check claimed touch arena            CCSize winSize = CCDirector::sharedDirector()->getWinSize();            CCSprite* selectSprite = (CCSprite*)getVirtualRenderer();            CCPoint cutPos = selectSprite->getPosition();            // CCLOG("getAlphaTouchEnable selectSprite X %f, Y %f", cutPos.x, cutPos.y);                    // get screen point            CCPoint wordpx = selectSprite->getParent()->convertToWorldSpace(cutPos);            // CCLOG("getAlphaTouchEnable convertToWorldSpace X %f, Y %f", wordpx.x, wordpx.y);                    selectSprite->setPosition(wordpx);                    CCRenderTexture *renderer = CCRenderTexture::create(winSize.width, winSize.height);            //selectSprite->addChild(renderer);                    renderer->begin();                    bool visible = selectSprite->isVisible();            if (visible)            {                selectSprite->visit();            }            else            {                selectSprite->setVisible(true);                selectSprite->visit();                selectSprite->setVisible(false);            }                    GLubyte pixelColors[4];        #if ( CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)            glReadPixels(point.x, point.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixelColors[0]);#else            glReadPixels(point.x, point.y, 1, 1, GL_ALPHA, GL_UNSIGNED_BYTE, &pixelColors[0]);#endif                    int alpha = pixelColors[0];            CCLOG("----alpha %d", alpha);                    renderer->end();                    selectSprite->setPosition(cutPos);                    if (alpha <= 20)            {                isTouchClaimed = false;            }            else            {                isTouchClaimed = true;            }        // check claimed touch arena    }    else    {        isTouchClaimed = true;    }    return isTouchClaimed;}

The above logic is to override the custom function of the widget.

Alphatouchcheck

The structure is built based on your own needs.

In Lua, you can check whether the transparent paper is detected.

Let's talk about it later. A waste of network memory.


Cocos2dx widget button transparent area Filter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.