refact reskin code

This commit is contained in:
yangxiao 2014-10-25 14:11:32 +08:00
parent 0696041ea9
commit 20decd5a39
2 changed files with 70 additions and 146 deletions

View File

@ -1027,124 +1027,50 @@ Sprite3DReskinTest::Sprite3DReskinTest()
}
void Sprite3DReskinTest::menuCallback_switchHair(Ref* sender)
{
_useHairId++;
if(_useHairId > 1 )
{
_useHairId = 0;
}
if(_useHairId >= 0 && _sprite)
{
for(int i = 0; i < 2; i++ )
{
auto subMesh = _sprite->getMeshByName(_girlHair[i]);
if(subMesh)
{
if(i == _useHairId )
{
subMesh->setVisible(true);
}
else
{
subMesh->setVisible(false);
}
}
}
}
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)
{
auto subMesh = _sprite->getMeshByName("Girl_Glasses01");
if(subMesh)
{
if(subMesh->isVisible())
{
subMesh->setVisible(false);
}
else
{
subMesh->setVisible(true);
}
}
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)
{
_useUpBodyId++;
if(_useUpBodyId > 1 )
{
_useUpBodyId = 0;
}
if(_useUpBodyId >= 0 && _sprite)
{
for(int i = 0; i < 2; i++ )
{
auto subMesh = _sprite->getMeshByName(_girlUpperBody[i]);
if(subMesh)
{
if(i == _useUpBodyId )
{
subMesh->setVisible(true);
}
else
{
subMesh->setVisible(false);
}
}
}
}
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)
{
_usePantsId++;
if(_usePantsId > 1 )
{
_usePantsId = 0;
}
if(_usePantsId >= 0 && _sprite)
{
for(int i = 0; i < 2; i++ )
{
auto subMesh = _sprite->getMeshByName(_girlPants[i]);
if(subMesh)
{
if(i == _usePantsId )
{
subMesh->setVisible(true);
}
else
{
subMesh->setVisible(false);
}
}
}
}
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)
{
_useShoesId++;
if(_useShoesId > 1 )
{
_useShoesId = 0;
}
if(_useShoesId >= 0 && _sprite)
{
for(int i = 0; i < 2; i++ )
{
auto subMesh = _sprite->getMeshByName(_girlShoes[i]);
if(subMesh)
{
if(i == _useShoesId )
{
subMesh->setVisible(true);
}
else
{
subMesh->setVisible(false);
}
}
}
}
std::string strShoes = _curSkin[SkinType::SHOES];
if (strShoes == "Girl_Shoes01")
_curSkin[SkinType::SHOES] = "Girl_Shoes02";
else
_curSkin[SkinType::SHOES] = "Girl_Shoes01";
applyCurSkin();
}
std::string Sprite3DReskinTest::title() const
{
return "Testing Sprite3D Reskin";
@ -1156,43 +1082,10 @@ std::string Sprite3DReskinTest::subtitle() const
void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p)
{
_girlPants[0]= "Girl_LowerBody01";
_girlPants[1]= "Girl_LowerBody02";
_girlUpperBody[0] = "Girl_UpperBody01";
_girlUpperBody[1] = "Girl_UpperBody02";
_girlShoes[0] = "Girl_Shoes01";
_girlShoes[1] = "Girl_Shoes02";
_girlHair[0]= "Girl_Hair01";
_girlHair[1]= "Girl_Hair02";
_usePantsId = 0;
_useUpBodyId = 0;
_useShoesId =0;
_useHairId = 0;
std::string fileName = "Sprite3DTest/ReskinGirl.c3b";
auto sprite = Sprite3D::create(fileName);
sprite->setScale(4);
sprite->setRotation3D(Vec3(0,0,0));
auto girlPants = sprite->getMeshByName(_girlPants[1]);
if(girlPants)
{
girlPants->setVisible(false);
}
auto girlShoes = sprite->getMeshByName(_girlShoes[1]);
if(girlShoes)
{
girlShoes->setVisible(false);
}
auto girlHair = sprite->getMeshByName(_girlHair[1]);
if(girlHair)
{
girlHair->setVisible(false);
}
auto girlUpBody = sprite->getMeshByName( _girlUpperBody[1]);
if(girlUpBody)
{
girlUpBody->setVisible(false);
}
addChild(sprite);
sprite->setPosition( Vec2( p.x, p.y-60) );
auto animation = Animation3D::create(fileName);
@ -1203,11 +1096,37 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p)
sprite->runAction(RepeatForever::create(animate));
}
_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] = "";
applyCurSkin();
}
void Sprite3DReskinTest::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
}
void Sprite3DReskinTest::applyCurSkin()
{
for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) {
auto mesh = _sprite->getMeshByIndex(i);
bool isVisible = false;
for (auto& it : _curSkin) {
if (mesh->getName() == it.second)
{
isVisible = true;
break;
}
}
_sprite->getMeshByIndex(i)->setVisible(isVisible);
}
}
Sprite3DWithOBBPerfromanceTest::Sprite3DWithOBBPerfromanceTest()
{
auto listener = EventListenerTouchAllAtOnce::create();

View File

@ -261,14 +261,19 @@ public:
void menuCallback_switchPants(Ref* sender);
void menuCallback_switchShoes(Ref* sender);
protected:
std::string _girlPants[2];
int _usePantsId;
std::string _girlUpperBody[2];
int _useUpBodyId;
std::string _girlShoes[2];
int _useShoesId;
std::string _girlHair[2];
int _useHairId;
void applyCurSkin();
enum class SkinType
{
UPPER_BODY = 0,
PANTS,
SHOES,
HAIR,
FACE,
HAND,
GLASSES,
};
std::map<SkinType, std::string> _curSkin;
cocos2d::Sprite3D* _sprite;
};
class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo