Initial creation of the Node Update Callback.

This commit is contained in:
brunoherbelin
2020-07-01 21:06:57 +02:00
parent d4b793ceb6
commit f8b165572f
5 changed files with 82 additions and 8 deletions

View File

@@ -203,6 +203,7 @@ set(VMIX_SRCS
Shader.cpp Shader.cpp
ImageShader.cpp ImageShader.cpp
ImageProcessingShader.cpp ImageProcessingShader.cpp
UpdateCallback.cpp
Scene.cpp Scene.cpp
Primitives.cpp Primitives.cpp
Mesh.cpp Mesh.cpp

View File

@@ -35,6 +35,13 @@ Node::Node() : initialized_(false), visible_(true), refcount_(0)
Node::~Node () Node::~Node ()
{ {
std::list<UpdateCallback *>::iterator iter;
for (iter=update_callbacks_.begin(); iter != update_callbacks_.end(); )
{
UpdateCallback *callback = *iter;
iter = update_callbacks_.erase(iter);
delete callback;
}
} }
void Node::copyTransform(Node *other) void Node::copyTransform(Node *other)
@@ -47,12 +54,13 @@ void Node::copyTransform(Node *other)
translation_ = other->translation_; translation_ = other->translation_;
} }
void Node::update( float ) void Node::update( float dt)
{ {
std::list<Node::NodeUpdateCallback>::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(); iter++)
{ {
(*iter)(this); if ((*iter)->enabled())
(*iter)->update(this, dt);
} }
// update transform matrix from attributes // update transform matrix from attributes

View File

@@ -12,7 +12,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include "GlmToolkit.h" #include "UpdateCallback.h"
// Forward declare classes referenced // Forward declare classes referenced
class Shader; class Shader;
@@ -73,9 +73,8 @@ public:
glm::vec3 scale_, rotation_, translation_; glm::vec3 scale_, rotation_, translation_;
// animation update callbacks // animation update callbacks
typedef void (* NodeUpdateCallback)(Node *); // list of callbacks to call at each update
// list of functions to call at each update std::list<UpdateCallback *> update_callbacks_;
std::list<NodeUpdateCallback> update_callbacks_;
}; };
@@ -91,7 +90,7 @@ public:
* should fill the points, colors and texture coordinates * should fill the points, colors and texture coordinates
* in their constructor. * in their constructor.
* *
* Primitive can be given a shader that is used during draw. * Primitive can be given a shader th0at is used during draw.
* *
*/ */
class Primitive : public Node { class Primitive : public Node {

30
UpdateCallback.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "UpdateCallback.h"
#include "Scene.h"
#include <glm/gtc/type_ptr.hpp>
UpdateCallback::UpdateCallback() : enabled_(true)
{
}
void MoveToCenterCallback::update(Node *n, float dt)
{
// set start position on first run or upon call of reset()
if (!initialized_){
initial_position_ = n->translation_;
initialized_ = true;
}
// calculate amplitude of movement
float percent = dt / duration_;
// perform movement : translation to the center (0, 0)
n->translation_ = initial_position_ - percent * initial_position_;
// detect end of movement
if ( glm::distance(initial_position_, n->translation_) < 0.1f) {
disable();
}
}

36
UpdateCallback.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef UPDATECALLBACK_H
#define UPDATECALLBACK_H
#include "GlmToolkit.h"
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; }
};
class MoveToCenterCallback : public UpdateCallback
{
float duration_;
bool initialized_;
glm::vec3 initial_position_;
public:
MoveToCenterCallback(float duration = 1000.f) : duration_(duration), initialized_(false) {}
void update(Node *n, float dt);
inline void reset() { initialized_ = false; }
};
#endif // UPDATECALLBACK_H