2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-06-06 12:02:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CCDisplayFactory.h"
|
|
|
|
#include "cocostudio/CCBone.h"
|
|
|
|
#include "cocostudio/CCArmature.h"
|
|
|
|
#include "cocostudio/CCSkin.h"
|
|
|
|
#include "cocostudio/CCArmatureDataManager.h"
|
|
|
|
#include "cocostudio/CCTransformHelp.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCParticleSystemQuad.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +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-09-15 19:08:45 +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-10-30 09:41:40 +08:00
|
|
|
void DisplayFactory::updateDisplay(Bone *bone, float dt, bool dirty)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-30 09:41:40 +08:00
|
|
|
Node *display = bone->getDisplayRenderNode();
|
|
|
|
CS_RETURN_IF(!display);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-12-09 11:14:53 +08:00
|
|
|
switch(bone->getDisplayRenderNodeType())
|
|
|
|
{
|
|
|
|
case CS_DISPLAY_SPRITE:
|
|
|
|
if (dirty)
|
|
|
|
{
|
|
|
|
static_cast<Skin*>(display)->updateArmatureTransform();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_PARTICLE:
|
|
|
|
updateParticleDisplay(bone, display, dt);
|
|
|
|
break;
|
|
|
|
case CS_DISPLAY_ARMATURE:
|
|
|
|
updateArmatureDisplay(bone, display, dt);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 transform = bone->getNodeToArmatureTransform();
|
2014-03-02 00:26:54 +08:00
|
|
|
display->setAdditionalTransform(&transform);
|
2013-12-09 11:14:53 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2013-06-07 10:52:32 +08:00
|
|
|
if (dirty)
|
|
|
|
{
|
2013-10-30 09:41:40 +08:00
|
|
|
DecorativeDisplay *decoDisplay = bone->getDisplayManager()->getCurrentDecorativeDisplay();
|
2013-09-15 19:08:45 +08:00
|
|
|
ColliderDetector *detector = decoDisplay->getColliderDetector();
|
2013-06-07 10:52:32 +08:00
|
|
|
if (detector)
|
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
|
2013-09-13 18:07:37 +08:00
|
|
|
CC_BREAK_IF(!detector->getBody());
|
2013-12-06 13:40:49 +08:00
|
|
|
#endif
|
2013-09-16 14:13:55 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 displayTransform = display->getNodeToParentTransform();
|
|
|
|
Vec2 anchorPoint = display->getAnchorPointInPoints();
|
2013-12-11 03:07:15 +08:00
|
|
|
anchorPoint = PointApplyTransform(anchorPoint, displayTransform);
|
2014-04-11 17:25:28 +08:00
|
|
|
displayTransform.m[12] = anchorPoint.x;
|
|
|
|
displayTransform.m[13] = anchorPoint.y;
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 t = TransformConcat( bone->getArmature()->getNodeToParentTransform(),displayTransform);
|
2013-09-13 18:07:37 +08:00
|
|
|
detector->updateTransform(t);
|
|
|
|
}
|
|
|
|
while (0);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::addSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +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-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::createSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-05 15:32:13 +08:00
|
|
|
Skin *skin = nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
SpriteDisplayData *displayData = (SpriteDisplayData *)decoDisplay->getDisplayData();
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
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
|
2015-09-01 11:26:09 +08:00
|
|
|
if(textureName.empty())
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
skin = Skin::create();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-15 19:08:45 +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-10-30 09:41:40 +08:00
|
|
|
decoDisplay->setDisplay(skin);
|
|
|
|
|
2013-11-05 15:32:13 +08:00
|
|
|
if (skin == nullptr)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
skin->setBone(bone);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
initSpriteDisplay(bone, decoDisplay, displayData->displayName.c_str(), skin);
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Armature *armature = bone->getArmature();
|
2013-09-13 18:07:37 +08:00
|
|
|
if (armature)
|
|
|
|
{
|
|
|
|
if (armature->getArmatureData()->dataVersion >= VERSION_COMBINED)
|
|
|
|
{
|
|
|
|
skin->setSkinData(displayData->skinData);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
skin->setSkinData(*bone->getBoneData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::initSpriteDisplay(Bone *bone, DecorativeDisplay *decoDisplay, const char *displayName, Skin *skin)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
|
|
|
//! remove .xxx
|
|
|
|
std::string textureName = displayName;
|
|
|
|
size_t startPos = textureName.find_last_of(".");
|
|
|
|
|
|
|
|
if(startPos != std::string::npos)
|
|
|
|
{
|
|
|
|
textureName = textureName.erase(startPos);
|
|
|
|
}
|
|
|
|
|
2013-09-16 15:16:05 +08:00
|
|
|
TextureData *textureData = ArmatureDataManager::getInstance()->getTextureData(textureName.c_str());
|
2013-06-07 10:52:32 +08:00
|
|
|
if(textureData)
|
|
|
|
{
|
|
|
|
//! Init display anchorPoint, every Texture have a anchor point
|
2014-05-15 01:07:09 +08:00
|
|
|
skin->setAnchorPoint(Vec2( textureData->pivotX, textureData->pivotY));
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
|
|
|
|
2013-12-06 13:40:49 +08:00
|
|
|
#if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT || ENABLE_PHYSICS_SAVE_CALCULATED_VERTEX
|
2013-12-13 19:40:38 +08:00
|
|
|
if (textureData && textureData->contourDataList.size() > 0)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
//! create ContourSprite
|
2013-09-15 19:08:45 +08:00
|
|
|
ColliderDetector *colliderDetector = ColliderDetector::create(bone);
|
2013-12-13 19:40:38 +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-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::addArmatureDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-30 09:41:40 +08:00
|
|
|
ArmatureDisplayData *adp = ArmatureDisplayData::create();
|
2013-09-15 19:08:45 +08:00
|
|
|
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-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::createArmatureDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
ArmatureDisplayData *displayData = (ArmatureDisplayData *)decoDisplay->getDisplayData();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Armature *armature = Armature::create(displayData->displayName.c_str(), bone);
|
2013-06-07 10:52:32 +08:00
|
|
|
|
|
|
|
decoDisplay->setDisplay(armature);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-10-30 09:41:40 +08:00
|
|
|
void DisplayFactory::updateArmatureDisplay(Bone *bone, Node *display, float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
Armature *armature = (Armature *)display;
|
2013-06-07 10:52:32 +08:00
|
|
|
if(armature)
|
|
|
|
{
|
|
|
|
armature->sortAllChildren();
|
|
|
|
armature->update(dt);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::addParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay, DisplayData *displayData)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-11-06 15:25:44 +08:00
|
|
|
ParticleDisplayData *adp = ParticleDisplayData::create();
|
2013-09-15 19:08:45 +08:00
|
|
|
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-09-15 19:08:45 +08:00
|
|
|
void DisplayFactory::createParticleDisplay(Bone *bone, DecorativeDisplay *decoDisplay)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-15 19:08:45 +08:00
|
|
|
ParticleDisplayData *displayData = (ParticleDisplayData *)decoDisplay->getDisplayData();
|
2013-12-06 13:40:49 +08:00
|
|
|
ParticleSystem *system = ParticleSystemQuad::create(displayData->displayName.c_str());
|
2013-11-06 11:20:16 +08:00
|
|
|
|
|
|
|
system->removeFromParent();
|
2014-03-03 14:56:56 +08:00
|
|
|
system->cleanup();
|
2013-11-06 11:20:16 +08:00
|
|
|
|
|
|
|
Armature *armature = bone->getArmature();
|
|
|
|
if (armature)
|
|
|
|
{
|
|
|
|
system->setParent(armature);
|
|
|
|
}
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
decoDisplay->setDisplay(system);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
2013-10-30 09:41:40 +08:00
|
|
|
void DisplayFactory::updateParticleDisplay(Bone *bone, Node *display, float dt)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-09-13 18:07:37 +08:00
|
|
|
ParticleSystem *system = (ParticleSystem *)display;
|
2013-09-15 19:08:45 +08:00
|
|
|
BaseData node;
|
|
|
|
TransformHelp::matrixToNode(bone->getNodeToArmatureTransform(), 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-10-15 18:00:03 +08:00
|
|
|
}
|