2013-09-13 16:38:21 +08:00
|
|
|
//
|
|
|
|
// NewEventDispatcherTest.cpp
|
|
|
|
// samples
|
|
|
|
//
|
|
|
|
// Created by James Chen on 9/13/13.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "NewEventDispatcherTest.h"
|
2013-09-18 22:23:02 +08:00
|
|
|
#include "testResource.h"
|
2013-09-13 16:38:21 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::function<Layer*()> createFunctions[] =
|
|
|
|
{
|
|
|
|
CL(TouchableSpriteTest),
|
2013-09-18 22:23:02 +08:00
|
|
|
CL(FixedPriorityTest),
|
|
|
|
CL(RemoveListenerWhenDispatching),
|
|
|
|
CL(CustomEventTest),
|
|
|
|
CL(LabelKeyboardEventTest),
|
2013-10-23 16:14:03 +08:00
|
|
|
CL(SpriteAccelerationEventTest),
|
2013-11-02 21:47:00 +08:00
|
|
|
CL(RemoveAndRetainNodeTest),
|
2013-12-21 16:56:28 +08:00
|
|
|
CL(RemoveListenerAfterAddingTest),
|
|
|
|
CL(DirectorEventTest),
|
2013-09-13 16:38:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int TEST_CASE_COUNT = sizeof(createFunctions) / sizeof(createFunctions[0]);
|
|
|
|
|
|
|
|
int sceneIdx=-1;
|
|
|
|
Layer* createTest(int index)
|
|
|
|
{
|
2013-11-14 09:36:33 +08:00
|
|
|
auto layer = (createFunctions[index])();;
|
2013-09-13 16:38:21 +08:00
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* nextAction();
|
|
|
|
Layer* backAction();
|
|
|
|
Layer* restartAction();
|
|
|
|
|
|
|
|
Layer* nextAction()
|
|
|
|
{
|
|
|
|
sceneIdx++;
|
|
|
|
sceneIdx = sceneIdx % TEST_CASE_COUNT;
|
|
|
|
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* backAction()
|
|
|
|
{
|
|
|
|
sceneIdx--;
|
|
|
|
if( sceneIdx < 0 )
|
|
|
|
sceneIdx = TEST_CASE_COUNT -1;
|
|
|
|
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layer* restartAction()
|
|
|
|
{
|
|
|
|
return createTest(sceneIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcherTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
auto layer = nextAction();
|
|
|
|
addChild(layer);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EventDispatcherTestDemo::onEnter()
|
|
|
|
{
|
|
|
|
BaseTest::onEnter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcherTestDemo::backCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto scene = new EventDispatcherTestScene();
|
|
|
|
auto layer = backAction();
|
|
|
|
|
|
|
|
scene->addChild(layer);
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
scene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcherTestDemo::nextCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto scene = new EventDispatcherTestScene();
|
|
|
|
auto layer = nextAction();
|
|
|
|
|
|
|
|
scene->addChild(layer);
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
scene->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventDispatcherTestDemo::restartCallback(Object* sender)
|
|
|
|
{
|
|
|
|
auto scene = new EventDispatcherTestScene();
|
|
|
|
auto layer = restartAction();
|
|
|
|
|
|
|
|
scene->addChild(layer);
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
scene->release();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string EventDispatcherTestDemo::title() const
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string EventDispatcherTestDemo::subtitle() const
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TouchableSpriteTest
|
|
|
|
void TouchableSpriteTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto sprite1 = Sprite::create("Images/CyanSquare.png");
|
|
|
|
sprite1->setPosition(origin+Point(size.width/2, size.height/2) + Point(-80, 80));
|
2013-09-13 18:01:32 +08:00
|
|
|
addChild(sprite1, 10);
|
2013-09-13 16:38:21 +08:00
|
|
|
|
|
|
|
auto sprite2 = Sprite::create("Images/MagentaSquare.png");
|
|
|
|
sprite2->setPosition(origin+Point(size.width/2, size.height/2));
|
2013-09-13 18:01:32 +08:00
|
|
|
addChild(sprite2, 20);
|
|
|
|
|
|
|
|
auto sprite3 = Sprite::create("Images/YellowSquare.png");
|
|
|
|
sprite3->setPosition(Point(0, 0));
|
|
|
|
sprite2->addChild(sprite3, 1);
|
2013-09-13 16:38:21 +08:00
|
|
|
|
|
|
|
// Make sprite1 touchable
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
2013-09-13 16:38:21 +08:00
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener1->onTouchBegan = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
2013-09-18 22:23:02 +08:00
|
|
|
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
|
2013-09-13 16:38:21 +08:00
|
|
|
target->setOpacity(180);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchMoved = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
target->setPosition(target->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
2013-09-13 18:51:00 +08:00
|
|
|
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
2013-09-13 16:38:21 +08:00
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
2013-09-18 22:23:02 +08:00
|
|
|
log("sprite onTouchesEnded.. ");
|
2013-09-13 16:38:21 +08:00
|
|
|
target->setOpacity(255);
|
2013-09-13 18:51:00 +08:00
|
|
|
if (target == sprite2)
|
|
|
|
{
|
|
|
|
sprite1->setZOrder(100);
|
|
|
|
}
|
|
|
|
else if(target == sprite1)
|
|
|
|
{
|
|
|
|
sprite1->setZOrder(0);
|
|
|
|
}
|
2013-09-13 16:38:21 +08:00
|
|
|
};
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
|
2013-09-20 14:14:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners", [this](Object* sender){
|
|
|
|
auto senderItem = static_cast<MenuItemFont*>(sender);
|
|
|
|
senderItem->setString("Only Next item could be clicked");
|
|
|
|
|
2013-10-29 14:59:05 +08:00
|
|
|
_eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);
|
2013-09-20 14:14:03 +08:00
|
|
|
|
|
|
|
auto nextItem = MenuItemFont::create("Next", [=](Object* sender){
|
|
|
|
nextCallback(nullptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
nextItem->setFontSizeObj(16);
|
|
|
|
nextItem->setPosition(VisibleRect::right() + Point(-100, -30));
|
|
|
|
|
|
|
|
auto menu2 = Menu::create(nextItem, NULL);
|
|
|
|
menu2->setPosition(Point(0, 0));
|
|
|
|
menu2->setAnchorPoint(Point(0, 0));
|
|
|
|
this->addChild(menu2);
|
|
|
|
});
|
|
|
|
|
|
|
|
removeAllTouchItem->setFontSizeObj(16);
|
|
|
|
removeAllTouchItem->setPosition(VisibleRect::right() + Point(-100, 0));
|
|
|
|
|
|
|
|
auto menu = Menu::create(removeAllTouchItem, nullptr);
|
|
|
|
menu->setPosition(Point(0, 0));
|
|
|
|
menu->setAnchorPoint(Point(0, 0));
|
|
|
|
addChild(menu);
|
2013-09-13 16:38:21 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TouchableSpriteTest::title() const
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
|
|
|
return "Touchable Sprite Test";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TouchableSpriteTest::subtitle() const
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
2013-09-18 22:23:02 +08:00
|
|
|
return "Please drag the blocks";
|
2013-09-13 16:38:21 +08:00
|
|
|
}
|
2013-09-16 15:43:20 +08:00
|
|
|
|
|
|
|
// FixedPriorityChangedTest
|
|
|
|
|
|
|
|
class TouchableSpriteWithFixedPriority : public Sprite
|
|
|
|
{
|
|
|
|
public:
|
2013-11-14 07:55:36 +08:00
|
|
|
|
|
|
|
CREATE_FUNC(TouchableSpriteWithFixedPriority);
|
|
|
|
|
2013-09-16 15:43:20 +08:00
|
|
|
TouchableSpriteWithFixedPriority()
|
|
|
|
: _listener(nullptr)
|
|
|
|
, _fixedPriority(0)
|
|
|
|
, _useNodePriority(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void setPriority(int fixedPriority) { _fixedPriority = fixedPriority; _useNodePriority = false; };
|
|
|
|
void setPriorityWithThis(bool useNodePriority) { _useNodePriority = useNodePriority; _fixedPriority = true; }
|
|
|
|
|
|
|
|
void onEnter() override
|
|
|
|
{
|
|
|
|
Sprite::onEnter();
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-16 15:43:20 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
2013-09-18 17:45:25 +08:00
|
|
|
listener->onTouchBegan = [=](Touch* touch, Event* event){
|
2013-09-16 15:43:20 +08:00
|
|
|
|
|
|
|
Point locationInNode = this->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = this->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
this->setColor(Color3B::RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-09-18 17:45:25 +08:00
|
|
|
listener->onTouchMoved = [=](Touch* touch, Event* event){
|
2013-09-16 15:43:20 +08:00
|
|
|
//this->setPosition(this->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
2013-09-18 17:45:25 +08:00
|
|
|
listener->onTouchEnded = [=](Touch* touch, Event* event){
|
2013-09-16 15:43:20 +08:00
|
|
|
this->setColor(Color3B::WHITE);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_useNodePriority)
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-09-16 15:43:20 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority);
|
2013-09-16 15:43:20 +08:00
|
|
|
}
|
|
|
|
_listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onExit() override
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_listener);
|
2013-09-16 15:43:20 +08:00
|
|
|
|
|
|
|
Sprite::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
EventListener* _listener;
|
|
|
|
int _fixedPriority;
|
|
|
|
bool _useNodePriority;
|
|
|
|
};
|
|
|
|
|
2013-09-16 16:02:48 +08:00
|
|
|
void FixedPriorityTest::onEnter()
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2013-11-14 07:55:36 +08:00
|
|
|
auto sprite1 = TouchableSpriteWithFixedPriority::create();
|
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2013-09-16 15:43:20 +08:00
|
|
|
sprite1->setPriority(30);
|
|
|
|
sprite1->setPosition(origin+Point(size.width/2, size.height/2) + Point(-80, 40));
|
|
|
|
addChild(sprite1, 10);
|
|
|
|
|
2013-11-14 07:55:36 +08:00
|
|
|
auto sprite2 = TouchableSpriteWithFixedPriority::create();
|
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
2013-09-16 15:43:20 +08:00
|
|
|
sprite2->setPriority(20);
|
|
|
|
sprite2->setPosition(origin+Point(size.width/2, size.height/2));
|
|
|
|
addChild(sprite2, 20);
|
|
|
|
|
2013-11-14 07:55:36 +08:00
|
|
|
auto sprite3 = TouchableSpriteWithFixedPriority::create();
|
|
|
|
sprite3->setTexture("Images/YellowSquare.png");
|
2013-09-16 15:43:20 +08:00
|
|
|
sprite3->setPriority(10);
|
|
|
|
sprite3->setPosition(Point(0, 0));
|
|
|
|
sprite2->addChild(sprite3, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string FixedPriorityTest::title() const
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
2013-09-16 16:02:48 +08:00
|
|
|
return "Fixed priority test";
|
2013-09-16 15:43:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string FixedPriorityTest::subtitle() const
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
|
|
|
return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be.";
|
|
|
|
}
|
2013-09-18 22:23:02 +08:00
|
|
|
|
|
|
|
// RemoveListenerWhenDispatching
|
|
|
|
void RemoveListenerWhenDispatching::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto sprite1 = Sprite::create("Images/CyanSquare.png");
|
|
|
|
sprite1->setPosition(origin+Point(size.width/2, size.height/2));
|
|
|
|
addChild(sprite1, 10);
|
|
|
|
|
|
|
|
// Make sprite1 touchable
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
2013-09-18 22:23:02 +08:00
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
setUserObject(listener1);
|
|
|
|
|
|
|
|
std::shared_ptr<bool> firstClick(new bool(true));
|
|
|
|
|
|
|
|
listener1->onTouchBegan = [=](Touch* touch, Event* event){
|
|
|
|
Point locationInNode = sprite1->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = sprite1->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
sprite1->setColor(Color3B::RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
|
|
|
sprite1->setColor(Color3B::WHITE);
|
|
|
|
};
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
2013-09-18 22:23:02 +08:00
|
|
|
|
|
|
|
auto statusLabel = LabelTTF::create("The sprite could be touched!", "", 20);
|
|
|
|
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
|
|
|
addChild(statusLabel);
|
|
|
|
std::shared_ptr<bool> enable(new bool(true));
|
|
|
|
// Enable/Disable item
|
|
|
|
auto toggleItem = MenuItemToggle::createWithCallback([=](Object* sender){
|
|
|
|
if (*enable)
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(listener1);
|
2013-09-18 22:23:02 +08:00
|
|
|
statusLabel->setString("The sprite could not be touched!");
|
|
|
|
|
|
|
|
(*enable) = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
|
2013-09-18 22:23:02 +08:00
|
|
|
statusLabel->setString("The sprite could be touched!");
|
|
|
|
|
|
|
|
(*enable) = true;
|
|
|
|
}
|
|
|
|
}, MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), NULL);
|
|
|
|
|
|
|
|
toggleItem->setPosition(origin + Point(size.width/2, 80));
|
|
|
|
auto menu = Menu::create(toggleItem, nullptr);
|
|
|
|
menu->setPosition(Point(0, 0));
|
|
|
|
menu->setAnchorPoint(Point(0, 0));
|
|
|
|
addChild(menu, -1);
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveListenerWhenDispatching::title() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Add and remove listener\n when dispatching event";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveListenerWhenDispatching::subtitle() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// CustomEventTest
|
|
|
|
void CustomEventTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2013-10-28 10:49:43 +08:00
|
|
|
MenuItemFont::setFontSize(20);
|
|
|
|
|
|
|
|
auto statusLabel = LabelTTF::create("No custom event 1 received!", "", 20);
|
2013-09-18 22:23:02 +08:00
|
|
|
statusLabel->setPosition(origin + Point(size.width/2, size.height-90));
|
|
|
|
addChild(statusLabel);
|
|
|
|
|
2013-10-28 10:49:43 +08:00
|
|
|
_listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){
|
|
|
|
std::string str("Custom event 1 received, ");
|
2013-09-18 22:23:02 +08:00
|
|
|
char* buf = static_cast<char*>(event->getUserData());
|
|
|
|
str += buf;
|
|
|
|
str += " times";
|
|
|
|
statusLabel->setString(str.c_str());
|
|
|
|
});
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_listener, 1);
|
2013-09-18 22:23:02 +08:00
|
|
|
|
2013-10-28 10:49:43 +08:00
|
|
|
auto sendItem = MenuItemFont::create("Send Custom Event 1", [=](Object* sender){
|
2013-09-18 22:23:02 +08:00
|
|
|
static int count = 0;
|
|
|
|
++count;
|
|
|
|
char* buf = new char[10];
|
|
|
|
sprintf(buf, "%d", count);
|
2013-10-28 10:49:43 +08:00
|
|
|
EventCustom event("game_custom_event1");
|
2013-09-18 22:23:02 +08:00
|
|
|
event.setUserData(buf);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->dispatchEvent(&event);
|
2013-10-28 10:49:43 +08:00
|
|
|
CC_SAFE_DELETE_ARRAY(buf);
|
2013-09-18 22:23:02 +08:00
|
|
|
});
|
|
|
|
sendItem->setPosition(origin + Point(size.width/2, size.height/2));
|
2013-10-28 10:49:43 +08:00
|
|
|
|
|
|
|
auto statusLabel2 = LabelTTF::create("No custom event 2 received!", "", 20);
|
|
|
|
statusLabel2->setPosition(origin + Point(size.width/2, size.height-120));
|
|
|
|
addChild(statusLabel2);
|
|
|
|
|
|
|
|
_listener2 = EventListenerCustom::create("game_custom_event2", [=](EventCustom* event){
|
|
|
|
std::string str("Custom event 2 received, ");
|
|
|
|
char* buf = static_cast<char*>(event->getUserData());
|
|
|
|
str += buf;
|
|
|
|
str += " times";
|
|
|
|
statusLabel2->setString(str.c_str());
|
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_listener2, 1);
|
|
|
|
|
|
|
|
auto sendItem2 = MenuItemFont::create("Send Custom Event 2", [=](Object* sender){
|
|
|
|
static int count = 0;
|
|
|
|
++count;
|
|
|
|
char* buf = new char[10];
|
|
|
|
sprintf(buf, "%d", count);
|
|
|
|
EventCustom event("game_custom_event2");
|
|
|
|
event.setUserData(buf);
|
|
|
|
_eventDispatcher->dispatchEvent(&event);
|
|
|
|
CC_SAFE_DELETE_ARRAY(buf);
|
|
|
|
});
|
|
|
|
sendItem2->setPosition(origin + Point(size.width/2, size.height/2 - 40));
|
|
|
|
|
|
|
|
auto menu = Menu::create(sendItem, sendItem2, nullptr);
|
2013-09-18 22:23:02 +08:00
|
|
|
menu->setPosition(Point(0, 0));
|
|
|
|
menu->setAnchorPoint(Point(0, 0));
|
|
|
|
addChild(menu, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CustomEventTest::onExit()
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_listener);
|
2013-10-28 10:49:43 +08:00
|
|
|
_eventDispatcher->removeEventListener(_listener2);
|
2013-09-18 22:23:02 +08:00
|
|
|
EventDispatcherTestDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string CustomEventTest::title() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Send custom event";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string CustomEventTest::subtitle() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// LabelKeyboardEventTest
|
|
|
|
void LabelKeyboardEventTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto statusLabel = LabelTTF::create("No keyboard event received!", "", 20);
|
|
|
|
statusLabel->setPosition(origin + Point(size.width/2, size.height/2));
|
|
|
|
addChild(statusLabel);
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerKeyboard::create();
|
|
|
|
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){
|
2013-09-18 22:23:02 +08:00
|
|
|
char buf[100] = {0};
|
|
|
|
sprintf(buf, "Key %d was pressed!", (int)keyCode);
|
|
|
|
auto label = static_cast<LabelTTF*>(event->getCurrentTarget());
|
|
|
|
label->setString(buf);
|
|
|
|
};
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){
|
2013-09-18 22:23:02 +08:00
|
|
|
char buf[100] = {0};
|
|
|
|
sprintf(buf, "Key %d was released!", (int)keyCode);
|
|
|
|
auto label = static_cast<LabelTTF*>(event->getCurrentTarget());
|
|
|
|
label->setString(buf);
|
|
|
|
};
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
|
2013-09-18 22:23:02 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string LabelKeyboardEventTest::title() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Label Receives Keyboard Event";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string LabelKeyboardEventTest::subtitle() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Please click keyboard\n(Only available on Desktop and Android)";
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpriteAccelerationEventTest
|
|
|
|
void SpriteAccelerationEventTest::onEnter()
|
|
|
|
{
|
|
|
|
#define FIX_POS(_pos, _min, _max) \
|
|
|
|
if (_pos < _min) \
|
|
|
|
_pos = _min; \
|
|
|
|
else if (_pos > _max) \
|
|
|
|
_pos = _max; \
|
|
|
|
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2013-09-18 22:45:48 +08:00
|
|
|
Device::setAccelerometerEnabled(true);
|
|
|
|
|
2013-09-18 22:23:02 +08:00
|
|
|
auto sprite = Sprite::create(s_Ball);
|
|
|
|
sprite->setPosition(origin + Point(size.width/2, size.height/2));
|
|
|
|
addChild(sprite);
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
auto listener = EventListenerAcceleration::create([=](Acceleration* acc, Event* event){
|
2013-09-18 22:23:02 +08:00
|
|
|
auto ballSize = sprite->getContentSize();
|
|
|
|
|
|
|
|
auto ptNow = sprite->getPosition();
|
|
|
|
|
2013-09-18 22:45:48 +08:00
|
|
|
log("acc: x = %lf, y = %lf", acc->x, acc->y);
|
|
|
|
|
|
|
|
ptNow.x += acc->x * 9.81f;
|
|
|
|
ptNow.y += acc->y * 9.81f;
|
2013-09-18 22:23:02 +08:00
|
|
|
|
|
|
|
FIX_POS(ptNow.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
|
|
|
|
FIX_POS(ptNow.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
|
|
|
|
sprite->setPosition(ptNow);
|
|
|
|
});
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);
|
2013-09-18 22:23:02 +08:00
|
|
|
}
|
|
|
|
|
2013-09-18 22:45:48 +08:00
|
|
|
void SpriteAccelerationEventTest::onExit()
|
|
|
|
{
|
|
|
|
Device::setAccelerometerEnabled(false);
|
2013-09-19 13:55:49 +08:00
|
|
|
EventDispatcherTestDemo::onExit();
|
2013-09-18 22:45:48 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string SpriteAccelerationEventTest::title() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Sprite Receives Acceleration Event";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string SpriteAccelerationEventTest::subtitle() const
|
2013-09-18 22:23:02 +08:00
|
|
|
{
|
|
|
|
return "Please move your device\n(Only available on mobile)";
|
2013-10-23 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAndRetainNodeTest
|
|
|
|
void RemoveAndRetainNodeTest::onEnter()
|
|
|
|
{
|
|
|
|
_spriteSaved = false;
|
|
|
|
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Point origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
_sprite = Sprite::create("Images/CyanSquare.png");
|
|
|
|
_sprite->setPosition(origin+Point(size.width/2, size.height/2));
|
|
|
|
addChild(_sprite, 10);
|
|
|
|
|
|
|
|
// Make sprite1 touchable
|
|
|
|
auto listener1 = EventListenerTouchOneByOne::create();
|
|
|
|
listener1->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener1->onTouchBegan = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
|
|
|
|
Point locationInNode = target->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = target->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
|
|
|
|
target->setOpacity(180);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchMoved = [](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
target->setPosition(target->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
|
|
|
listener1->onTouchEnded = [=](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
log("sprite onTouchesEnded.. ");
|
|
|
|
target->setOpacity(255);
|
|
|
|
};
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite);
|
2013-10-23 16:14:03 +08:00
|
|
|
|
|
|
|
this->runAction(Sequence::create(DelayTime::create(5.0f),
|
|
|
|
CallFunc::create([this](){
|
|
|
|
_spriteSaved = true;
|
|
|
|
_sprite->retain();
|
|
|
|
_sprite->removeFromParent();
|
|
|
|
}),
|
|
|
|
DelayTime::create(5.0f),
|
|
|
|
CallFunc::create([this](){
|
|
|
|
_spriteSaved = false;
|
|
|
|
this->addChild(_sprite);
|
|
|
|
_sprite->release();
|
|
|
|
}),
|
|
|
|
nullptr
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveAndRetainNodeTest::onExit()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onExit();
|
|
|
|
if (_spriteSaved)
|
|
|
|
{
|
|
|
|
_sprite->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveAndRetainNodeTest::title() const
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
|
|
|
return "RemoveAndRetainNodeTest";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveAndRetainNodeTest::subtitle() const
|
2013-10-23 16:14:03 +08:00
|
|
|
{
|
2013-10-28 16:00:19 +08:00
|
|
|
return "Sprite should be removed after 5s, add to scene again after 5s";
|
2013-11-02 21:47:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//RemoveListenerAfterAddingTest
|
|
|
|
void RemoveListenerAfterAddingTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
auto item1 = MenuItemFont::create("Click Me 1", [this](Object* sender){
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
|
|
|
CCASSERT(false, "Should not come here!");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
|
|
|
_eventDispatcher->removeEventListener(listener);
|
|
|
|
});
|
|
|
|
|
|
|
|
item1->setPosition(VisibleRect::center() + Point(0, 80));
|
|
|
|
|
|
|
|
auto addNextButton = [this](){
|
2013-11-02 22:05:04 +08:00
|
|
|
auto next = MenuItemFont::create("Please Click Me To Reset!", [this](Object* sender){
|
|
|
|
this->restartCallback(nullptr);
|
|
|
|
});
|
2013-11-02 21:47:00 +08:00
|
|
|
next->setPosition(VisibleRect::center() + Point(0, -40));
|
|
|
|
|
|
|
|
auto menu = Menu::create(next, nullptr);
|
|
|
|
menu->setPosition(VisibleRect::leftBottom());
|
|
|
|
menu->setAnchorPoint(Point::ZERO);
|
|
|
|
this->addChild(menu);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto item2 = MenuItemFont::create("Click Me 2", [=](Object* sender){
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
|
|
|
CCASSERT(false, "Should not come here!");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
|
|
|
_eventDispatcher->removeEventListeners(EventListener::Type::TOUCH_ONE_BY_ONE);
|
|
|
|
|
|
|
|
addNextButton();
|
|
|
|
});
|
|
|
|
|
|
|
|
item2->setPosition(VisibleRect::center() + Point(0, 40));
|
|
|
|
|
|
|
|
auto item3 = MenuItemFont::create("Click Me 3", [=](Object* sender){
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
|
|
|
CCASSERT(false, "Should not come here!");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
|
|
|
_eventDispatcher->removeAllEventListeners();
|
|
|
|
|
|
|
|
addNextButton();
|
|
|
|
});
|
|
|
|
|
|
|
|
item3->setPosition(VisibleRect::center());
|
|
|
|
|
|
|
|
auto menu = Menu::create(item1, item2, item3, nullptr);
|
|
|
|
menu->setPosition(VisibleRect::leftBottom());
|
|
|
|
menu->setAnchorPoint(Point::ZERO);
|
|
|
|
|
|
|
|
addChild(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveListenerAfterAddingTest::onExit()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onExit();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveListenerAfterAddingTest::title() const
|
2013-11-02 21:47:00 +08:00
|
|
|
{
|
|
|
|
return "RemoveListenerAfterAddingTest";
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string RemoveListenerAfterAddingTest::subtitle() const
|
2013-11-02 21:47:00 +08:00
|
|
|
{
|
|
|
|
return "Should not crash!";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
//
|
|
|
|
//DirectorEventTest
|
|
|
|
//
|
2014-01-06 18:26:14 +08:00
|
|
|
DirectorEventTest::DirectorEventTest()
|
|
|
|
:_count1(0)
|
|
|
|
,_count2(0)
|
|
|
|
,_count3(0)
|
|
|
|
,_count4(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-21 16:56:28 +08:00
|
|
|
void DirectorEventTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
|
|
|
Size s = Director::getInstance()->getWinSize();
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_label1 = Label::createWithTTF("Update: 0", "fonts/arial.ttf", 20);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label1->setPosition(30,s.height/2 + 60);
|
|
|
|
this->addChild(_label1);
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_label2 = Label::createWithTTF("Visit: 0", "fonts/arial.ttf", 20);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label2->setPosition(30,s.height/2 + 20);
|
|
|
|
this->addChild(_label2);
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_label3 = Label::createWithTTF("Draw: 0", "fonts/arial.ttf", 20);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label3->setPosition(30,30);
|
|
|
|
_label3->setPosition(30,s.height/2 - 20);
|
|
|
|
this->addChild(_label3);
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_label4 = Label::createWithTTF("Projection: 0", "fonts/arial.ttf", 20);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label4->setPosition(30,30);
|
|
|
|
_label4->setPosition(30,s.height/2 - 60);
|
|
|
|
this->addChild(_label4);
|
|
|
|
|
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_event1 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_UPDATE, std::bind(&DirectorEventTest::onEvent1, this, std::placeholders::_1));
|
|
|
|
_event2 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_VISIT, std::bind(&DirectorEventTest::onEvent2, this, std::placeholders::_1));
|
|
|
|
_event3 = dispatcher->addCustomEventListener(Director::EVENT_AFTER_DRAW, [&](EventCustom *event) {
|
|
|
|
char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf)-1, "Draw: %d", _count3++);
|
|
|
|
_label3->setString(buf);
|
|
|
|
});
|
|
|
|
_event4 = dispatcher->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, [&](EventCustom *event) {
|
|
|
|
char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf)-1, "Projection: %d", _count4++);
|
|
|
|
_label4->setString(buf);
|
|
|
|
});
|
|
|
|
|
|
|
|
_event1->retain();
|
|
|
|
_event2->retain();
|
|
|
|
_event3->retain();
|
|
|
|
_event4->retain();
|
|
|
|
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectorEventTest::update(float dt)
|
|
|
|
{
|
|
|
|
static float time = 0;
|
|
|
|
|
|
|
|
time += dt;
|
|
|
|
if(time > 0.5) {
|
|
|
|
Director::getInstance()->setProjection(Director::Projection::_2D);
|
|
|
|
time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DirectorEventTest::onExit()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onExit();
|
|
|
|
|
|
|
|
auto dispatcher = Director::getInstance()->getEventDispatcher();
|
|
|
|
dispatcher->removeEventListener(_event1);
|
|
|
|
dispatcher->removeEventListener(_event2);
|
|
|
|
dispatcher->removeEventListener(_event3);
|
|
|
|
dispatcher->removeEventListener(_event4);
|
2013-12-21 16:56:28 +08:00
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
_event1->release();
|
|
|
|
_event2->release();
|
|
|
|
_event3->release();
|
|
|
|
_event4->release();
|
2013-12-21 16:56:28 +08:00
|
|
|
}
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
void DirectorEventTest::onEvent1(EventCustom *event)
|
2013-12-21 16:56:28 +08:00
|
|
|
{
|
2013-12-22 02:55:16 +08:00
|
|
|
char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf)-1, "Update: %d", _count1++);
|
|
|
|
_label1->setString(buf);
|
2013-12-21 16:56:28 +08:00
|
|
|
}
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
void DirectorEventTest::onEvent2(EventCustom *event)
|
2013-12-21 16:56:28 +08:00
|
|
|
{
|
2013-12-22 02:55:16 +08:00
|
|
|
char buf[20];
|
|
|
|
snprintf(buf, sizeof(buf)-1, "Visit: %d", _count2++);
|
|
|
|
_label2->setString(buf);
|
2013-12-21 16:56:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string DirectorEventTest::title() const
|
|
|
|
{
|
|
|
|
return "Testing Director Events";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DirectorEventTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "after visit, after draw, after update, projection changed";
|
|
|
|
}
|
|
|
|
|
|
|
|
|