Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TweenEngine.h
- #pragma once
- #include "Tween.h"
- class TweenEngine
- {
- public:
- enum TWEEN_EASE { LINEAR, CUBIC, EASE_IN_OUT };
- TweenEngine();
- virtual ~TweenEngine();
- std::shared_ptr<Tween> newTween(float duration, int tweenEaseType = LINEAR);
- void update(float dt);
- void clear();
- unsigned int getTweenCount();
- private:
- std::vector< std::shared_ptr< Tween > > tweens_;
- };
- // Endof TweenEngine.h
- // TweenEngine.cpp
- #include "TweenEngine.h"
- TweenEngine::TweenEngine()
- {
- }
- TweenEngine::~TweenEngine()
- {
- }
- void TweenEngine::update( float dt )
- {
- for( int i = 0; i < tweens_.size(); ++i )
- {
- tweens_[i]->update(dt);
- if (tweens_[i]->isFinished()) {
- tweens_.erase(tweens_.begin() + i);
- --i;
- }
- }
- }
- void TweenEngine::clear(){
- tweens_.clear();
- }
- unsigned int TweenEngine::getTweenCount() {
- return tweens_.size();
- }
- std::shared_ptr<Tween> TweenEngine::newTween(float duration, int tweenEaseType ) {
- std::shared_ptr<Tween> tween = NULL;
- switch (tweenEaseType) {
- case TweenEngine::LINEAR:
- tween = std::make_shared<Tween>(Tween(duration,
- [](float currTime, float endTime, float startY, float endY){
- return (startY * (endTime - currTime) + (endY * currTime)) / endTime;
- }));
- break;
- case TweenEngine::CUBIC:
- tween = std::make_shared<Tween>(Tween(duration,
- [](float currTime, float endTime, float startY, float endY){
- float normalizedTime = currTime * currTime / (endTime * endTime); // currTime is [0, endTime]
- return (startY * (1.f - normalizedTime) + (endY * normalizedTime));
- }));
- break;
- case TweenEngine::EASE_IN_OUT:
- return NULL;
- //tween = std::make_shared<Tween>(Tween(duration,
- // [](float currTime, float endTime, float startY, float endY){
- // float normalizedTime = (currTime - 0.5f) * (currTime - 0.5f) / ( endTime * endTime ); // currTime is [0, endTime]
- // return (startY * (1.f - normalizedTime ) + (endY * normalizedTime));
- //}));
- break;
- default:
- return NULL;
- break;
- }
- tweens_.push_back(tween);
- return tween;
- }
- // End of TweenEngine.cpp
- // Tween.h
- #pragma once
- #include <functional>
- #include <vector>
- #include <memory>
- class Tween
- {
- public:
- enum finish_enums { FINISHED, STOPPED };
- static std::function<float(float, float, float, float)> LINEAR;
- Tween(float duration, std::function<float(float, float, float, float)> );
- virtual ~Tween();
- void update(float dt);
- void addValue(float* f, float endValue);
- void start();
- void stop();
- void setCallBack(std::function<void(int)> );
- bool isFinished();
- private:
- std::function<void(int)> func_;
- std::function<float(float, float, float, float)> traverserFunc_;
- std::vector < float > startVals_;
- std::vector < float > endVals_;
- std::vector < float* > vals_;
- float time_ = 0.f;
- float endTime_ = 0.f;
- bool running_ = false;
- bool finished_ = false;
- };
- // End of Tween.h
- // Tween.cpp
- #include "Tween.h"
- #include <string>
- #include <memory>
- Tween::Tween(float duration, std::function<float(float, float, float, float)> func) : time_(0.f), endTime_(duration), running_(false),
- traverserFunc_(func), finished_(false)
- {
- }
- Tween::~Tween()
- {
- }
- void Tween::setCallBack(std::function<void(int)> cb){
- func_ = cb;
- }
- void Tween::addValue( float* f, float endValue){
- this->startVals_.push_back((*f));
- this->vals_.push_back( ( f )) ;
- this->endVals_.push_back(endValue);
- }
- void Tween::start(){
- if ( time_ <= endTime_ )
- running_ = true;
- }
- void Tween::stop() {
- if (!running_ && func_){
- func_(Tween::STOPPED);
- }
- finished_ = true;
- running_ = false;
- }
- void Tween::update(float dt) {
- if (!running_) return;
- time_ += dt;
- if (time_ > endTime_) {
- time_ = endTime_;
- running_ = false;
- }
- for (unsigned int i = 0; i < vals_.size(); ++i) {
- (*vals_[i]) = traverserFunc_(time_, endTime_, startVals_[i], endVals_[i]);
- }
- if (!running_ ) {
- if ( func_ )
- func_(Tween::FINISHED);
- finished_ = true;
- }
- }
- bool Tween::isFinished() {
- return finished_;
- }
- // End of Tween.cpp
- // Main file
- #include <memory>
- #include "TweenEngine.h"
- #include "Tween.h"
- std::shared_ptr< TweenEngine > tweenEngine = std::make_shared<TweenEngine>( TweenEngine() );
- struct TestingStruct {
- float x = 8.f;
- float y = 4.f;
- float z = 12.f;
- };
- TestingStruct ts;
- TestingStruct ts2;
- void SkeletalAnimation::startTween()
- {
- std::shared_ptr<Tween> tween = tweenEngine->newTween(3.f, TweenEngine::CUBIC);
- tween->addValue(&ts.x, 1.f);
- tween->addValue(&ts.y, -200.f);
- tween->addValue(&ts.z, 20.f);
- tween->setCallBack([](int finishType) {
- if (finishType == Tween::FINISHED) {
- OutputDebugString("We have finished first tween!\n");
- std::shared_ptr<Tween> tween = tweenEngine->newTween(2.f);
- tween->addValue(&ts2.x, 10000.f);
- tween->start();
- }
- }
- );
- tween->start();
- }
- void SkeletalAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- using namespace Update;
- // Take the frame time step, which is stored as a float
- float timeStep = eventData[P_TIMESTEP].GetFloat();
- tweenEngine->update(timeStep);
- std::string tmpStr = "Tweens running: " + std::to_string(tweenEngine->getTweenCount()) + "\n";
- OutputDebugString(tmpStr.c_str());
- // Move the camera, scale movement with time step
- MoveCamera(timeStep);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement