2014-01-14 06:48:12 +08:00
/****************************************************************************
Copyright ( c ) 2012 cocos2d - x . org
2018-01-29 16:25:32 +08:00
Copyright ( c ) 2013 - 2016 Chukong Technologies Inc .
Copyright ( c ) 2017 - 2018 Xiamen Yaji Software Co . , Ltd .
2014-01-14 06:48:12 +08:00
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 .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-03-21 13:36:24 +08:00
# include "NodeTest.h"
2014-07-02 17:07:36 +08:00
# include <regex>
2012-03-21 13:36:24 +08:00
# include "../testResource.h"
2015-04-09 08:37:30 +08:00
USING_NS_CC ;
2010-08-26 15:09:02 +08:00
enum
{
2012-04-19 14:35:52 +08:00
kTagSprite1 = 1 ,
kTagSprite2 = 2 ,
kTagSprite3 = 3 ,
kTagSlider ,
2012-03-21 13:36:24 +08:00
} ;
2011-07-08 15:57:46 +08:00
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
// TestCocosNodeDemo
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
2015-04-03 14:31:03 +08:00
CocosNodeTests : : CocosNodeTests ( )
{
//ADD_TEST_CASE(CameraTest1);
// TODO: Camera has been removed from CCNode; add new feature to support it
// ADD_TEST_CASE(CameraTest2);
//ADD_TEST_CASE(CameraCenterTest);
ADD_TEST_CASE ( NodeTest2 ) ;
ADD_TEST_CASE ( NodeTest4 ) ;
ADD_TEST_CASE ( NodeTest5 ) ;
ADD_TEST_CASE ( NodeTest6 ) ;
ADD_TEST_CASE ( StressTest1 ) ;
ADD_TEST_CASE ( StressTest2 ) ;
ADD_TEST_CASE ( NodeToWorld ) ;
ADD_TEST_CASE ( NodeToWorld3D ) ;
ADD_TEST_CASE ( SchedulerTest1 ) ;
ADD_TEST_CASE ( SchedulerCallbackTest ) ;
2019-02-20 17:07:31 +08:00
ADD_TEST_CASE ( CameraOrbitTest ) ;
2015-04-03 14:31:03 +08:00
// TODO: Camera has been removed from CCNode; add new feature to support it
//ADD_TEST_CASE(CameraZoomTest);
ADD_TEST_CASE ( ConvertToNode ) ;
ADD_TEST_CASE ( NodeOpaqueTest ) ;
ADD_TEST_CASE ( NodeNonOpaqueTest ) ;
ADD_TEST_CASE ( NodeGlobalZValueTest ) ;
ADD_TEST_CASE ( NodeNormalizedPositionTest1 ) ;
ADD_TEST_CASE ( NodeNormalizedPositionTest2 ) ;
ADD_TEST_CASE ( NodeNormalizedPositionBugTest ) ;
ADD_TEST_CASE ( NodeNameTest ) ;
2016-07-15 05:58:04 +08:00
ADD_TEST_CASE ( Issue16100Test ) ;
2016-10-31 10:29:04 +08:00
ADD_TEST_CASE ( Issue16735Test ) ;
2010-08-26 15:09:02 +08:00
}
2012-03-21 13:36:24 +08:00
TestCocosNodeDemo : : TestCocosNodeDemo ( void )
{
}
TestCocosNodeDemo : : ~ TestCocosNodeDemo ( void )
{
}
2013-12-19 05:52:10 +08:00
std : : string TestCocosNodeDemo : : title ( ) const
2010-08-26 15:09:02 +08:00
{
2014-03-11 13:58:43 +08:00
return " Node Test " ;
2010-08-26 15:09:02 +08:00
}
//------------------------------------------------------------------
//
2015-04-03 14:31:03 +08:00
// NodeTest2
2010-08-26 15:09:02 +08:00
//
//------------------------------------------------------------------
2015-04-03 14:31:03 +08:00
void NodeTest2 : : onEnter ( )
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
TestCocosNodeDemo : : onEnter ( ) ;
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
auto sp2 = Sprite : : create ( s_pathSister2 ) ;
auto sp3 = Sprite : : create ( s_pathSister1 ) ;
auto sp4 = Sprite : : create ( s_pathSister2 ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
sp1 - > setPosition ( Vec2 ( 100.0f , s . height / 2 ) ) ;
sp2 - > setPosition ( Vec2 ( 380.0f , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
addChild ( sp1 ) ;
addChild ( sp2 ) ;
sp3 - > setScale ( 0.25f ) ;
sp4 - > setScale ( 0.25f ) ;
sp1 - > addChild ( sp3 ) ;
sp2 - > addChild ( sp4 ) ;
2013-08-16 16:05:27 +08:00
auto a1 = RotateBy : : create ( 2 , 360 ) ;
auto a2 = ScaleBy : : create ( 2 , 2 ) ;
2012-04-19 14:35:52 +08:00
2014-07-10 00:45:27 +08:00
auto action1 = RepeatForever : : create ( Sequence : : create ( a1 , a2 , a2 - > reverse ( ) , nullptr ) ) ;
2013-08-16 16:05:27 +08:00
auto action2 = RepeatForever : : create ( Sequence : : create (
2013-06-19 07:12:28 +08:00
a1 - > clone ( ) ,
a2 - > clone ( ) ,
a2 - > reverse ( ) ,
2014-07-10 00:45:27 +08:00
nullptr )
2013-06-19 07:12:28 +08:00
) ;
2012-04-19 14:35:52 +08:00
2014-05-15 01:07:09 +08:00
sp2 - > setAnchorPoint ( Vec2 ( 0 , 0 ) ) ;
2012-04-19 14:35:52 +08:00
sp1 - > runAction ( action1 ) ;
sp2 - > runAction ( action2 ) ;
2010-08-26 15:09:02 +08:00
}
2015-04-03 14:31:03 +08:00
std : : string NodeTest2 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " anchorPoint and children " ;
2010-08-26 15:09:02 +08:00
}
2012-03-21 13:36:24 +08:00
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
2015-04-03 14:31:03 +08:00
// NodeTest4
2010-08-26 15:09:02 +08:00
//
//------------------------------------------------------------------
2012-04-19 14:35:52 +08:00
# define SID_DELAY2 1
# define SID_DELAY4 2
2010-08-26 15:09:02 +08:00
2015-04-03 14:31:03 +08:00
NodeTest4 : : NodeTest4 ( )
2012-03-21 13:36:24 +08:00
{
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
auto sp2 = Sprite : : create ( s_pathSister2 ) ;
2012-04-19 14:35:52 +08:00
2014-05-15 01:07:09 +08:00
sp1 - > setPosition ( Vec2 ( 100 , 160 ) ) ;
sp2 - > setPosition ( Vec2 ( 380 , 160 ) ) ;
2012-04-19 14:35:52 +08:00
addChild ( sp1 , 0 , 2 ) ;
addChild ( sp2 , 0 , 3 ) ;
2015-04-03 14:31:03 +08:00
schedule ( CC_CALLBACK_1 ( NodeTest4 : : delay2 , this ) , 2.0f , " delay2_key " ) ;
schedule ( CC_CALLBACK_1 ( NodeTest4 : : delay4 , this ) , 4.0f , " delay4_key " ) ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2015-04-03 14:31:03 +08:00
void NodeTest4 : : delay2 ( float dt )
2010-08-26 15:09:02 +08:00
{
2013-08-16 16:05:27 +08:00
auto node = static_cast < Sprite * > ( getChildByTag ( 2 ) ) ;
auto action1 = RotateBy : : create ( 1 , 360 ) ;
2012-04-19 14:35:52 +08:00
node - > runAction ( action1 ) ;
2010-08-26 15:09:02 +08:00
}
2015-04-03 14:31:03 +08:00
void NodeTest4 : : delay4 ( float dt )
2010-08-26 15:09:02 +08:00
{
2014-10-04 08:11:39 +08:00
unschedule ( " delay4_key " ) ;
2012-04-19 14:35:52 +08:00
removeChildByTag ( 3 , false ) ;
2010-08-26 15:09:02 +08:00
}
2015-04-03 14:31:03 +08:00
std : : string NodeTest4 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " tags " ;
2010-08-26 15:09:02 +08:00
}
2012-03-21 13:36:24 +08:00
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
2015-04-03 14:31:03 +08:00
// NodeTest5
2010-08-26 15:09:02 +08:00
//
//------------------------------------------------------------------
2015-04-03 14:31:03 +08:00
NodeTest5 : : NodeTest5 ( )
2012-03-21 13:36:24 +08:00
{
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
auto sp2 = Sprite : : create ( s_pathSister2 ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
sp1 - > setPosition ( Vec2 ( 100.0f , 160.0f ) ) ;
sp2 - > setPosition ( Vec2 ( 380.0f , 160.0f ) ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
auto rot = RotateBy : : create ( 2.0f , 360.0f ) ;
2013-08-16 16:05:27 +08:00
auto rot_back = rot - > reverse ( ) ;
2014-07-10 00:45:27 +08:00
auto forever = RepeatForever : : create ( Sequence : : create ( rot , rot_back , nullptr ) ) ;
2013-08-16 16:05:27 +08:00
auto forever2 = forever - > clone ( ) ;
2012-04-19 14:35:52 +08:00
forever - > setTag ( 101 ) ;
forever2 - > setTag ( 102 ) ;
addChild ( sp1 , 0 , kTagSprite1 ) ;
addChild ( sp2 , 0 , kTagSprite2 ) ;
sp1 - > runAction ( forever ) ;
sp2 - > runAction ( forever2 ) ;
2015-04-03 14:31:03 +08:00
schedule ( CC_CALLBACK_1 ( NodeTest5 : : addAndRemove , this ) , 2.0f , " add_and_remove_key " ) ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2015-04-03 14:31:03 +08:00
void NodeTest5 : : addAndRemove ( float dt )
2010-08-26 15:09:02 +08:00
{
2013-08-16 16:05:27 +08:00
auto sp1 = getChildByTag ( kTagSprite1 ) ;
auto sp2 = getChildByTag ( kTagSprite2 ) ;
2010-08-26 15:09:02 +08:00
2012-04-19 14:35:52 +08:00
sp1 - > retain ( ) ;
sp2 - > retain ( ) ;
removeChild ( sp1 , false ) ;
removeChild ( sp2 , true ) ;
addChild ( sp1 , 0 , kTagSprite1 ) ;
addChild ( sp2 , 0 , kTagSprite2 ) ;
sp1 - > release ( ) ;
sp2 - > release ( ) ;
2010-08-26 15:09:02 +08:00
}
2015-04-03 14:31:03 +08:00
std : : string NodeTest5 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " remove and cleanup " ;
2010-08-26 15:09:02 +08:00
}
2012-03-21 13:36:24 +08:00
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
2015-04-03 14:31:03 +08:00
// NodeTest6
2010-08-26 15:09:02 +08:00
//
//------------------------------------------------------------------
2015-04-03 14:31:03 +08:00
NodeTest6 : : NodeTest6 ( )
2012-03-21 13:36:24 +08:00
{
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
auto sp11 = Sprite : : create ( s_pathSister1 ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto sp2 = Sprite : : create ( s_pathSister2 ) ;
auto sp21 = Sprite : : create ( s_pathSister2 ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
sp1 - > setPosition ( Vec2 ( 100.0f , 160.0f ) ) ;
sp2 - > setPosition ( Vec2 ( 380.0f , 160.0f ) ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
auto rot = RotateBy : : create ( 2.0f , 360.0f ) ;
2013-08-16 16:05:27 +08:00
auto rot_back = rot - > reverse ( ) ;
2014-07-10 00:45:27 +08:00
auto forever1 = RepeatForever : : create ( Sequence : : create ( rot , rot_back , nullptr ) ) ;
2013-08-16 16:05:27 +08:00
auto forever11 = forever1 - > clone ( ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto forever2 = forever1 - > clone ( ) ;
auto forever21 = forever1 - > clone ( ) ;
2012-04-19 14:35:52 +08:00
addChild ( sp1 , 0 , kTagSprite1 ) ;
sp1 - > addChild ( sp11 ) ;
addChild ( sp2 , 0 , kTagSprite2 ) ;
sp2 - > addChild ( sp21 ) ;
sp1 - > runAction ( forever1 ) ;
sp11 - > runAction ( forever11 ) ;
sp2 - > runAction ( forever2 ) ;
sp21 - > runAction ( forever21 ) ;
2015-04-03 14:31:03 +08:00
schedule ( CC_CALLBACK_1 ( NodeTest6 : : addAndRemove , this ) , 2.0f , " add_and_remove_key " ) ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2015-04-03 14:31:03 +08:00
void NodeTest6 : : addAndRemove ( float dt )
2010-08-26 15:09:02 +08:00
{
2013-08-16 16:05:27 +08:00
auto sp1 = getChildByTag ( kTagSprite1 ) ;
auto sp2 = getChildByTag ( kTagSprite2 ) ;
2010-08-26 15:09:02 +08:00
2012-04-19 14:35:52 +08:00
sp1 - > retain ( ) ;
sp2 - > retain ( ) ;
removeChild ( sp1 , false ) ;
removeChild ( sp2 , true ) ;
addChild ( sp1 , 0 , kTagSprite1 ) ;
addChild ( sp2 , 0 , kTagSprite2 ) ;
sp1 - > release ( ) ;
sp2 - > release ( ) ;
2010-08-26 15:09:02 +08:00
}
2015-04-03 14:31:03 +08:00
std : : string NodeTest6 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " remove/cleanup with children " ;
2010-08-26 15:09:02 +08:00
}
2014-03-11 13:58:43 +08:00
//------------------------------------------------------------------
2010-08-26 15:09:02 +08:00
//
// StressTest1
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
StressTest1 : : StressTest1 ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2010-08-26 15:09:02 +08:00
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
2012-04-19 14:35:52 +08:00
addChild ( sp1 , 0 , kTagSprite1 ) ;
2014-05-15 01:07:09 +08:00
sp1 - > setPosition ( Vec2 ( s . width / 2 , s . height / 2 ) ) ;
2010-08-26 15:09:02 +08:00
2014-10-04 08:11:39 +08:00
schedule ( CC_CALLBACK_1 ( StressTest1 : : shouldNotCrash , this ) , 1.0f , " should_not_crash_key " ) ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2012-06-08 13:55:28 +08:00
void StressTest1 : : shouldNotCrash ( float dt )
2010-08-26 15:09:02 +08:00
{
2014-10-04 08:11:39 +08:00
unschedule ( " should_not_crash_key " ) ;
2010-08-26 15:09:02 +08:00
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2010-08-26 15:09:02 +08:00
2012-04-19 14:35:52 +08:00
// if the node has timers, it crashes
2013-08-16 16:05:27 +08:00
auto explosion = ParticleSun : : create ( ) ;
2013-11-07 21:48:39 +08:00
explosion - > setTexture ( Director : : getInstance ( ) - > getTextureCache ( ) - > addImage ( " Images/fire.png " ) ) ;
2012-04-19 14:35:52 +08:00
// if it doesn't, it works Ok.
2013-08-16 16:05:27 +08:00
// auto explosion = [Sprite create:@"grossinis_sister2.png");
2010-08-26 15:09:02 +08:00
2014-05-15 01:07:09 +08:00
explosion - > setPosition ( Vec2 ( s . width / 2 , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:17:10 +08:00
runAction ( Sequence : : create (
RotateBy : : create ( 2 , 360 ) ,
2013-07-16 03:43:22 +08:00
CallFuncN : : create ( CC_CALLBACK_1 ( StressTest1 : : removeMe , this ) ) ,
2014-07-10 00:45:27 +08:00
nullptr ) ) ;
2012-04-19 14:35:52 +08:00
addChild ( explosion ) ;
2010-08-26 15:09:02 +08:00
}
// remove
2013-06-20 14:17:10 +08:00
void StressTest1 : : removeMe ( Node * node )
2015-04-09 15:43:01 +08:00
{
2015-04-03 14:31:03 +08:00
getTestSuite ( ) - > enterNextTest ( ) ;
2010-08-26 15:09:02 +08:00
}
2014-02-28 23:02:22 +08:00
std : : string StressTest1 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " stress test #1: no crashes " ;
2010-08-26 15:09:02 +08:00
}
//------------------------------------------------------------------
//
2015-04-03 14:31:03 +08:00
// StressNodeTest2
2010-08-26 15:09:02 +08:00
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
StressTest2 : : StressTest2 ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto sublayer = Layer : : create ( ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto sp1 = Sprite : : create ( s_pathSister1 ) ;
2019-09-24 11:31:35 +08:00
sp1 - > setPosition ( Vec2 ( 80.0f , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
2019-09-24 11:31:35 +08:00
auto move = MoveBy : : create ( 3 , Vec2 ( 350.0f , 0.0f ) ) ;
2013-08-16 16:05:27 +08:00
auto move_ease_inout3 = EaseInOut : : create ( move - > clone ( ) , 2.0f ) ;
auto move_ease_inout_back3 = move_ease_inout3 - > reverse ( ) ;
2014-07-10 00:45:27 +08:00
auto seq3 = Sequence : : create ( move_ease_inout3 , move_ease_inout_back3 , nullptr ) ;
2013-06-20 14:17:10 +08:00
sp1 - > runAction ( RepeatForever : : create ( seq3 ) ) ;
2012-04-19 14:35:52 +08:00
sublayer - > addChild ( sp1 , 1 ) ;
2013-08-16 16:05:27 +08:00
auto fire = ParticleFire : : create ( ) ;
2013-11-07 21:48:39 +08:00
fire - > setTexture ( Director : : getInstance ( ) - > getTextureCache ( ) - > addImage ( " Images/fire.png " ) ) ;
2019-09-24 11:31:35 +08:00
fire - > setPosition ( Vec2 ( 80.0f , s . height / 2 - 50 ) ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto copy_seq3 = seq3 - > clone ( ) ;
2016-10-31 10:29:04 +08:00
2013-06-20 14:17:10 +08:00
fire - > runAction ( RepeatForever : : create ( copy_seq3 ) ) ;
2012-04-19 14:35:52 +08:00
sublayer - > addChild ( fire , 2 ) ;
2014-10-04 08:11:39 +08:00
schedule ( CC_CALLBACK_1 ( StressTest2 : : shouldNotLeak , this ) , 6.0f , " should_not_leak_key " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sublayer , 0 , kTagSprite1 ) ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2012-06-08 13:55:28 +08:00
void StressTest2 : : shouldNotLeak ( float dt )
2010-08-26 15:09:02 +08:00
{
2014-10-04 08:11:39 +08:00
unschedule ( " should_not_leak_key " ) ;
2013-08-16 16:05:27 +08:00
auto sublayer = static_cast < Layer * > ( getChildByTag ( kTagSprite1 ) ) ;
2012-04-19 14:35:52 +08:00
sublayer - > removeAllChildrenWithCleanup ( true ) ;
2010-08-26 15:09:02 +08:00
}
2014-02-28 23:02:22 +08:00
std : : string StressTest2 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " stress test #2: no leaks " ;
2010-08-26 15:09:02 +08:00
}
//------------------------------------------------------------------
//
// SchedulerTest1
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
SchedulerTest1 : : SchedulerTest1 ( )
{
2013-08-16 16:05:27 +08:00
auto layer = Layer : : create ( ) ;
2014-01-22 13:47:29 +08:00
//CCLOG("retain count after init is %d", layer->getReferenceCount()); // 1
2012-04-19 14:35:52 +08:00
addChild ( layer , 0 ) ;
2014-01-22 13:47:29 +08:00
//CCLOG("retain count after addChild is %d", layer->getReferenceCount()); // 2
2012-04-19 14:35:52 +08:00
2014-10-04 08:11:39 +08:00
layer - > schedule ( CC_CALLBACK_1 ( SchedulerTest1 : : doSomething , this ) , " do_something_key " ) ;
2017-02-10 10:35:52 +08:00
//CCLOG("retain count after schedule is %d", layer->getReferenceCount()); // 3 : (objective-c version), but win32 version is still 2, because Timer class don't save target.
2012-04-19 14:35:52 +08:00
2014-10-04 08:11:39 +08:00
layer - > unschedule ( " do_something_key " ) ;
2014-01-22 13:47:29 +08:00
//CCLOG("retain count after unschedule is %d", layer->getReferenceCount()); // STILL 3! (win32 is '2')
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
2012-06-08 13:55:28 +08:00
void SchedulerTest1 : : doSomething ( float dt )
2010-08-26 15:09:02 +08:00
{
}
2014-02-28 23:02:22 +08:00
std : : string SchedulerTest1 : : subtitle ( ) const
2010-08-26 15:09:02 +08:00
{
2012-04-19 14:35:52 +08:00
return " cocosnode scheduler test #1 " ;
2010-08-26 15:09:02 +08:00
}
2014-10-03 12:25:55 +08:00
//------------------------------------------------------------------
//
// SchedulerCallbackTest
//
//------------------------------------------------------------------
SchedulerCallbackTest : : SchedulerCallbackTest ( )
{
auto node = Node : : create ( ) ;
addChild ( node , 0 ) ;
node - > setName ( " a node " ) ;
_total = 0 ;
node - > schedule ( [ & ] ( float dt ) {
_total + = dt ;
log ( " hello world: %f - total: %f " , dt , _total ) ;
}
, 0.5
, " some_key " ) ;
node - > scheduleOnce ( [ & ] ( float dt ) {
// the local variable "node" will go out of scope, so I have to get it from "this"
auto anode = this - > getChildByName ( " a node " ) ;
anode - > unschedule ( " some_key " ) ;
}
, 5
, " ignore_key " ) ;
}
void SchedulerCallbackTest : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
log ( " --onEnter-- Must be called before the scheduled lambdas " ) ;
}
std : : string SchedulerCallbackTest : : subtitle ( ) const
{
return " Node scheduler with lambda " ;
}
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
// NodeToWorld
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
NodeToWorld : : NodeToWorld ( )
{
2012-04-19 14:35:52 +08:00
//
// This code tests that nodeToParent works OK:
// - It tests different anchor Points
// - It tests different children anchor points
2013-08-16 16:05:27 +08:00
auto back = Sprite : : create ( s_back3 ) ;
2012-04-19 14:35:52 +08:00
addChild ( back , - 10 ) ;
2014-05-15 01:07:09 +08:00
back - > setAnchorPoint ( Vec2 ( 0 , 0 ) ) ;
2013-08-16 16:05:27 +08:00
auto backSize = back - > getContentSize ( ) ;
2012-04-19 14:35:52 +08:00
2013-08-16 16:05:27 +08:00
auto item = MenuItemImage : : create ( s_PlayNormal , s_PlaySelect ) ;
2014-07-10 00:45:27 +08:00
auto menu = Menu : : create ( item , nullptr ) ;
2012-04-19 14:35:52 +08:00
menu - > alignItemsVertically ( ) ;
2014-05-15 01:07:09 +08:00
menu - > setPosition ( Vec2 ( backSize . width / 2 , backSize . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
back - > addChild ( menu ) ;
2019-09-24 11:31:35 +08:00
auto rot = RotateBy : : create ( 5.0f , 360.0f ) ;
2013-08-16 16:05:27 +08:00
auto fe = RepeatForever : : create ( rot ) ;
2012-04-19 14:35:52 +08:00
item - > runAction ( fe ) ;
2019-09-24 11:31:35 +08:00
auto move = MoveBy : : create ( 3.0f , Vec2 ( 200.0f , 0.0f ) ) ;
2013-08-16 16:05:27 +08:00
auto move_back = move - > reverse ( ) ;
2014-07-10 00:45:27 +08:00
auto seq = Sequence : : create ( move , move_back , nullptr ) ;
2013-08-16 16:05:27 +08:00
auto fe2 = RepeatForever : : create ( seq ) ;
2012-04-19 14:35:52 +08:00
back - > runAction ( fe2 ) ;
2012-03-21 13:36:24 +08:00
}
2014-02-28 23:02:22 +08:00
std : : string NodeToWorld : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " nodeToParent transform " ;
2012-03-21 13:36:24 +08:00
}
2014-01-10 06:26:36 +08:00
//------------------------------------------------------------------
//
// NodeToWorld3D
//
//------------------------------------------------------------------
NodeToWorld3D : : NodeToWorld3D ( )
{
//
// This code tests that nodeToParent works OK:
// - It tests different anchor Points
// - It tests different children anchor points
Size s = Director : : getInstance ( ) - > getWinSize ( ) ;
auto parent = Node : : create ( ) ;
parent - > setContentSize ( s ) ;
2019-09-24 11:31:35 +08:00
parent - > setAnchorPoint ( Vec2 ( 0.5f , 0.5f ) ) ;
2014-01-10 06:26:36 +08:00
parent - > setPosition ( s . width / 2 , s . height / 2 ) ;
this - > addChild ( parent ) ;
auto back = Sprite : : create ( s_back3 ) ;
parent - > addChild ( back , - 10 ) ;
2019-09-24 11:31:35 +08:00
back - > setAnchorPoint ( Vec2 ( 0.0f , 0.0f ) ) ;
2014-01-10 06:26:36 +08:00
auto backSize = back - > getContentSize ( ) ;
auto item = MenuItemImage : : create ( s_PlayNormal , s_PlaySelect ) ;
2014-07-10 00:45:27 +08:00
auto menu = Menu : : create ( item , nullptr ) ;
2014-01-10 06:26:36 +08:00
menu - > alignItemsVertically ( ) ;
2014-05-15 01:07:09 +08:00
menu - > setPosition ( Vec2 ( backSize . width / 2 , backSize . height / 2 ) ) ;
2014-01-10 06:26:36 +08:00
back - > addChild ( menu ) ;
auto rot = RotateBy : : create ( 5 , 360 ) ;
auto fe = RepeatForever : : create ( rot ) ;
item - > runAction ( fe ) ;
2019-09-24 11:31:35 +08:00
auto move = MoveBy : : create ( 3 , Vec2 ( 200.0f , 0.0f ) ) ;
2014-01-10 06:26:36 +08:00
auto move_back = move - > reverse ( ) ;
2014-07-10 00:45:27 +08:00
auto seq = Sequence : : create ( move , move_back , nullptr ) ;
2014-01-10 06:26:36 +08:00
auto fe2 = RepeatForever : : create ( seq ) ;
back - > runAction ( fe2 ) ;
auto orbit = OrbitCamera : : create ( 10 , 0 , 1 , 0 , 360 , 0 , 90 ) ;
parent - > runAction ( orbit ) ;
}
2014-02-28 23:02:22 +08:00
std : : string NodeToWorld3D : : subtitle ( ) const
2014-01-10 06:26:36 +08:00
{
return " nodeToParent transform in 3D " ;
}
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
// CameraOrbitTest
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
void CameraOrbitTest : : onEnter ( )
{
2012-04-19 14:35:52 +08:00
TestCocosNodeDemo : : onEnter ( ) ;
2014-03-31 18:17:00 +08:00
_preProjection = Director : : getInstance ( ) - > getProjection ( ) ;
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( true ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( true ) ;
2013-07-26 04:36:19 +08:00
Director : : getInstance ( ) - > setProjection ( Director : : Projection : : _3D ) ;
2012-03-21 13:36:24 +08:00
}
void CameraOrbitTest : : onExit ( )
{
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( false ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( false ) ;
2014-03-31 18:17:00 +08:00
Director : : getInstance ( ) - > setProjection ( _preProjection ) ;
2012-04-19 14:35:52 +08:00
TestCocosNodeDemo : : onExit ( ) ;
2012-03-21 13:36:24 +08:00
}
CameraOrbitTest : : CameraOrbitTest ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-03-21 13:36:24 +08:00
2013-08-16 16:05:27 +08:00
auto p = Sprite : : create ( s_back3 ) ;
2012-04-19 14:35:52 +08:00
addChild ( p , 0 ) ;
2014-05-15 01:07:09 +08:00
p - > setPosition ( Vec2 ( s . width / 2 , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
p - > setOpacity ( 128 ) ;
2013-06-20 14:17:10 +08:00
Sprite * sprite ;
OrbitCamera * orbit ;
Size ss ;
2012-04-19 14:35:52 +08:00
// LEFT
2012-03-21 13:36:24 +08:00
s = p - > getContentSize ( ) ;
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
sprite - > setScale ( 0.5f ) ;
p - > addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 1 , s . height / 2 ) ) ;
2013-06-20 14:17:10 +08:00
orbit = OrbitCamera : : create ( 2 , 1 , 0 , 0 , 360 , 0 , 0 ) ;
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// CENTER
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
sprite - > setScale ( 1.0f ) ;
p - > addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 2 , s . height / 2 ) ) ;
2013-06-20 14:17:10 +08:00
orbit = OrbitCamera : : create ( 2 , 1 , 0 , 0 , 360 , 45 , 0 ) ;
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// RIGHT
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
sprite - > setScale ( 2.0f ) ;
p - > addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 3 , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
ss = sprite - > getContentSize ( ) ;
2013-06-20 14:17:10 +08:00
orbit = OrbitCamera : : create ( 2 , 1 , 0 , 0 , 360 , 90 , - 45 ) ,
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// PARENT
2013-06-20 14:17:10 +08:00
orbit = OrbitCamera : : create ( 10 , 1 , 0 , 0 , 360 , 0 , 90 ) ;
p - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
setScale ( 1 ) ;
2012-03-21 13:36:24 +08:00
}
2014-02-28 23:02:22 +08:00
std : : string CameraOrbitTest : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Camera Orbit test " ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
// CameraZoomTest
//
//------------------------------------------------------------------
2012-03-21 13:36:24 +08:00
void CameraZoomTest : : onEnter ( )
{
2012-04-19 14:35:52 +08:00
TestCocosNodeDemo : : onEnter ( ) ;
2014-03-31 18:17:00 +08:00
_preProjection = Director : : getInstance ( ) - > getProjection ( ) ;
2013-07-26 04:36:19 +08:00
Director : : getInstance ( ) - > setProjection ( Director : : Projection : : _3D ) ;
2012-03-21 13:36:24 +08:00
}
void CameraZoomTest : : onExit ( )
{
2014-03-31 18:17:00 +08:00
Director : : getInstance ( ) - > setProjection ( _preProjection ) ;
2012-04-19 14:35:52 +08:00
TestCocosNodeDemo : : onExit ( ) ;
2012-03-21 13:36:24 +08:00
}
CameraZoomTest : : CameraZoomTest ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:17:10 +08:00
Sprite * sprite ;
2013-12-28 14:34:52 +08:00
// Camera *cam;
2012-04-19 14:35:52 +08:00
// LEFT
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 1 , s . height / 2 ) ) ;
2013-12-21 08:33:31 +08:00
// cam = sprite->getCamera();
// cam->setEye(0, 0, 415/2);
// cam->setCenter(0, 0, 0);
2012-04-19 14:35:52 +08:00
// CENTER
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 , 40 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 2 , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
// RIGHT
2013-07-24 06:20:22 +08:00
sprite = Sprite : : create ( s_pathGrossini ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 , 20 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * 3 , s . height / 2 ) ) ;
2012-04-19 14:35:52 +08:00
2013-06-15 14:03:30 +08:00
_z = 0 ;
2012-04-19 14:35:52 +08:00
scheduleUpdate ( ) ;
2012-03-21 13:36:24 +08:00
}
2012-06-08 13:55:28 +08:00
void CameraZoomTest : : update ( float dt )
2012-03-21 13:36:24 +08:00
{
2013-06-20 14:17:10 +08:00
Node * sprite ;
2013-12-21 08:33:31 +08:00
// Camera *cam;
2013-06-15 14:03:30 +08:00
_z + = dt * 100 ;
2012-04-19 14:35:52 +08:00
sprite = getChildByTag ( 20 ) ;
2013-12-21 08:33:31 +08:00
// cam = sprite->getCamera();
// cam->setEye(0, 0, _z);
2012-04-19 14:35:52 +08:00
sprite = getChildByTag ( 40 ) ;
2013-12-21 08:33:31 +08:00
// cam = sprite->getCamera();
// cam->setEye(0, 0, -_z);
2012-03-21 13:36:24 +08:00
}
2014-02-28 23:02:22 +08:00
std : : string CameraZoomTest : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Camera Zoom test " ;
2012-03-21 13:36:24 +08:00
}
2010-08-26 15:09:02 +08:00
//------------------------------------------------------------------
//
// CameraCenterTest
//
//------------------------------------------------------------------
2014-03-31 18:17:00 +08:00
void CameraCenterTest : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
_preProjection = Director : : getInstance ( ) - > getProjection ( ) ;
Director : : getInstance ( ) - > setProjection ( Director : : Projection : : _3D ) ;
}
void CameraCenterTest : : onExit ( )
{
2014-04-10 16:13:07 +08:00
TestCocosNodeDemo : : onExit ( ) ;
2014-03-31 18:17:00 +08:00
Director : : getInstance ( ) - > setProjection ( _preProjection ) ;
}
2012-03-21 13:36:24 +08:00
CameraCenterTest : : CameraCenterTest ( )
{
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:17:10 +08:00
Sprite * sprite ;
OrbitCamera * orbit ;
2012-04-19 14:35:52 +08:00
// LEFT-TOP
2013-06-20 14:17:10 +08:00
sprite = Sprite : : create ( " Images/white-512x512.png " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 5 * 1 , s . height / 5 * 1 ) ) ;
2013-07-08 18:11:32 +08:00
sprite - > setColor ( Color3B : : RED ) ;
2019-09-24 11:31:35 +08:00
sprite - > setTextureRect ( Rect ( 0.0f , 0.0f , 120.0f , 50.0f ) ) ;
orbit = OrbitCamera : : create ( 10.0f , 1.0f , 0.0f , 0.0f , 360.0f , 0.0f , 0.0f ) ;
2013-06-20 14:17:10 +08:00
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2014-05-15 01:07:09 +08:00
// [sprite setAnchorPoint: Vec2(0,1));
2012-04-19 14:35:52 +08:00
// LEFT-BOTTOM
2013-06-20 14:17:10 +08:00
sprite = Sprite : : create ( " Images/white-512x512.png " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 , 40 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 5 * 1 , s . height / 5 * 4 ) ) ;
2013-07-08 18:11:32 +08:00
sprite - > setColor ( Color3B : : BLUE ) ;
2019-09-24 11:31:35 +08:00
sprite - > setTextureRect ( Rect ( 0.0f , 0.0f , 120.0f , 50.0f ) ) ;
orbit = OrbitCamera : : create ( 10.0f , 1.0f , 0.0f , 0.0f , 360.0f , 0.0f , 0.0f ) ;
2013-06-20 14:17:10 +08:00
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// RIGHT-TOP
2013-06-20 14:17:10 +08:00
sprite = Sprite : : create ( " Images/white-512x512.png " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 5 * 4 , s . height / 5 * 1 ) ) ;
2013-07-08 18:11:32 +08:00
sprite - > setColor ( Color3B : : YELLOW ) ;
2019-09-24 11:31:35 +08:00
sprite - > setTextureRect ( Rect ( 0.0f , 0.0f , 120.0f , 50.0f ) ) ;
orbit = OrbitCamera : : create ( 10.0f , 1.0f , 0.0f , 0.0f , 360.0f , 0.0f , 0.0f ) ;
2013-06-20 14:17:10 +08:00
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// RIGHT-BOTTOM
2013-06-20 14:17:10 +08:00
sprite = Sprite : : create ( " Images/white-512x512.png " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 , 40 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 5 * 4 , s . height / 5 * 4 ) ) ;
2013-07-08 18:11:32 +08:00
sprite - > setColor ( Color3B : : GREEN ) ;
2019-09-24 11:31:35 +08:00
sprite - > setTextureRect ( Rect ( 0.0f , 0.0f , 120.0f , 50.0f ) ) ;
orbit = OrbitCamera : : create ( 10.0f , 1.0f , 0.0f , 0.0f , 360.0f , 0.0f , 0.0f ) ;
2013-06-20 14:17:10 +08:00
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-04-19 14:35:52 +08:00
// CENTER
2013-06-20 14:17:10 +08:00
sprite = Sprite : : create ( " Images/white-512x512.png " ) ;
2012-04-19 14:35:52 +08:00
addChild ( sprite , 0 , 40 ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 2 , s . height / 2 ) ) ;
2013-07-08 18:11:32 +08:00
sprite - > setColor ( Color3B : : WHITE ) ;
2019-09-24 11:31:35 +08:00
sprite - > setTextureRect ( Rect ( 0.0f , 0.0f , 120.0f , 50.0f ) ) ;
orbit = OrbitCamera : : create ( 10.0f , 1.0f , 0.0f , 0.0f , 360.0f , 0.0f , 0.0f ) ;
2013-06-20 14:17:10 +08:00
sprite - > runAction ( RepeatForever : : create ( orbit ) ) ;
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string CameraCenterTest : : title ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Camera Center test " ;
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string CameraCenterTest : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Sprites should rotate at the same speed " ;
2012-03-21 13:36:24 +08:00
}
2011-02-23 16:47:25 +08:00
//------------------------------------------------------------------
//
// ConvertToNode
//
2012-03-21 13:36:24 +08:00
//------------------------------------------------------------------
ConvertToNode : : ConvertToNode ( )
{
2013-10-23 16:14:03 +08:00
auto listener = EventListenerTouchAllAtOnce : : create ( ) ;
listener - > onTouchesEnded = CC_CALLBACK_2 ( ConvertToNode : : onTouchesEnded , this ) ;
2013-10-26 15:04:01 +08:00
_eventDispatcher - > addEventListenerWithSceneGraphPriority ( listener , this ) ;
2013-10-23 16:14:03 +08:00
2013-08-16 16:05:27 +08:00
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
2012-03-21 13:36:24 +08:00
2013-08-16 16:05:27 +08:00
auto rotate = RotateBy : : create ( 10 , 360 ) ;
auto action = RepeatForever : : create ( rotate ) ;
2012-03-21 13:36:24 +08:00
for ( int i = 0 ; i < 3 ; i + + )
{
2013-08-16 16:05:27 +08:00
auto sprite = Sprite : : create ( " Images/grossini.png " ) ;
2014-05-15 01:07:09 +08:00
sprite - > setPosition ( Vec2 ( s . width / 4 * ( i + 1 ) , s . height / 2 ) ) ;
2012-03-21 13:36:24 +08:00
2013-08-16 16:05:27 +08:00
auto point = Sprite : : create ( " Images/r1.png " ) ;
2012-03-21 13:36:24 +08:00
point - > setScale ( 0.25f ) ;
point - > setPosition ( sprite - > getPosition ( ) ) ;
addChild ( point , 10 , 100 + i ) ;
switch ( i )
{
case 0 :
2014-05-15 01:07:09 +08:00
sprite - > setAnchorPoint ( Vec2 : : ZERO ) ;
2012-03-21 13:36:24 +08:00
break ;
case 1 :
2014-05-15 01:07:09 +08:00
sprite - > setAnchorPoint ( Vec2 ( 0.5f , 0.5f ) ) ;
2012-03-21 13:36:24 +08:00
break ;
case 2 :
2014-05-15 01:07:09 +08:00
sprite - > setAnchorPoint ( Vec2 ( 1 , 1 ) ) ;
2012-03-21 13:36:24 +08:00
break ;
}
point - > setPosition ( sprite - > getPosition ( ) ) ;
2013-07-09 05:38:14 +08:00
sprite - > runAction ( action - > clone ( ) ) ;
2012-03-21 13:36:24 +08:00
addChild ( sprite , i ) ;
}
}
2013-09-03 18:22:03 +08:00
void ConvertToNode : : onTouchesEnded ( const std : : vector < Touch * > & touches , Event * event )
2012-03-21 13:36:24 +08:00
{
2013-09-03 18:22:03 +08:00
for ( auto & touch : touches )
2012-03-21 13:36:24 +08:00
{
2013-08-16 16:05:27 +08:00
auto location = touch - > getLocation ( ) ;
2012-03-21 13:36:24 +08:00
for ( int i = 0 ; i < 3 ; i + + )
{
2013-08-16 16:05:27 +08:00
auto node = getChildByTag ( 100 + i ) ;
2014-05-15 01:07:09 +08:00
Vec2 p1 , p2 ;
2012-03-21 13:36:24 +08:00
p1 = node - > convertToNodeSpaceAR ( location ) ;
p2 = node - > convertToNodeSpace ( location ) ;
CCLOG ( " AR: x=%.2f, y=%.2f -- Not AR: x=%.2f, y=%.2f " , p1 . x , p1 . y , p2 . x , p2 . y ) ;
}
2012-04-19 14:35:52 +08:00
}
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string ConvertToNode : : title ( ) const
2012-03-21 13:36:24 +08:00
{
return " Convert To Node Space " ;
}
2013-12-19 05:52:10 +08:00
std : : string ConvertToNode : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
return " testing convertToNodeSpace / AR. Touch and see console " ;
}
/// NodeOpaqueTest
NodeOpaqueTest : : NodeOpaqueTest ( )
{
2014-07-10 00:45:27 +08:00
Sprite * background = nullptr ;
2012-03-21 13:36:24 +08:00
2012-04-19 14:35:52 +08:00
for ( int i = 0 ; i < 50 ; i + + )
{
2013-06-20 14:17:10 +08:00
background = Sprite : : create ( " Images/background1.png " ) ;
2013-07-26 04:36:19 +08:00
background - > setBlendFunc ( BlendFunc : : ALPHA_PREMULTIPLIED ) ;
2014-05-15 01:07:09 +08:00
background - > setAnchorPoint ( Vec2 : : ZERO ) ;
2012-04-19 14:35:52 +08:00
addChild ( background ) ;
}
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string NodeOpaqueTest : : title ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Node Opaque Test " ;
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string NodeOpaqueTest : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Node rendered with GL_BLEND disabled " ;
2012-03-21 13:36:24 +08:00
}
/// NodeNonOpaqueTest
NodeNonOpaqueTest : : NodeNonOpaqueTest ( )
{
2014-07-10 00:45:27 +08:00
Sprite * background = nullptr ;
2012-03-21 13:36:24 +08:00
2012-04-19 14:35:52 +08:00
for ( int i = 0 ; i < 50 ; i + + )
{
2013-06-20 14:17:10 +08:00
background = Sprite : : create ( " Images/background1.jpg " ) ;
2013-07-26 04:36:19 +08:00
background - > setBlendFunc ( BlendFunc : : DISABLE ) ;
2014-05-15 01:07:09 +08:00
background - > setAnchorPoint ( Vec2 : : ZERO ) ;
2012-04-19 14:35:52 +08:00
addChild ( background ) ;
}
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string NodeNonOpaqueTest : : title ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Node Non Opaque Test " ;
2012-03-21 13:36:24 +08:00
}
2013-12-19 05:52:10 +08:00
std : : string NodeNonOpaqueTest : : subtitle ( ) const
2012-03-21 13:36:24 +08:00
{
2012-04-19 14:35:52 +08:00
return " Node rendered with GL_BLEND enabled " ;
2012-03-21 13:36:24 +08:00
}
2014-01-19 03:35:27 +08:00
/// NodeGlobalZValueTest
NodeGlobalZValueTest : : NodeGlobalZValueTest ( )
{
Size s = Director : : getInstance ( ) - > getWinSize ( ) ;
for ( int i = 0 ; i < 9 ; i + + )
{
Sprite * sprite ;
auto parent = Node : : create ( ) ;
if ( i = = 4 ) {
sprite = Sprite : : create ( " Images/grossinis_sister2.png " ) ;
_sprite = sprite ;
_sprite - > setGlobalZOrder ( - 1 ) ;
}
else
sprite = Sprite : : create ( " Images/grossinis_sister1.png " ) ;
parent - > addChild ( sprite ) ;
this - > addChild ( parent ) ;
float w = sprite - > getContentSize ( ) . width ;
sprite - > setPosition ( s . width / 2 - w * 0.7 * ( i - 5 ) , s . height / 2 ) ;
}
this - > scheduleUpdate ( ) ;
}
void NodeGlobalZValueTest : : update ( float dt )
{
static float accum = 0 ;
accum + = dt ;
if ( accum > 1 ) {
float z = _sprite - > getGlobalZOrder ( ) ;
_sprite - > setGlobalZOrder ( - z ) ;
accum = 0 ;
}
}
std : : string NodeGlobalZValueTest : : title ( ) const
{
return " Global Z Value " ;
}
std : : string NodeGlobalZValueTest : : subtitle ( ) const
{
return " Center Sprite should change go from foreground to background " ;
}
2014-01-10 06:26:36 +08:00
2014-01-09 08:28:18 +08:00
//
2014-01-10 06:26:36 +08:00
// MySprite: Used by CameraTest1 and CameraTest2
2014-01-09 08:28:18 +08:00
//
class MySprite : public Sprite
{
public :
static MySprite * create ( const std : : string & spritefilename )
{
2014-08-28 07:31:57 +08:00
auto sprite = new ( std : : nothrow ) MySprite ;
2014-01-09 08:28:18 +08:00
sprite - > initWithFile ( spritefilename ) ;
sprite - > autorelease ( ) ;
2019-02-19 16:06:17 +08:00
// auto shaderState = GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR);
// sprite->setGLProgramState(shaderState);
2014-01-09 08:28:18 +08:00
return sprite ;
}
2014-05-31 07:42:05 +08:00
virtual void draw ( Renderer * renderer , const Mat4 & transform , uint32_t flags ) override ;
void onDraw ( const Mat4 & transform , uint32_t flags ) ;
2014-01-09 08:28:18 +08:00
protected :
CustomCommand _customCommand ;
2014-01-10 06:26:36 +08:00
2014-01-09 08:28:18 +08:00
} ;
2014-05-31 07:42:05 +08:00
void MySprite : : draw ( Renderer * renderer , const Mat4 & transform , uint32_t flags )
2014-01-09 08:28:18 +08:00
{
2015-01-16 06:00:49 +08:00
_customCommand . init ( _globalZOrder , transform , flags ) ;
2014-05-31 07:42:05 +08:00
_customCommand . func = CC_CALLBACK_0 ( MySprite : : onDraw , this , transform , flags ) ;
2014-03-01 08:10:48 +08:00
renderer - > addCommand ( & _customCommand ) ;
2014-01-09 08:28:18 +08:00
}
2014-05-31 07:42:05 +08:00
void MySprite : : onDraw ( const Mat4 & transform , uint32_t flags )
2014-01-09 08:28:18 +08:00
{
2019-02-19 16:06:17 +08:00
// getGLProgram()->use();
// getGLProgram()->setUniformsForBuiltins(transform);
2014-01-09 08:28:18 +08:00
2019-03-13 14:11:40 +08:00
// cocos2d::utils::setBlending(_blendFunc.src, _blendFunc.dst);
2014-01-09 08:28:18 +08:00
2019-05-22 09:49:11 +08:00
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D, _texture->getName());
2018-09-11 14:39:30 +08:00
2019-02-19 16:06:17 +08:00
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
// glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
2014-01-09 08:28:18 +08:00
2014-01-23 03:19:03 +08:00
# define kQuadSize sizeof(_quad.bl)
2019-02-19 16:06:17 +08:00
// size_t offset = (size_t)&_quad;
2014-01-09 08:28:18 +08:00
// vertex
int diff = offsetof ( V3F_C4B_T2F , vertices ) ;
2019-02-19 16:06:17 +08:00
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
2014-01-09 08:28:18 +08:00
2017-03-17 10:44:38 +08:00
// texCoords
2014-01-09 08:28:18 +08:00
diff = offsetof ( V3F_C4B_T2F , texCoords ) ;
2019-02-19 16:06:17 +08:00
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
2014-01-09 08:28:18 +08:00
// color
diff = offsetof ( V3F_C4B_T2F , colors ) ;
2019-02-19 16:06:17 +08:00
// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
2014-01-09 08:28:18 +08:00
2019-06-05 17:58:33 +08:00
//TODO coulsonwang
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
//
// CHECK_GL_ERROR_DEBUG();
2014-02-08 11:37:44 +08:00
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES ( 1 , 4 ) ;
2014-01-09 08:28:18 +08:00
}
2014-01-10 06:26:36 +08:00
//------------------------------------------------------------------
//
// CameraTest1
//
//------------------------------------------------------------------
void CameraTest1 : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
2014-03-31 18:17:00 +08:00
_preProjection = Director : : getInstance ( ) - > getProjection ( ) ;
2014-01-10 06:26:36 +08:00
Director : : getInstance ( ) - > setProjection ( Director : : Projection : : _3D ) ;
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( true ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( true ) ;
2014-01-10 06:26:36 +08:00
}
void CameraTest1 : : onExit ( )
{
2014-03-31 18:17:00 +08:00
Director : : getInstance ( ) - > setProjection ( _preProjection ) ;
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( false ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( false ) ;
2014-01-10 06:26:36 +08:00
TestCocosNodeDemo : : onExit ( ) ;
}
CameraTest1 : : CameraTest1 ( )
{
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
_sprite1 = MySprite : : create ( s_back3 ) ;
addChild ( _sprite1 ) ;
2014-05-15 01:07:09 +08:00
_sprite1 - > setPosition ( Vec2 ( 1 * s . width / 4 , s . height / 2 ) ) ;
2014-01-10 06:26:36 +08:00
_sprite1 - > setScale ( 0.5 ) ;
_sprite2 = Sprite : : create ( s_back3 ) ;
addChild ( _sprite2 ) ;
2014-05-15 01:07:09 +08:00
_sprite2 - > setPosition ( Vec2 ( 3 * s . width / 4 , s . height / 2 ) ) ;
2014-01-10 06:26:36 +08:00
_sprite2 - > setScale ( 0.5 ) ;
auto camera = OrbitCamera : : create ( 10 , 0 , 1 , 0 , 360 , 0 , 0 ) ;
_sprite1 - > runAction ( camera ) ;
_sprite2 - > runAction ( camera - > clone ( ) ) ;
}
std : : string CameraTest1 : : title ( ) const
{
return " Camera Test 1 " ;
}
std : : string CameraTest1 : : subtitle ( ) const
{
return " Both images should rotate with a 3D effect " ;
}
2014-05-31 07:42:05 +08:00
2014-01-10 06:26:36 +08:00
//------------------------------------------------------------------
//
// CameraTest2
//
//------------------------------------------------------------------
2014-01-09 08:28:18 +08:00
void CameraTest2 : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
2014-03-31 18:17:00 +08:00
_preProjection = Director : : getInstance ( ) - > getProjection ( ) ;
2014-01-09 08:28:18 +08:00
Director : : getInstance ( ) - > setProjection ( Director : : Projection : : _3D ) ;
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( true ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( true ) ;
2014-01-09 08:28:18 +08:00
}
void CameraTest2 : : onExit ( )
{
2014-03-31 18:17:00 +08:00
Director : : getInstance ( ) - > setProjection ( _preProjection ) ;
2019-06-30 11:36:52 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthTest ( false ) ;
2019-09-04 10:02:03 +08:00
Director : : getInstance ( ) - > getRenderer ( ) - > setDepthWrite ( false ) ;
2014-01-09 08:28:18 +08:00
TestCocosNodeDemo : : onExit ( ) ;
}
CameraTest2 : : CameraTest2 ( )
{
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
_sprite1 = MySprite : : create ( s_back3 ) ;
addChild ( _sprite1 ) ;
2014-05-15 01:07:09 +08:00
_sprite1 - > setPosition ( Vec2 ( 1 * s . width / 4 , s . height / 2 ) ) ;
2014-01-09 08:28:18 +08:00
_sprite1 - > setScale ( 0.5 ) ;
_sprite2 = Sprite : : create ( s_back3 ) ;
addChild ( _sprite2 ) ;
2014-05-15 01:07:09 +08:00
_sprite2 - > setPosition ( Vec2 ( 3 * s . width / 4 , s . height / 2 ) ) ;
2014-01-09 08:28:18 +08:00
_sprite2 - > setScale ( 0.5 ) ;
2014-05-15 01:07:09 +08:00
Vec3 eye ( 150 , 0 , 200 ) , center ( 0 , 0 , 0 ) , up ( 0 , 1 , 0 ) ;
2014-01-09 08:28:18 +08:00
2014-05-15 01:07:09 +08:00
Mat4 lookupMatrix ;
Mat4 : : createLookAt ( eye , center , up , & lookupMatrix ) ;
2014-01-09 08:28:18 +08:00
2014-05-15 01:07:09 +08:00
Mat4 lookupMatrix2 = lookupMatrix ;
2014-04-08 23:05:33 +08:00
_sprite1 - > setAdditionalTransform ( & lookupMatrix2 ) ;
_sprite2 - > setAdditionalTransform ( & lookupMatrix2 ) ;
2014-01-14 04:52:07 +08:00
}
std : : string CameraTest2 : : title ( ) const
{
return " Camera Test 2 " ;
}
std : : string CameraTest2 : : subtitle ( ) const
{
return " Both images should look the same " ;
2014-01-09 08:28:18 +08:00
}
2014-05-31 07:42:05 +08:00
//------------------------------------------------------------------
//
// NodeNormalizedPositionTest1
//
//------------------------------------------------------------------
NodeNormalizedPositionTest1 : : NodeNormalizedPositionTest1 ( )
{
Sprite * sprites [ 5 ] ;
2014-06-05 00:48:35 +08:00
Vec2 positions [ 5 ] ;
2019-09-24 11:31:35 +08:00
positions [ 0 ] = Vec2 ( 0.0f , 0.0f ) ;
positions [ 1 ] = Vec2 ( 0.0f , 1.0f ) ;
positions [ 2 ] = Vec2 ( 0.5f , 0.5f ) ;
positions [ 3 ] = Vec2 ( 1.0f , 0.0f ) ;
positions [ 4 ] = Vec2 ( 1.0f , 1.0f ) ;
2014-05-31 07:42:05 +08:00
for ( int i = 0 ; i < 5 ; i + + ) {
sprites [ i ] = Sprite : : create ( " Images/grossini.png " ) ;
2016-10-27 09:45:40 +08:00
sprites [ i ] - > setPositionNormalized ( positions [ i ] ) ;
2014-05-31 07:42:05 +08:00
addChild ( sprites [ i ] ) ;
}
}
std : : string NodeNormalizedPositionTest1 : : title ( ) const
{
return " setNormalizedPositon() " ;
}
std : : string NodeNormalizedPositionTest1 : : subtitle ( ) const
{
return " 5 sprites: One in the center, the others on the corners " ;
}
//------------------------------------------------------------------
//
// NodeNormalizedPositionTest2
//
//------------------------------------------------------------------
NodeNormalizedPositionTest2 : : NodeNormalizedPositionTest2 ( )
: _accum ( 0 )
{
Sprite * sprites [ 5 ] ;
2014-06-05 01:11:16 +08:00
Vec2 positions [ 5 ] ;
2019-09-24 11:31:35 +08:00
positions [ 0 ] = Vec2 ( 0.0f , 0.0f ) ;
positions [ 1 ] = Vec2 ( 0.0f , 1.0f ) ;
positions [ 2 ] = Vec2 ( 0.5f , 0.5f ) ;
positions [ 3 ] = Vec2 ( 1.0f , 0.0f ) ;
positions [ 4 ] = Vec2 ( 1.0f , 1.0f ) ;
2014-05-31 07:42:05 +08:00
for ( int i = 0 ; i < 5 ; i + + ) {
sprites [ i ] = Sprite : : create ( " Images/grossini.png " ) ;
2016-10-27 09:45:40 +08:00
sprites [ i ] - > setPositionNormalized ( positions [ i ] ) ;
2014-05-31 07:42:05 +08:00
addChild ( sprites [ i ] ) ;
}
scheduleUpdate ( ) ;
setContentSize ( Director : : getInstance ( ) - > getWinSize ( ) ) ;
_copyContentSize = getContentSize ( ) ;
// setAnchorPoint(Vec2(0.5,0.5));
2016-10-27 09:45:40 +08:00
// setPositionNormalized(Vec2(0.5,0.5));
2014-05-31 07:42:05 +08:00
}
std : : string NodeNormalizedPositionTest2 : : title ( ) const
{
return " setNormalizedPositon() #2 " ;
}
std : : string NodeNormalizedPositionTest2 : : subtitle ( ) const
{
return " 5 sprites: One in the center, the others on the corners of its parents " ;
}
void NodeNormalizedPositionTest2 : : update ( float dt )
{
_accum + = dt ;
// for 5 seconds
float norm = sinf ( _accum ) ;
Size s = Size ( _copyContentSize . width * norm , _copyContentSize . height * norm ) ;
setContentSize ( s ) ;
CCLOG ( " s: %f,%f " , s . width , s . height ) ;
}
2014-06-12 13:55:23 +08:00
2014-09-01 11:29:40 +08:00
//------------------------------------------------------------------
//
// NodeNormalizedPositionBugTest
//
//------------------------------------------------------------------
NodeNormalizedPositionBugTest : : NodeNormalizedPositionBugTest ( )
: _accum ( 0 )
{
Vec2 position ;
2019-09-24 11:31:35 +08:00
position = Vec2 ( 0.5f , 0.5f ) ;
2014-09-01 11:29:40 +08:00
sprite = Sprite : : create ( " Images/grossini.png " ) ;
2016-10-27 09:45:40 +08:00
sprite - > setPositionNormalized ( position ) ;
2014-09-01 11:29:40 +08:00
addChild ( sprite ) ;
scheduleUpdate ( ) ;
}
std : : string NodeNormalizedPositionBugTest : : title ( ) const
{
return " NodeNormalizedPositionBugTest " ;
}
std : : string NodeNormalizedPositionBugTest : : subtitle ( ) const
{
return " When changing sprite normalizedPosition, the sprite doesn't move! " ;
}
void NodeNormalizedPositionBugTest : : update ( float dt )
{
_accum + = dt ;
// for 5 seconds
float norm = clampf ( sinf ( _accum ) , 0 , 1.0 ) ;
2016-10-27 09:45:40 +08:00
sprite - > setPositionNormalized ( Vec2 ( norm , norm ) ) ;
2014-09-01 11:29:40 +08:00
}
2014-06-12 13:55:23 +08:00
std : : string NodeNameTest : : title ( ) const
{
return " getName()/setName()/getChildByName()/enumerateChildren() " ;
}
std : : string NodeNameTest : : subtitle ( ) const
{
return " see console " ;
}
void NodeNameTest : : onEnter ( )
{
2015-04-03 14:31:03 +08:00
TestCocosNodeDemo : : onEnter ( ) ;
2014-06-12 13:55:23 +08:00
2014-10-04 08:11:39 +08:00
this - > scheduleOnce ( CC_CALLBACK_1 ( NodeNameTest : : test , this ) , 0.05f , " test_key " ) ;
2014-07-01 18:23:20 +08:00
}
2016-05-18 16:26:13 +08:00
void NodeNameTest : : onExit ( )
{
TestCocosNodeDemo : : onExit ( ) ;
}
2014-07-01 18:23:20 +08:00
void NodeNameTest : : test ( float dt )
{
2014-06-17 11:43:03 +08:00
auto parent = Node : : create ( ) ;
2014-06-12 13:55:23 +08:00
// setName(), getName() and getChildByName()
char name [ 20 ] ;
for ( int i = 0 ; i < 10 ; + + i )
{
sprintf ( name , " node%d " , i ) ;
auto node = Node : : create ( ) ;
node - > setName ( name ) ;
2014-06-17 11:43:03 +08:00
parent - > addChild ( node ) ;
2014-06-12 13:55:23 +08:00
}
2014-06-17 11:43:03 +08:00
for ( int i = 0 ; i < 10 ; + + i )
2014-06-12 13:55:23 +08:00
{
2014-06-17 11:43:03 +08:00
sprintf ( name , " node%d " , i ) ;
auto node = parent - > getChildByName ( name ) ;
2014-06-12 13:55:23 +08:00
log ( " find child: %s " , node - > getName ( ) . c_str ( ) ) ;
}
// enumerateChildren()
2014-06-17 11:43:03 +08:00
// name = regular expression
2014-06-26 14:05:30 +08:00
int i = 0 ;
2014-07-02 17:07:36 +08:00
parent = Node : : create ( ) ;
for ( int i = 0 ; i < 100 ; + + i )
{
auto node = Node : : create ( ) ;
sprintf ( name , " node%d " , i ) ;
node - > setName ( name ) ;
parent - > addChild ( node ) ;
}
i = 0 ;
parent - > enumerateChildren ( " node[[:digit:]]+ " , [ & i ] ( Node * node ) - > bool {
+ + i ;
return false ;
} ) ;
CCAssert ( i = = 100 , " " ) ;
i = 0 ;
parent - > enumerateChildren ( " node[[:digit:]]+ " , [ & i ] ( Node * node ) - > bool {
+ + i ;
return true ;
} ) ;
CCAssert ( i = = 1 , " " ) ;
2014-06-17 11:43:03 +08:00
// enumerateChildren
// name = node[[digit]]+/node
parent = Node : : create ( ) ;
2016-05-10 16:19:58 +08:00
for ( int i = 0 ; i < 10 ; + + i )
2014-06-17 11:43:03 +08:00
{
auto node = Node : : create ( ) ;
sprintf ( name , " node%d " , i ) ;
node - > setName ( name ) ;
parent - > addChild ( node ) ;
2016-05-10 16:19:58 +08:00
for ( int j = 0 ; j < 10 ; + + j )
2014-06-17 11:43:03 +08:00
{
auto child = Node : : create ( ) ;
child - > setName ( " node " ) ;
node - > addChild ( child ) ;
}
}
i = 0 ;
parent - > enumerateChildren ( " node1/node " , [ & i ] ( Node * node ) - > bool {
+ + i ;
return false ;
} ) ;
2016-05-10 16:19:58 +08:00
CCAssert ( i = = 10 , " " ) ;
2014-06-17 11:43:03 +08:00
i = 0 ;
parent - > enumerateChildren ( " node1/node " , [ & i ] ( Node * node ) - > bool {
+ + i ;
return true ;
} ) ;
CCAssert ( i = = 1 , " " ) ;
2014-07-01 16:09:25 +08:00
// search from root
2014-07-02 17:07:36 +08:00
parent = Node : : create ( ) ;
2016-05-10 16:19:58 +08:00
for ( int i = 0 ; i < 10 ; + + i )
2014-06-17 11:43:03 +08:00
{
auto node = Node : : create ( ) ;
2014-07-02 17:07:36 +08:00
sprintf ( name , " node%d " , i ) ;
node - > setName ( name ) ;
2014-06-17 11:43:03 +08:00
parent - > addChild ( node ) ;
2016-05-10 16:19:58 +08:00
for ( int j = 0 ; j < 10 ; + + j )
2014-06-17 11:43:03 +08:00
{
auto child = Node : : create ( ) ;
2014-07-02 17:07:36 +08:00
child - > setName ( " node " ) ;
2014-06-17 11:43:03 +08:00
node - > addChild ( child ) ;
}
}
i = 0 ;
2014-07-02 17:07:36 +08:00
parent - > enumerateChildren ( " node[[:digit:]]+/node " , [ & i ] ( Node * node ) - > bool {
2014-06-17 11:43:03 +08:00
+ + i ;
return false ;
} ) ;
2016-05-10 16:19:58 +08:00
CCAssert ( i = = 100 , " " ) ;
2014-06-17 11:43:03 +08:00
i = 0 ;
2014-07-02 17:07:36 +08:00
parent - > enumerateChildren ( " node[[:digit:]]+/node " , [ & i ] ( Node * node ) - > bool {
2014-06-17 11:43:03 +08:00
+ + i ;
2014-07-02 17:07:36 +08:00
return true ;
2014-06-17 11:43:03 +08:00
} ) ;
2014-07-02 17:07:36 +08:00
CCAssert ( i = = 1 , " " ) ;
2014-06-12 13:55:23 +08:00
2014-07-01 16:09:25 +08:00
// search from parent
// name is xxx/..
2014-07-02 17:07:36 +08:00
i = 0 ;
2019-10-17 11:12:25 +08:00
parent - > getChildByName ( " node1 " ) - > enumerateChildren ( " node[[:digit:]]+/node/.. " , [ & i ] ( Node * node ) - > bool {
2014-07-02 17:07:36 +08:00
+ + i ;
return true ;
} ) ;
CCAssert ( i = = 1 , " " ) ;
i = 0 ;
2019-10-17 11:12:25 +08:00
parent - > getChildByName ( " node1 " ) - > enumerateChildren ( " node[[:digit:]]+/node/.. " , [ & i ] ( Node * node ) - > bool {
2014-07-02 17:07:36 +08:00
+ + i ;
return false ;
} ) ;
2016-05-10 16:19:58 +08:00
CCAssert ( i = = 100 , " " ) ;
2014-07-01 16:09:25 +08:00
2014-07-10 11:07:39 +08:00
// name = //xxx : search recursively
parent = Node : : create ( ) ;
2016-05-10 16:19:58 +08:00
for ( int j = 0 ; j < 10 ; j + + )
2014-07-02 17:07:36 +08:00
{
auto node = Node : : create ( ) ;
sprintf ( name , " node%d " , j ) ;
node - > setName ( name ) ;
parent - > addChild ( node ) ;
2016-05-10 16:19:58 +08:00
for ( int k = 0 ; k < 10 ; + + k )
2014-07-02 17:07:36 +08:00
{
auto child = Node : : create ( ) ;
sprintf ( name , " node%d " , k ) ;
child - > setName ( name ) ;
node - > addChild ( child ) ;
}
}
i = 0 ;
2014-07-10 11:07:39 +08:00
parent - > enumerateChildren ( " //node[[:digit:]]+ " , [ & i ] ( Node * node ) - > bool {
2014-07-02 17:07:36 +08:00
+ + i ;
return false ;
} ) ;
2016-05-10 16:19:58 +08:00
CCAssert ( i = = 110 , " " ) ; // 100(children) + 10(parent)
2014-07-02 17:07:36 +08:00
i = 0 ;
2014-07-10 11:07:39 +08:00
parent - > enumerateChildren ( " //node[[:digit:]]+ " , [ & i ] ( Node * node ) - > bool {
2014-07-02 17:07:36 +08:00
+ + i ;
return true ;
} ) ;
CCAssert ( i = = 1 , " " ) ;
i = 0 ;
2019-10-17 11:12:25 +08:00
parent - > getChildByName ( " node1 " ) - > enumerateChildren ( " //node[[:digit:]]+/.. " , [ & i ] ( Node * node ) - > bool {
2014-07-02 17:07:36 +08:00
+ + i ;
return false ;
} ) ;
2019-10-17 11:12:25 +08:00
CCAssert ( i = = 110 , " " ) ;
2014-06-26 14:05:30 +08:00
// utils::findChildren()
parent = Node : : create ( ) ;
for ( int i = 0 ; i < 50 ; + + i )
{
auto child = Node : : create ( ) ;
child - > setName ( " node " ) ;
parent - > addChild ( child ) ;
}
auto findChildren = utils : : findChildren ( * parent , " node " ) ;
CCAssert ( findChildren . size ( ) = = 50 , " " ) ;
2014-06-12 13:55:23 +08:00
}
2016-07-15 05:58:04 +08:00
//------------------------------------------------------------------
//
// Issue16100Test
//
//------------------------------------------------------------------
void Issue16100Test : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
// create user camera
auto s = Director : : getInstance ( ) - > getWinSize ( ) ;
auto delay = DelayTime : : create ( 0.1f ) ;
auto f = CallFunc : : create ( [ this , s ] ( )
{
auto camera = Camera : : createOrthographic ( s . width * 2 , s . height * 2 , - 1024 , 1024 ) ;
camera - > setCameraFlag ( CameraFlag : : USER1 ) ;
addChild ( camera ) ;
} ) ;
this - > runAction ( Sequence : : createWithTwoActions ( delay , f ) ) ;
// grossini using default camera
auto sprite = Sprite : : create ( " Images/grossini.png " ) ;
this - > addChild ( sprite ) ;
sprite - > setPosition ( - 200 , s . height / 3 ) ;
2019-09-24 11:31:35 +08:00
auto moveby = MoveBy : : create ( 2 , Vec2 ( 400.0f , 0.0f ) ) ;
2016-07-15 05:58:04 +08:00
auto movebyback = moveby - > reverse ( ) ;
auto seq = Sequence : : create ( moveby , movebyback , nullptr ) ;
auto forever = RepeatForever : : create ( seq ) ;
sprite - > runAction ( forever ) ;
sprite - > setCameraMask ( ( int ) CameraFlag : : DEFAULT ) ;
// grossini's sister using user camera
auto sister = Sprite : : create ( " Images/grossinis_sister1.png " ) ;
this - > addChild ( sister ) ;
sister - > setPosition ( - 200 , s . height * 2 / 3 ) ;
2019-09-24 11:31:35 +08:00
auto moveby1 = MoveBy : : create ( 2 , Vec2 ( 400.0f , 0.0f ) ) ;
2016-07-15 05:58:04 +08:00
auto movebyback1 = moveby1 - > reverse ( ) ;
auto seq1 = Sequence : : create ( moveby1 , movebyback1 , nullptr ) ;
auto forever1 = RepeatForever : : create ( seq1 ) ;
sister - > runAction ( forever1 ) ;
sister - > setCameraMask ( ( int ) CameraFlag : : USER1 ) ;
}
void Issue16100Test : : onExit ( )
{
TestCocosNodeDemo : : onExit ( ) ;
}
std : : string Issue16100Test : : title ( ) const
{
return " Issue 16100 " ;
}
std : : string Issue16100Test : : subtitle ( ) const
{
return " Sprite should appear on the screen " ;
}
2016-10-31 10:29:04 +08:00
//------------------------------------------------------------------
//
// Issue16735Test
//
//------------------------------------------------------------------
void Issue16735Test : : onEnter ( )
{
TestCocosNodeDemo : : onEnter ( ) ;
auto visibleSize = Director : : getInstance ( ) - > getVisibleSize ( ) ;
auto origin = Director : : getInstance ( ) - > getVisibleOrigin ( ) ;
auto sprite1 = Sprite : : create ( " Images/grossini.png " ) ;
sprite1 - > setPosition ( Vec2 ( visibleSize / 2 ) + origin ) ;
addChild ( sprite1 ) ;
auto sprite2 = Sprite : : create ( " Images/grossini.png " ) ;
sprite2 - > setPosition ( Vec2 ( visibleSize / 2 ) + origin ) ;
sprite2 - > setSkewX ( 30 ) ;
sprite2 - > setScale ( 2 ) ;
sprite2 - > setRotation ( 30 ) ;
addChild ( sprite2 ) ;
auto d = DrawNode : : create ( ) ;
d - > drawLine ( Vec2 ( origin . x , origin . y + visibleSize . height / 2 ) , Vec2 ( origin . x + visibleSize . width , origin . y + visibleSize . height / 2 ) , Color4F : : RED ) ;
d - > drawLine ( Vec2 ( origin . x + visibleSize . width / 2 , origin . y ) , Vec2 ( origin . x + visibleSize . width / 2 , origin . y + visibleSize . height ) , Color4F : : RED ) ;
addChild ( d ) ;
}
void Issue16735Test : : onExit ( )
{
TestCocosNodeDemo : : onExit ( ) ;
}
std : : string Issue16735Test : : title ( ) const
{
return " Issue 16735 " ;
}
std : : string Issue16735Test : : subtitle ( ) const
{
return " Sprite should appear on the center of screen " ;
}