Hello,
I am migrating from V2.2 to V3.2 and I need to use cocos2d-x ClippingNode.
In V2.2, I was using a custom ClippingNode, which was defined as such:
ClippingNode.h
//use to clip children to the zone defined by contentsize
class ClippingNode : public CCNode, public CCActionTweenDelegate
{
public:
virtual void visit();
static ClippingNode* create();
virtual void updateTweenAction(float value, const char* key);
};
ClippingNode.cpp
void ClippingNode::visit()
{
if (!this->isVisible()) {
return;
}
glEnable(GL_SCISSOR_TEST);
CCPoint bottomLeft = this->convertToWorldSpaceAR(ccp(this->getContentSize().width * this->getAnchorPoint().x * -1, this->getContentSize().height * this->getAnchorPoint().y * -1));
CCPoint topRight = this->convertToWorldSpaceAR(ccp(this->getContentSize().width * (1 - this->getAnchorPoint().x), this->getContentSize().height * (1 - this->getAnchorPoint().y)));
topRight = ccpSub(topRight, bottomLeft);
glScissor(bottomLeft.x,
bottomLeft.y,
topRight.x,
topRight.y);
CCNode::visit();
glDisable(GL_SCISSOR_TEST);
}
ClippingNode* ClippingNode::create()
{
ClippingNode* newNode = new ClippingNode();
newNode->autorelease();
return newNode;
}
void ClippingNode::updateTweenAction(float value, const char* key)
{
if(strcmp(key, "width") == 0)
{
setContentSize(CCSize(value, getContentSize().height));
}
else if(strcmp(key, "height") == 0)
{
setContentSize(CCSize(getContentSize().width, value));
}
}
My two basic needs:
- have the ClippingNode clip its children to what’s inside its ContentSize
- be able to change that ContentSize with an ActionTween
In V3.2, there is a ClippingNode in cocos2d-x. The problem is it requires a stencil. Using a sprite is not practical, since I either need a specific texture the right size, or I have to re-calculate the scale, which seems ugly to me (and add a dependency on a texture I do not want).
I looked at cpp-test, and it uses a DrawNode somewhere. The problem is: I can’t get it to work.
Working code, where myNode is a CCSprite loaded from CocosBuilder, which have the same position/anchorpoint/scale/parent as my ClippingNode, and opacity 0 (since I don’t want to actually show it):
myNode->setVisible(true);
clipNode->setStencil(myNode);
Code not working, nothing of what’s inside the ClippingNode is shown:
auto stencil = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(0, 0);
rectangle[1] = Vec2(clipNode->getContentSize().width, 0);
rectangle[2] = Vec2(clipNode->getContentSize().width, clipNode->getContentSize().height);
rectangle[3] = Vec2(0, clipNode->getContentSize().height);
Color4F white(1, 1, 1, 1);
stencil->drawPolygon(rectangle, 4, white, 1, white);
stencil->setPosition(clipNode->getPosition());
stencil->setAnchorPoint(clipNode->getAnchorPoint());
stencil->setContentSize(clipNode->getContentSize());
clipNode->getParent()->addChild(stencil);
clipNode->setStencil(stencil);
What’s wrong with that code?
As for the ActionTween, I guess I’ll move it to the stencil, which will be a subclass of DrawNode.