// Drawings.cpp
#include "Drawings.h"
Drawings::Drawings(){
drawActive=false;
glLineWidth(3.0 * CC_CONTENT_SCALE_FACTOR());
}
Drawings* Drawings::create(){
auto node = new Drawings();
if (node) {
node->autorelease();
return node;
}
CC_SAFE_DELETE(node);
return NULL;
};
void Drawings:: draw(){
if(drawActive){
DrawPrimitives::setDrawColor4B(255, 0, 0, 255);
DrawPrimitives::drawLine(startPoint, endPoint);
DrawPrimitives::drawCircle(startPoint, 100, CC_DEGREES_TO_RADIANS(360), 10, false);
}
}
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
drawClip=Drawings::create();
//drawClip->drawActive=true;
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
log("scale%f",Director::getInstance()->getContentScaleFactor());
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 1);
this->addChild(drawClip,100);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = LabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
//Vector<Sprite*> spriteGroup;
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
//eventlistener
auto listener=EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
//lambda function
listener->onTouchBegan=[&](Touch* touch,Event* event){
log("touch received");
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point touchLocation=target->convertToNodeSpace(touch->getLocation());
Size s=target->getContentSize();
Rect rect=Rect(0,0,s.width,s.height);
//check the click area
if(rect.containsPoint(touchLocation)){
target->setOpacity(180);
return true;
}else{
return false;
}
};
listener->onTouchMoved=[&](Touch* touch,Event* event){
auto target=static_cast<Sprite*>(event->getCurrentTarget());
auto startPoint=touch->getLocationInView();
auto endPoint=touch->getPreviousLocationInView();
// converting touch cordinate to gl equivalent;
startPoint = Director::getInstance()->convertToGL(startPoint);
endPoint = Director::getInstance()->convertToGL(endPoint);
drawClip->endPoint=endPoint;
drawClip->startPoint=startPoint;
drawClip->drawActive=true;
target->setPosition(target->getPosition()+touch->getDelta());
};
listener->onTouchEnded=[&](Touch*touch,Event* event){
auto target=static_cast<Sprite*>(event->getCurrentTarget());
drawClip->drawActive=false;
Rect tg=target->getBoundingBox();
for(int i=0;i<3;++i){
auto tempSprite=spriteGroup.at(i);
Rect temp=tempSprite->getBoundingBox();
if(tg.intersectsRect(temp)&&tempSprite->getTag()!=target->getTag()){
log("hit sucess%d",tempSprite->getTag());
}
}
log("touchEnded");
};
//multiple sprites in loop;
bool once=true;
for(int i=0;i<3;++i){
auto sprite2=Sprite::create("circle.png");
sprite2->setTag(i);
if(once){
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite2);
once=false;
}else{
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener->clone(), sprite2);
}
(once)?log("hello"):log("nohello");
sprite2->setPosition((i+1)*100,200);
spriteGroup.pushBack(sprite2);
this->addChild(sprite2,2);
}
// opacity takes glubyte ie. 0-200
spriteGroup.at(0)->setOpacity(100);
// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
return true;
}
void HelloWorld::menuCloseCallback(Object* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}