From e0b9878e585f43cf950c091a3e430c552b68f9cd Mon Sep 17 00:00:00 2001 From: SunLightJuly Date: Mon, 29 Sep 2014 15:07:05 +0800 Subject: [PATCH] added ClippingRegionNode --- cocos/2d/CCClippingRegionNode.cpp | 84 +++++++++++++++++++++++++++++++ cocos/2d/CCClippingRegionNode.h | 77 ++++++++++++++++++++++++++++ cocos/cocos2d.h | 1 + tools/tolua/cocos2dx.ini | 2 +- 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 cocos/2d/CCClippingRegionNode.cpp create mode 100644 cocos/2d/CCClippingRegionNode.h diff --git a/cocos/2d/CCClippingRegionNode.cpp b/cocos/2d/CCClippingRegionNode.cpp new file mode 100644 index 0000000000..5b9eb81781 --- /dev/null +++ b/cocos/2d/CCClippingRegionNode.cpp @@ -0,0 +1,84 @@ + +#include "CCClippingRegionNode.h" +#include "base/CCDirector.h" +#include "renderer/CCRenderer.h" +#include "math/Vec2.h" +#include "CCGLView.h" + +NS_CC_BEGIN + +ClippingRegionNode* ClippingRegionNode::create(const Rect& clippingRegion) +{ + ClippingRegionNode* node = new ClippingRegionNode(); + if (node && node->init()) { + node->setClippingRegion(clippingRegion); + node->autorelease(); + } else { + CC_SAFE_DELETE(node); + } + + return node; +} + +ClippingRegionNode* ClippingRegionNode::create(void) +{ + ClippingRegionNode* node = new ClippingRegionNode(); + if (node && node->init()) { + node->autorelease(); + } else { + CC_SAFE_DELETE(node); + } + + return node; +} + +void ClippingRegionNode::setClippingRegion(const Rect &clippingRegion) +{ + _clippingRegion = clippingRegion; +} + +void ClippingRegionNode::onBeforeVisitScissor() +{ + if (_clippingEnabled) { + glEnable(GL_SCISSOR_TEST); + + 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)); + GLView* glView = Director::getInstance()->getOpenGLView(); + glView->setScissorInPoints(pos.x * scaleX, + pos.y * scaleY, + _clippingRegion.size.width * scaleX, + _clippingRegion.size.height * scaleY); + } +} + +void ClippingRegionNode::onAfterVisitScissor() +{ + if (_clippingEnabled) + { + glDisable(GL_SCISSOR_TEST); + } +} + +void ClippingRegionNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) +{ + _beforeVisitCmdScissor.init(_globalZOrder); + _beforeVisitCmdScissor.func = CC_CALLBACK_0(ClippingRegionNode::onBeforeVisitScissor, this); + renderer->addCommand(&_beforeVisitCmdScissor); + + Node::visit(renderer, parentTransform, parentFlags); + + _afterVisitCmdScissor.init(_globalZOrder); + _afterVisitCmdScissor.func = CC_CALLBACK_0(ClippingRegionNode::onAfterVisitScissor, this); + renderer->addCommand(&_afterVisitCmdScissor); +} + +NS_CC_END diff --git a/cocos/2d/CCClippingRegionNode.h b/cocos/2d/CCClippingRegionNode.h new file mode 100644 index 0000000000..065ed013a9 --- /dev/null +++ b/cocos/2d/CCClippingRegionNode.h @@ -0,0 +1,77 @@ +/* + * cocos2d for iPhone: http://www.cocos2d-iphone.org + * cocos2d-x: http://www.cocos2d-x.org + * + * Copyright (c) 2012 Pierre-David BĂ©langer + * Copyright (c) 2012 cocos2d-x.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef __MISCNODE_CCCLIPPING_REGION_NODE_H__ +#define __MISCNODE_CCCLIPPING_REGION_NODE_H__ + +#include "2d/CCNode.h" +#include "renderer/CCCustomCommand.h" +#include "platform/CCGL.h" + +NS_CC_BEGIN + +class CC_DLL ClippingRegionNode : public Node +{ +public: + static ClippingRegionNode* create(const Rect& clippingRegion); + static ClippingRegionNode* create(void); + + const Rect getClippingRegion(void) { + return _clippingRegion; + } + void setClippingRegion(const Rect& clippingRegion); + + bool isClippingEnabled(void) { + return _clippingEnabled; + } + void setClippingEnabled(bool enabled) { + _clippingEnabled = enabled; + } + + //virtual void draw(Renderer* renderer, const Mat4 &transform, uint32_t flags) override; + virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override; + +protected: + ClippingRegionNode(void) + : _clippingEnabled(true) + { + } + + void onBeforeVisitScissor(); + void onAfterVisitScissor(); + + Rect _clippingRegion; + bool _clippingEnabled; + + CustomCommand _beforeVisitCmdScissor; + CustomCommand _afterVisitCmdScissor; +}; + +NS_CC_END + +#endif + diff --git a/cocos/cocos2d.h b/cocos/cocos2d.h index e322b14a0d..c743044558 100644 --- a/cocos/cocos2d.h +++ b/cocos/cocos2d.h @@ -125,6 +125,7 @@ THE SOFTWARE. #include "2d/CCMenu.h" #include "2d/CCMenuItem.h" #include "2d/CCClippingNode.h" +#include "2d/CCClippingRegionNode.h" #include "2d/CCMotionStreak.h" #include "2d/CCProgressTimer.h" #include "2d/CCRenderTexture.h" diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index 8583530c0e..75994ba2d6 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -26,7 +26,7 @@ headers = %(cocosdir)s/cocos/cocos2d.h %(cocosdir)s/cocos/2d/CCProtectedNode.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* .*TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode MotionStreak ^Ref$ UserDefault GLViewImpl GLView Image Event(?!.*(Physics).*).* Component ProtectedNode Console GLProgramCache GLProgramState Camera Device +classes = New.* Sprite.* Scene Node.* Director Layer.* Menu.* Touch .*Action.* Move.* Rotate.* Blink.* Tint.* Sequence Repeat.* Fade.* Ease.* Scale.* Transition.* Spawn Animat.* Flip.* Delay.* Skew.* Jump.* Place.* Show.* Progress.* PointArray ToggleVisibility.* RemoveSelf Hide Particle.* Label.* Atlas.* TextureCache.* Texture2D Cardinal.* CatmullRom.* ParallaxNode TileMap.* .*TMX.* CallFunc RenderTexture GridAction Grid3DAction GridBase$ .+Grid Shaky3D Waves3D FlipX3D FlipY3D Speed ActionManager Set Scheduler Timer Orbit.* Follow.* Bezier.* CardinalSpline.* Camera.* DrawNode .*3D$ Liquid$ Waves$ ShuffleTiles$ TurnOffTiles$ Split.* Twirl$ FileUtils$ GLProgram ShaderCache Application ClippingNode ClippingRegionNode MotionStreak ^Ref$ UserDefault GLViewImpl GLView Image Event(?!.*(Physics).*).* Component ProtectedNode Console GLProgramCache GLProgramState Camera Device # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also