axmol/cocos/editor-support/cocostudio/CCSkin.cpp

246 lines
6.7 KiB
C++
Raw Normal View History

2013-06-06 12:02:54 +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.
****************************************************************************/
#include "CCSpriteFrame.h"
#include "CCSpriteFrameCache.h"
#include "CCDirector.h"
#include "renderer/CCRenderer.h"
#include "cocostudio/CCSkin.h"
#include "cocostudio/CCTransformHelp.h"
#include "cocostudio/CCSpriteFrameCacheHelper.h"
#include "cocostudio/CCArmature.h"
2013-06-06 12:02:54 +08:00
using namespace cocos2d;
namespace cocostudio {
2013-06-06 12:02:54 +08:00
#if CC_SPRITEBATCHNODE_RENDER_SUBPIXEL
#define RENDER_IN_SUBPIXEL
#else
#define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
#endif
2013-10-30 09:41:40 +08:00
#define SET_VERTEX3F(_v_, _x_, _y_, _z_) (_v_).x = (_x_); (_v_).y = (_y_); (_v_).z = (_z_);
Skin *Skin::create()
2013-06-06 12:02:54 +08:00
{
Skin *skin = new Skin();
2013-06-07 10:52:32 +08:00
if(skin && skin->init())
{
skin->autorelease();
return skin;
}
CC_SAFE_DELETE(skin);
return nullptr;
2013-06-06 12:02:54 +08:00
}
Skin *Skin::createWithSpriteFrameName(const std::string& pszSpriteFrameName)
2013-06-06 12:02:54 +08:00
{
Skin *skin = new Skin();
2013-06-07 10:52:32 +08:00
if(skin && skin->initWithSpriteFrameName(pszSpriteFrameName))
{
skin->autorelease();
return skin;
}
CC_SAFE_DELETE(skin);
return nullptr;
2013-06-06 12:02:54 +08:00
}
Skin *Skin::create(const std::string& pszFileName)
2013-06-06 12:02:54 +08:00
{
Skin *skin = new Skin();
2013-09-13 18:07:37 +08:00
if(skin && skin->initWithFile(pszFileName))
{
skin->autorelease();
return skin;
}
CC_SAFE_DELETE(skin);
return nullptr;
2013-09-13 18:07:37 +08:00
}
Skin::Skin()
: _bone(nullptr)
, _armature(nullptr)
2013-09-15 20:24:25 +08:00
, _displayName("")
2013-09-13 18:07:37 +08:00
{
_skinTransform = Matrix::identity();
2013-09-13 18:07:37 +08:00
}
2013-11-06 16:37:30 +08:00
bool Skin::initWithSpriteFrameName(const std::string& spriteFrameName)
2013-09-13 18:07:37 +08:00
{
2013-11-06 16:37:30 +08:00
CCAssert(spriteFrameName != "", "");
2013-09-13 18:07:37 +08:00
2013-11-06 16:37:30 +08:00
SpriteFrame *pFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(spriteFrameName);
2013-10-30 09:41:40 +08:00
bool ret = true;
2013-09-13 18:07:37 +08:00
if (pFrame != nullptr)
2013-10-30 09:41:40 +08:00
{
ret = initWithSpriteFrame(pFrame);
}
else
{
2013-11-06 16:37:30 +08:00
CCLOG("Cann't find CCSpriteFrame with %s. Please check your .plist file", spriteFrameName.c_str());
2013-10-30 09:41:40 +08:00
ret = false;
2013-09-13 18:07:37 +08:00
}
2013-11-06 16:37:30 +08:00
_displayName = spriteFrameName;
2013-10-30 09:41:40 +08:00
2013-09-13 18:07:37 +08:00
return ret;
2013-06-06 12:02:54 +08:00
}
2013-11-06 16:37:30 +08:00
bool Skin::initWithFile(const std::string& filename)
2013-06-06 12:02:54 +08:00
{
2013-11-06 16:37:30 +08:00
bool ret = Sprite::initWithFile(filename);
2013-06-06 12:02:54 +08:00
2013-11-06 16:37:30 +08:00
_displayName = filename;
2013-06-06 12:02:54 +08:00
2013-09-13 18:07:37 +08:00
return ret;
2013-06-06 12:02:54 +08:00
}
void Skin::setSkinData(const BaseData &var)
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
_skinData = var;
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
setScaleX(_skinData.scaleX);
setScaleY(_skinData.scaleY);
2014-03-24 10:12:40 +08:00
setRotationSkewX(CC_RADIANS_TO_DEGREES(_skinData.skewX));
setRotationSkewY(CC_RADIANS_TO_DEGREES(-_skinData.skewY));
2013-09-15 20:24:25 +08:00
setPosition(Point(_skinData.x, _skinData.y));
2013-09-13 18:07:37 +08:00
2013-09-15 20:24:25 +08:00
_skinTransform = getNodeToParentTransform();
2013-10-29 16:57:06 +08:00
updateArmatureTransform();
2013-06-06 12:02:54 +08:00
}
const BaseData &Skin::getSkinData() const
2013-06-06 12:02:54 +08:00
{
2013-09-15 20:24:25 +08:00
return _skinData;
2013-06-06 12:02:54 +08:00
}
void Skin::updateArmatureTransform()
2013-09-13 18:07:37 +08:00
{
2013-12-17 17:00:01 +08:00
_transform = TransformConcat(_bone->getNodeToArmatureTransform(), _skinTransform);
// if(_armature && _armature->getBatchNode())
// {
// _transform = TransformConcat(_transform, _armature->getNodeToParentTransform());
// }
2013-09-13 18:07:37 +08:00
}
void Skin::updateTransform()
2013-06-06 12:02:54 +08:00
{
2013-06-07 10:52:32 +08:00
// If it is not visible, or one of its ancestors is not visible, then do nothing:
if( !_visible)
2013-06-07 10:52:32 +08:00
{
2014-04-02 15:06:16 +08:00
_quad.br.vertices = _quad.tl.vertices = _quad.tr.vertices = _quad.bl.vertices = Vector3(0, 0, 0);
2013-06-07 10:52:32 +08:00
}
else
{
//
// calculate the Quad based on the Affine Matrix
//
2013-09-13 18:07:37 +08:00
Size &size = _rect.size;
2013-06-07 10:52:32 +08:00
float x1 = _offsetPosition.x;
float y1 = _offsetPosition.y;
2013-06-07 10:52:32 +08:00
float x2 = x1 + size.width;
float y2 = y1 + size.height;
2014-04-08 23:05:33 +08:00
float x = _transform.m[12];
float y = _transform.m[13];
2013-06-07 10:52:32 +08:00
2014-04-08 23:05:33 +08:00
float cr = _transform.m[0];
float sr = _transform.m[1];
float cr2 = _transform.m[5];
float sr2 = -_transform.m[4];
2013-06-07 10:52:32 +08:00
float ax = x1 * cr - y1 * sr2 + x;
float ay = x1 * sr + y1 * cr2 + y;
float bx = x2 * cr - y1 * sr2 + x;
float by = x2 * sr + y1 * cr2 + y;
float cx = x2 * cr - y2 * sr2 + x;
float cy = x2 * sr + y2 * cr2 + y;
float dx = x1 * cr - y2 * sr2 + x;
float dy = x1 * sr + y2 * cr2 + y;
2014-02-23 11:16:42 +08:00
SET_VERTEX3F( _quad.bl.vertices, RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), _positionZ );
SET_VERTEX3F( _quad.br.vertices, RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), _positionZ );
SET_VERTEX3F( _quad.tl.vertices, RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), _positionZ );
SET_VERTEX3F( _quad.tr.vertices, RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), _positionZ );
2013-06-07 10:52:32 +08:00
}
// MARMALADE CHANGE: ADDED CHECK FOR nullptr, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
if (_textureAtlas)
2013-06-07 10:52:32 +08:00
{
_textureAtlas->updateQuad(&_quad, _textureAtlas->getTotalQuads());
2013-06-07 10:52:32 +08:00
}
2013-06-06 12:02:54 +08:00
}
Matrix Skin::getNodeToWorldTransform() const
2013-09-13 18:07:37 +08:00
{
return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(), _transform);
2013-09-13 18:07:37 +08:00
}
Matrix Skin::getNodeToWorldTransformAR() const
2013-09-13 18:07:37 +08:00
{
2014-04-11 17:25:28 +08:00
Matrix displayTransform = _transform;
2013-09-13 18:07:37 +08:00
Point anchorPoint = _anchorPointInPoints;
2013-12-11 03:07:15 +08:00
anchorPoint = PointApplyTransform(anchorPoint, displayTransform);
2013-09-13 18:07:37 +08:00
2014-04-11 17:25:28 +08:00
displayTransform.m[12] = anchorPoint.x;
displayTransform.m[13] = anchorPoint.y;
2013-09-13 18:07:37 +08:00
return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(),displayTransform);
2013-09-13 18:07:37 +08:00
}
2014-04-08 22:07:35 +08:00
void Skin::draw(Renderer *renderer, const Matrix &transform, bool transformUpdated)
2013-12-19 10:42:06 +08:00
{
2014-04-11 17:25:28 +08:00
Matrix mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2013-12-19 10:42:06 +08:00
//TODO implement z order
_quadCommand.init(_globalZOrder, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1, mv);
renderer->addCommand(&_quadCommand);
2013-12-19 10:42:06 +08:00
}
2013-10-30 09:41:40 +08:00
void Skin::setBone(Bone *bone)
{
_bone = bone;
if(Armature *armature = _bone->getArmature())
{
_armature = armature;
}
}
2013-11-05 19:53:38 +08:00
Bone *Skin::getBone() const
2013-10-30 09:41:40 +08:00
{
return _bone;
}
}