当前位置:网站首页>[cocos2d-x] erasable layer:erasablelayer
[cocos2d-x] erasable layer:erasablelayer
2022-06-23 06:00:00 【linchaolong】
explain
Source code
#ifndef __ERASABLELAYER_H__
#define __ERASABLELAYER_H__
#include "cocos2d.h"
USING_NS_CC;
// http://blog.csdn.net/linchaolong
// Erasable Layer
class ErasableLayer :public Layer
{
public:
// establish ErasableLayer
//1. mask
//2. Eraser ( It can be Sprite Or the path of the texture )
static ErasableLayer* create(const char* layerPath, const char* erasaPath);
static ErasableLayer* create(Node *layer, Sprite* erasa);
static ErasableLayer* create(Node *layer, const char* erasaPath);
static ErasableLayer* create(const char* layerPath, Sprite* erasa);
// Restore
void clear();
// Erasable
void setErasable(bool);
bool onTouchBegan(Touch* touch, Event *event);
void onTouchesMoved(Touch* touch, Event *event);
void onTouchEnded(Touch* touch, Event *event);
protected:
ErasableLayer();
~ErasableLayer();
bool init(Node *layer, Sprite* erasa);
void tick(float);
private:
Node* layer_;
Sprite* erasa_;
bool isErasable_;
RenderTexture* rt_;
};
#endif
#include "ErasableLayer.h"
ErasableLayer::ErasableLayer() :isErasable_(true)
{
}
ErasableLayer::~ErasableLayer()
{
CC_SAFE_RELEASE(layer_);
CC_SAFE_RELEASE(erasa_);
}
ErasableLayer* ErasableLayer::create(const char* layerPath, const char* erasaPath){
CCSprite* layer = CCSprite::create(layerPath);
CCSprite* erasa = CCSprite::create(erasaPath);
ErasableLayer* ret = new ErasableLayer;
if (ret->init(layer, erasa))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
ErasableLayer* ErasableLayer::create(Node* layer, Sprite* erasa){
ErasableLayer* ret = new ErasableLayer;
if (ret->init(layer, erasa))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
ErasableLayer* ErasableLayer::create(Node* layer, const char* erasaPath){
CCSprite* erasa = CCSprite::create(erasaPath);
ErasableLayer* ret = new ErasableLayer;
if (ret->init(layer, erasa))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
ErasableLayer* ErasableLayer::create(const char* layerPath, Sprite* erasa){
CCSprite* layer = CCSprite::create(layerPath);
ErasableLayer* ret = new ErasableLayer;
if (ret->init(layer, erasa))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
bool ErasableLayer::init(Node *layer, Sprite* erasa){
if (!Layer::init())
{
return false;
}
layer_ = layer;
erasa_ = erasa;
layer_->retain();
erasa_->retain();
this->setContentSize(layer_->getContentSize());
// Set the color blending mode
BlendFunc erasaBf = { GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }; // Source factor : The value is 0, The color of the eraser is transparent ; Objective factors : Target color transparency minus source color transparency
erasa_->setBlendFunc(erasaBf);
auto size = layer_->getContentSize();
rt_ = RenderTexture::create(size.width, size.height);
rt_->setAnchorPoint(Vec2(0, 0));
rt_->setPosition(Vec2(size.width/2, size.height/2));
this->addChild(rt_);
clear();
schedule(CC_SCHEDULE_SELECTOR(ErasableLayer::tick));
// Touch event
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(ErasableLayer::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(ErasableLayer::onTouchesMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(ErasableLayer::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
listener->setSwallowTouches(false);
return true;
}
void ErasableLayer::tick(float){
if (!isErasable_)
{
return;
}
// Set source color alpha The value is the maximum , Target color alpha Value minus the source color alpha After the value, it will be 0 了 , The mixed effect becomes transparent , In this way, the eraser effect is realized .
erasa_->setOpacity(255);
// to update RenderTexture
rt_->begin();
// draw
erasa_->visit();
rt_->end();
}
void ErasableLayer::clear(){
layer_->setAnchorPoint(Vec2(0, 0));
layer_->setPosition(Vec2(0, 0));
rt_->begin();
layer_->visit();
rt_->end();
}
void ErasableLayer::setErasable(bool flag){
isErasable_ = flag;
}
bool ErasableLayer::onTouchBegan(Touch* touch, Event *event)
{
erasa_->setPosition(this->convertToNodeSpace(touch->getLocation()));
return true;
}
void ErasableLayer::onTouchesMoved(Touch* touch, Event *event){
erasa_->setPosition(this->convertToNodeSpace(touch->getLocation()));
}
void ErasableLayer::onTouchEnded(Touch* touch, Event *event)
{
erasa_->setPosition(this->convertToNodeSpace(touch->getLocation()));
}
// Invoke the sample
// mask
auto maskLayer = LayerColor::create(Color4B(0,0,255,200));
maskLayer->setContentSize(sprite->getContentSize());
// Create erasable Layer
auto layer = ErasableLayer::create(maskLayer, "eraser.png");
layer->setPosition(Vec2(visibleSize.width / 4, visibleSize.height / 4));
this->addChild(layer);

边栏推荐
- PAT 乙等 1021 个位数统计
- What is the magic of digital collections? Which reliable teams are currently developing
- node中操作mongoDB
- PAT 乙等 1014 C语言
- runc 符号链接挂载与容器逃逸漏洞预警(CVE-2021-30465)
- Kotlin android简单Activity跳转、handler和thread简单配合使用
- Ansible 使用普通用户管理被控端
- 数字藏品火热背后需要强大的技术团队支持 北方技术团队
- matplotlib savefig多个图片叠加问题
- Raspberry pie assert preliminary exercise
猜你喜欢

True question of MySQL interview (29) -- case - finding favorite movies

三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会

How to specify the output path of pig register Project Log

HierarchyViewer工具找不到 HierarchyViewer位置

The construction of digital factory can be divided into three aspects

MDM data cleaning function development description

Prometheus, incluxdb2.2 installation and flume_ Export download compile use

jvm-03.jvm内存模型

jvm-06.垃圾回收器

最优传输理论下对抗攻击可解释性
随机推荐
PAT 乙等 1012 C语言
Raspberry pie assert preliminary exercise
Opportunities and challenges of digital collections from the perspective of technology development team
Implementation of linear list linked list structure
ORB_ Slam2 operation
Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
jvm-01.指令重排
技能自检 | 想当测试Leader,这6项技能你会吗?
True MySQL interview question (21) - Finance - overdue loan
Wireshark TS | video app cannot play
PAT 乙等 1016 C语言
ant使用总结(三):批量打包apk
How to specify the output path of pig register Project Log
Use of visdom
App SHA1 acquisition program Baidu map Gaode map simple program for acquiring SHA1 value
Prometheus, incluxdb2.2 installation and flume_ Export download compile use
jvm-06.垃圾回收器
The 510000 prize pool invites you to participate in the competition -- the second Alibaba cloud ECS cloudbuild developer competition is coming
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
ORB_SLAM2运行