2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCSpriteFrame.h"
|
|
|
|
#include "2d/CCSpriteFrameCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2014-01-17 13:35:58 +08:00
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "editor-support/cocostudio/CCSkin.h"
|
|
|
|
#include "editor-support/cocostudio/CCTransformHelp.h"
|
|
|
|
#include "editor-support/cocostudio/CCArmature.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
#if CC_SPRITEBATCHNODE_RENDER_SUBPIXEL
|
|
|
|
#define RENDER_IN_SUBPIXEL
|
|
|
|
#else
|
|
|
|
#define RENDER_IN_SUBPIXEL(__ARGS__) (ceil(__ARGS__))
|
|
|
|
#endif
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Skin *Skin::create()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Skin *skin = new (std::nothrow) Skin();
|
2013-06-07 10:52:32 +08:00
|
|
|
if(skin && skin->init())
|
|
|
|
{
|
|
|
|
skin->autorelease();
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skin);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:05:46 +08:00
|
|
|
Skin *Skin::createWithSpriteFrameName(const std::string& pszSpriteFrameName)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Skin *skin = new (std::nothrow) Skin();
|
2013-06-07 10:52:32 +08:00
|
|
|
if(skin && skin->initWithSpriteFrameName(pszSpriteFrameName))
|
|
|
|
{
|
|
|
|
skin->autorelease();
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skin);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-23 14:05:46 +08:00
|
|
|
Skin *Skin::create(const std::string& pszFileName)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Skin *skin = new (std::nothrow) Skin();
|
2013-09-13 18:07:37 +08:00
|
|
|
if(skin && skin->initWithFile(pszFileName))
|
|
|
|
{
|
|
|
|
skin->autorelease();
|
|
|
|
return skin;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(skin);
|
2013-11-05 15:32:13 +08:00
|
|
|
return nullptr;
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Skin::Skin()
|
2013-11-05 15:32:13 +08:00
|
|
|
: _bone(nullptr)
|
|
|
|
, _armature(nullptr)
|
2013-09-15 20:24:25 +08:00
|
|
|
, _displayName("")
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
_skinTransform = Mat4::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
|
|
|
|
2013-11-05 15:32:13 +08:00
|
|
|
if (pFrame != nullptr)
|
2013-10-30 09:41:40 +08:00
|
|
|
{
|
|
|
|
ret = initWithSpriteFrame(pFrame);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-17 10:12:54 +08:00
|
|
|
CCLOG("Can'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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +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));
|
2014-08-28 11:41:18 +08:00
|
|
|
setPosition(_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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +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
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +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:
|
2013-06-15 14:03:30 +08:00
|
|
|
if( !_visible)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_quad.br.vertices.setZero();
|
|
|
|
_quad.tl.vertices.setZero();
|
|
|
|
_quad.tr.vertices.setZero();
|
|
|
|
_quad.bl.vertices.setZero();
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// calculate the Quad based on the Affine Matrix
|
|
|
|
//
|
2014-07-15 11:31:10 +08:00
|
|
|
Mat4 transform = getNodeToParentTransform();
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-09-13 18:07:37 +08:00
|
|
|
Size &size = _rect.size;
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +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;
|
|
|
|
|
2015-08-11 10:34:22 +08:00
|
|
|
if (_flippedX)
|
|
|
|
{
|
|
|
|
std::swap(x1, x2);
|
|
|
|
}
|
|
|
|
if (_flippedY)
|
|
|
|
{
|
|
|
|
std::swap(y1, y2);
|
|
|
|
}
|
|
|
|
|
2014-07-15 11:31:10 +08:00
|
|
|
float x = transform.m[12];
|
|
|
|
float y = transform.m[13];
|
2013-06-07 10:52:32 +08:00
|
|
|
|
2014-07-15 11:31:10 +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;
|
|
|
|
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_quad.bl.vertices.set(RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), _positionZ);
|
|
|
|
_quad.br.vertices.set(RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), _positionZ);
|
|
|
|
_quad.tl.vertices.set(RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), _positionZ);
|
|
|
|
_quad.tr.vertices.set(RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), _positionZ);
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-11-05 15:32:13 +08:00
|
|
|
// MARMALADE CHANGE: ADDED CHECK FOR nullptr, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_textureAtlas)
|
2013-06-07 10:52:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->updateQuad(&_quad, _textureAtlas->getTotalQuads());
|
2013-06-07 10:52:32 +08:00
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Skin::getNodeToWorldTransform() const
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2014-01-16 12:10:41 +08:00
|
|
|
return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(), _transform);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 Skin::getNodeToWorldTransformAR() const
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 displayTransform = _transform;
|
|
|
|
Vec2 anchorPoint = _anchorPointInPoints;
|
2013-09-13 18:07:37 +08:00
|
|
|
|
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
|
|
|
|
2014-01-24 11:05:34 +08:00
|
|
|
return TransformConcat( _bone->getArmature()->getNodeToWorldTransform(),displayTransform);
|
2013-09-13 18:07:37 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void Skin::draw(Renderer *renderer, const Mat4 &/*transform*/, uint32_t flags)
|
2013-12-19 10:42:06 +08:00
|
|
|
{
|
2014-11-07 09:17:32 +08:00
|
|
|
auto mv = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2013-12-19 10:42:06 +08:00
|
|
|
|
2016-07-25 17:31:54 +08:00
|
|
|
// TODO: implement z order
|
|
|
|
_quadCommand.init(_globalZOrder,
|
|
|
|
_texture,
|
|
|
|
getGLProgramState(),
|
|
|
|
_blendFunc,
|
|
|
|
&_quad,
|
|
|
|
1,
|
|
|
|
mv,
|
|
|
|
flags);
|
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|