From 3c0779885d4901724f9af732a7831914820ae606 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Wed, 19 Nov 2014 16:50:09 +0800 Subject: [PATCH 1/2] refact reskin test code --- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 106 ++++++++---------- .../Classes/Sprite3DTest/Sprite3DTest.h | 12 +- 2 files changed, 55 insertions(+), 63 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 0dd338e502..44c0a9ed42 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1102,69 +1102,38 @@ Sprite3DReskinTest::Sprite3DReskinTest() _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); TTFConfig ttfConfig("fonts/arial.ttf", 20); auto label1 = Label::createWithTTF(ttfConfig,"Hair"); - auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchHair,this) ); + auto item1 = MenuItemLabel::create(label1,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label2 = Label::createWithTTF(ttfConfig,"Glasses"); - auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchGlasses,this) ); + auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label3 = Label::createWithTTF(ttfConfig,"Coat"); - auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchCoat,this) ); + auto item3 = MenuItemLabel::create(label3,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label4 = Label::createWithTTF(ttfConfig,"Pants"); - auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchPants,this) ); + auto item4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); auto label5 = Label::createWithTTF(ttfConfig,"Shoes"); - auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_switchShoes,this) ); + auto item5 = MenuItemLabel::create(label5,CC_CALLBACK_1(Sprite3DReskinTest::menuCallback_reSkin,this) ); item1->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*4 ) ); item2->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *5 ) ); item3->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height*6 ) ); item4->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *7 ) ); item5->setPosition( Vec2(VisibleRect::left().x+50, VisibleRect::bottom().y+item1->getContentSize().height *8 ) ); + item1->setUserData((void*)SkinType::HAIR); + item2->setUserData((void*)SkinType::GLASSES); + item3->setUserData((void*)SkinType::UPPER_BODY); + item4->setUserData((void*)SkinType::PANTS); + item5->setUserData((void*)SkinType::SHOES); auto pMenu1 = CCMenu::create(item1, item2, item3, item4, item5, nullptr); pMenu1->setPosition(Vec2(0,0)); this->addChild(pMenu1, 10); } -void Sprite3DReskinTest::menuCallback_switchHair(Ref* sender) +void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { - std::string str = _curSkin[SkinType::HAIR]; - if (str == "Girl_Hair01") - _curSkin[SkinType::HAIR] = "Girl_Hair02"; - else - _curSkin[SkinType::HAIR] = "Girl_Hair01"; - applyCurSkin(); -} -void Sprite3DReskinTest::menuCallback_switchGlasses(Ref* sender) -{ - std::string str = _curSkin[SkinType::GLASSES]; - if (str == "Girl_Glasses01") - _curSkin[SkinType::GLASSES] = ""; - else - _curSkin[SkinType::GLASSES] = "Girl_Glasses01"; - applyCurSkin(); -} -void Sprite3DReskinTest::menuCallback_switchCoat(Ref* sender) -{ - std::string str = _curSkin[SkinType::UPPER_BODY]; - if (str == "Girl_UpperBody01") - _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody02"; - else - _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; - applyCurSkin(); -} -void Sprite3DReskinTest::menuCallback_switchPants(Ref* sender) -{ - std::string str = _curSkin[SkinType::PANTS]; - if (str == "Girl_LowerBody01") - _curSkin[SkinType::PANTS] = "Girl_LowerBody02"; - else - _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; - applyCurSkin(); -} -void Sprite3DReskinTest::menuCallback_switchShoes(Ref* sender) -{ - std::string strShoes = _curSkin[SkinType::SHOES]; - if (strShoes == "Girl_Shoes01") - _curSkin[SkinType::SHOES] = "Girl_Shoes02"; - else - _curSkin[SkinType::SHOES] = "Girl_Shoes01"; - applyCurSkin(); + int index = (int)(((MenuItemLabel*)sender)->getUserData()); + if (index < (int)SkinType::MAX_TYPE) + { + _curSkin[index] = (_curSkin[index] + 1) % _skins[index].size(); + applyCurSkin(); + } } std::string Sprite3DReskinTest::title() const @@ -1193,13 +1162,36 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) } _sprite = sprite; - _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; - _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; - _curSkin[SkinType::SHOES] = "Girl_Shoes01"; - _curSkin[SkinType::HAIR] = "Girl_Hair01"; - _curSkin[SkinType::FACE] = "Girl_Face01"; - _curSkin[SkinType::HAND] = "Girl_Hand01"; - _curSkin[SkinType::GLASSES] = ""; + auto& body = _skins[(int)SkinType::UPPER_BODY]; + body.push_back("Girl_UpperBody01"); + body.push_back("Girl_UpperBody02"); + + auto& pants = _skins[(int)SkinType::PANTS]; + pants.push_back("Girl_LowerBody01"); + pants.push_back("Girl_LowerBody02"); + + auto& shoes = _skins[(int)SkinType::SHOES]; + shoes.push_back("Girl_Shoes01"); + shoes.push_back("Girl_Shoes02"); + + auto& hair = _skins[(int)SkinType::HAIR]; + hair.push_back("Girl_Hair01"); + hair.push_back("Girl_Hair02"); + + auto& face = _skins[(int)SkinType::FACE]; + face.push_back("Girl_Face01"); + face.push_back("Girl_Face02"); + + auto& hand = _skins[(int)SkinType::HAND]; + hand.push_back("Girl_Hand01"); + hand.push_back("Girl_Hand02"); + + auto& glasses = _skins[(int)SkinType::GLASSES]; + glasses.push_back(""); + glasses.push_back("Girl_Glasses01"); + + memset(_curSkin, 0, (int)SkinType::MAX_TYPE * sizeof(int)); + applyCurSkin(); } @@ -1212,8 +1204,8 @@ void Sprite3DReskinTest::applyCurSkin() for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) { auto mesh = _sprite->getMeshByIndex(static_cast(i)); bool isVisible = false; - for (auto& it : _curSkin) { - if (mesh->getName() == it.second) + for (int j = 0; j < (int)SkinType::MAX_TYPE; j++) { + if (mesh->getName() == _skins[j].at(_curSkin[j])) { isVisible = true; break; diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 80e471a45b..232d1d98b6 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -268,11 +268,8 @@ public: virtual std::string subtitle() const override; void onTouchesEnded(const std::vector& touches, Event* event); void addNewSpriteWithCoords(Vec2 p); - void menuCallback_switchHair(Ref* sender); - void menuCallback_switchGlasses(Ref* sender); - void menuCallback_switchCoat(Ref* sender); - void menuCallback_switchPants(Ref* sender); - void menuCallback_switchShoes(Ref* sender); + + void menuCallback_reSkin(Ref* sender); protected: void applyCurSkin(); @@ -285,8 +282,11 @@ protected: FACE, HAND, GLASSES, + MAX_TYPE, }; - std::map _curSkin; + + std::vector _skins[(int)SkinType::MAX_TYPE]; //all skins + int _curSkin[(int)SkinType::MAX_TYPE]; //current skin index cocos2d::Sprite3D* _sprite; }; class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo From 437ee04d5deb01de32f6c32de22380f625177702 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Fri, 28 Nov 2014 14:12:38 +0800 Subject: [PATCH 2/2] fix linux --- tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp | 2 +- tools/cocos2d-console | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 44c0a9ed42..4614aa248c 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1128,7 +1128,7 @@ Sprite3DReskinTest::Sprite3DReskinTest() } void Sprite3DReskinTest::menuCallback_reSkin(Ref* sender) { - int index = (int)(((MenuItemLabel*)sender)->getUserData()); + long index = (long)(((MenuItemLabel*)sender)->getUserData()); if (index < (int)SkinType::MAX_TYPE) { _curSkin[index] = (_curSkin[index] + 1) % _skins[index].size(); diff --git a/tools/cocos2d-console b/tools/cocos2d-console index 40e5835664..aa82951329 160000 --- a/tools/cocos2d-console +++ b/tools/cocos2d-console @@ -1 +1 @@ -Subproject commit 40e583566474a9e1e0ed1c7ab7a746f4fa354fa0 +Subproject commit aa829513296354961e814078e646108e9cb92a2d