Skip to content

Instantly share code, notes, and snippets.

@HJfod
Created March 19, 2023 10:41
Show Gist options
  • Save HJfod/ba8e6b637874eaee1305f5d079e61a3a to your computer and use it in GitHub Desktop.
Save HJfod/ba8e6b637874eaee1305f5d079e61a3a to your computer and use it in GitHub Desktop.
/**
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <cocos2d.h>
using namespace cocos2d;
/**
* Class for dealing with clipping (rendering only parts of nodes). Simply add
* children to this node, and use the content size to adjust the area being clipped.
*/
class Clip : public CCClippingNode {
protected:
CCLayerColor* m_stencil;
bool init(bool invert) {
m_stencil = CCLayerColor::create({ 255, 255, 255, 255 });
if (!CCClippingNode::init(m_stencil))
return false;
this->setInverted(invert);
this->updateClippingRect();
return true;
}
void updateClippingRect() {
m_stencil->setContentSize(m_obContentSize);
}
public:
/**
* Create a Clip. All ares of children that exceed the clip's content size
* are not rendered (clipped out)
* @param invert Invert the clipping behaviour, so only parts of children
* outside the clip's content size are rendered
*/
static Clip* create(bool invert = false) {
auto ret = new Clip();
if (ret && ret->init(invert)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
void setContentSize(CCSize const& size) override {
CCClippingNode::setContentSize(size);
this->updateClippingRect();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment