2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (C) 2010 Lam Pham
|
|
|
|
|
|
|
|
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 "CCActionProgressTimer.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "misc_nodes/CCProgressTimer.h"
|
|
|
|
#include "cocoa/CCZone.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
#define kProgressTimerCast ProgressTimer*
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of ProgressTo
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTo* ProgressTo::create(float duration, float fPercent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTo *pProgressTo = new ProgressTo();
|
2012-04-19 14:35:52 +08:00
|
|
|
pProgressTo->initWithDuration(duration, fPercent);
|
|
|
|
pProgressTo->autorelease();
|
|
|
|
|
|
|
|
return pProgressTo;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ProgressTo::initWithDuration(float duration, float fPercent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (ActionInterval::initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_to = fPercent;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTo* ProgressTo::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new ProgressTo();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _to);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressTo* ProgressTo::reverse() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCAssert(false, "reverse() not supported in ProgressTo");
|
2013-06-19 06:06:53 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* ProgressTo::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
ProgressTo* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (ProgressTo*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new ProgressTo();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _to);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTo::startWithTarget(Node *pTarget)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::startWithTarget(pTarget);
|
2013-06-15 14:03:30 +08:00
|
|
|
_from = ((kProgressTimerCast)(pTarget))->getPercentage();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// XXX: Is this correct ?
|
2013-06-20 14:13:12 +08:00
|
|
|
// Adding it to support Repeat
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_from == 100)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_from = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressTo::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of ProgressFromTo
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressFromTo* ProgressFromTo::create(float duration, float fFromPercentage, float fToPercentage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressFromTo *pProgressFromTo = new ProgressFromTo();
|
2012-04-19 14:35:52 +08:00
|
|
|
pProgressFromTo->initWithDuration(duration, fFromPercentage, fToPercentage);
|
|
|
|
pProgressFromTo->autorelease();
|
|
|
|
|
|
|
|
return pProgressFromTo;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ProgressFromTo::initWithDuration(float duration, float fFromPercentage, float fToPercentage)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (ActionInterval::initWithDuration(duration))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_to = fToPercentage;
|
|
|
|
_from = fFromPercentage;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressFromTo* ProgressFromTo::clone() const
|
2013-06-19 06:06:53 +08:00
|
|
|
{
|
|
|
|
// no copy constructor
|
2013-06-20 14:13:12 +08:00
|
|
|
auto a = new ProgressFromTo();
|
2013-06-19 06:06:53 +08:00
|
|
|
a->initWithDuration(_duration, _from, _to);
|
|
|
|
a->autorelease();
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* ProgressFromTo::copyWithZone(Zone *pZone)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Zone* pNewZone = NULL;
|
|
|
|
ProgressFromTo* pCopy = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
if(pZone && pZone->_copyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//in case of being called at sub class
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = (ProgressFromTo*)(pZone->_copyObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pCopy = new ProgressFromTo();
|
|
|
|
pZone = pNewZone = new Zone(pCopy);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::copyWithZone(pZone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pCopy->initWithDuration(_duration, _from, _to);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_SAFE_DELETE(pNewZone);
|
|
|
|
return pCopy;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ProgressFromTo* ProgressFromTo::reverse(void) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return ProgressFromTo::create(_duration, _to, _from);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressFromTo::startWithTarget(Node *pTarget)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ActionInterval::startWithTarget(pTarget);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ProgressFromTo::update(float time)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|