diff --git a/Mixer.cpp b/Mixer.cpp index 1831164..02d7ece 100644 --- a/Mixer.cpp +++ b/Mixer.cpp @@ -326,6 +326,8 @@ void Mixer::insertSource(Source *s, bool makecurrent) // set this new source as current setCurrentSource( sit ); + +// s->group(View::MIXING)->update_callbacks_.push_back(new MoveToCenterCallback); } } } @@ -403,6 +405,9 @@ void Mixer::setCurrentSource(SourceList::iterator it) // show status as current (*current_source_)->setMode(Source::CURRENT); + + (*current_source_)->group(View::MIXING)->update_callbacks_.push_back(new BounceScaleCallback); + (*current_source_)->group(View::LAYER)->update_callbacks_.push_back(new BounceScaleCallback); } } diff --git a/Scene.cpp b/Scene.cpp index 3f7f424..a456257 100644 --- a/Scene.cpp +++ b/Scene.cpp @@ -57,10 +57,20 @@ void Node::copyTransform(Node *other) void Node::update( float dt) { std::list::iterator iter; - for (iter=update_callbacks_.begin(); iter != update_callbacks_.end(); iter++) + for (iter=update_callbacks_.begin(); iter != update_callbacks_.end(); ) { - if ((*iter)->enabled()) - (*iter)->update(this, dt); + UpdateCallback *callback = *iter; + + if (callback->enabled()) + callback->update(this, dt); + + if (callback->finished()) { + iter = update_callbacks_.erase(iter); + delete callback; + } + else { + iter++; + } } // update transform matrix from attributes diff --git a/UpdateCallback.cpp b/UpdateCallback.cpp index 167ab82..304a16a 100644 --- a/UpdateCallback.cpp +++ b/UpdateCallback.cpp @@ -1,16 +1,19 @@ #include "UpdateCallback.h" +#include "defines.h" #include "Scene.h" +#include "Log.h" #include -UpdateCallback::UpdateCallback() : enabled_(true) +UpdateCallback::UpdateCallback() : enabled_(true), finished_(false) { } void MoveToCenterCallback::update(Node *n, float dt) { + // set start position on first run or upon call of reset() if (!initialized_){ initial_position_ = n->translation_; @@ -18,13 +21,37 @@ void MoveToCenterCallback::update(Node *n, float dt) } // calculate amplitude of movement - float percent = dt / duration_; + progress_ += dt / duration_; // perform movement : translation to the center (0, 0) - n->translation_ = initial_position_ - percent * initial_position_; + n->translation_.x = initial_position_.x - progress_ * initial_position_.x; + n->translation_.y = initial_position_.y - progress_ * initial_position_.y; - // detect end of movement - if ( glm::distance(initial_position_, n->translation_) < 0.1f) { - disable(); + // end of movement + if ( progress_ > 1.f ) { + n->translation_.x = 0.f; + n->translation_.y = 0.f; + finished_ = true; + } +} + +void BounceScaleCallback::update(Node *n, float dt) +{ + // set start scale on first run or upon call of reset() + if (!initialized_){ + initial_scale_ = n->scale_; + initialized_ = true; + } + + // calculate amplitude of movement + progress_ += dt / duration_; + + n->scale_.x = initial_scale_.x + (initial_scale_.x * 0.05f) * sin(M_PI * progress_); + n->scale_.y = initial_scale_.y + (initial_scale_.y * 0.05f) * sin(M_PI * progress_); + + // end of movement + if ( progress_ > 1.f) { + n->scale_ = initial_scale_; + finished_ = true; } } diff --git a/UpdateCallback.h b/UpdateCallback.h index 8adc07c..5efd161 100644 --- a/UpdateCallback.h +++ b/UpdateCallback.h @@ -7,30 +7,46 @@ class Node; class UpdateCallback { - bool enabled_; public: UpdateCallback(); virtual void update(Node *, float) = 0; - inline bool enabled() const {return enabled_;} - inline void enable() { enabled_ = true; } - inline void disable() { enabled_ = false; } + inline bool finished() const { return finished_; } + inline bool enabled() const { return enabled_; } + +protected: + bool enabled_; + bool finished_; }; class MoveToCenterCallback : public UpdateCallback { float duration_; + float progress_; bool initialized_; glm::vec3 initial_position_; public: - MoveToCenterCallback(float duration = 1000.f) : duration_(duration), initialized_(false) {} + MoveToCenterCallback(float duration = 1000.f) : duration_(duration), progress_(0.f), initialized_(false) {} void update(Node *n, float dt); inline void reset() { initialized_ = false; } }; +class BounceScaleCallback : public UpdateCallback +{ + float duration_; + float progress_; + bool initialized_; + glm::vec3 initial_scale_; + +public: + BounceScaleCallback(float duration = 100.f) : duration_(duration), progress_(0.f), initialized_(false) {} + + void update(Node *n, float dt); +}; + #endif // UPDATECALLBACK_H