Allow the omission of the "from" attribute from the animations in the

layout. This will start the animation from the current value rather than a
programmed one.
This commit is contained in:
Pieter Hulshoff 2016-06-05 11:37:46 +02:00
parent 9da55a844e
commit ccfc0325a9
5 changed files with 81 additions and 29 deletions

View File

@ -110,6 +110,11 @@ float Tween::animate(double elapsedTime)
return animateSingle(type, start, end, duration, elapsedTime);
}
float Tween::animate(double elapsedTime, double startValue)
{
return animateSingle(type, startValue, end, duration, elapsedTime);
}
//todo: SDL likes floats, consider having casting being performed elsewhere
float Tween::animateSingle(TweenAlgorithm type, double start, double end, double duration, double elapsedTime)
{
@ -369,3 +374,9 @@ double Tween::easeInOutCircular(double t, double d, double b, double c)
t -= 2;
return c/2 * (sqrt(1 - t*t) + 1) + b;
}
double Tween::getStart()
{
return start;
}

View File

@ -27,11 +27,13 @@ public:
Tween(TweenProperty name, TweenAlgorithm type, double start, double end, double duration);
float animate(double elapsedTime);
float animate(double elapsedTime, double startValue);
static float animateSingle(TweenAlgorithm type, double start, double end, double duration, double elapsedTime);
static TweenAlgorithm getTweenType(std::string name);
static bool getTweenProperty(std::string name, TweenProperty &property);
TweenProperty property;
double duration;
double getStart();
private:
static double easeInQuadratic(double elapsedTime, double duration, double b, double c);

View File

@ -18,6 +18,7 @@
#include "../../Graphics/ViewInfo.h"
#include "../../Utility/Log.h"
#include "../../SDL.h"
#include <cmath>
Component::Component(Page &p)
: page(p)
@ -134,18 +135,20 @@ void Component::update(float dt)
currentTweens_ = newTweens;
currentTweenIndex_ = 0;
elapsedTweenTime_ = 0;
storeViewInfo_ = baseViewInfo;
currentTweenComplete_ = false;
}
animationRequested_ = false;
}
else if (tweens_ && currentTweenComplete_)
{
animationType_ = "idle";
currentTweens_ = tweens_->getAnimation( "idle", menuIndex_ );
currentTweenIndex_ = 0;
elapsedTweenTime_ = 0;
currentTweenComplete_ = false;
animationRequested_ = false;
animationType_ = "idle";
currentTweens_ = tweens_->getAnimation( "idle", menuIndex_ );
currentTweenIndex_ = 0;
elapsedTweenTime_ = 0;
storeViewInfo_ = baseViewInfo;
currentTweenComplete_ = false;
animationRequested_ = false;
}
currentTweenComplete_ = animate();
@ -196,64 +199,94 @@ bool Component::animate()
//todo: too many levels of nesting
if(elapsedTime < tween->duration)
{
currentDone = false;
}
else
{
elapsedTime = static_cast<float>(tween->duration);
}
float value = tween->animate(elapsedTime);
switch(tween->property)
{
case TWEEN_PROPERTY_X:
baseViewInfo.X = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.X = tween->animate(elapsedTime, storeViewInfo_.X);
else
baseViewInfo.X = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_Y:
baseViewInfo.Y = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.Y = tween->animate(elapsedTime, storeViewInfo_.Y);
else
baseViewInfo.Y = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_HEIGHT:
baseViewInfo.Height = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.Height = tween->animate(elapsedTime, storeViewInfo_.Height);
else
baseViewInfo.Height = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_WIDTH:
baseViewInfo.Width = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.Width = tween->animate(elapsedTime, storeViewInfo_.Width);
else
baseViewInfo.Width = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_ANGLE:
baseViewInfo.Angle = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.Angle = tween->animate(elapsedTime, storeViewInfo_.Angle);
else
baseViewInfo.Angle = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_ALPHA:
baseViewInfo.Alpha = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.Alpha = tween->animate(elapsedTime, storeViewInfo_.Alpha);
else
baseViewInfo.Alpha = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_X_ORIGIN:
baseViewInfo.XOrigin = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.XOrigin = tween->animate(elapsedTime, storeViewInfo_.XOrigin);
else
baseViewInfo.XOrigin = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_Y_ORIGIN:
baseViewInfo.YOrigin = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.YOrigin = tween->animate(elapsedTime, storeViewInfo_.YOrigin);
else
baseViewInfo.YOrigin = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_X_OFFSET:
baseViewInfo.XOffset = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.XOffset = tween->animate(elapsedTime, storeViewInfo_.XOffset);
else
baseViewInfo.XOffset = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_Y_OFFSET:
baseViewInfo.YOffset = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.YOffset = tween->animate(elapsedTime, storeViewInfo_.YOffset);
else
baseViewInfo.YOffset = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_FONT_SIZE:
baseViewInfo.FontSize = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.FontSize = tween->animate(elapsedTime, storeViewInfo_.FontSize);
else
baseViewInfo.FontSize = tween->animate(elapsedTime);
break;
case TWEEN_PROPERTY_BACKGROUND_ALPHA:
baseViewInfo.BackgroundAlpha = value;
if (std::isnan( tween->getStart() ))
baseViewInfo.BackgroundAlpha = tween->animate(elapsedTime, storeViewInfo_.BackgroundAlpha);
else
baseViewInfo.BackgroundAlpha = tween->animate(elapsedTime);
break;
}
}

View File

@ -60,6 +60,7 @@ private:
Animation *currentTweens_;
SDL_Texture *backgroundTexture_;
ViewInfo storeViewInfo_;
unsigned int currentTweenIndex_;
bool currentTweenComplete_;
float elapsedTweenTime_;

View File

@ -37,6 +37,7 @@
#include <stdexcept>
#include <vector>
#include <map>
#include <limits>
using namespace rapidxml;
@ -952,17 +953,21 @@ void PageBuilder::getAnimationEvents(xml_node<> *node, TweenSet &tweens)
{
Logger::write(Logger::ZONE_ERROR, "Layout", "Animate tag missing \"type\" attribute");
}
else if(!from)
{
Logger::write(Logger::ZONE_ERROR, "Layout", "Animate tag missing \"from\" attribute");
}
else if(!to)
{
Logger::write(Logger::ZONE_ERROR, "Layout", "Animate tag missing \"to\" attribute");
}
else
{
float fromValue = Utils::convertFloat(from->value());
float fromValue;
if (from)
{
fromValue = Utils::convertFloat(from->value());
}
else
{
fromValue = std::numeric_limits<double>::quiet_NaN();
}
float toValue = Utils::convertFloat(to->value());
float durationValue = Utils::convertFloat(durationXml->value());