2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
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
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
EventDispatcherTests::EventDispatcherTests()
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
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);
|
2017-05-19 14:03:51 +08:00
|
|
|
ADD_TEST_CASE(PauseResumeTargetTest2);
|
|
|
|
ADD_TEST_CASE(PauseResumeTargetTest3);
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(Issue4129);
|
|
|
|
ADD_TEST_CASE(Issue4160);
|
|
|
|
ADD_TEST_CASE(DanglingNodePointersTest);
|
|
|
|
ADD_TEST_CASE(RegisterAndUnregisterWhileEventHanldingTest);
|
2016-09-21 11:32:33 +08:00
|
|
|
ADD_TEST_CASE(WindowEventsTest);
|
2015-07-01 14:39:35 +08:00
|
|
|
ADD_TEST_CASE(Issue8194);
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(Issue9898)
|
2013-09-13 16:38:21 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string EventDispatcherTestDemo::title() const
|
2013-09-13 16:38:21 +08:00
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TouchableSpriteTest
|
|
|
|
void TouchableSpriteTest::onEnter()
|
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-13 16:38:21 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-04-10 15:31:53 +08:00
|
|
|
auto containerForSprite1 = Node::create();
|
2013-09-13 16:38:21 +08:00
|
|
|
auto sprite1 = Sprite::create("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 80));
|
2014-04-10 15:31:53 +08:00
|
|
|
containerForSprite1->addChild(sprite1);
|
|
|
|
addChild(containerForSprite1, 10);
|
2013-09-13 16:38:21 +08:00
|
|
|
|
|
|
|
auto sprite2 = Sprite::create("Images/MagentaSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2013-09-13 18:01:32 +08:00
|
|
|
addChild(sprite2, 20);
|
|
|
|
|
|
|
|
auto sprite3 = Sprite::create("Images/YellowSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite3->setPosition(Vec2(0, 0));
|
2013-09-13 18:01:32 +08:00
|
|
|
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());
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
2013-09-13 16:38:21 +08:00
|
|
|
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)
|
|
|
|
{
|
2014-04-10 15:31:53 +08:00
|
|
|
containerForSprite1->setLocalZOrder(100);
|
2013-09-13 18:51:00 +08:00
|
|
|
}
|
|
|
|
else if(target == sprite1)
|
|
|
|
{
|
2014-04-10 15:31:53 +08:00
|
|
|
containerForSprite1->setLocalZOrder(0);
|
2013-09-13 18:51:00 +08:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto removeAllTouchItem = MenuItemFont::create("Remove All Touch Listeners", [this](Ref* sender){
|
2013-09-20 14:14:03 +08:00
|
|
|
auto senderItem = static_cast<MenuItemFont*>(sender);
|
|
|
|
senderItem->setString("Only Next item could be clicked");
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);
|
2013-09-20 14:14:03 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto nextItem = MenuItemFont::create("Next", [=](Ref* sender){
|
2015-04-03 14:31:03 +08:00
|
|
|
getTestSuite()->enterNextTest();
|
2013-09-20 14:14:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
nextItem->setFontSizeObj(16);
|
2014-05-15 01:07:09 +08:00
|
|
|
nextItem->setPosition(VisibleRect::right() + Vec2(-100, -30));
|
2013-09-20 14:14:03 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu2 = Menu::create(nextItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu2->setPosition(Vec2(0, 0));
|
|
|
|
menu2->setAnchorPoint(Vec2(0, 0));
|
2013-09-20 14:14:03 +08:00
|
|
|
this->addChild(menu2);
|
|
|
|
});
|
|
|
|
|
|
|
|
removeAllTouchItem->setFontSizeObj(16);
|
2014-05-15 01:07:09 +08:00
|
|
|
removeAllTouchItem->setPosition(VisibleRect::right() + Vec2(-100, 0));
|
2013-09-20 14:14:03 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(removeAllTouchItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
|
|
|
menu->setAnchorPoint(Vec2(0, 0));
|
2013-09-20 14:14:03 +08:00
|
|
|
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
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
class TouchableSprite : public Sprite
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
|
|
|
public:
|
2014-03-02 15:54:36 +08:00
|
|
|
static TouchableSprite* create(int priority = 0)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) TouchableSprite(priority);
|
2014-03-02 15:54:36 +08:00
|
|
|
if (ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2013-11-14 07:55:36 +08:00
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
protected:
|
|
|
|
TouchableSprite(int priority)
|
2013-09-16 15:43:20 +08:00
|
|
|
: _listener(nullptr)
|
2014-03-02 15:54:36 +08:00
|
|
|
, _fixedPriority(priority)
|
2014-03-02 16:22:57 +08:00
|
|
|
, _removeListenerOnTouchEnded(false)
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
|
|
|
}
|
2017-05-19 14:03:51 +08:00
|
|
|
|
|
|
|
virtual bool init() override
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
2017-05-19 14:03:51 +08:00
|
|
|
if (!Sprite::init())
|
|
|
|
return false;
|
|
|
|
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-16 15:43:20 +08:00
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
2017-05-19 14:03:51 +08:00
|
|
|
listener->onTouchBegan = [this](Touch* touch, Event* event){
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = this->convertToNodeSpace(touch->getLocation());
|
2013-09-16 15:43:20 +08:00
|
|
|
Size s = this->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
2017-05-19 14:03:51 +08:00
|
|
|
log("TouchableSprite: onTouchBegan ...");
|
2013-09-16 15:43:20 +08:00
|
|
|
this->setColor(Color3B::RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2016-12-12 09:39:20 +08:00
|
|
|
listener->onTouchEnded = [this](Touch* touch, Event* event){
|
2017-05-19 14:03:51 +08:00
|
|
|
log("TouchableSprite: onTouchEnded ...");
|
2013-09-16 15:43:20 +08:00
|
|
|
this->setColor(Color3B::WHITE);
|
2014-03-02 16:22:57 +08:00
|
|
|
|
|
|
|
if (_removeListenerOnTouchEnded)
|
|
|
|
{
|
2016-12-12 09:39:20 +08:00
|
|
|
_eventDispatcher->removeEventListener(_listener);
|
|
|
|
_listener = nullptr;
|
2014-03-02 16:22:57 +08:00
|
|
|
}
|
2013-09-16 15:43:20 +08:00
|
|
|
};
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
if (_fixedPriority != 0)
|
|
|
|
{
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, _fixedPriority);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
}
|
2014-03-02 16:22:57 +08:00
|
|
|
|
2013-09-16 15:43:20 +08:00
|
|
|
_listener = listener;
|
2017-05-19 14:03:51 +08:00
|
|
|
return true;
|
2013-09-16 15:43:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 14:03:51 +08:00
|
|
|
virtual void onExit() override
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
2017-05-19 14:03:51 +08:00
|
|
|
if (_listener != nullptr && _fixedPriority != 0)
|
2016-12-12 09:39:20 +08:00
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_listener);
|
|
|
|
}
|
2013-09-16 15:43:20 +08:00
|
|
|
|
|
|
|
Sprite::onExit();
|
|
|
|
}
|
2017-05-19 14:03:51 +08:00
|
|
|
public:
|
2014-03-02 16:22:57 +08:00
|
|
|
void removeListenerOnTouchEnded(bool toRemove) { _removeListenerOnTouchEnded = toRemove; };
|
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
inline EventListener* getListener() { return _listener; };
|
|
|
|
|
2013-09-16 15:43:20 +08:00
|
|
|
private:
|
|
|
|
EventListener* _listener;
|
|
|
|
int _fixedPriority;
|
2014-03-02 16:22:57 +08:00
|
|
|
bool _removeListenerOnTouchEnded;
|
2013-09-16 15:43:20 +08:00
|
|
|
};
|
|
|
|
|
2013-09-16 16:02:48 +08:00
|
|
|
void FixedPriorityTest::onEnter()
|
2013-09-16 15:43:20 +08:00
|
|
|
{
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-16 15:43:20 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite1 = TouchableSprite::create(30);
|
2013-11-14 07:55:36 +08:00
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 40));
|
2013-09-16 15:43:20 +08:00
|
|
|
addChild(sprite1, 10);
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite2 = TouchableSprite::create(20);
|
2013-11-14 07:55:36 +08:00
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2013-09-16 15:43:20 +08:00
|
|
|
addChild(sprite2, 20);
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite3 = TouchableSprite::create(10);
|
2013-11-14 07:55:36 +08:00
|
|
|
sprite3->setTexture("Images/YellowSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite3->setPosition(Vec2(0, 0));
|
2013-09-16 15:43:20 +08:00
|
|
|
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();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-18 22:23:02 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto sprite1 = Sprite::create("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2013-09-18 22:23:02 +08:00
|
|
|
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){
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = sprite1->convertToNodeSpace(touch->getLocation());
|
2013-09-18 22:23:02 +08:00
|
|
|
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
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto statusLabel = Label::createWithSystemFont("The sprite could be touched!", "", 20);
|
2014-05-15 01:07:09 +08:00
|
|
|
statusLabel->setPosition(origin + Vec2(size.width/2, size.height-90));
|
2013-09-18 22:23:02 +08:00
|
|
|
addChild(statusLabel);
|
|
|
|
std::shared_ptr<bool> enable(new bool(true));
|
|
|
|
// Enable/Disable item
|
2014-02-20 10:53:49 +08:00
|
|
|
auto toggleItem = MenuItemToggle::createWithCallback([=](Ref* sender){
|
2013-09-18 22:23:02 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-07-10 00:45:27 +08:00
|
|
|
}, MenuItemFont::create("Enabled"), MenuItemFont::create("Disabled"), nullptr);
|
2013-09-18 22:23:02 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
toggleItem->setPosition(origin + Vec2(size.width/2, 80));
|
2013-09-18 22:23:02 +08:00
|
|
|
auto menu = Menu::create(toggleItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
|
|
|
menu->setAnchorPoint(Vec2(0, 0));
|
2013-09-18 22:23:02 +08:00
|
|
|
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();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-18 22:23:02 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2013-10-28 10:49:43 +08:00
|
|
|
MenuItemFont::setFontSize(20);
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto statusLabel = Label::createWithSystemFont("No custom event 1 received!", "", 20);
|
2014-05-15 01:07:09 +08:00
|
|
|
statusLabel->setPosition(origin + Vec2(size.width/2, size.height-90));
|
2013-09-18 22:23:02 +08:00
|
|
|
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
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto sendItem = MenuItemFont::create("Send Custom Event 1", [=](Ref* 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
|
|
|
});
|
2014-05-15 01:07:09 +08:00
|
|
|
sendItem->setPosition(origin + Vec2(size.width/2, size.height/2));
|
2013-10-28 10:49:43 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto statusLabel2 = Label::createWithSystemFont("No custom event 2 received!", "", 20);
|
2014-05-15 01:07:09 +08:00
|
|
|
statusLabel2->setPosition(origin + Vec2(size.width/2, size.height-120));
|
2013-10-28 10:49:43 +08:00
|
|
|
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);
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto sendItem2 = MenuItemFont::create("Send Custom Event 2", [=](Ref* sender){
|
2013-10-28 10:49:43 +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);
|
|
|
|
});
|
2014-05-15 01:07:09 +08:00
|
|
|
sendItem2->setPosition(origin + Vec2(size.width/2, size.height/2 - 40));
|
2013-10-28 10:49:43 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(sendItem, sendItem2, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
|
|
|
menu->setAnchorPoint(Vec2(0, 0));
|
2013-09-18 22:23:02 +08:00
|
|
|
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();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-18 22:23:02 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto statusLabel = Label::createWithSystemFont("No keyboard event received!", "", 20);
|
2014-05-15 01:07:09 +08:00
|
|
|
statusLabel->setPosition(origin + Vec2(size.width/2, size.height/2));
|
2013-09-18 22:23:02 +08:00
|
|
|
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);
|
2014-03-26 23:33:58 +08:00
|
|
|
auto label = static_cast<Label*>(event->getCurrentTarget());
|
2013-09-18 22:23:02 +08:00
|
|
|
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);
|
2014-03-26 23:33:58 +08:00
|
|
|
auto label = static_cast<Label*>(event->getCurrentTarget());
|
2013-09-18 22:23:02 +08:00
|
|
|
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
|
|
|
{
|
2015-05-29 23:15:04 +08:00
|
|
|
return "Please click keyboard\n(Only available on Desktop, Android\nand Windows Universal Apps)";
|
2013-09-18 22:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SpriteAccelerationEventTest
|
|
|
|
void SpriteAccelerationEventTest::onEnter()
|
|
|
|
{
|
|
|
|
#define FIX_POS(_pos, _min, _max) \
|
|
|
|
if (_pos < _min) \
|
|
|
|
_pos = _min; \
|
|
|
|
else if (_pos > _max) \
|
|
|
|
_pos = _max; \
|
|
|
|
|
|
|
|
EventDispatcherTestDemo::onEnter();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-09-18 22:23:02 +08:00
|
|
|
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);
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setPosition(origin + Vec2(size.width/2, size.height/2));
|
2013-09-18 22:23:02 +08:00
|
|
|
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();
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2013-10-23 16:14:03 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
_sprite = Sprite::create("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
_sprite->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2013-10-23 16:14:03 +08:00
|
|
|
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());
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
2013-10-23 16:14:03 +08:00
|
|
|
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();
|
2017-05-19 14:03:51 +08:00
|
|
|
_sprite->removeFromParentAndCleanup(false);
|
2013-10-23 16:14:03 +08:00
|
|
|
}),
|
|
|
|
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();
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto item1 = MenuItemFont::create("Click Me 1", [this](Ref* sender){
|
2013-11-02 21:47:00 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
item1->setPosition(VisibleRect::center() + Vec2(0, 80));
|
2013-11-02 21:47:00 +08:00
|
|
|
|
|
|
|
auto addNextButton = [this](){
|
2014-02-20 10:53:49 +08:00
|
|
|
auto next = MenuItemFont::create("Please Click Me To Reset!", [this](Ref* sender){
|
2015-04-03 14:31:03 +08:00
|
|
|
getTestSuite()->restartCurrTest();
|
2013-11-02 22:05:04 +08:00
|
|
|
});
|
2014-05-15 01:07:09 +08:00
|
|
|
next->setPosition(VisibleRect::center() + Vec2(0, -40));
|
2013-11-02 21:47:00 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(next, nullptr);
|
|
|
|
menu->setPosition(VisibleRect::leftBottom());
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setAnchorPoint(Vec2::ZERO);
|
2013-11-02 21:47:00 +08:00
|
|
|
this->addChild(menu);
|
|
|
|
};
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto item2 = MenuItemFont::create("Click Me 2", [=](Ref* sender){
|
2013-11-02 21:47:00 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->onTouchBegan = [](Touch* touch, Event* event) -> bool{
|
|
|
|
CCASSERT(false, "Should not come here!");
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(listener, -1);
|
2014-03-02 15:54:36 +08:00
|
|
|
_eventDispatcher->removeEventListenersForType(EventListener::Type::TOUCH_ONE_BY_ONE);
|
2013-11-02 21:47:00 +08:00
|
|
|
|
|
|
|
addNextButton();
|
|
|
|
});
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
item2->setPosition(VisibleRect::center() + Vec2(0, 40));
|
2013-11-02 21:47:00 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto item3 = MenuItemFont::create("Click Me 3", [=](Ref* sender){
|
2013-11-02 21:47:00 +08:00
|
|
|
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());
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setAnchorPoint(Vec2::ZERO);
|
2013-11-02 21:47:00 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 20);
|
|
|
|
|
|
|
|
_label1 = Label::createWithTTF(ttfConfig, "Update: 0");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label1->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label1->setPosition(30,s.height/2 + 60);
|
|
|
|
this->addChild(_label1);
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
_label2 = Label::createWithTTF(ttfConfig, "Visit: 0");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label2->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label2->setPosition(30,s.height/2 + 20);
|
|
|
|
this->addChild(_label2);
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
_label3 = Label::createWithTTF(ttfConfig, "Draw: 0");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label3->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
2013-12-21 16:56:28 +08:00
|
|
|
_label3->setPosition(30,30);
|
|
|
|
_label3->setPosition(30,s.height/2 - 20);
|
|
|
|
this->addChild(_label3);
|
|
|
|
|
2014-01-11 22:33:07 +08:00
|
|
|
_label4 = Label::createWithTTF(ttfConfig, "Projection: 0");
|
2014-05-15 01:07:09 +08:00
|
|
|
_label4->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
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();
|
|
|
|
|
2014-04-02 17:12:09 +08:00
|
|
|
Director::getInstance()->setProjection(Director::Projection::DEFAULT);
|
|
|
|
|
2013-12-22 02:55:16 +08:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2014-01-21 10:23:05 +08:00
|
|
|
// GlobalZTouchTest
|
|
|
|
GlobalZTouchTest::GlobalZTouchTest()
|
|
|
|
: _sprite(nullptr)
|
|
|
|
, _accum(0)
|
|
|
|
{
|
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
2014-01-21 10:23:05 +08:00
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
listener->onTouchBegan = [](Touch* touch, Event* event){
|
2014-01-21 10:23:05 +08:00
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
|
2014-01-21 10:23:05 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
listener->onTouchMoved = [](Touch* touch, Event* event){
|
2014-01-21 10:23:05 +08:00
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
target->setPosition(target->getPosition() + touch->getDelta());
|
|
|
|
};
|
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
listener->onTouchEnded = [=](Touch* touch, Event* event){
|
2014-01-21 10:23:05 +08:00
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
log("sprite onTouchesEnded.. ");
|
|
|
|
target->setOpacity(255);
|
|
|
|
};
|
|
|
|
|
|
|
|
const int SPRITE_COUNT = 8;
|
|
|
|
|
|
|
|
for (int i = 0; i < SPRITE_COUNT; i++)
|
|
|
|
{
|
|
|
|
Sprite *sprite;
|
|
|
|
if(i==4)
|
|
|
|
{
|
|
|
|
sprite = Sprite::create("Images/CyanSquare.png");
|
|
|
|
_sprite = sprite;
|
|
|
|
_sprite->setGlobalZOrder(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite = Sprite::create("Images/YellowSquare.png");
|
|
|
|
}
|
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), sprite);
|
2014-01-21 10:23:05 +08:00
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
this->addChild(sprite);
|
2014-01-21 10:23:05 +08:00
|
|
|
|
|
|
|
Size visibleSize = Director::getInstance()->getVisibleSize();
|
|
|
|
sprite->setPosition(VisibleRect::left().x + visibleSize.width / (SPRITE_COUNT - 1) * i, VisibleRect::center().y);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalZTouchTest::update(float dt)
|
|
|
|
{
|
|
|
|
_accum += dt;
|
|
|
|
if( _accum > 2.0f) {
|
|
|
|
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";
|
|
|
|
}
|
2013-12-21 16:56:28 +08:00
|
|
|
|
2014-01-27 16:30:20 +08:00
|
|
|
StopPropagationTest::StopPropagationTest()
|
|
|
|
{
|
|
|
|
static const int TAG_BLUE_SPRITE = 101;
|
|
|
|
static const int TAG_BLUE_SPRITE2 = 102;
|
|
|
|
|
|
|
|
auto touchOneByOneListener = EventListenerTouchOneByOne::create();
|
|
|
|
touchOneByOneListener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
touchOneByOneListener->onTouchBegan = [=](Touch* touch, Event* event){
|
|
|
|
// Skip if don't touch top half screen.
|
|
|
|
if (!this->isPointInTopHalfAreaOfScreen(touch->getLocation()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
CCASSERT(target->getTag() == TAG_BLUE_SPRITE, "Yellow blocks shouldn't response event.");
|
|
|
|
|
|
|
|
if (this->isPointInNode(touch->getLocation(), target))
|
|
|
|
{
|
|
|
|
target->setOpacity(180);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop propagation, so yellow blocks will not be able to receive event.
|
|
|
|
event->stopPropagation();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
touchOneByOneListener->onTouchEnded = [=](Touch* touch, Event* event){
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
target->setOpacity(255);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto touchAllAtOnceListener = EventListenerTouchAllAtOnce::create();
|
|
|
|
touchAllAtOnceListener->onTouchesBegan = [=](const std::vector<Touch*>& touches, Event* event){
|
|
|
|
// Skip if don't touch top half screen.
|
|
|
|
if (this->isPointInTopHalfAreaOfScreen(touches[0]->getLocation()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
CCASSERT(target->getTag() == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
|
|
|
|
|
|
|
|
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){
|
|
|
|
// Skip if don't touch top half screen.
|
|
|
|
if (this->isPointInTopHalfAreaOfScreen(touches[0]->getLocation()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
|
|
|
CCASSERT(target->getTag() == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
|
|
|
|
|
|
|
|
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();
|
2016-11-16 09:48:37 +08:00
|
|
|
keyboardEventListener->onKeyPressed = [](EventKeyboard::KeyCode /*key*/, Event* event){
|
2014-01-27 16:30:20 +08:00
|
|
|
auto target = static_cast<Sprite*>(event->getCurrentTarget());
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
CC_UNUSED_PARAM(target);
|
2014-01-27 16:30:20 +08:00
|
|
|
CCASSERT(target->getTag() == TAG_BLUE_SPRITE || target->getTag() == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
|
|
|
|
// Stop propagation, so yellow blocks will not be able to receive event.
|
|
|
|
event->stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
const int SPRITE_COUNT = 8;
|
|
|
|
|
|
|
|
for (int i = 0; i < SPRITE_COUNT; i++)
|
|
|
|
{
|
|
|
|
Sprite* sprite;
|
|
|
|
Sprite* sprite2;
|
|
|
|
|
|
|
|
if(i==4)
|
|
|
|
{
|
|
|
|
sprite = Sprite::create("Images/CyanSquare.png");
|
|
|
|
sprite->setTag(TAG_BLUE_SPRITE);
|
|
|
|
addChild(sprite, 100);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchOneByOneListener->clone(), sprite);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardEventListener->clone(), sprite);
|
|
|
|
|
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool StopPropagationTest::isPointInNode(Vec2 pt, Node* node)
|
2014-01-27 16:30:20 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 locationInNode = node->convertToNodeSpace(pt);
|
2014-01-27 16:30:20 +08:00
|
|
|
Size s = node->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool StopPropagationTest::isPointInTopHalfAreaOfScreen(Vec2 pt)
|
2014-01-27 16:30:20 +08:00
|
|
|
{
|
|
|
|
Size winSize = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
if (pt.y >= winSize.height/2) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2014-02-28 15:46:48 +08:00
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
// PauseResumeTargetTest
|
|
|
|
PauseResumeTargetTest::PauseResumeTargetTest()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2014-03-02 15:54:36 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto sprite1 = TouchableSprite::create();
|
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 40));
|
2014-03-02 15:54:36 +08:00
|
|
|
addChild(sprite1, -10);
|
|
|
|
|
|
|
|
auto sprite2 = TouchableSprite::create();
|
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-03-02 15:54:36 +08:00
|
|
|
addChild(sprite2, -20);
|
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
auto sprite3 = TouchableSprite::create(100); // Sprite3 uses fixed priority listener
|
2014-03-02 15:54:36 +08:00
|
|
|
sprite3->setTexture("Images/YellowSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite3->setPosition(Vec2(0, 0));
|
2014-03-02 15:54:36 +08:00
|
|
|
sprite2->addChild(sprite3, -1);
|
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
auto popup = MenuItemFont::create("Popup", [=](Ref* sender){
|
2014-03-02 15:54:36 +08:00
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
sprite3->getListener()->setEnabled(false);
|
2014-03-02 15:54:36 +08:00
|
|
|
_eventDispatcher->pauseEventListenersForTarget(this, true);
|
|
|
|
|
|
|
|
auto colorLayer = LayerColor::create(Color4B(0, 0, 255, 100));
|
|
|
|
this->addChild(colorLayer, 99999);
|
|
|
|
|
2014-04-03 15:32:16 +08:00
|
|
|
auto closeItem = MenuItemFont::create("close", [=](Ref* sender){
|
2014-03-02 15:54:36 +08:00
|
|
|
colorLayer->removeFromParent();
|
|
|
|
_eventDispatcher->resumeEventListenersForTarget(this, true);
|
2014-04-03 15:32:16 +08:00
|
|
|
sprite3->getListener()->setEnabled(true);
|
2014-03-02 15:54:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
closeItem->setPosition(VisibleRect::center());
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto closeMenu = Menu::create(closeItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
closeMenu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
|
|
|
closeMenu->setPosition(Vec2::ZERO);
|
2014-03-02 15:54:36 +08:00
|
|
|
|
|
|
|
colorLayer->addChild(closeMenu);
|
|
|
|
});
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
popup->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
2014-03-02 15:54:36 +08:00
|
|
|
popup->setPosition(VisibleRect::right());
|
|
|
|
|
|
|
|
auto menu = Menu::create(popup, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
2014-03-02 15:54:36 +08:00
|
|
|
|
|
|
|
addChild(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
PauseResumeTargetTest::~PauseResumeTargetTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PauseResumeTargetTest::title() const
|
|
|
|
{
|
|
|
|
return "PauseResumeTargetTest";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PauseResumeTargetTest::subtitle() const
|
|
|
|
{
|
2014-04-03 15:32:16 +08:00
|
|
|
return "Yellow block uses fixed priority";
|
2014-03-02 15:54:36 +08:00
|
|
|
}
|
|
|
|
|
2017-05-19 14:03:51 +08:00
|
|
|
// PauseResumeTargetTest2
|
|
|
|
PauseResumeTargetTest2::PauseResumeTargetTest2()
|
|
|
|
{
|
|
|
|
MenuItemFont::getFontSize();
|
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
_touchableSprite = TouchableSprite::create();
|
|
|
|
_touchableSprite->retain();
|
|
|
|
_touchableSprite->setTexture("Images/CyanSquare.png");
|
|
|
|
_touchableSprite->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 40));
|
|
|
|
addChild(_touchableSprite);
|
|
|
|
|
|
|
|
_itemPauseTouch = MenuItemFont::create("pauseTouch", [=](Ref* sender){
|
|
|
|
_itemPauseTouch->setEnabled(false);
|
|
|
|
_itemResumeTouch->setEnabled(true);
|
|
|
|
|
|
|
|
_eventDispatcher->pauseEventListenersForTarget(_touchableSprite);
|
|
|
|
});
|
|
|
|
|
|
|
|
_itemPauseTouch->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
|
|
|
_itemPauseTouch->setPosition(VisibleRect::right() + Vec2(-150, 0));
|
|
|
|
|
|
|
|
_itemResumeTouch = MenuItemFont::create("resumeTouch", [=](Ref* sender){
|
|
|
|
_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){
|
|
|
|
_itemAddToScene->setEnabled(false);
|
|
|
|
_itemRemoveFromScene->setEnabled(true);
|
|
|
|
|
|
|
|
this->addChild(_touchableSprite);
|
|
|
|
});
|
|
|
|
|
|
|
|
_itemAddToScene->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
|
|
|
_itemAddToScene->setPosition(VisibleRect::right() + Vec2(-150, -50));
|
|
|
|
|
|
|
|
_itemRemoveFromScene = MenuItemFont::create("removeFromScene", [=](Ref* sender){
|
|
|
|
_itemAddToScene->setEnabled(true);
|
|
|
|
_itemRemoveFromScene->setEnabled(false);
|
|
|
|
_touchableSprite->removeFromParentAndCleanup(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
_itemRemoveFromScene->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
|
|
|
|
_itemRemoveFromScene->setPosition(VisibleRect::right() + Vec2(0, -50));
|
|
|
|
|
|
|
|
_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();
|
|
|
|
|
|
|
|
_touchableSprite = Sprite::create("Images/CyanSquare.png");
|
|
|
|
_touchableSprite->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 40));
|
|
|
|
addChild(_touchableSprite);
|
|
|
|
|
|
|
|
auto item = MenuItemFont::create("addListener", [=](Ref* sender){
|
|
|
|
|
|
|
|
MenuItemFont* senderItem = static_cast<MenuItemFont*>(sender);
|
|
|
|
senderItem->setEnabled(false);
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
|
|
|
|
listener->onTouchBegan = [=](Touch* touch, Event* event){
|
|
|
|
Vec2 locationInNode = _touchableSprite->convertToNodeSpace(touch->getLocation());
|
|
|
|
Size s = _touchableSprite->getContentSize();
|
|
|
|
Rect rect = Rect(0, 0, s.width, s.height);
|
|
|
|
|
|
|
|
if (rect.containsPoint(locationInNode))
|
|
|
|
{
|
|
|
|
log("TouchableSprite: onTouchBegan ...");
|
|
|
|
_touchableSprite->setColor(Color3B::RED);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
listener->onTouchEnded = [this](Touch* touch, Event* event){
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PauseResumeTargetTest3::title() const
|
|
|
|
{
|
|
|
|
return "PauseResumeTargetTest3";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PauseResumeTargetTest3::subtitle() const
|
|
|
|
{
|
|
|
|
return "Sprite should not be touchable";
|
|
|
|
}
|
|
|
|
|
2014-03-02 16:22:57 +08:00
|
|
|
// Issue4129
|
2014-02-28 15:46:48 +08:00
|
|
|
Issue4129::Issue4129()
|
|
|
|
: _bugFixed(false)
|
|
|
|
{
|
|
|
|
_customlistener = _eventDispatcher->addCustomEventListener(EVENT_COME_TO_BACKGROUND, [this](EventCustom* event){
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithSystemFont("Yeah, this issue was fixed.", "", 20);
|
2014-05-15 01:07:09 +08:00
|
|
|
label->setAnchorPoint(Vec2(0, 0.5f));
|
2017-03-20 11:24:29 +08:00
|
|
|
label->setPosition(Vec2(VisibleRect::left() + Vec2(0, 30)));
|
2014-02-28 15:46:48 +08:00
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
// After test, remove it.
|
|
|
|
_eventDispatcher->removeEventListener(_customlistener);
|
2014-03-06 10:45:29 +08:00
|
|
|
_customlistener = nullptr;
|
|
|
|
|
2014-02-28 15:46:48 +08:00
|
|
|
_bugFixed = true;
|
|
|
|
});
|
2014-03-06 10:45:29 +08:00
|
|
|
|
2014-02-28 15:46:48 +08:00
|
|
|
auto removeAllTouchItem = MenuItemFont::create("Remove All Listeners", [this](Ref* sender){
|
|
|
|
auto senderItem = static_cast<MenuItemFont*>(sender);
|
|
|
|
senderItem->setString("Only 'Reset' item could be clicked");
|
|
|
|
|
|
|
|
_eventDispatcher->removeAllEventListeners();
|
|
|
|
|
|
|
|
auto nextItem = MenuItemFont::create("Reset", [=](Ref* sender){
|
|
|
|
CCASSERT(_bugFixed, "This issue was not fixed!");
|
2015-04-03 14:31:03 +08:00
|
|
|
getTestSuite()->restartCurrTest();
|
2014-02-28 15:46:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
nextItem->setFontSizeObj(16);
|
2014-05-15 01:07:09 +08:00
|
|
|
nextItem->setPosition(VisibleRect::right() + Vec2(-100, -30));
|
2014-02-28 15:46:48 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu2 = Menu::create(nextItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu2->setPosition(Vec2(0, 0));
|
|
|
|
menu2->setAnchorPoint(Vec2(0, 0));
|
2014-02-28 15:46:48 +08:00
|
|
|
this->addChild(menu2);
|
|
|
|
|
|
|
|
// Simulate to dispatch 'come to background' event
|
|
|
|
_eventDispatcher->dispatchCustomEvent(EVENT_COME_TO_BACKGROUND);
|
|
|
|
});
|
|
|
|
|
|
|
|
removeAllTouchItem->setFontSizeObj(16);
|
2014-05-15 01:07:09 +08:00
|
|
|
removeAllTouchItem->setPosition(VisibleRect::right() + Vec2(-100, 0));
|
2014-02-28 15:46:48 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(removeAllTouchItem, nullptr);
|
2014-05-15 01:07:09 +08:00
|
|
|
menu->setPosition(Vec2(0, 0));
|
|
|
|
menu->setAnchorPoint(Vec2(0, 0));
|
2014-02-28 15:46:48 +08:00
|
|
|
addChild(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
Issue4129::~Issue4129()
|
|
|
|
{
|
2014-03-06 10:45:29 +08:00
|
|
|
if (_customlistener)
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_customlistener);
|
|
|
|
}
|
2014-02-28 15:46:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue4129::title() const
|
|
|
|
{
|
|
|
|
return "Issue 4129: Remove All Listeners";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue4129::subtitle() const
|
|
|
|
{
|
|
|
|
return "Should see 'Yeah, this issue was fixed.'";
|
|
|
|
}
|
2014-03-02 16:22:57 +08:00
|
|
|
|
|
|
|
// Issue4160
|
|
|
|
Issue4160::Issue4160()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2014-03-02 16:22:57 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite1 = TouchableSprite::create(-30);
|
2014-03-02 16:22:57 +08:00
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80, 40));
|
2014-03-02 16:22:57 +08:00
|
|
|
addChild(sprite1, -10);
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite2 = TouchableSprite::create(-20);
|
2014-03-02 16:22:57 +08:00
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
|
|
|
sprite2->removeListenerOnTouchEnded(true);
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-03-02 16:22:57 +08:00
|
|
|
addChild(sprite2, -20);
|
|
|
|
|
2014-03-02 15:54:36 +08:00
|
|
|
auto sprite3 = TouchableSprite::create(-10);
|
2014-03-02 16:22:57 +08:00
|
|
|
sprite3->setTexture("Images/YellowSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite3->setPosition(Vec2(0, 0));
|
2014-03-02 16:22:57 +08:00
|
|
|
sprite2->addChild(sprite3, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Issue4160::~Issue4160()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2014-03-28 07:00:21 +08:00
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
// DanglingNodePointersTest
|
|
|
|
class DanglingNodePointersTestSprite : public Sprite
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
typedef std::function<void (DanglingNodePointersTestSprite * sprite)> TappedCallback;
|
2014-03-28 07:00:21 +08:00
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
static DanglingNodePointersTestSprite * create(const TappedCallback & tappedCallback)
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
auto ret = new (std::nothrow) DanglingNodePointersTestSprite(tappedCallback);
|
2014-03-28 07:00:21 +08:00
|
|
|
|
|
|
|
if (ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
DanglingNodePointersTestSprite(const TappedCallback & tappedCallback)
|
2014-03-28 07:00:21 +08:00
|
|
|
:
|
|
|
|
_eventListener(nullptr),
|
|
|
|
_tappedCallback(tappedCallback)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
void onEnter() override
|
|
|
|
{
|
|
|
|
Sprite::onEnter();
|
|
|
|
|
|
|
|
_eventListener = EventListenerTouchOneByOne::create();
|
|
|
|
_eventListener->setSwallowTouches(false);
|
|
|
|
|
|
|
|
_eventListener->onTouchBegan = [this](Touch* touch, Event* event) -> bool
|
|
|
|
{
|
|
|
|
_tappedCallback(this);
|
|
|
|
return false; // Don't claim the touch so it can propagate
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventListener->onTouchEnded = [](Touch* touch, Event* event)
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
};
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(_eventListener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onExit() override
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListenersForTarget(this);
|
|
|
|
_eventListener = nullptr;
|
|
|
|
Sprite::onExit();
|
|
|
|
}
|
|
|
|
|
2014-11-26 09:53:52 +08:00
|
|
|
private:
|
|
|
|
EventListenerTouchOneByOne* _eventListener;
|
|
|
|
TappedCallback _tappedCallback;
|
2014-03-28 07:00:21 +08:00
|
|
|
};
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
DanglingNodePointersTest::DanglingNodePointersTest()
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
2014-03-28 07:38:06 +08:00
|
|
|
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1 && COCOS2D_DEBUG > 0
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2014-03-28 07:00:21 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
auto callback2 = [](DanglingNodePointersTestSprite * sprite2)
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
|
|
|
CCASSERT(false, "This should never be called because the sprite gets removed from it's parent and destroyed!");
|
|
|
|
exit(1);
|
|
|
|
};
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
auto callback1 = [callback2, origin, size](DanglingNodePointersTestSprite * sprite1)
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
2014-03-28 07:38:06 +08:00
|
|
|
DanglingNodePointersTestSprite * sprite2 = dynamic_cast<DanglingNodePointersTestSprite*>(sprite1->getChildren().at(0));
|
2014-03-28 07:00:21 +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
|
|
|
|
|
|
|
|
// Recreate sprite 1 again
|
2014-03-28 07:38:06 +08:00
|
|
|
sprite2 = DanglingNodePointersTestSprite::create(callback2);
|
2014-03-28 07:00:21 +08:00
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-03-28 07:00:21 +08:00
|
|
|
sprite1->addChild(sprite2, -20);
|
|
|
|
};
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
auto sprite1 = DanglingNodePointersTestSprite::create(callback1); // Sprite 1 will receive touch before sprite 2
|
2014-03-28 07:00:21 +08:00
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-03-28 07:00:21 +08:00
|
|
|
addChild(sprite1, -10);
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
auto sprite2 = DanglingNodePointersTestSprite::create(callback2); // Sprite 2 will be removed when sprite 1 is touched, should never receive an event.
|
2014-03-28 07:00:21 +08:00
|
|
|
sprite2->setTexture("Images/MagentaSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-03-28 07:00:21 +08:00
|
|
|
sprite1->addChild(sprite2, -20);
|
2014-03-28 07:38:06 +08:00
|
|
|
|
|
|
|
#endif
|
2014-03-28 07:00:21 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
DanglingNodePointersTest::~DanglingNodePointersTest()
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
std::string DanglingNodePointersTest::title() const
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
2014-03-28 07:38:06 +08:00
|
|
|
return "DanglingNodePointersTest";
|
2014-03-28 07:00:21 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 07:38:06 +08:00
|
|
|
std::string DanglingNodePointersTest::subtitle() const
|
2014-03-28 07:00:21 +08:00
|
|
|
{
|
2014-03-28 07:38:06 +08:00
|
|
|
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1 && COCOS2D_DEBUG > 0
|
|
|
|
return "Tap the square - should not crash!";
|
|
|
|
#else
|
|
|
|
return "For test to work, must be compiled with:\n"
|
|
|
|
"CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS == 1\n&& COCOS2D_DEBUG > 0";
|
|
|
|
#endif
|
2014-03-28 07:00:21 +08:00
|
|
|
}
|
2014-04-08 07:46:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
RegisterAndUnregisterWhileEventHanldingTest::RegisterAndUnregisterWhileEventHanldingTest()
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 origin = Director::getInstance()->getVisibleOrigin();
|
2014-04-08 07:46:03 +08:00
|
|
|
Size size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto callback1 = [=](DanglingNodePointersTestSprite * sprite)
|
|
|
|
{
|
|
|
|
auto callback2 = [](DanglingNodePointersTestSprite * sprite)
|
|
|
|
{
|
|
|
|
CCASSERT(false, "This should never get called!");
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoreleasePool pool;
|
|
|
|
|
|
|
|
auto sprite2 = DanglingNodePointersTestSprite::create(callback2);
|
|
|
|
sprite2->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-04-08 07:46:03 +08:00
|
|
|
|
|
|
|
addChild(sprite2, 0);
|
|
|
|
removeChild(sprite2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto sprite1 = DanglingNodePointersTestSprite::create(callback1);
|
|
|
|
sprite1->setTexture("Images/CyanSquare.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite1->setPosition(origin+Vec2(size.width/2, size.height/2));
|
2014-04-08 07:46:03 +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!";
|
|
|
|
}
|
2015-01-13 18:37:45 +08:00
|
|
|
|
2016-09-21 11:32:33 +08:00
|
|
|
//
|
|
|
|
WindowEventsTest::WindowEventsTest()
|
|
|
|
{
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
|
|
|
|
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! >>> ");
|
|
|
|
});
|
|
|
|
#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.";
|
|
|
|
#else
|
|
|
|
return "Unsupported platform.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-01 14:39:35 +08:00
|
|
|
// https://github.com/cocos2d/cocos2d-x/issues/8194
|
|
|
|
Issue8194::Issue8194()
|
|
|
|
{
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
auto size = Director::getInstance()->getVisibleSize();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerA, nodeA);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerB, nodeB);
|
|
|
|
|
|
|
|
nodesAdded = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
menuItem->setPosition(origin.x + size.width/2, origin.y + size.height/2);
|
|
|
|
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.'";
|
|
|
|
}
|
|
|
|
|
2015-01-13 18:37:45 +08:00
|
|
|
Issue9898::Issue9898()
|
|
|
|
{
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
auto size = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
auto nodeA = Node::create();
|
|
|
|
addChild(nodeA);
|
|
|
|
|
|
|
|
_listener = cocos2d::EventListenerCustom::create("Issue9898", [&](cocos2d::EventCustom *event){
|
|
|
|
_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);
|
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
addChild(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue9898::title() const
|
|
|
|
{
|
2015-07-01 14:39:35 +08:00
|
|
|
return "Issue 9898";
|
2015-01-13 18:37:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Issue9898::subtitle() const
|
|
|
|
{
|
|
|
|
return "Should not crash if dispatch event after remove\n event listener in callback";
|
|
|
|
}
|