axmol/tests/cpp-tests/Classes/MotionStreakTest/MotionStreakTest.cpp

273 lines
8.0 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
https://axis-project.github.io/
2021-12-28 16:06:23 +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:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +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.
****************************************************************************/
#include "MotionStreakTest.h"
#include "../testResource.h"
USING_NS_AX;
2021-12-28 16:06:23 +08:00
enum
{
kTagLabel = 1,
kTagSprite1 = 2,
kTagSprite2 = 3,
2012-06-12 16:56:34 +08:00
};
MotionStreakTests::MotionStreakTests()
{
ADD_TEST_CASE(MotionStreakTest1);
ADD_TEST_CASE(MotionStreakTest2);
ADD_TEST_CASE(Issue1358);
ADD_TEST_CASE(Issue12226);
}
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest1
//
//------------------------------------------------------------------
2010-08-25 17:02:58 +08:00
void MotionStreakTest1::onEnter()
{
MotionStreakTest::onEnter();
2010-08-25 17:02:58 +08:00
auto s = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
// the root object just rotates around
_root = Sprite::create(s_pathR1);
addChild(_root, 1);
2021-12-28 16:06:23 +08:00
_root->setPosition(Vec2(s.width / 2, s.height / 2));
// the target object is offset from root, and the streak is moved to follow it
_target = Sprite::create(s_pathR1);
_root->addChild(_target);
2021-12-28 16:06:23 +08:00
_target->setPosition(Vec2(s.width / 4, 0.0f));
// create the streak object and add it to the scene
_streak = MotionStreak::create(2, 3, 32, Color3B::GREEN, s_streak);
addChild(_streak);
2017-02-24 12:45:41 +08:00
// schedule an update on each frame so we can synchronize the streak with the target
2022-07-15 19:17:01 +08:00
schedule(AX_SCHEDULE_SELECTOR(MotionStreakTest1::onUpdate));
2021-12-28 16:06:23 +08:00
auto a1 = RotateBy::create(2, 360);
2010-08-25 17:02:58 +08:00
auto action1 = RepeatForever::create(a1);
2021-12-28 16:06:23 +08:00
auto motion = MoveBy::create(2, Vec2(100, 0));
_root->runAction(RepeatForever::create(Sequence::create(motion, motion->reverse(), nullptr)));
_root->runAction(action1);
2012-03-22 14:32:32 +08:00
auto colorAction = RepeatForever::create(Sequence::create(
2021-12-28 16:06:23 +08:00
TintTo::create(0.2f, 255, 0, 0), TintTo::create(0.2f, 0, 255, 0), TintTo::create(0.2f, 0, 0, 255),
TintTo::create(0.2f, 0, 255, 255), TintTo::create(0.2f, 255, 255, 0), TintTo::create(0.2f, 255, 0, 255),
TintTo::create(0.2f, 255, 255, 255), nullptr));
2012-03-22 14:32:32 +08:00
_streak->runAction(colorAction);
2010-08-25 17:02:58 +08:00
}
2012-06-08 13:55:28 +08:00
void MotionStreakTest1::onUpdate(float delta)
{
2021-12-28 16:06:23 +08:00
_streak->setPosition(_target->convertToWorldSpace(Vec2::ZERO));
}
std::string MotionStreakTest1::title() const
2010-08-25 17:02:58 +08:00
{
return "MotionStreak test 1";
}
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest2
//
//------------------------------------------------------------------
2010-08-25 17:02:58 +08:00
void MotionStreakTest2::onEnter()
{
MotionStreakTest::onEnter();
2010-08-25 17:02:58 +08:00
2021-12-28 16:06:23 +08:00
auto listener = EventListenerTouchAllAtOnce::create();
2022-07-15 19:17:01 +08:00
listener->onTouchesMoved = AX_CALLBACK_2(MotionStreakTest2::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
2021-12-28 16:06:23 +08:00
auto s = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
// create the streak object and add it to the scene
2021-12-28 16:06:23 +08:00
_streak = MotionStreak::create(3, 3, 64, Color3B::WHITE, s_streak);
addChild(_streak);
2021-12-28 16:06:23 +08:00
_streak->setPosition(Vec2(s.width / 2, s.height / 2));
2010-08-25 17:02:58 +08:00
}
void MotionStreakTest2::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
2010-08-25 17:02:58 +08:00
{
auto touchLocation = touches[0]->getLocation();
2021-12-28 16:06:23 +08:00
_streak->setPosition(touchLocation);
}
std::string MotionStreakTest2::title() const
2010-08-25 17:02:58 +08:00
{
return "MotionStreak test";
}
2019-05-22 09:52:40 +08:00
std::string MotionStreakTest2::subtitle() const
{
return "Need Touch Movement";
}
2012-06-12 16:56:34 +08:00
//------------------------------------------------------------------
//
// Issue1358
//
//------------------------------------------------------------------
void Issue1358::onEnter()
{
MotionStreakTest::onEnter();
2021-12-28 16:06:23 +08:00
2012-06-12 16:56:34 +08:00
// ask director the the window size
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
_streak = MotionStreak::create(2.0f, 1.0f, 50.0f, Color3B(255, 255, 0), "Images/Icon.png");
addChild(_streak);
2021-12-28 16:06:23 +08:00
_center = Vec2(size.width / 2, size.height / 2);
_radius = size.width / 3;
_angle = 0.0f;
2022-07-15 19:17:01 +08:00
schedule(AX_SCHEDULE_SELECTOR(Issue1358::update), 0);
2012-06-12 16:56:34 +08:00
}
void Issue1358::update(float dt)
{
_angle += 1.0f;
2021-12-28 16:06:23 +08:00
_streak->setPosition(
Vec2(_center.x + cosf(_angle / 180 * M_PI) * _radius, _center.y + sinf(_angle / 180 * M_PI) * _radius));
2012-06-12 16:56:34 +08:00
}
std::string Issue1358::title() const
2012-06-12 16:56:34 +08:00
{
return "Issue 1358";
}
std::string Issue1358::subtitle() const
2012-06-12 16:56:34 +08:00
{
return "The tail should use the texture";
}
//------------------------------------------------------------------
//
// Issue12226
//
//------------------------------------------------------------------
void Issue12226::onEnter()
{
MotionStreakTest::onEnter();
// ask director the the window size
auto size = Director::getInstance()->getWinSize();
2021-12-28 16:06:23 +08:00
auto radius = size.width / 3;
auto outer = Sprite::create("Images/grossini.png");
2021-12-28 16:06:23 +08:00
outer->setPosition(size / 2);
addChild(outer);
_streak = MotionStreak::create(1.0f, 3, radius * 1.5f, Color3B(0xA0, 0xA0, 0xA0), "ccb/particle-smoke.png");
2021-12-28 16:06:23 +08:00
// motionStreak->setOpacity(0x70);
_streak->setPosition(outer->getPosition());
this->addChild(_streak, outer->getLocalZOrder() - 1);
outer->setUserData(_streak);
const uint32_t length = (radius * 0.95);
std::function<void(float)> updateMotionStreak = [=](float dt) {
2021-12-28 16:06:23 +08:00
Vec2 position =
2022-07-15 19:17:01 +08:00
Vec2(outer->getPositionX() + length * cosf(-1 * AX_DEGREES_TO_RADIANS(outer->getRotation() + 90.0f)),
outer->getPositionY() + length * sinf(-1 * AX_DEGREES_TO_RADIANS(outer->getRotation() + 90.0f)));
_streak->setPosition(position);
};
2022-07-15 19:17:01 +08:00
outer->schedule(updateMotionStreak, 1 / 240.0f, AX_REPEAT_FOREVER, 0, "motion1scheduler");
2021-12-28 16:06:23 +08:00
auto rot = RotateBy::create(2, 360);
auto forever = RepeatForever::create(rot);
outer->runAction(forever);
}
std::string Issue12226::title() const
{
return "Github Issue 12226";
}
std::string Issue12226::subtitle() const
{
return "Image should look without artifacts";
}
2010-08-25 17:02:58 +08:00
//------------------------------------------------------------------
//
// MotionStreakTest
//
//------------------------------------------------------------------
2021-12-28 16:06:23 +08:00
MotionStreakTest::MotionStreakTest() {}
2021-12-28 16:06:23 +08:00
MotionStreakTest::~MotionStreakTest() {}
std::string MotionStreakTest::title() const
2010-08-25 17:02:58 +08:00
{
return "No title";
2010-08-25 17:02:58 +08:00
}
std::string MotionStreakTest::subtitle() const
2012-06-12 16:56:34 +08:00
{
return "";
}
2010-08-25 17:02:58 +08:00
void MotionStreakTest::onEnter()
{
TestCase::onEnter();
2010-08-25 17:02:58 +08:00
auto s = Director::getInstance()->getWinSize();
2010-08-25 17:02:58 +08:00
2022-07-15 19:17:01 +08:00
auto itemMode = MenuItemToggle::createWithCallback(AX_CALLBACK_1(MotionStreakTest::modeCallback, this),
2021-12-28 16:06:23 +08:00
MenuItemFont::create("Use High Quality Mode"),
MenuItemFont::create("Use Fast Mode"), nullptr);
2012-03-22 14:32:32 +08:00
auto menuMode = Menu::create(itemMode, nullptr);
addChild(menuMode);
2012-03-22 14:32:32 +08:00
2021-12-28 16:06:23 +08:00
menuMode->setPosition(Vec2(s.width / 2, s.height / 4));
2012-03-22 14:32:32 +08:00
}
2021-12-28 16:06:23 +08:00
void MotionStreakTest::modeCallback(Ref* pSender)
2012-03-22 14:32:32 +08:00
{
bool fastMode = _streak->isFastMode();
2021-12-28 16:06:23 +08:00
_streak->setFastMode(!fastMode);
2010-08-25 17:02:58 +08:00
}