Advertisement
Guest User

something tweeninglike?

a guest
Feb 18th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. // TweenEngine.h
  2. #pragma once
  3.  
  4. #include "Tween.h"
  5. class TweenEngine
  6. {
  7. public:
  8.     enum TWEEN_EASE { LINEAR, CUBIC, EASE_IN_OUT };
  9.     TweenEngine();
  10.     virtual ~TweenEngine();
  11.  
  12.  
  13.     std::shared_ptr<Tween> newTween(float duration, int tweenEaseType = LINEAR);
  14.    
  15.     void update(float dt);
  16.     void clear();
  17.  
  18.     unsigned int getTweenCount();
  19.  
  20. private:
  21.     std::vector< std::shared_ptr< Tween > > tweens_;
  22. };
  23.  
  24. // Endof TweenEngine.h
  25.  
  26.  
  27.  
  28. // TweenEngine.cpp
  29.  
  30. #include "TweenEngine.h"
  31.  
  32.  
  33. TweenEngine::TweenEngine()
  34. {
  35. }
  36.  
  37.  
  38. TweenEngine::~TweenEngine()
  39. {
  40. }
  41.  
  42. void TweenEngine::update( float dt )
  43. {
  44.  
  45.     for( int i = 0; i < tweens_.size(); ++i )
  46.     {
  47.         tweens_[i]->update(dt);
  48.         if (tweens_[i]->isFinished()) {
  49.             tweens_.erase(tweens_.begin() + i);
  50.             --i;
  51.         }
  52.  
  53.     }
  54. }
  55. void TweenEngine::clear(){
  56.     tweens_.clear();
  57. }
  58. unsigned int TweenEngine::getTweenCount() {
  59.     return tweens_.size();
  60. }
  61.  
  62. std::shared_ptr<Tween> TweenEngine::newTween(float duration, int tweenEaseType ) {
  63.     std::shared_ptr<Tween> tween = NULL;
  64.     switch (tweenEaseType) {
  65.         case TweenEngine::LINEAR:
  66.             tween = std::make_shared<Tween>(Tween(duration,
  67.                 [](float currTime, float endTime, float startY, float endY){
  68.                 return (startY * (endTime - currTime) + (endY * currTime)) / endTime;
  69.             }));
  70.         break;
  71.  
  72.         case TweenEngine::CUBIC:
  73.             tween = std::make_shared<Tween>(Tween(duration,
  74.                 [](float currTime, float endTime, float startY, float endY){
  75.                 float normalizedTime = currTime * currTime / (endTime * endTime); // currTime is [0, endTime]
  76.                 return (startY * (1.f - normalizedTime) + (endY * normalizedTime));
  77.             }));
  78.             break;
  79.  
  80.         case TweenEngine::EASE_IN_OUT:
  81.             return NULL;
  82.             //tween = std::make_shared<Tween>(Tween(duration,
  83.             //  [](float currTime, float endTime, float startY, float endY){
  84.             //  float normalizedTime = (currTime - 0.5f) * (currTime - 0.5f) / ( endTime * endTime ); // currTime is [0, endTime]
  85.             //  return (startY * (1.f - normalizedTime ) + (endY * normalizedTime));
  86.             //}));
  87.  
  88.         break;
  89.  
  90.         default:
  91.             return NULL;
  92.         break;
  93.  
  94.     }
  95.  
  96.     tweens_.push_back(tween);
  97.     return tween;
  98. }
  99.  
  100. // End of TweenEngine.cpp
  101.  
  102. // Tween.h
  103.  
  104. #pragma once
  105.  
  106.  
  107. #include <functional>
  108. #include <vector>
  109. #include <memory>
  110. class Tween
  111. {
  112. public:
  113.     enum finish_enums { FINISHED, STOPPED };
  114.  
  115.     static std::function<float(float, float, float, float)> LINEAR;
  116.     Tween(float duration, std::function<float(float, float, float, float)> );
  117.     virtual ~Tween();
  118.  
  119.     void update(float dt);
  120.  
  121.     void addValue(float* f, float endValue);
  122.     void start();
  123.     void stop();
  124.  
  125.     void setCallBack(std::function<void(int)> );
  126.     bool isFinished();
  127. private:
  128.     std::function<void(int)> func_;
  129.     std::function<float(float, float, float, float)> traverserFunc_;
  130.     std::vector < float > startVals_;
  131.     std::vector < float > endVals_;
  132.     std::vector < float* > vals_;
  133.  
  134.     float time_ = 0.f;
  135.     float endTime_ = 0.f;
  136.  
  137.     bool running_ = false;
  138.     bool finished_ = false;
  139. };
  140.  
  141. // End of Tween.h
  142.  
  143.  
  144.  
  145. // Tween.cpp
  146. #include "Tween.h"
  147.  
  148. #include <string>
  149. #include <memory>
  150.  
  151. Tween::Tween(float duration, std::function<float(float, float, float, float)> func) : time_(0.f), endTime_(duration), running_(false),
  152. traverserFunc_(func), finished_(false)
  153. {
  154. }
  155.  
  156.  
  157. Tween::~Tween()
  158. {
  159. }
  160. void Tween::setCallBack(std::function<void(int)> cb){
  161.     func_ = cb;
  162. }
  163.  
  164. void Tween::addValue( float* f, float endValue){
  165.    
  166.     this->startVals_.push_back((*f));
  167.     this->vals_.push_back( ( f )) ;
  168.     this->endVals_.push_back(endValue);
  169. }
  170.  
  171. void Tween::start(){
  172.     if ( time_ <= endTime_ )
  173.         running_ = true;
  174. }
  175. void Tween::stop() {
  176.     if (!running_ && func_){
  177.         func_(Tween::STOPPED);
  178.  
  179.     }
  180.     finished_ = true;
  181.     running_ = false;
  182. }
  183.  
  184. void Tween::update(float dt) {
  185.    
  186.     if (!running_) return;
  187.  
  188.     time_ += dt;
  189.     if (time_ > endTime_)  {
  190.         time_ = endTime_;
  191.         running_ = false;
  192.  
  193.     }
  194.  
  195.  
  196.     for (unsigned int i = 0; i < vals_.size(); ++i) {
  197.  
  198.         (*vals_[i]) = traverserFunc_(time_, endTime_, startVals_[i], endVals_[i]);
  199.     }
  200.     if (!running_ ) {
  201.         if ( func_ )
  202.             func_(Tween::FINISHED);
  203.         finished_ = true;
  204.     }
  205.  
  206. }
  207.  
  208. bool Tween::isFinished() {
  209.     return finished_;
  210. }
  211.  
  212.  
  213. // End of Tween.cpp
  214.  
  215.  
  216. // Main file
  217.  
  218. #include <memory>
  219. #include "TweenEngine.h"
  220. #include "Tween.h"
  221.  
  222. std::shared_ptr< TweenEngine > tweenEngine =  std::make_shared<TweenEngine>( TweenEngine() );
  223. struct TestingStruct {
  224.     float x = 8.f;
  225.     float y = 4.f;
  226.     float z = 12.f;
  227. };
  228.  
  229. TestingStruct ts;
  230. TestingStruct ts2;
  231.  
  232. void SkeletalAnimation::startTween()
  233. {
  234.     std::shared_ptr<Tween> tween = tweenEngine->newTween(3.f, TweenEngine::CUBIC);
  235.  
  236.     tween->addValue(&ts.x, 1.f);
  237.     tween->addValue(&ts.y, -200.f);
  238.     tween->addValue(&ts.z, 20.f);
  239.  
  240.     tween->setCallBack([](int finishType) {
  241.         if (finishType == Tween::FINISHED) {
  242.  
  243.             OutputDebugString("We have finished first tween!\n");
  244.  
  245.             std::shared_ptr<Tween> tween = tweenEngine->newTween(2.f);
  246.             tween->addValue(&ts2.x, 10000.f);
  247.             tween->start();
  248.         }
  249.     }
  250.     );
  251.     tween->start();
  252.  
  253. }
  254.  
  255.  
  256. void SkeletalAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  257. {
  258.     using namespace Update;
  259.  
  260.     // Take the frame time step, which is stored as a float
  261.     float timeStep = eventData[P_TIMESTEP].GetFloat();
  262.  
  263.     tweenEngine->update(timeStep);
  264.     std::string tmpStr = "Tweens running: " + std::to_string(tweenEngine->getTweenCount()) + "\n";
  265.     OutputDebugString(tmpStr.c_str());
  266.  
  267.     // Move the camera, scale movement with time step
  268.     MoveCamera(timeStep);
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement