Merge pull request #8706 from SunLightJuly/add_ClippingRectangleNode

Add clipping rectangle node
This commit is contained in:
minggo 2014-10-15 15:09:29 +08:00
commit 16ad1977b0
11 changed files with 223 additions and 0 deletions

View File

@ -1913,6 +1913,10 @@
DA8C62A319E52C6400000516 /* ioapi_mem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DA8C62A019E52C6400000516 /* ioapi_mem.cpp */; };
DA8C62A419E52C6400000516 /* ioapi_mem.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8C62A119E52C6400000516 /* ioapi_mem.h */; };
DA8C62A519E52C6400000516 /* ioapi_mem.h in Headers */ = {isa = PBXBuildFile; fileRef = DA8C62A119E52C6400000516 /* ioapi_mem.h */; };
DABC9FA919E7DFA900FA252C /* CCClippingRectangleNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DABC9FA719E7DFA900FA252C /* CCClippingRectangleNode.cpp */; };
DABC9FAA19E7DFA900FA252C /* CCClippingRectangleNode.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DABC9FA719E7DFA900FA252C /* CCClippingRectangleNode.cpp */; };
DABC9FAB19E7DFA900FA252C /* CCClippingRectangleNode.h in Headers */ = {isa = PBXBuildFile; fileRef = DABC9FA819E7DFA900FA252C /* CCClippingRectangleNode.h */; };
DABC9FAC19E7DFA900FA252C /* CCClippingRectangleNode.h in Headers */ = {isa = PBXBuildFile; fileRef = DABC9FA819E7DFA900FA252C /* CCClippingRectangleNode.h */; };
ED9C6A9418599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
ED9C6A9518599AD8000A5232 /* CCNodeGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */; };
ED9C6A9618599AD8000A5232 /* CCNodeGrid.h in Headers */ = {isa = PBXBuildFile; fileRef = ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */; };
@ -2952,6 +2956,8 @@
B67C624519D4186F00F11FC6 /* ccShader_3D_PositionNormalTex.vert */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = ccShader_3D_PositionNormalTex.vert; sourceTree = "<group>"; };
DA8C62A019E52C6400000516 /* ioapi_mem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ioapi_mem.cpp; sourceTree = "<group>"; };
DA8C62A119E52C6400000516 /* ioapi_mem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ioapi_mem.h; sourceTree = "<group>"; };
DABC9FA719E7DFA900FA252C /* CCClippingRectangleNode.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CCClippingRectangleNode.cpp; sourceTree = "<group>"; };
DABC9FA819E7DFA900FA252C /* CCClippingRectangleNode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCClippingRectangleNode.h; sourceTree = "<group>"; };
ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = CCNodeGrid.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCNodeGrid.h; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -3356,6 +3362,8 @@
1A5701FF180BCBBD0088DEC7 /* misc-nodes */ = {
isa = PBXGroup;
children = (
DABC9FA719E7DFA900FA252C /* CCClippingRectangleNode.cpp */,
DABC9FA819E7DFA900FA252C /* CCClippingRectangleNode.h */,
ED9C6A9218599AD8000A5232 /* CCNodeGrid.cpp */,
ED9C6A9318599AD8000A5232 /* CCNodeGrid.h */,
1A57020C180BCBF40088DEC7 /* CCProgressTimer.cpp */,
@ -5089,6 +5097,7 @@
15AE18A319AAD33D00C27E9E /* CCParticleSystemQuadLoader.h in Headers */,
46A170F01807CECA005B8026 /* CCPhysicsWorld.h in Headers */,
15AE199D19AAD39600C27E9E /* ScrollViewReader.h in Headers */,
DABC9FAB19E7DFA900FA252C /* CCClippingRectangleNode.h in Headers */,
50ABBEC11925AB6F00A911A9 /* CCValue.h in Headers */,
15AE19EB19AAD3A700C27E9E /* SlotData.h in Headers */,
B276EF631988D1D500CD400F /* CCVertexIndexBuffer.h in Headers */,
@ -5625,6 +5634,7 @@
1A57006C180BC5A10088DEC7 /* CCActionCatmullRom.h in Headers */,
15AE195C19AAD35100C27E9E /* CCSGUIReader.h in Headers */,
5034CA3A191D591100CE6051 /* ccShader_PositionColorLengthTexture.frag in Headers */,
DABC9FAC19E7DFA900FA252C /* CCClippingRectangleNode.h in Headers */,
50ABBEC41925AB6F00A911A9 /* CCVector.h in Headers */,
50ABBE501925AB6F00A911A9 /* CCEventCustom.h in Headers */,
15AE1AD719AAD40300C27E9E /* b2WheelJoint.h in Headers */,
@ -6501,6 +6511,7 @@
15AE190B19AAD35000C27E9E /* CCDisplayFactory.cpp in Sources */,
1A9DCA27180E6955007A3AD4 /* CCGLBufferedNode.cpp in Sources */,
50ABBD541925AB0000A911A9 /* TransformUtils.cpp in Sources */,
DABC9FA919E7DFA900FA252C /* CCClippingRectangleNode.cpp in Sources */,
3E2F27A619CFBFE100E7C490 /* AudioEngine.cpp in Sources */,
15AE1A3619AAD3D500C27E9E /* b2PolygonShape.cpp in Sources */,
50ABBD9B1925AB4100A911A9 /* ccGLStateCache.cpp in Sources */,
@ -6637,6 +6648,7 @@
1A570072180BC5A10088DEC7 /* CCActionGrid.cpp in Sources */,
15AE1ABC19AAD40300C27E9E /* b2PolygonAndCircleContact.cpp in Sources */,
15AE1ADA19AAD41000C27E9E /* b2Rope.cpp in Sources */,
DABC9FAA19E7DFA900FA252C /* CCClippingRectangleNode.cpp in Sources */,
15AE1BF119AAE01E00C27E9E /* CCControlPotentiometer.cpp in Sources */,
15AE1B8419AADA9A00C27E9E /* UITextField.cpp in Sources */,
15AE198E19AAD36E00C27E9E /* CheckBoxReader.cpp in Sources */,

View File

@ -0,0 +1,84 @@
#include "CCClippingRectangleNode.h"
#include "base/CCDirector.h"
#include "renderer/CCRenderer.h"
#include "math/Vec2.h"
#include "CCGLView.h"
NS_CC_BEGIN
ClippingRectangleNode* ClippingRectangleNode::create(const Rect& clippingRegion)
{
ClippingRectangleNode* node = new ClippingRectangleNode();
if (node && node->init()) {
node->setClippingRegion(clippingRegion);
node->autorelease();
} else {
CC_SAFE_DELETE(node);
}
return node;
}
ClippingRectangleNode* ClippingRectangleNode::create()
{
ClippingRectangleNode* node = new ClippingRectangleNode();
if (node && node->init()) {
node->autorelease();
} else {
CC_SAFE_DELETE(node);
}
return node;
}
void ClippingRectangleNode::setClippingRegion(const Rect &clippingRegion)
{
_clippingRegion = clippingRegion;
}
void ClippingRectangleNode::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 ClippingRectangleNode::onAfterVisitScissor()
{
if (_clippingEnabled)
{
glDisable(GL_SCISSOR_TEST);
}
}
void ClippingRectangleNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
_beforeVisitCmdScissor.init(_globalZOrder);
_beforeVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onBeforeVisitScissor, this);
renderer->addCommand(&_beforeVisitCmdScissor);
Node::visit(renderer, parentTransform, parentFlags);
_afterVisitCmdScissor.init(_globalZOrder);
_afterVisitCmdScissor.func = CC_CALLBACK_0(ClippingRectangleNode::onAfterVisitScissor, this);
renderer->addCommand(&_afterVisitCmdScissor);
}
NS_CC_END

View File

@ -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_RECTANGLE_NODE_H__
#define __MISCNODE_CCCLIPPING_RECTANGLE_NODE_H__
#include "2d/CCNode.h"
#include "renderer/CCCustomCommand.h"
#include "platform/CCGL.h"
NS_CC_BEGIN
class CC_DLL ClippingRectangleNode : public Node
{
public:
static ClippingRectangleNode* create(const Rect& clippingRegion);
static ClippingRectangleNode* create();
const Rect& getClippingRegion() const {
return _clippingRegion;
}
void setClippingRegion(const Rect& clippingRegion);
const bool isClippingEnabled() const {
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:
ClippingRectangleNode()
: _clippingEnabled(true)
{
}
void onBeforeVisitScissor();
void onAfterVisitScissor();
Rect _clippingRegion;
bool _clippingEnabled;
CustomCommand _beforeVisitCmdScissor;
CustomCommand _afterVisitCmdScissor;
};
NS_CC_END
#endif

View File

@ -21,6 +21,7 @@ set(COCOS_2D_SRC
2d/CCAnimation.cpp
2d/CCAtlasNode.cpp
2d/CCClippingNode.cpp
2d/CCClippingRectangleNode.cpp
2d/CCComponentContainer.cpp
2d/CCComponent.cpp
2d/CCDrawingPrimitives.cpp

View File

@ -477,6 +477,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClCompile Include="CCAnimationCache.cpp" />
<ClCompile Include="CCAtlasNode.cpp" />
<ClCompile Include="CCClippingNode.cpp" />
<ClCompile Include="CCClippingRectangleNode.cpp" />
<ClCompile Include="CCComponent.cpp" />
<ClCompile Include="CCComponentContainer.cpp" />
<ClCompile Include="CCDrawingPrimitives.cpp" />
@ -896,6 +897,7 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClInclude Include="CCAnimationCache.h" />
<ClInclude Include="CCAtlasNode.h" />
<ClInclude Include="CCClippingNode.h" />
<ClInclude Include="CCClippingRectangleNode.h" />
<ClInclude Include="CCComponent.h" />
<ClInclude Include="CCComponentContainer.h" />
<ClInclude Include="CCDrawingPrimitives.h" />

View File

@ -334,6 +334,9 @@
<ClCompile Include="CCClippingNode.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="CCClippingRectangleNode.cpp">
<Filter>2d</Filter>
</ClCompile>
<ClCompile Include="CCComponent.cpp">
<Filter>2d</Filter>
</ClCompile>
@ -1373,6 +1376,9 @@
<ClInclude Include="CCClippingNode.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="CCClippingRectangleNode.h">
<Filter>2d</Filter>
</ClInclude>
<ClInclude Include="CCComponent.h">
<Filter>2d</Filter>
</ClInclude>

View File

@ -25,6 +25,7 @@ cocos2d.cpp \
2d/CCAnimationCache.cpp \
2d/CCAtlasNode.cpp \
2d/CCClippingNode.cpp \
2d/CCClippingRectangleNode.cpp \
2d/CCComponent.cpp \
2d/CCComponentContainer.cpp \
2d/CCDrawNode.cpp \

View File

@ -126,6 +126,7 @@ THE SOFTWARE.
#include "2d/CCMenu.h"
#include "2d/CCMenuItem.h"
#include "2d/CCClippingNode.h"
#include "2d/CCClippingRectangleNode.h"
#include "2d/CCMotionStreak.h"
#include "2d/CCProgressTimer.h"
#include "2d/CCRenderTexture.h"

View File

@ -90,6 +90,8 @@
"cocos/2d/CCAtlasNode.h",
"cocos/2d/CCClippingNode.cpp",
"cocos/2d/CCClippingNode.h",
"cocos/2d/CCClippingRectangleNode.cpp",
"cocos/2d/CCClippingRectangleNode.h",
"cocos/2d/CCComponent.cpp",
"cocos/2d/CCComponent.h",
"cocos/2d/CCComponentContainer.cpp",

View File

@ -33,6 +33,7 @@ static std::function<Layer*()> createFunctions[] = {
CL(RawStencilBufferTest5),
CL(RawStencilBufferTest6),
CL(ClippingToRenderTextureTest),
CL(ClippingRectangleNodeTest),
};
static int sceneIdx=-1;
@ -1097,6 +1098,32 @@ void ClippingToRenderTextureTest::reproduceBug()
rt->end();
}
// ClippingRectangleNodeDemo
std::string ClippingRectangleNodeTest::title() const
{
return "ClippingRectangleNode Test";
}
std::string ClippingRectangleNodeTest::subtitle() const
{
return "more effectively";
}
void ClippingRectangleNodeTest::setup()
{
auto clipper = ClippingRectangleNode::create();
clipper->setClippingRegion(Rect(this->getContentSize().width / 2 - 100, this->getContentSize().height / 2 - 100, 200, 200));
clipper->setTag( kTagClipperNode );
this->addChild(clipper);
auto content = Sprite::create(s_back2);
content->setTag( kTagContentNode );
content->setAnchorPoint( Vec2(0.5, 0.5) );
content->setPosition(this->getContentSize().width / 2, this->getContentSize().height / 2);
clipper->addChild(content);
}
// main entry point

View File

@ -244,6 +244,16 @@ public:
virtual std::string subtitle() const override;
};
class ClippingRectangleNodeTest : public BaseClippingNodeTest
{
public:
CREATE_FUNC(ClippingRectangleNodeTest);
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual void setup();
};
class ClippingNodeTestScene : public TestScene
{