2014-09-29 15:07:05 +08:00
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
#include "CCClippingRectangleNode.h"
|
2014-09-29 15:07:05 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "math/Vec2.h"
|
|
|
|
#include "CCGLView.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
ClippingRectangleNode* ClippingRectangleNode::create(const Rect& clippingRegion)
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
2014-10-11 11:49:01 +08:00
|
|
|
ClippingRectangleNode* node = new ClippingRectangleNode();
|
2014-09-29 15:07:05 +08:00
|
|
|
if (node && node->init()) {
|
|
|
|
node->setClippingRegion(clippingRegion);
|
|
|
|
node->autorelease();
|
|
|
|
} else {
|
|
|
|
CC_SAFE_DELETE(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
ClippingRectangleNode* ClippingRectangleNode::create()
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
2014-10-11 11:49:01 +08:00
|
|
|
ClippingRectangleNode* node = new ClippingRectangleNode();
|
2014-09-29 15:07:05 +08:00
|
|
|
if (node && node->init()) {
|
|
|
|
node->autorelease();
|
|
|
|
} else {
|
|
|
|
CC_SAFE_DELETE(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
void ClippingRectangleNode::setClippingRegion(const Rect &clippingRegion)
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
|
|
|
_clippingRegion = clippingRegion;
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
void ClippingRectangleNode::onBeforeVisitScissor()
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
|
|
|
if (_clippingEnabled) {
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2015-09-18 11:31:37 +08:00
|
|
|
float scaleX = _scaleX;
|
|
|
|
float scaleY = _scaleY;
|
|
|
|
Node *parent = this->getParent();
|
|
|
|
while (parent) {
|
|
|
|
scaleX *= parent->getScaleX();
|
|
|
|
scaleY *= parent->getScaleY();
|
|
|
|
parent = parent->getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Point pos = convertToWorldSpace(Point(_clippingRegion.origin.x, _clippingRegion.origin.y));
|
2014-09-29 15:07:05 +08:00
|
|
|
GLView* glView = Director::getInstance()->getOpenGLView();
|
2015-11-12 09:49:49 +08:00
|
|
|
#ifdef CC_STUDIO_ENABLED_VIEW // for cocostudio only
|
|
|
|
glView->setScissorInPoints(pos.x * scaleX,
|
|
|
|
pos.y * scaleY,
|
|
|
|
_clippingRegion.size.width * scaleX,
|
|
|
|
_clippingRegion.size.height * scaleY);
|
|
|
|
#else
|
2015-09-18 13:39:22 +08:00
|
|
|
glView->setScissorInPoints(pos.x,
|
|
|
|
pos.y,
|
2015-09-18 11:31:37 +08:00
|
|
|
_clippingRegion.size.width * scaleX,
|
|
|
|
_clippingRegion.size.height * scaleY);
|
2015-11-12 09:49:49 +08:00
|
|
|
#endif
|
2014-09-29 15:07:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
void ClippingRectangleNode::onAfterVisitScissor()
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
|
|
|
if (_clippingEnabled)
|
|
|
|
{
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-11 11:49:01 +08:00
|
|
|
void ClippingRectangleNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2014-09-29 15:07:05 +08:00
|
|
|
{
|
|
|
|
_beforeVisitCmdScissor.init(_globalZOrder);
|
2014-10-11 11:49:01 +08:00
|
|
|
_beforeVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onBeforeVisitScissor, this);
|
2014-09-29 15:07:05 +08:00
|
|
|
renderer->addCommand(&_beforeVisitCmdScissor);
|
|
|
|
|
|
|
|
Node::visit(renderer, parentTransform, parentFlags);
|
|
|
|
|
|
|
|
_afterVisitCmdScissor.init(_globalZOrder);
|
2014-10-11 11:49:01 +08:00
|
|
|
_afterVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onAfterVisitScissor, this);
|
2014-09-29 15:07:05 +08:00
|
|
|
renderer->addCommand(&_afterVisitCmdScissor);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|