class LayerTest1 : public LayerTest
{
public:
CREATE_FUNC(LayerTest1);
virtual void onEnter() override;
virtual std::string subtitle() const override;
void updateSize(Vec2 &touchLocation);
void onTouchesBegan(const std::vector<Touch*>& touches, Event *event);
void onTouchesMoved(const std::vector<Touch*>& touches, Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches, Event *event);
};
void LayerTest1::updateSize(Vec2 &touchLocation)
{
auto s = Director::getInstance()->getWinSize();
auto newSize = Size( fabs(touchLocation.x - s.width/2)*2, fabs(touchLocation.y - s.height/2)*2);
auto l = (LayerColor*) getChildByTag(kTagLayer);
l->setContentSize( newSize );
}
void LayerTest1::onEnter()
{
LayerTest::onEnter();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(LayerTest1::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(LayerTest1::onTouchesMoved, this);
listener->onTouchesEnded = CC_CALLBACK_2(LayerTest1::onTouchesEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto s = Director::getInstance()->getWinSize();
auto layer = LayerColor::create( Color4B(0xFF, 0x00, 0x00, 0x80), 200, 200);
layer->ignoreAnchorPointForPosition(false);
layer->setPosition( Vec2(s.width/2, s.height/2) );
addChild(layer, 1, kTagLayer);
}
void LayerTest1::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
onTouchesMoved(touches, event);
}
void LayerTest1::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
auto touchLocation = touches[0]->getLocation();
updateSize(touchLocation);
}
void LayerTest1::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
{
onTouchesMoved(touches, event);
}
std::string LayerTest1::subtitle() const
{
return "ColorLayer resize (tap & move)";
}