axmol/tests/cpp-tests/Classes/NewEventDispatcherTest/NewEventDispatcherTest.cpp

1804 lines
59 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-02-24 18:45:25 +08:00
https://adxeproject.github.io/
2019-11-23 20:27:39 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2019-11-23 20:27:39 +08:00
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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
//
// NewEventDispatcherTest.cpp
// samples
//
// Created by James Chen on 9/13/13.
//
//
#include "NewEventDispatcherTest.h"
#include "testResource.h"
USING_NS_CC;
namespace {
class TextButton : public Label {
public:
static TextButton *
create(std::string_view text, const std::function<void(TextButton *)> &onTriggered) {
auto ret = new TextButton();
TTFConfig ttfconfig("fonts/arial.ttf", 25);
if (ret->setTTFConfig(ttfconfig)) {
ret->setString(text);
ret->_onTriggered = onTriggered;
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
void setEnabled(bool enabled) {
_enabled = enabled;
if (_enabled) {
this->setColor(Color3B::WHITE);
} else {
this->setColor(Color3B::GRAY);
}
}
private:
TextButton() : _onTriggered(nullptr), _enabled(true) {
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(TextButton::onTouchBegan, this);
listener->onTouchEnded = CC_CALLBACK_2(TextButton::onTouchEnded, this);
listener->onTouchCancelled = CC_CALLBACK_2(TextButton::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool touchHits(Touch *touch) {
auto hitPos = this->convertToNodeSpace(touch->getLocation());
if (hitPos.x >= 0 && hitPos.y >= 0 && hitPos.x <= _contentSize.width &&
hitPos.y <= _contentSize.height) {
return true;
}
return false;
}
bool onTouchBegan(Touch *touch, Event *event) {
auto hits = touchHits(touch);
if (hits) {
scaleButtonTo(0.95f);
}
return hits;
}
void onTouchEnded(Touch *touch, Event *event) {
if (_enabled) {
auto hits = touchHits(touch);
if (hits && _onTriggered) {
_onTriggered(this);
}
}
scaleButtonTo(1);
}
void onTouchCancelled(Touch *touch, Event *event) { scaleButtonTo(1); }
void scaleButtonTo(float scale) {
auto action = ScaleTo::create(0.05f, scale);
action->setTag(10000);
stopActionByTag(10000);
runAction(action);
}
std::function<void(TextButton *)> _onTriggered;
bool _enabled;
};
}
2019-11-23 20:27:39 +08:00
EventDispatcherTests::EventDispatcherTests()
{
ADD_TEST_CASE(TouchableSpriteTest);
ADD_TEST_CASE(FixedPriorityTest);
ADD_TEST_CASE(RemoveListenerWhenDispatching);
ADD_TEST_CASE(CustomEventTest);
ADD_TEST_CASE(LabelKeyboardEventTest);
ADD_TEST_CASE(SpriteAccelerationEventTest);
ADD_TEST_CASE(RemoveAndRetainNodeTest);
ADD_TEST_CASE(RemoveListenerAfterAddingTest);
ADD_TEST_CASE(DirectorEventTest);
ADD_TEST_CASE(GlobalZTouchTest);
ADD_TEST_CASE(StopPropagationTest);
ADD_TEST_CASE(PauseResumeTargetTest);
ADD_TEST_CASE(PauseResumeTargetTest2);
ADD_TEST_CASE(PauseResumeTargetTest3);
ADD_TEST_CASE(Issue4129);
ADD_TEST_CASE(Issue4160);
ADD_TEST_CASE(DanglingNodePointersTest);
ADD_TEST_CASE(RegisterAndUnregisterWhileEventHanldingTest);
ADD_TEST_CASE(WindowEventsTest);
ADD_TEST_CASE(Issue8194);
ADD_TEST_CASE(Issue9898)
}
std::string EventDispatcherTestDemo::title() const
{
return "No title";
}
// TouchableSpriteTest
void TouchableSpriteTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto containerForSprite1 = Node::create();
auto sprite1 = Sprite::create("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 80.0f));
2019-11-23 20:27:39 +08:00
containerForSprite1->addChild(sprite1);
addChild(containerForSprite1, 10);
2019-11-23 20:27:39 +08:00
auto sprite2 = Sprite::create("Images/MagentaSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite2, 20);
2019-11-23 20:27:39 +08:00
auto sprite3 = Sprite::create("Images/YellowSquare.png");
sprite3->setPosition(Vec2(0, 0));
sprite2->addChild(sprite3, 1);
2019-11-23 20:27:39 +08:00
// Make sprite1 touchable
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
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) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
};
listener1->onTouchEnded = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
target->setOpacity(255);
if (target == sprite2)
{
containerForSprite1->setLocalZOrder(100);
}
else if (target == sprite1)
2019-11-23 20:27:39 +08:00
{
containerForSprite1->setLocalZOrder(0);
}
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners", [this](Ref* sender) {
2019-11-23 20:27:39 +08:00
auto senderItem = static_cast<MenuItemFont*>(sender);
senderItem->setString("Only Next item could be clicked");
2019-11-23 20:27:39 +08:00
_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);
auto nextItem = MenuItemFont::create("Next", [=](Ref* sender) { getTestSuite()->enterNextTest(); });
2019-11-23 20:27:39 +08:00
nextItem->setFontSizeObj(16);
nextItem->setPosition(VisibleRect::right() + Vec2(-100.0f, -30.0f));
2019-11-23 20:27:39 +08:00
auto menu2 = Menu::create(nextItem, nullptr);
menu2->setPosition(Vec2(0, 0));
menu2->setAnchorPoint(Vec2(0, 0));
this->addChild(menu2);
});
2019-11-23 20:27:39 +08:00
removeAllTouchItem->setFontSizeObj(16);
removeAllTouchItem->setPosition(VisibleRect::right() + Vec2(-100.0f, 0.0f));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(removeAllTouchItem, nullptr);
menu->setPosition(Vec2(0, 0));
menu->setAnchorPoint(Vec2(0, 0));
addChild(menu);
}
std::string TouchableSpriteTest::title() const
{
return "Touchable Sprite Test";
}
std::string TouchableSpriteTest::subtitle() const
{
return "Please drag the blocks";
}
// FixedPriorityChangedTest
class TouchableSprite : public Sprite
{
public:
static TouchableSprite* create(int priority = 0)
{
2021-12-08 00:11:53 +08:00
auto ret = new TouchableSprite(priority);
if (ret->init())
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
protected:
TouchableSprite(int priority) : _listener(nullptr), _fixedPriority(priority), _removeListenerOnTouchEnded(false) {}
2019-11-23 20:27:39 +08:00
virtual bool init() override
{
if (!Sprite::init())
return false;
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [this](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = this->convertToNodeSpace(touch->getLocation());
Size s = this->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
log("TouchableSprite: onTouchBegan ...");
this->setColor(Color3B::RED);
return true;
}
return false;
};
listener->onTouchEnded = [this](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
log("TouchableSprite: onTouchEnded ...");
this->setColor(Color3B::WHITE);
2019-11-23 20:27:39 +08:00
if (_removeListenerOnTouchEnded)
{
_eventDispatcher->removeEventListener(_listener);
_listener = nullptr;
}
};
2019-11-23 20:27:39 +08:00
if (_fixedPriority != 0)
{
_eventDispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority);
}
else
{
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
_listener = listener;
return true;
}
2019-11-23 20:27:39 +08:00
virtual void onExit() override
{
if (_listener != nullptr && _fixedPriority != 0)
{
_eventDispatcher->removeEventListener(_listener);
}
2019-11-23 20:27:39 +08:00
Sprite::onExit();
}
2019-11-23 20:27:39 +08:00
public:
void removeListenerOnTouchEnded(bool toRemove) { _removeListenerOnTouchEnded = toRemove; };
2019-11-23 20:27:39 +08:00
inline EventListener* getListener() { return _listener; };
2019-11-23 20:27:39 +08:00
private:
EventListener* _listener;
int _fixedPriority;
bool _removeListenerOnTouchEnded;
};
void FixedPriorityTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto sprite1 = TouchableSprite::create(30);
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 40.0f));
2019-11-23 20:27:39 +08:00
addChild(sprite1, 10);
2019-11-23 20:27:39 +08:00
auto sprite2 = TouchableSprite::create(20);
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite2, 20);
2019-11-23 20:27:39 +08:00
auto sprite3 = TouchableSprite::create(10);
sprite3->setTexture("Images/YellowSquare.png");
sprite3->setPosition(Vec2(0, 0));
sprite2->addChild(sprite3, 1);
}
std::string FixedPriorityTest::title() const
{
return "Fixed priority test";
}
std::string FixedPriorityTest::subtitle() const
{
return "Fixed Priority, Blue: 30, Red: 20, Yellow: 10\n The lower value the higher priority will be.";
}
// RemoveListenerWhenDispatching
void RemoveListenerWhenDispatching::onEnter()
{
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto sprite1 = Sprite::create("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite1, 10);
2019-11-23 20:27:39 +08:00
// Make sprite1 touchable
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
setUserObject(listener1);
2019-11-23 20:27:39 +08:00
std::shared_ptr<bool> firstClick(new bool(true));
listener1->onTouchBegan = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = sprite1->convertToNodeSpace(touch->getLocation());
Size s = sprite1->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
sprite1->setColor(Color3B::RED);
return true;
}
return false;
};
listener1->onTouchEnded = [=](Touch* touch, Event* event) { sprite1->setColor(Color3B::WHITE); };
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
2019-11-23 20:27:39 +08:00
auto statusLabel = Label::createWithSystemFont("The sprite could be touched!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width / 2, size.height - 90));
2019-11-23 20:27:39 +08:00
addChild(statusLabel);
std::shared_ptr<bool> enable(new bool(true));
// Enable/Disable item
auto toggleItem = MenuItemToggle::createWithCallback(
[=](Ref* sender) {
if (*enable)
{
_eventDispatcher->removeEventListener(listener1);
statusLabel->setString("The sprite could not be touched!");
(*enable) = false;
}
else
{
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
statusLabel->setString("The sprite could be touched!");
(*enable) = true;
}
},
MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), nullptr);
toggleItem->setPosition(origin + Vec2(size.width / 2, 80.0f));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(toggleItem, nullptr);
menu->setPosition(Vec2(0.0f, 0.0f));
menu->setAnchorPoint(Vec2(0.0f, 0.0f));
addChild(menu, -1);
}
std::string RemoveListenerWhenDispatching::title() const
{
return "Add and remove listener\n when dispatching event";
}
std::string RemoveListenerWhenDispatching::subtitle() const
{
return "";
}
// CustomEventTest
void CustomEventTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
MenuItemFont::setFontSize(20);
2019-11-23 20:27:39 +08:00
auto statusLabel = Label::createWithSystemFont("No custom event 1 received!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width / 2, size.height - 90));
2019-11-23 20:27:39 +08:00
addChild(statusLabel);
_listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event) {
2019-11-23 20:27:39 +08:00
std::string str("Custom event 1 received, ");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
str += " times";
statusLabel->setString(str.c_str());
});
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithFixedPriority(_listener, 1);
auto sendItem = MenuItemFont::create("Send Custom Event 1", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
static int count = 0;
++count;
char* buf = new char[10];
sprintf(buf, "%d", count);
EventCustom event("game_custom_event1");
event.setUserData(buf);
_eventDispatcher->dispatchEvent(&event);
CC_SAFE_DELETE_ARRAY(buf);
});
sendItem->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
auto statusLabel2 = Label::createWithSystemFont("No custom event 2 received!", "", 20);
statusLabel2->setPosition(origin + Vec2(size.width / 2, size.height - 120));
2019-11-23 20:27:39 +08:00
addChild(statusLabel2);
_listener2 = EventListenerCustom::create("game_custom_event2", [=](EventCustom* event) {
2019-11-23 20:27:39 +08:00
std::string str("Custom event 2 received, ");
char* buf = static_cast<char*>(event->getUserData());
str += buf;
str += " times";
statusLabel2->setString(str.c_str());
});
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithFixedPriority(_listener2, 1);
auto sendItem2 = MenuItemFont::create("Send Custom Event 2", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
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 + Vec2(size.width / 2, size.height / 2 - 40));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(sendItem, sendItem2, nullptr);
menu->setPosition(Vec2(0, 0));
menu->setAnchorPoint(Vec2(0, 0));
addChild(menu, -1);
}
void CustomEventTest::onExit()
{
_eventDispatcher->removeEventListener(_listener);
_eventDispatcher->removeEventListener(_listener2);
EventDispatcherTestDemo::onExit();
}
std::string CustomEventTest::title() const
{
return "Send custom event";
}
std::string CustomEventTest::subtitle() const
{
return "";
}
// LabelKeyboardEventTest
void LabelKeyboardEventTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
#ifdef CC_PLATFORM_PC
2019-11-23 20:27:39 +08:00
auto statusLabel = Label::createWithSystemFont("No keyboard event received!", "", 20);
statusLabel->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(statusLabel);
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event) {
2019-11-23 20:27:39 +08:00
char buf[100] = {0};
sprintf(buf, "Key %d was pressed!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
switch (keyCode)
{
case EventKeyboard::KeyCode::KEY_1:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_LEFT);
break;
case EventKeyboard::KeyCode::KEY_4:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER_LEFT);
break;
case EventKeyboard::KeyCode::KEY_7:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_LEFT);
break;
case EventKeyboard::KeyCode::KEY_8:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_CENTER);
break;
case EventKeyboard::KeyCode::KEY_9:
Director::getInstance()->setStatsAnchor(AnchorPreset::TOP_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_6:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_3:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_RIGHT);
break;
case EventKeyboard::KeyCode::KEY_2:
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_CENTER);
break;
case EventKeyboard::KeyCode::KEY_5:
Director::getInstance()->setStatsAnchor(AnchorPreset::CENTER);
break;
default:
break;
}
2019-11-23 20:27:39 +08:00
};
listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event) {
2019-11-23 20:27:39 +08:00
char buf[100] = {0};
sprintf(buf, "Key %d was released!", (int)keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
label->setString(buf);
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, statusLabel);
#else
auto& layerSize = this->getContentSize();
static AnchorPreset anchor = AnchorPreset::BOTTOM_LEFT;
anchor = AnchorPreset::BOTTOM_LEFT;
auto playPrev = TextButton::create("Show Fps Prev Pos", [=](TextButton* button) {
if (anchor > AnchorPreset::BOTTOM_LEFT)
{
anchor = static_cast<AnchorPreset>((int)anchor - 1);
Director::getInstance()->setStatsAnchor(anchor);
}
});
playPrev->setPosition(layerSize.width * 0.35f, layerSize.height * 0.5f);
addChild(playPrev);
auto playNext = TextButton::create("Show Fps Next Pos", [=](TextButton* button) {
if (anchor < AnchorPreset::TOP_RIGHT)
{
anchor = static_cast<AnchorPreset>((int)anchor + 1);
Director::getInstance()->setStatsAnchor(anchor);
}
});
playNext->setPosition(layerSize.width * 0.65f, layerSize.height * 0.5f);
addChild(playNext);
Director::getInstance()->setStatsAnchor(AnchorPreset::BOTTOM_LEFT);
#endif
2019-11-23 20:27:39 +08:00
}
std::string LabelKeyboardEventTest::title() const
{
return "Label Receives Keyboard Event";
}
std::string LabelKeyboardEventTest::subtitle() const
{
#ifdef CC_PLATFORM_PC
return "Press keys 1 through 9 to change the stats anchor on the screen.";
#else
return "Change the stats anchor [1-9]";
#endif
2019-11-23 20:27:39 +08:00
}
// SpriteAccelerationEventTest
void SpriteAccelerationEventTest::onEnter()
{
#define FIX_POS(_pos, _min, _max) \
if (_pos < _min) \
_pos = _min; \
else if (_pos > _max) \
_pos = _max;
2019-11-23 20:27:39 +08:00
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
Device::setAccelerometerEnabled(true);
2019-11-23 20:27:39 +08:00
auto sprite = Sprite::create(s_Ball);
sprite->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite);
auto listener = EventListenerAcceleration::create([=](Acceleration* acc, Event* event) {
auto ballSize = sprite->getContentSize();
auto ptNow = sprite->getPosition();
2019-11-23 20:27:39 +08:00
log("acc: x = %lf, y = %lf", acc->x, acc->y);
2019-11-23 20:27:39 +08:00
ptNow.x += acc->x * 9.81f;
ptNow.y += acc->y * 9.81f;
FIX_POS(ptNow.x, (VisibleRect::left().x + ballSize.width / 2.0),
(VisibleRect::right().x - ballSize.width / 2.0f));
FIX_POS(ptNow.y, (VisibleRect::bottom().y + ballSize.height / 2.0),
(VisibleRect::top().y - ballSize.height / 2.0f));
2019-11-23 20:27:39 +08:00
sprite->setPosition(ptNow);
});
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);
}
void SpriteAccelerationEventTest::onExit()
{
Device::setAccelerometerEnabled(false);
EventDispatcherTestDemo::onExit();
}
std::string SpriteAccelerationEventTest::title() const
{
return "Sprite Receives Acceleration Event";
}
std::string SpriteAccelerationEventTest::subtitle() const
{
return "Please move your device\n(Only available on mobile)";
}
// RemoveAndRetainNodeTest
void RemoveAndRetainNodeTest::onEnter()
{
_spriteSaved = false;
2019-11-23 20:27:39 +08:00
EventDispatcherTestDemo::onEnter();
2019-11-23 20:27:39 +08:00
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
_sprite = Sprite::create("Images/CyanSquare.png");
_sprite->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(_sprite, 10);
2019-11-23 20:27:39 +08:00
// Make sprite1 touchable
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
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) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
};
listener1->onTouchEnded = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
target->setOpacity(255);
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _sprite);
this->runAction(Sequence::create(DelayTime::create(5.0f), CallFunc::create([this]() {
_spriteSaved = true;
_sprite->retain();
_sprite->removeFromParentAndCleanup(false);
2019-11-23 20:27:39 +08:00
}),
DelayTime::create(5.0f), CallFunc::create([this]() {
_spriteSaved = false;
this->addChild(_sprite);
_sprite->release();
2019-11-23 20:27:39 +08:00
}),
nullptr));
2019-11-23 20:27:39 +08:00
}
void RemoveAndRetainNodeTest::onExit()
{
EventDispatcherTestDemo::onExit();
if (_spriteSaved)
{
_sprite->release();
}
}
std::string RemoveAndRetainNodeTest::title() const
{
return "RemoveAndRetainNodeTest";
}
std::string RemoveAndRetainNodeTest::subtitle() const
{
return "Sprite should be removed after 5s, add to scene again after 5s";
}
// RemoveListenerAfterAddingTest
2019-11-23 20:27:39 +08:00
void RemoveListenerAfterAddingTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
auto item1 = MenuItemFont::create("Click Me 1", [this](Ref* sender) {
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool {
2019-11-23 20:27:39 +08:00
CCASSERT(false, "Should not come here!");
return true;
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeEventListener(listener);
});
item1->setPosition(VisibleRect::center() + Vec2(0.0f, 80.0f));
auto addNextButton = [this]() {
auto next = MenuItemFont::create("Please Click Me To Reset!",
[this](Ref* sender) { getTestSuite()->restartCurrTest(); });
2019-11-23 20:27:39 +08:00
next->setPosition(VisibleRect::center() + Vec2(0.0f, -40.0f));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(next, nullptr);
menu->setPosition(VisibleRect::leftBottom());
menu->setAnchorPoint(Vec2::ZERO);
this->addChild(menu);
};
auto item2 = MenuItemFont::create("Click Me 2", [=](Ref* sender) {
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool {
2019-11-23 20:27:39 +08:00
CCASSERT(false, "Should not come here!");
return true;
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);
2019-11-23 20:27:39 +08:00
addNextButton();
});
2019-11-23 20:27:39 +08:00
item2->setPosition(VisibleRect::center() + Vec2(0.0f, 40.0f));
auto item3 = MenuItemFont::create("Click Me 3", [=](Ref* sender) {
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool {
2019-11-23 20:27:39 +08:00
CCASSERT(false, "Should not come here!");
return true;
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
_eventDispatcher->removeAllEventListeners();
2019-11-23 20:27:39 +08:00
addNextButton();
});
2019-11-23 20:27:39 +08:00
item3->setPosition(VisibleRect::center());
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(item1, item2, item3, nullptr);
menu->setPosition(VisibleRect::leftBottom());
menu->setAnchorPoint(Vec2::ZERO);
addChild(menu);
}
void RemoveListenerAfterAddingTest::onExit()
{
EventDispatcherTestDemo::onExit();
}
std::string RemoveListenerAfterAddingTest::title() const
{
return "RemoveListenerAfterAddingTest";
}
std::string RemoveListenerAfterAddingTest::subtitle() const
{
return "Should not crash!";
}
//
// DirectorEventTest
2019-11-23 20:27:39 +08:00
//
DirectorEventTest::DirectorEventTest() : _count1(0), _count2(0), _count3(0), _count4(0) {}
2019-11-23 20:27:39 +08:00
void DirectorEventTest::onEnter()
{
EventDispatcherTestDemo::onEnter();
Size s = Director::getInstance()->getWinSize();
TTFConfig ttfConfig("fonts/arial.ttf", 20);
_label1 = Label::createWithTTF(ttfConfig, "Update: 0");
_label1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
_label1->setPosition(30, s.height / 2 + 60);
2019-11-23 20:27:39 +08:00
this->addChild(_label1);
_label2 = Label::createWithTTF(ttfConfig, "Visit: 0");
_label2->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
_label2->setPosition(30, s.height / 2 + 20);
2019-11-23 20:27:39 +08:00
this->addChild(_label2);
_label3 = Label::createWithTTF(ttfConfig, "Draw: 0");
_label3->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
_label3->setPosition(30, 30);
_label3->setPosition(30, s.height / 2 - 20);
2019-11-23 20:27:39 +08:00
this->addChild(_label3);
_label4 = Label::createWithTTF(ttfConfig, "Projection: 0");
_label4->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
_label4->setPosition(30, 30);
_label4->setPosition(30, s.height / 2 - 60);
2019-11-23 20:27:39 +08:00
this->addChild(_label4);
auto dispatcher = Director::getInstance()->getEventDispatcher();
_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) {
2019-11-23 20:27:39 +08:00
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Draw: %d", _count3++);
2019-11-23 20:27:39 +08:00
_label3->setString(buf);
});
_event4 = dispatcher->addCustomEventListener(Director::EVENT_PROJECTION_CHANGED, [&](EventCustom* event) {
2019-11-23 20:27:39 +08:00
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Projection: %d", _count4++);
2019-11-23 20:27:39 +08:00
_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)
{
2019-11-23 20:27:39 +08:00
Director::getInstance()->setProjection(Director::Projection::_2D);
time = 0;
}
}
void DirectorEventTest::onExit()
{
EventDispatcherTestDemo::onExit();
Director::getInstance()->setProjection(Director::Projection::DEFAULT);
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->removeEventListener(_event1);
dispatcher->removeEventListener(_event2);
dispatcher->removeEventListener(_event3);
dispatcher->removeEventListener(_event4);
_event1->release();
_event2->release();
_event3->release();
_event4->release();
}
void DirectorEventTest::onEvent1(EventCustom* event)
2019-11-23 20:27:39 +08:00
{
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Update: %d", _count1++);
2019-11-23 20:27:39 +08:00
_label1->setString(buf);
}
void DirectorEventTest::onEvent2(EventCustom* event)
2019-11-23 20:27:39 +08:00
{
char buf[20];
snprintf(buf, sizeof(buf) - 1, "Visit: %d", _count2++);
2019-11-23 20:27:39 +08:00
_label2->setString(buf);
}
std::string DirectorEventTest::title() const
{
return "Testing Director Events";
}
std::string DirectorEventTest::subtitle() const
{
return "after visit, after draw, after update, projection changed";
}
// GlobalZTouchTest
GlobalZTouchTest::GlobalZTouchTest() : _sprite(nullptr), _accum(0)
2019-11-23 20:27:39 +08:00
{
2019-11-23 20:27:39 +08:00
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
target->setOpacity(180);
return true;
}
return false;
};
listener->onTouchMoved = [](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
};
listener->onTouchEnded = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
log("sprite onTouchesEnded.. ");
target->setOpacity(255);
};
2019-11-23 20:27:39 +08:00
const int SPRITE_COUNT = 8;
2019-11-23 20:27:39 +08:00
for (int i = 0; i < SPRITE_COUNT; i++)
{
Sprite* sprite;
if (i == 4)
2019-11-23 20:27:39 +08:00
{
sprite = Sprite::create("Images/CyanSquare.png");
2019-11-23 20:27:39 +08:00
_sprite = sprite;
_sprite->setGlobalZOrder(-1);
}
else
{
sprite = Sprite::create("Images/YellowSquare.png");
}
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sprite);
2019-11-23 20:27:39 +08:00
this->addChild(sprite);
2019-11-23 20:27:39 +08:00
Size visibleSize = Director::getInstance()->getVisibleSize();
sprite->setPosition(VisibleRect::left().x + visibleSize.width / (SPRITE_COUNT - 1) * i,
VisibleRect::center().y);
2019-11-23 20:27:39 +08:00
}
2019-11-23 20:27:39 +08:00
this->scheduleUpdate();
}
void GlobalZTouchTest::update(float dt)
{
_accum += dt;
if (_accum > 2.0f)
{
2019-11-23 20:27:39 +08:00
float z = _sprite->getGlobalZOrder();
_sprite->setGlobalZOrder(-z);
_accum = 0;
}
}
std::string GlobalZTouchTest::title() const
{
return "Global Z Value, Try touch blue sprite";
}
std::string GlobalZTouchTest::subtitle() const
{
return "Blue Sprite should change go from foreground to background";
}
StopPropagationTest::StopPropagationTest()
{
static const int TAG_BLUE_SPRITE = 101;
2019-11-23 20:27:39 +08:00
static const int TAG_BLUE_SPRITE2 = 102;
auto touchOneByOneListener = EventListenerTouchOneByOne::create();
touchOneByOneListener->setSwallowTouches(true);
touchOneByOneListener->onTouchBegan = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
// Skip if don't touch top half screen.
if (!this->isPointInTopHalfAreaOfScreen(touch->getLocation()))
return false;
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
CCASSERT(target->getTag() == TAG_BLUE_SPRITE, "Yellow blocks shouldn't response event.");
2019-11-23 20:27:39 +08:00
if (this->isPointInNode(touch->getLocation(), target))
{
target->setOpacity(180);
return true;
}
2019-11-23 20:27:39 +08:00
// Stop propagation, so yellow blocks will not be able to receive event.
event->stopPropagation();
return false;
};
touchOneByOneListener->onTouchEnded = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setOpacity(255);
};
auto touchAllAtOnceListener = EventListenerTouchAllAtOnce::create();
touchAllAtOnceListener->onTouchesBegan = [=](const std::vector<Touch*>& touches, Event* event) {
2019-11-23 20:27:39 +08:00
// Skip if don't touch top half screen.
if (this->isPointInTopHalfAreaOfScreen(touches[0]->getLocation()))
return;
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
CCASSERT(target->getTag() == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
2019-11-23 20:27:39 +08:00
if (this->isPointInNode(touches[0]->getLocation(), target))
{
target->setOpacity(180);
}
// Stop propagation, so yellow blocks will not be able to receive event.
event->stopPropagation();
};
touchAllAtOnceListener->onTouchesEnded = [=](const std::vector<Touch*>& touches, Event* event) {
2019-11-23 20:27:39 +08:00
// Skip if don't touch top half screen.
if (this->isPointInTopHalfAreaOfScreen(touches[0]->getLocation()))
return;
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
CCASSERT(target->getTag() == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
2019-11-23 20:27:39 +08:00
if (this->isPointInNode(touches[0]->getLocation(), target))
{
target->setOpacity(255);
}
// Stop propagation, so yellow blocks will not be able to receive event.
event->stopPropagation();
};
auto keyboardEventListener = EventListenerKeyboard::create();
keyboardEventListener->onKeyPressed = [](EventKeyboard::KeyCode /*key*/, Event* event) {
2019-11-23 20:27:39 +08:00
auto target = static_cast<Sprite*>(event->getCurrentTarget());
CC_UNUSED_PARAM(target);
CCASSERT(target->getTag() == TAG_BLUE_SPRITE || target->getTag() == TAG_BLUE_SPRITE2,
"Yellow blocks shouldn't response event.");
2019-11-23 20:27:39 +08:00
// Stop propagation, so yellow blocks will not be able to receive event.
event->stopPropagation();
};
2019-11-23 20:27:39 +08:00
const int SPRITE_COUNT = 8;
2019-11-23 20:27:39 +08:00
for (int i = 0; i < SPRITE_COUNT; i++)
{
Sprite* sprite;
Sprite* sprite2;
if (i == 4)
2019-11-23 20:27:39 +08:00
{
sprite = Sprite::create("Images/CyanSquare.png");
sprite->setTag(TAG_BLUE_SPRITE);
addChild(sprite, 100);
2019-11-23 20:27:39 +08:00
sprite2 = Sprite::create("Images/CyanSquare.png");
sprite2->setTag(TAG_BLUE_SPRITE2);
addChild(sprite2, 100);
}
else
{
sprite = Sprite::create("Images/YellowSquare.png");
addChild(sprite, 0);
sprite2 = Sprite::create("Images/YellowSquare.png");
addChild(sprite2, 0);
}
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchOneByOneListener->clone(), sprite);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardEventListener->clone(), sprite);
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchAllAtOnceListener->clone(), sprite2);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardEventListener->clone(), sprite2);
Size visibleSize = Director::getInstance()->getVisibleSize();
sprite->setPosition(VisibleRect::left().x + visibleSize.width / (SPRITE_COUNT - 1) * i,
VisibleRect::center().y + sprite2->getContentSize().height / 2 + 10);
sprite2->setPosition(VisibleRect::left().x + visibleSize.width / (SPRITE_COUNT - 1) * i,
VisibleRect::center().y - sprite2->getContentSize().height / 2 - 10);
2019-11-23 20:27:39 +08:00
}
}
bool StopPropagationTest::isPointInNode(Vec2 pt, Node* node)
{
Vec2 locationInNode = node->convertToNodeSpace(pt);
Size s = node->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
return true;
}
return false;
}
bool StopPropagationTest::isPointInTopHalfAreaOfScreen(Vec2 pt)
{
Size winSize = Director::getInstance()->getWinSize();
if (pt.y >= winSize.height / 2)
{
2019-11-23 20:27:39 +08:00
return true;
}
2019-11-23 20:27:39 +08:00
return false;
}
std::string StopPropagationTest::title() const
{
return "Stop Propagation Test";
}
std::string StopPropagationTest::subtitle() const
{
return "Shouldn't crash and only blue block could be clicked";
}
// PauseResumeTargetTest
PauseResumeTargetTest::PauseResumeTargetTest()
{
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto sprite1 = TouchableSprite::create();
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 40.0f));
2019-11-23 20:27:39 +08:00
addChild(sprite1, -10);
2019-11-23 20:27:39 +08:00
auto sprite2 = TouchableSprite::create();
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite2, -20);
auto sprite3 = TouchableSprite::create(100); // Sprite3 uses fixed priority listener
2019-11-23 20:27:39 +08:00
sprite3->setTexture("Images/YellowSquare.png");
sprite3->setPosition(Vec2(0, 0));
sprite2->addChild(sprite3, -1);
auto popup = MenuItemFont::create("Popup", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
sprite3->getListener()->setEnabled(false);
_eventDispatcher->pauseEventListenersForTarget(this, true);
2019-11-23 20:27:39 +08:00
auto colorLayer = LayerColor::create(Color4B(0, 0, 255, 100));
this->addChild(colorLayer, 99999);
auto closeItem = MenuItemFont::create("close", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
colorLayer->removeFromParent();
_eventDispatcher->resumeEventListenersForTarget(this, true);
sprite3->getListener()->setEnabled(true);
});
2019-11-23 20:27:39 +08:00
closeItem->setPosition(VisibleRect::center());
2019-11-23 20:27:39 +08:00
auto closeMenu = Menu::create(closeItem, nullptr);
closeMenu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
closeMenu->setPosition(Vec2::ZERO);
2019-11-23 20:27:39 +08:00
colorLayer->addChild(closeMenu);
});
2019-11-23 20:27:39 +08:00
popup->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
popup->setPosition(VisibleRect::right());
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(popup, nullptr);
menu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
menu->setPosition(Vec2::ZERO);
2019-11-23 20:27:39 +08:00
addChild(menu);
}
PauseResumeTargetTest::~PauseResumeTargetTest() {}
2019-11-23 20:27:39 +08:00
std::string PauseResumeTargetTest::title() const
{
return "PauseResumeTargetTest";
}
std::string PauseResumeTargetTest::subtitle() const
{
return "Yellow block uses fixed priority";
}
// PauseResumeTargetTest2
PauseResumeTargetTest2::PauseResumeTargetTest2()
{
MenuItemFont::getFontSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
_touchableSprite = TouchableSprite::create();
_touchableSprite->retain();
_touchableSprite->setTexture("Images/CyanSquare.png");
_touchableSprite->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 40.0f));
2019-11-23 20:27:39 +08:00
addChild(_touchableSprite);
_itemPauseTouch = MenuItemFont::create("pauseTouch", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
_itemPauseTouch->setEnabled(false);
_itemResumeTouch->setEnabled(true);
_eventDispatcher->pauseEventListenersForTarget(_touchableSprite);
});
_itemPauseTouch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
_itemPauseTouch->setPosition(VisibleRect::right() + Vec2(-150.0f, 0.0f));
_itemResumeTouch = MenuItemFont::create("resumeTouch", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
_itemPauseTouch->setEnabled(true);
_itemResumeTouch->setEnabled(false);
_eventDispatcher->resumeEventListenersForTarget(_touchableSprite);
});
_itemResumeTouch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
_itemResumeTouch->setPosition(VisibleRect::right() + Vec2(0, 0));
_itemAddToScene = MenuItemFont::create("addToScene", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
_itemAddToScene->setEnabled(false);
_itemRemoveFromScene->setEnabled(true);
this->addChild(_touchableSprite);
});
_itemAddToScene->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
_itemAddToScene->setPosition(VisibleRect::right() + Vec2(-150.0f, -50.0f));
_itemRemoveFromScene = MenuItemFont::create("removeFromScene", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
_itemAddToScene->setEnabled(true);
_itemRemoveFromScene->setEnabled(false);
_touchableSprite->removeFromParentAndCleanup(false);
});
_itemRemoveFromScene->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
_itemRemoveFromScene->setPosition(VisibleRect::right() + Vec2(0.0f, -50.0f));
_itemAddToScene->setEnabled(false);
_itemResumeTouch->setEnabled(false);
_itemPauseTouch->setFontSizeObj(20);
_itemResumeTouch->setFontSizeObj(20);
_itemAddToScene->setFontSizeObj(20);
_itemRemoveFromScene->setFontSizeObj(20);
auto menu = Menu::create(_itemPauseTouch, _itemResumeTouch, _itemAddToScene, _itemRemoveFromScene, nullptr);
menu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
menu->setPosition(Vec2::ZERO);
addChild(menu);
}
PauseResumeTargetTest2::~PauseResumeTargetTest2()
{
_touchableSprite->release();
}
std::string PauseResumeTargetTest2::title() const
{
return "PauseResumeTargetTest2";
}
std::string PauseResumeTargetTest2::subtitle() const
{
return "";
}
// PauseResumeTargetTest3
PauseResumeTargetTest3::PauseResumeTargetTest3()
{
MenuItemFont::getFontSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
_touchableSprite = Sprite::create("Images/CyanSquare.png");
_touchableSprite->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 40.0f));
2019-11-23 20:27:39 +08:00
addChild(_touchableSprite);
auto item = MenuItemFont::create("addListener", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
MenuItemFont* senderItem = static_cast<MenuItemFont*>(sender);
senderItem->setEnabled(false);
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
Vec2 locationInNode = _touchableSprite->convertToNodeSpace(touch->getLocation());
Size s = _touchableSprite->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
2019-11-23 20:27:39 +08:00
if (rect.containsPoint(locationInNode))
{
log("TouchableSprite: onTouchBegan ...");
_touchableSprite->setColor(Color3B::RED);
return true;
}
return false;
};
listener->onTouchEnded = [this](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
log("TouchableSprite: onTouchEnded ...");
_touchableSprite->setColor(Color3B::WHITE);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, _touchableSprite);
_eventDispatcher->pauseEventListenersForTarget(_touchableSprite);
});
item->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
item->setPosition(VisibleRect::right());
auto menu = Menu::create(item, nullptr);
menu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
menu->setPosition(Vec2::ZERO);
addChild(menu);
}
PauseResumeTargetTest3::~PauseResumeTargetTest3() {}
2019-11-23 20:27:39 +08:00
std::string PauseResumeTargetTest3::title() const
{
return "PauseResumeTargetTest3";
}
std::string PauseResumeTargetTest3::subtitle() const
{
return "Sprite should not be touchable";
}
// Issue4129
Issue4129::Issue4129() : _bugFixed(false)
2019-11-23 20:27:39 +08:00
{
_customlistener = _eventDispatcher->addCustomEventListener(EVENT_COME_TO_BACKGROUND, [this](EventCustom* event) {
2019-11-23 20:27:39 +08:00
auto label = Label::createWithSystemFont("Yeah, this issue was fixed.", "", 20);
label->setAnchorPoint(Vec2(0.0f, 0.5f));
label->setPosition(Vec2(VisibleRect::left() + Vec2(0.0f, 30.0f)));
this->addChild(label);
2019-11-23 20:27:39 +08:00
// After test, remove it.
_eventDispatcher->removeEventListener(_customlistener);
_customlistener = nullptr;
_bugFixed = true;
});
auto removeAllTouchItem = MenuItemFont::create("Remove All Listeners", [this](Ref* sender) {
2019-11-23 20:27:39 +08:00
auto senderItem = static_cast<MenuItemFont*>(sender);
senderItem->setString("Only 'Reset' item could be clicked");
2019-11-23 20:27:39 +08:00
_eventDispatcher->removeAllEventListeners();
auto nextItem = MenuItemFont::create("Reset", [=](Ref* sender) {
2019-11-23 20:27:39 +08:00
CCASSERT(_bugFixed, "This issue was not fixed!");
getTestSuite()->restartCurrTest();
});
2019-11-23 20:27:39 +08:00
nextItem->setFontSizeObj(16);
nextItem->setPosition(VisibleRect::right() + Vec2(-100.0f, -30.0f));
2019-11-23 20:27:39 +08:00
auto menu2 = Menu::create(nextItem, nullptr);
menu2->setPosition(Vec2(0, 0));
menu2->setAnchorPoint(Vec2(0, 0));
this->addChild(menu2);
2019-11-23 20:27:39 +08:00
// Simulate to dispatch 'come to background' event
_eventDispatcher->dispatchCustomEvent(EVENT_COME_TO_BACKGROUND);
});
2019-11-23 20:27:39 +08:00
removeAllTouchItem->setFontSizeObj(16);
removeAllTouchItem->setPosition(VisibleRect::right() + Vec2(-100.0f, 0.0f));
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(removeAllTouchItem, nullptr);
menu->setPosition(Vec2(0, 0));
menu->setAnchorPoint(Vec2(0, 0));
addChild(menu);
}
Issue4129::~Issue4129()
{
if (_customlistener)
{
_eventDispatcher->removeEventListener(_customlistener);
}
}
std::string Issue4129::title() const
{
return "Issue 4129: Remove All Listeners";
}
std::string Issue4129::subtitle() const
{
return "Should see 'Yeah, this issue was fixed.'";
}
// Issue4160
Issue4160::Issue4160()
{
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto sprite1 = TouchableSprite::create(-30);
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2) + Vec2(-80.0f, 40.0f));
2019-11-23 20:27:39 +08:00
addChild(sprite1, -10);
2019-11-23 20:27:39 +08:00
auto sprite2 = TouchableSprite::create(-20);
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->removeListenerOnTouchEnded(true);
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite2, -20);
2019-11-23 20:27:39 +08:00
auto sprite3 = TouchableSprite::create(-10);
sprite3->setTexture("Images/YellowSquare.png");
sprite3->setPosition(Vec2(0, 0));
sprite2->addChild(sprite3, -1);
}
Issue4160::~Issue4160() {}
2019-11-23 20:27:39 +08:00
std::string Issue4160::title() const
{
return "Issue 4160: Out of range exception";
}
std::string Issue4160::subtitle() const
{
return "Touch the red block twice \n should not crash and the red one couldn't be touched";
}
// DanglingNodePointersTest
class DanglingNodePointersTestSprite : public Sprite
{
public:
typedef std::function<void(DanglingNodePointersTestSprite* sprite)> TappedCallback;
static DanglingNodePointersTestSprite* create(const TappedCallback& tappedCallback)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new DanglingNodePointersTestSprite(tappedCallback);
2021-12-08 00:11:53 +08:00
if (ret->init())
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
protected:
DanglingNodePointersTestSprite(const TappedCallback& tappedCallback)
: _eventListener(nullptr), _tappedCallback(tappedCallback)
{}
2019-11-23 20:27:39 +08:00
public:
void onEnter() override
{
Sprite::onEnter();
2019-11-23 20:27:39 +08:00
_eventListener = EventListenerTouchOneByOne::create();
_eventListener->setSwallowTouches(false);
_eventListener->onTouchBegan = [this](Touch* touch, Event* event) -> bool {
2019-11-23 20:27:39 +08:00
_tappedCallback(this);
return false; // Don't claim the touch so it can propagate
2019-11-23 20:27:39 +08:00
};
_eventListener->onTouchEnded = [](Touch* touch, Event* event) {
2019-11-23 20:27:39 +08:00
// Do nothing
};
2019-11-23 20:27:39 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(_eventListener, this);
}
2019-11-23 20:27:39 +08:00
void onExit() override
{
_eventDispatcher->removeEventListenersForTarget(this);
_eventListener = nullptr;
Sprite::onExit();
}
private:
2019-11-23 20:27:39 +08:00
EventListenerTouchOneByOne* _eventListener;
TappedCallback _tappedCallback;
};
DanglingNodePointersTest::DanglingNodePointersTest()
{
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1 && COCOS2D_DEBUG > 0
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
auto callback2 = [](DanglingNodePointersTestSprite* sprite2) {
2019-11-23 20:27:39 +08:00
CCASSERT(false, "This should never be called because the sprite gets removed from it's parent and destroyed!");
exit(1);
};
auto callback1 = [callback2, origin, size](DanglingNodePointersTestSprite* sprite1) {
DanglingNodePointersTestSprite* sprite2 =
dynamic_cast<DanglingNodePointersTestSprite*>(sprite1->getChildren().at(0));
2019-11-23 20:27:39 +08:00
CCASSERT(sprite2, "The first child of sprite 1 should be sprite 2!");
CCASSERT(
sprite2->getReferenceCount() == 1,
"There should only be 1 reference to sprite 1, from it's parent node. Hence removing it will destroy it!");
sprite1->removeAllChildren(); // This call should cause sprite 2 to be destroyed
2019-11-23 20:27:39 +08:00
// Recreate sprite 1 again
sprite2 = DanglingNodePointersTestSprite::create(callback2);
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
sprite1->addChild(sprite2, -20);
};
auto sprite1 = DanglingNodePointersTestSprite::create(callback1); // Sprite 1 will receive touch before sprite 2
2019-11-23 20:27:39 +08:00
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite1, -10);
auto sprite2 = DanglingNodePointersTestSprite::create(
callback2); // Sprite 2 will be removed when sprite 1 is touched, should never receive an event.
2019-11-23 20:27:39 +08:00
sprite2->setTexture("Images/MagentaSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
sprite1->addChild(sprite2, -20);
2019-11-23 20:27:39 +08:00
#endif
}
DanglingNodePointersTest::~DanglingNodePointersTest() {}
2019-11-23 20:27:39 +08:00
std::string DanglingNodePointersTest::title() const
{
return "DanglingNodePointersTest";
}
std::string DanglingNodePointersTest::subtitle() const
{
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1 && COCOS2D_DEBUG > 0
return "Tap the square - should not crash!";
2019-11-23 20:27:39 +08:00
#else
return "For test to work, must be compiled with:\n"
"CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1\n&& COCOS2D_DEBUG > 0";
2019-11-23 20:27:39 +08:00
#endif
}
RegisterAndUnregisterWhileEventHanldingTest::RegisterAndUnregisterWhileEventHanldingTest()
{
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = Director::getInstance()->getVisibleSize();
auto callback1 = [=](DanglingNodePointersTestSprite* sprite) {
auto callback2 = [](DanglingNodePointersTestSprite* sprite) {
2019-11-23 20:27:39 +08:00
CCASSERT(false, "This should never get called!");
};
2019-11-23 20:27:39 +08:00
{
AutoreleasePool pool;
2019-11-23 20:27:39 +08:00
auto sprite2 = DanglingNodePointersTestSprite::create(callback2);
sprite2->setTexture("Images/CyanSquare.png");
sprite2->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite2, 0);
removeChild(sprite2);
}
};
2019-11-23 20:27:39 +08:00
auto sprite1 = DanglingNodePointersTestSprite::create(callback1);
sprite1->setTexture("Images/CyanSquare.png");
sprite1->setPosition(origin + Vec2(size.width / 2, size.height / 2));
2019-11-23 20:27:39 +08:00
addChild(sprite1, -10);
}
std::string RegisterAndUnregisterWhileEventHanldingTest::title() const
{
return "RegisterAndUnregisterWhileEventHanldingTest";
}
std::string RegisterAndUnregisterWhileEventHanldingTest::subtitle() const
{
return "Tap the square multiple times - should not crash!";
2019-11-23 20:27:39 +08:00
}
//
WindowEventsTest::WindowEventsTest()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || \
(CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
2019-11-23 20:27:39 +08:00
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_RESIZED, [](EventCustom* event) {
// TODO: need to create resizeable window
log("<<< WINDOW RESIZED! >>> ");
});
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_FOCUSED,
[](EventCustom* event) { log("<<< WINDOW FOCUSED! >>> "); });
dispatcher->addCustomEventListener(GLViewImpl::EVENT_WINDOW_UNFOCUSED,
[](EventCustom* event) { log("<<< WINDOW BLURRED! >>> "); });
2019-11-23 20:27:39 +08:00
#endif
}
std::string WindowEventsTest::title() const
{
return "WindowEventsTest";
}
std::string WindowEventsTest::subtitle() const
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || \
(CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
return "Resize and Switch to another window and back. Read Logs.";
2019-11-23 20:27:39 +08:00
#else
return "Unsupported platform.";
2019-11-23 20:27:39 +08:00
#endif
}
// https://github.com/cocos2d/cocos2d-x/issues/8194
Issue8194::Issue8194()
{
auto origin = Director::getInstance()->getVisibleOrigin();
auto size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
static bool nodesAdded = false;
#define tagA 100
#define tagB 101
// dispatch custom event in another custom event, make the custom event "Issue8194" take effect immediately
_listener =
getEventDispatcher()->addCustomEventListener(Director::EVENT_AFTER_UPDATE, [this](cocos2d::EventCustom* event) {
if (nodesAdded)
{
// CCLOG("Fire Issue8194 Event");
getEventDispatcher()->dispatchCustomEvent("Issue8194");
// clear test nodes and listeners
getEventDispatcher()->removeCustomEventListeners("Issue8194");
removeChildByTag(tagA);
removeChildByTag(tagB);
nodesAdded = false;
}
});
2019-11-23 20:27:39 +08:00
// When click this menuitem, it will add two node A and B, then send a custom event.
// Because Node B's localZOrder < A's, the custom event should process by node B.
auto menuItem = MenuItemFont::create("Dispatch Custom Event", [this](Ref* sender) {
2019-11-23 20:27:39 +08:00
// add nodeA to scene
auto nodeA = Node::create();
addChild(nodeA, 1, tagA);
cocos2d::EventListenerCustom* listenerA =
cocos2d::EventListenerCustom::create("Issue8194", [&](cocos2d::EventCustom* event) {
_subtitleLabel->setString("Bug has been fixed.");
event->stopPropagation();
});
2019-11-23 20:27:39 +08:00
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerA, nodeA);
2019-11-23 20:27:39 +08:00
// add nodeB to scene
auto nodeB = Node::create();
addChild(nodeB, -1, tagB);
cocos2d::EventListenerCustom* listenerB =
cocos2d::EventListenerCustom::create("Issue8194", [&](cocos2d::EventCustom* event) {
_subtitleLabel->setString("Bug exist yet.");
event->stopPropagation();
});
2019-11-23 20:27:39 +08:00
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerB, nodeB);
2019-11-23 20:27:39 +08:00
nodesAdded = true;
});
menuItem->setPosition(origin.x + size.width / 2, origin.y + size.height / 2);
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(menuItem, nullptr);
menu->setPosition(Vec2::ZERO);
addChild(menu);
}
Issue8194::~Issue8194()
{
getEventDispatcher()->removeEventListener(_listener);
}
std::string Issue8194::title() const
{
return "Issue 8194";
}
std::string Issue8194::subtitle() const
{
return "After click button, should show 'Bug has been fixed.'";
2019-11-23 20:27:39 +08:00
}
Issue9898::Issue9898()
{
auto origin = Director::getInstance()->getVisibleOrigin();
auto size = Director::getInstance()->getVisibleSize();
2019-11-23 20:27:39 +08:00
auto nodeA = Node::create();
addChild(nodeA);
_listener = cocos2d::EventListenerCustom::create("Issue9898", [&](cocos2d::EventCustom* event) {
2019-11-23 20:27:39 +08:00
_eventDispatcher->removeEventListener(_listener);
_eventDispatcher->dispatchCustomEvent("Issue9898");
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(_listener, nodeA);
auto menuItem = MenuItemFont::create("Dispatch Custom Event",
[&](Ref* sender) { _eventDispatcher->dispatchCustomEvent("Issue9898"); });
menuItem->setPosition(origin.x + size.width / 2, origin.y + size.height / 2);
2019-11-23 20:27:39 +08:00
auto menu = Menu::create(menuItem, nullptr);
menu->setPosition(Vec2::ZERO);
addChild(menu);
}
std::string Issue9898::title() const
{
return "Issue 9898";
}
std::string Issue9898::subtitle() const
{
return "Should not crash if dispatch event after remove\n event listener in callback";
2019-11-23 20:27:39 +08:00
}