Confirmed working implementation of Node UpdateCallback. For now example

used in visual effect on selecting a source as current.
This commit is contained in:
brunoherbelin
2020-07-01 23:39:36 +02:00
parent d638145520
commit 6cc756c401
4 changed files with 72 additions and 14 deletions

View File

@@ -326,6 +326,8 @@ void Mixer::insertSource(Source *s, bool makecurrent)
// set this new source as current // set this new source as current
setCurrentSource( sit ); 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 // show status as current
(*current_source_)->setMode(Source::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);
} }
} }

View File

@@ -57,10 +57,20 @@ void Node::copyTransform(Node *other)
void Node::update( float dt) void Node::update( float dt)
{ {
std::list<UpdateCallback *>::iterator iter; std::list<UpdateCallback *>::iterator iter;
for (iter=update_callbacks_.begin(); iter != update_callbacks_.end(); iter++) for (iter=update_callbacks_.begin(); iter != update_callbacks_.end(); )
{ {
if ((*iter)->enabled()) UpdateCallback *callback = *iter;
(*iter)->update(this, dt);
if (callback->enabled())
callback->update(this, dt);
if (callback->finished()) {
iter = update_callbacks_.erase(iter);
delete callback;
}
else {
iter++;
}
} }
// update transform matrix from attributes // update transform matrix from attributes

View File

@@ -1,16 +1,19 @@
#include "UpdateCallback.h" #include "UpdateCallback.h"
#include "defines.h"
#include "Scene.h" #include "Scene.h"
#include "Log.h"
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
UpdateCallback::UpdateCallback() : enabled_(true) UpdateCallback::UpdateCallback() : enabled_(true), finished_(false)
{ {
} }
void MoveToCenterCallback::update(Node *n, float dt) void MoveToCenterCallback::update(Node *n, float dt)
{ {
// set start position on first run or upon call of reset() // set start position on first run or upon call of reset()
if (!initialized_){ if (!initialized_){
initial_position_ = n->translation_; initial_position_ = n->translation_;
@@ -18,13 +21,37 @@ void MoveToCenterCallback::update(Node *n, float dt)
} }
// calculate amplitude of movement // calculate amplitude of movement
float percent = dt / duration_; progress_ += dt / duration_;
// perform movement : translation to the center (0, 0) // 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 // end of movement
if ( glm::distance(initial_position_, n->translation_) < 0.1f) { if ( progress_ > 1.f ) {
disable(); 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;
} }
} }

View File

@@ -7,30 +7,46 @@ class Node;
class UpdateCallback class UpdateCallback
{ {
bool enabled_;
public: public:
UpdateCallback(); UpdateCallback();
virtual void update(Node *, float) = 0; virtual void update(Node *, float) = 0;
inline bool finished() const { return finished_; }
inline bool enabled() const { return enabled_; } inline bool enabled() const { return enabled_; }
inline void enable() { enabled_ = true; }
inline void disable() { enabled_ = false; } protected:
bool enabled_;
bool finished_;
}; };
class MoveToCenterCallback : public UpdateCallback class MoveToCenterCallback : public UpdateCallback
{ {
float duration_; float duration_;
float progress_;
bool initialized_; bool initialized_;
glm::vec3 initial_position_; glm::vec3 initial_position_;
public: 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); void update(Node *n, float dt);
inline void reset() { initialized_ = false; } 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 #endif // UPDATECALLBACK_H