2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCDisplayFactory.h"
|
|
|
|
#include "../CCBone.h"
|
|
|
|
#include "../CCArmature.h"
|
|
|
|
#include "../display/CCSkin.h"
|
|
|
|
#include "../display/CCShaderNode.h"
|
|
|
|
#include "../utils/CCSpriteFrameCacheHelper.h"
|
|
|
|
#include "../utils/CCArmatureDataManager.h"
|
|
|
|
#include "../utils/CCTransformHelp.h"
|
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
namespace cocos2d { namespace extension { namespace armature {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::addDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
switch(displayData->displayType)
|
|
|
|
{
|
|
|
|
case CS_DISPLAY_SPRITE:
|
|
|
|
addSpriteDisplay(bone, decoDisplay, displayData);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_PARTICLE:
|
|
|
|
addParticleDisplay(bone, decoDisplay, displayData);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_ARMATURE:
|
|
|
|
addArmatureDisplay(bone, decoDisplay, displayData);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::createDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
switch(decoDisplay->getDisplayData()->displayType)
|
|
|
|
{
|
|
|
|
case CS_DISPLAY_SPRITE:
|
|
|
|
createSpriteDisplay(bone, decoDisplay);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_PARTICLE:
|
|
|
|
createParticleDisplay(bone, decoDisplay);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_ARMATURE:
|
|
|
|
createArmatureDisplay(bone, decoDisplay);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::updateDisplay(Bone *bone, DecorativeDisplay *decoDisplay, float dt, bool dirty)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CS_RETURN_IF(!decoDisplay);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_DETECT
|
2013-06-07 10:52:32 +08:00
|
|
|
if (dirty)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector *detector = decoDisplay->getColliderDetector();
|
2013-06-07 10:52:32 +08:00
|
|
|
if (detector)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
AffineTransform t = AffineTransformConcat(bone->nodeToArmatureTransform(), bone->getArmature()->nodeToWorldTransform());
|
2013-06-07 10:52:32 +08:00
|
|
|
detector->updateTransform(t);
|
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
#endif
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
switch(decoDisplay->getDisplayData()->displayType)
|
|
|
|
{
|
|
|
|
case CS_DISPLAY_SPRITE:
|
|
|
|
updateSpriteDisplay(bone, decoDisplay, dt, dirty);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_PARTICLE:
|
|
|
|
updateParticleDisplay(bone, decoDisplay, dt, dirty);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_ARMATURE:
|
|
|
|
updateArmatureDisplay(bone, decoDisplay, dt, dirty);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::addSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
SpriteDisplayData *sdp = SpriteDisplayData::create();
|
|
|
|
sdp->copy((SpriteDisplayData *)displayData);
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplayData(sdp);
|
|
|
|
createSpriteDisplay(bone, decoDisplay);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::createSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Skin *skin = NULL;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
SpriteDisplayData *displayData = (SpriteDisplayData *)decoDisplay->getDisplayData();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
//! remove .xxx
|
|
|
|
std::string textureName = displayData->displayName;
|
|
|
|
size_t startPos = textureName.find_last_of(".");
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
if(startPos != std::string::npos)
|
|
|
|
{
|
|
|
|
textureName = textureName.erase(startPos);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
//! create display
|
2013-06-07 14:01:03 +08:00
|
|
|
if(textureName.length() == 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
skin = Skin::create();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
skin = Skin::createWithSpriteFrameName((textureName + ".png").c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TextureAtlas *atlas = SpriteFrameCacheHelper::sharedSpriteFrameCacheHelper()->getTextureAtlas((textureName + ".png").c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
skin->setTextureAtlas(atlas);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TextureData *textureData = ArmatureDataManager::sharedArmatureDataManager()->getTextureData(textureName.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
if(textureData)
|
|
|
|
{
|
|
|
|
//! Init display anchorPoint, every Texture have a anchor point
|
|
|
|
skin->setAnchorPoint(ccp( textureData->pivotX, textureData->pivotY));
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
skin->setBone(bone);
|
|
|
|
skin->setSkinData(*bone->getBoneData());
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplay(skin);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
#if ENABLE_PHYSICS_DETECT
|
2013-06-07 10:52:32 +08:00
|
|
|
if (textureData && textureData->contourDataList.count() > 0)
|
|
|
|
{
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
//! create ContourSprite
|
2013-06-20 14:15:53 +08:00
|
|
|
ColliderDetector *colliderDetector = ColliderDetector::create(bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
colliderDetector->addContourDataList(&textureData->contourDataList);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setColliderDetector(colliderDetector);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::updateSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay, float dt, bool dirty)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Skin *skin = (Skin *)decoDisplay->getDisplay();
|
2013-06-07 10:52:32 +08:00
|
|
|
skin->updateTransform();
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::addArmatureDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureDisplayData *adp = ArmatureDisplayData::create(); ;
|
|
|
|
adp->copy((ArmatureDisplayData *)displayData);
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplayData(adp);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
createArmatureDisplay(bone, decoDisplay);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::createArmatureDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ArmatureDisplayData *displayData = (ArmatureDisplayData *)decoDisplay->getDisplayData();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = Armature::create(displayData->displayName.c_str(), bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-06 12:02:54 +08:00
|
|
|
/*
|
|
|
|
* because this bone have called this name, so armature should change it's name, or it can't add to
|
2013-06-20 14:15:53 +08:00
|
|
|
* Armature's bone children.
|
2013-06-06 12:02:54 +08:00
|
|
|
*/
|
|
|
|
armature->setName((bone->getName() + "_armatureChild").c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
decoDisplay->setDisplay(armature);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::updateArmatureDisplay(Bone *bone, DecorativeDisplay *decoDisplay, float dt, bool dirty)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
CS_RETURN_IF(!dirty);
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Armature *armature = bone->getChildArmature();
|
2013-06-07 10:52:32 +08:00
|
|
|
if(armature)
|
|
|
|
{
|
|
|
|
armature->sortAllChildren();
|
|
|
|
armature->update(dt);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::addParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ParticleDisplayData *adp = ParticleDisplayData::create(); ;
|
|
|
|
adp->copy((ParticleDisplayData *)displayData);
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplayData(adp);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
createParticleDisplay(bone, decoDisplay);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::createParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ParticleDisplayData *displayData = (ParticleDisplayData *)decoDisplay->getDisplayData();
|
|
|
|
ParticleSystem *system = ParticleSystemQuad::create(displayData->plist.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplay(system);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::updateParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay, float dt, bool dirty)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ParticleSystem *system = (ParticleSystem *)decoDisplay->getDisplay();
|
|
|
|
BaseData node;
|
|
|
|
TransformHelp::matrixToNode(bone->nodeToArmatureTransform(), node);
|
2013-06-07 10:52:32 +08:00
|
|
|
system->setPosition(node.x, node.y);
|
|
|
|
system->setScaleX(node.scaleX);
|
|
|
|
system->setScaleY(node.scaleY);
|
|
|
|
system->update(dt);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::addShaderDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ShaderDisplayData *sdp = ShaderDisplayData::create();
|
|
|
|
sdp->copy((ShaderDisplayData *)displayData);
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplayData(sdp);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
createShaderDisplay(bone, decoDisplay);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-06-20 14:15:53 +08:00
|
|
|
void DisplayFactory::createShaderDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ShaderDisplayData *displayData = (ShaderDisplayData *)decoDisplay->getDisplayData();
|
|
|
|
ShaderNode *sn = ShaderNode::shaderNodeWithVertex(displayData->vert.c_str(), displayData->frag.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplay(sn);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-17 21:32:15 +08:00
|
|
|
}}} // namespace cocos2d { namespace extension { namespace armature {
|