2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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 "2d/CCAnimation.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new AnimationFrame();
|
|
|
|
if (ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationFrame::AnimationFrame() : _spriteFrame(nullptr), _delayUnits(0.0f) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationFrame::~AnimationFrame()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing AnimationFrame: %p", this);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
CC_SAFE_RELEASE(_spriteFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimationFrame* AnimationFrame::clone() const
|
|
|
|
{
|
|
|
|
// no copy constructor
|
2021-12-08 00:11:53 +08:00
|
|
|
auto frame = new AnimationFrame();
|
2021-12-25 10:04:45 +08:00
|
|
|
frame->initWithSpriteFrame(_spriteFrame->clone(), _delayUnits, _userInfo);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
frame->autorelease();
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
// implementation of Animation
|
|
|
|
|
|
|
|
Animation* Animation::create()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2019-11-23 20:27:39 +08:00
|
|
|
animation->init();
|
|
|
|
animation->autorelease();
|
|
|
|
|
|
|
|
return animation;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* Animation::createWithSpriteFrames(const Vector<SpriteFrame*>& frames,
|
|
|
|
float delay /* = 0.0f*/,
|
|
|
|
unsigned int loops /* = 1*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2019-11-23 20:27:39 +08:00
|
|
|
animation->initWithSpriteFrames(frames, delay, loops);
|
|
|
|
animation->autorelease();
|
|
|
|
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* Animation::create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames,
|
|
|
|
float delayPerUnit,
|
|
|
|
unsigned int loops /* = 1 */)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2019-11-23 20:27:39 +08:00
|
|
|
animation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
|
|
|
animation->autorelease();
|
|
|
|
return animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Animation::init()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
_delayPerUnit = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Animation::initWithSpriteFrames(const Vector<SpriteFrame*>& frames,
|
|
|
|
float delay /* = 0.0f*/,
|
|
|
|
unsigned int loops /* = 1*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_delayPerUnit = delay;
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = loops;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
for (auto& spriteFrame : frames)
|
|
|
|
{
|
|
|
|
auto animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
|
|
|
|
_frames.pushBack(animFrame);
|
|
|
|
_totalDelayUnits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Animation::initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrames,
|
|
|
|
float delayPerUnit,
|
|
|
|
unsigned int loops)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_delayPerUnit = delayPerUnit;
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = loops;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
setFrames(arrayOfAnimationFrames);
|
|
|
|
|
|
|
|
for (auto& animFrame : _frames)
|
|
|
|
{
|
|
|
|
_totalDelayUnits += animFrame->getDelayUnits();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Animation::Animation()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _totalDelayUnits(0.0f), _delayPerUnit(0.0f), _duration(0.0f), _restoreOriginalFrame(false), _loops(0)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Animation::~Animation()
|
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing Animation: %p", this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Animation::addSpriteFrame(SpriteFrame* spriteFrame)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1.0f, ValueMap());
|
2019-11-23 20:27:39 +08:00
|
|
|
_frames.pushBack(animFrame);
|
|
|
|
|
|
|
|
// update duration
|
|
|
|
_totalDelayUnits++;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Animation::addSpriteFrameWithFile(std::string_view filename)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = Director::getInstance()->getTextureCache()->addImage(filename);
|
|
|
|
Rect rect = Rect::ZERO;
|
|
|
|
rect.size = texture->getContentSize();
|
|
|
|
SpriteFrame* frame = SpriteFrame::createWithTexture(texture, rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
addSpriteFrame(frame);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Animation::addSpriteFrameWithTexture(Texture2D* pobTexture, const Rect& rect)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* frame = SpriteFrame::createWithTexture(pobTexture, rect);
|
2019-11-23 20:27:39 +08:00
|
|
|
addSpriteFrame(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
float Animation::getDuration() const
|
|
|
|
{
|
|
|
|
return _totalDelayUnits * _delayPerUnit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Animation* Animation::clone() const
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// no copy constructor
|
2021-12-08 00:11:53 +08:00
|
|
|
auto a = new Animation();
|
2019-11-23 20:27:39 +08:00
|
|
|
a->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
|
|
|
|
a->setRestoreOriginalFrame(_restoreOriginalFrame);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|