Skip to content

Commit

Permalink
add ui::LoadingBar support to Progress actions (cocos2d#18748)
Browse files Browse the repository at this point in the history
Just checked for the loading bar target and cast appropriately.

I made this edit in Github, but the source edit was from my old fork that I can't get to merge in tankorsmash/cocos2dx-mytracker@aac2a0b
  • Loading branch information
tankorsmash authored and huangwei1024 committed Jun 20, 2019
1 parent 3bcf6ed commit 8926ba7
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions cocos/2d/CCActionProgressTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ THE SOFTWARE.
****************************************************************************/
#include "2d/CCActionProgressTimer.h"
#include "2d/CCProgressTimer.h"
#include "ui/UILoadingBar.h"

NS_CC_BEGIN

#define kProgressTimerCast ProgressTimer*

// implementation of ProgressTo

ProgressTo* ProgressTo::create(float duration, float percent)
{
ProgressTo *progressTo = new (std::nothrow) ProgressTo();
Expand All @@ -41,7 +41,6 @@ ProgressTo* ProgressTo::create(float duration, float percent)
progressTo->autorelease();
return progressTo;
}

delete progressTo;
return nullptr;
}
Expand Down Expand Up @@ -73,12 +72,23 @@ ProgressTo* ProgressTo::reverse() const
void ProgressTo::startWithTarget(Node *target)
{
ActionInterval::startWithTarget(target);
_from = ((kProgressTimerCast)(target))->getPercentage();

ui::LoadingBar* loading_bar = dynamic_cast<ui::LoadingBar*>(target);
if (loading_bar){
_from = loading_bar->getPercent();
} else {
_from = static_cast<ProgressTimer*>(target)->getPercentage();
};
}

void ProgressTo::update(float time)
{
((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
ui::LoadingBar* loading_bar = dynamic_cast<ui::LoadingBar*>(_target);
if (loading_bar){
loading_bar->setPercent(_from + (_to - _from) * time);
} else {
static_cast<ProgressTimer*>(_target)->setPercentage(_from + (_to - _from) * time);
};
}

// implementation of ProgressFromTo
Expand All @@ -90,14 +100,14 @@ ProgressFromTo* ProgressFromTo::create(float duration, float fromPercentage, flo
progressFromTo->autorelease();
return progressFromTo;
}

delete progressFromTo;
return nullptr;
}

bool ProgressFromTo::initWithDuration(float duration, float fromPercentage, float toPercentage)
{
if (ActionInterval::initWithDuration(duration))
if (ActionInterval::initWithDuration(duration))
{
_to = toPercentage;
_from = fromPercentage;
Expand Down Expand Up @@ -127,7 +137,12 @@ void ProgressFromTo::startWithTarget(Node *target)

void ProgressFromTo::update(float time)
{
((kProgressTimerCast)(_target))->setPercentage(_from + (_to - _from) * time);
ui::LoadingBar* loading_bar = dynamic_cast<ui::LoadingBar*>(_target);
if (loading_bar){
loading_bar->setPercent(_from + (_to - _from) * time);
} else {
static_cast<ProgressTimer*>(_target)->setPercentage(_from + (_to - _from) * time);
};
}

NS_CC_END

0 comments on commit 8926ba7

Please sign in to comment.