2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCAnimation.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
AnimationFrame* AnimationFrame::create(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
2013-11-28 16:02:03 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new AnimationFrame();
|
|
|
|
if (ret->initWithSpriteFrame(spriteFrame, delayUnits, userInfo))
|
2013-11-28 16:02:03 +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) {}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
bool AnimationFrame::initWithSpriteFrame(SpriteFrame* spriteFrame, float delayUnits, const ValueMap& userInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
setSpriteFrame(spriteFrame);
|
|
|
|
setDelayUnits(delayUnits);
|
|
|
|
setUserInfo(userInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AnimationFrame::~AnimationFrame()
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
CCLOGINFO("deallocing AnimationFrame: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_spriteFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
AnimationFrame* AnimationFrame::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
2015-01-08 10:22:45 +08:00
|
|
|
// 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);
|
2013-06-19 06:06:53 +08:00
|
|
|
|
2015-01-08 10:22:45 +08:00
|
|
|
frame->autorelease();
|
|
|
|
return frame;
|
2013-06-19 06:06:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Animation
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-12-04 07:02:39 +08:00
|
|
|
Animation* Animation::create()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2013-12-18 17:47:20 +08:00
|
|
|
animation->init();
|
|
|
|
animation->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return animation;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +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*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2014-04-02 18:06:17 +08:00
|
|
|
animation->initWithSpriteFrames(frames, delay, loops);
|
2013-12-18 17:47:20 +08:00
|
|
|
animation->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return animation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* Animation::create(const Vector<AnimationFrame*>& arrayOfAnimationFrameNames,
|
|
|
|
float delayPerUnit,
|
|
|
|
unsigned int loops /* = 1 */)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Animation* animation = new Animation();
|
2013-12-18 17:47:20 +08:00
|
|
|
animation->initWithAnimationFrames(arrayOfAnimationFrameNames, delayPerUnit, loops);
|
|
|
|
animation->autorelease();
|
|
|
|
return animation;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Animation::init()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = 1;
|
2013-11-28 16:02:03 +08:00
|
|
|
_delayPerUnit = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Animation::initWithSpriteFrames(const Vector<SpriteFrame*>& frames,
|
|
|
|
float delay /* = 0.0f*/,
|
|
|
|
unsigned int loops /* = 1*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delayPerUnit = delay;
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = loops;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& spriteFrame : frames)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
auto animFrame = AnimationFrame::create(spriteFrame, 1, ValueMap());
|
2013-12-05 10:35:10 +08:00
|
|
|
_frames.pushBack(animFrame);
|
2013-11-28 16:02:03 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool Animation::initWithAnimationFrames(const Vector<AnimationFrame*>& arrayOfAnimationFrames,
|
|
|
|
float delayPerUnit,
|
|
|
|
unsigned int loops)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delayPerUnit = delayPerUnit;
|
2021-12-25 10:04:45 +08:00
|
|
|
_loops = loops;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
setFrames(arrayOfAnimationFrames);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& animFrame : _frames)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits += animFrame->getDelayUnits();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation::Animation()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _totalDelayUnits(0.0f), _delayPerUnit(0.0f), _duration(0.0f), _restoreOriginalFrame(false), _loops(0)
|
|
|
|
{}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
Animation::~Animation()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing Animation: %p", this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
void Animation::addSpriteFrame(SpriteFrame* spriteFrame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, 1.0f, ValueMap());
|
2013-12-05 10:35:10 +08:00
|
|
|
_frames.pushBack(animFrame);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update duration
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalDelayUnits++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void Animation::addSpriteFrameWithFile(std::string_view filename)
|
2012-04-19 14:35:52 +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);
|
2013-12-18 17:47:20 +08:00
|
|
|
addSpriteFrame(frame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Animation::addSpriteFrameWithTexture(Texture2D* pobTexture, const Rect& rect)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteFrame* frame = SpriteFrame::createWithTexture(pobTexture, rect);
|
2013-12-18 17:47:20 +08:00
|
|
|
addSpriteFrame(frame);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
float Animation::getDuration() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalDelayUnits * _delayPerUnit;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Animation* Animation::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// no copy constructor
|
2021-12-08 00:11:53 +08:00
|
|
|
auto a = new Animation();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithAnimationFrames(_frames, _delayPerUnit, _loops);
|
|
|
|
a->setRestoreOriginalFrame(_restoreOriginalFrame);
|
2015-01-08 10:22:45 +08:00
|
|
|
a->autorelease();
|
|
|
|
return a;
|
2013-06-19 06:06:53 +08:00
|
|
|
}
|
|
|
|
|
2014-06-19 19:45:24 +08:00
|
|
|
NS_CC_END
|