Bugfix Callbacks

Added duration to SetAlpha callback. Saving and loading Play callback.
This commit is contained in:
Bruno Herbelin
2022-02-11 00:28:25 +01:00
parent 74eca2e527
commit f50411e9db
7 changed files with 91 additions and 31 deletions

View File

@@ -1151,11 +1151,22 @@ void SessionLoader::visit (SourceCallback &)
{ {
} }
void SessionLoader::visit (Play &c)
{
bool p = true;
xmlCurrent_->QueryBoolAttribute("play", &p);
c.setValue(p);
}
void SessionLoader::visit (SetAlpha &c) void SessionLoader::visit (SetAlpha &c)
{ {
float a = 0.f; float a = 0.f;
xmlCurrent_->QueryFloatAttribute("alpha", &a); xmlCurrent_->QueryFloatAttribute("alpha", &a);
c.setValue(a); c.setValue(a);
float d = 0.f;
xmlCurrent_->QueryFloatAttribute("duration", &d);
c.setDuration(d);
} }
void SessionLoader::visit (SetDepth &c) void SessionLoader::visit (SetDepth &c)

View File

@@ -71,6 +71,7 @@ public:
void visit (Grab&) override; void visit (Grab&) override;
void visit (Resize&) override; void visit (Resize&) override;
void visit (Turn&) override; void visit (Turn&) override;
void visit (Play&) override;
static void XMLToNode(const tinyxml2::XMLElement *xml, Node &n); static void XMLToNode(const tinyxml2::XMLElement *xml, Node &n);
static void XMLToSourcecore(tinyxml2::XMLElement *xml, SourceCore &s); static void XMLToSourcecore(tinyxml2::XMLElement *xml, SourceCore &s);

View File

@@ -764,9 +764,15 @@ void SessionVisitor::visit (SourceCallback &c)
xmlCurrent_->SetAttribute("type", (uint) c.type()); xmlCurrent_->SetAttribute("type", (uint) c.type());
} }
void SessionVisitor::visit (Play &c)
{
xmlCurrent_->SetAttribute("play", c.value());
}
void SessionVisitor::visit (SetAlpha &c) void SessionVisitor::visit (SetAlpha &c)
{ {
xmlCurrent_->SetAttribute("alpha", c.value()); xmlCurrent_->SetAttribute("alpha", c.value());
xmlCurrent_->SetAttribute("duration", c.duration());
} }
void SessionVisitor::visit (SetDepth &c) void SessionVisitor::visit (SetDepth &c)

View File

@@ -77,6 +77,7 @@ public:
void visit (Grab&) override; void visit (Grab&) override;
void visit (Resize&) override; void visit (Resize&) override;
void visit (Turn&) override; void visit (Turn&) override;
void visit (Play&) override;
static tinyxml2::XMLElement *NodeToXML(const Node &n, tinyxml2::XMLDocument *doc); static tinyxml2::XMLElement *NodeToXML(const Node &n, tinyxml2::XMLDocument *doc);
static tinyxml2::XMLElement *ImageToXML(const FrameBufferImage *img, tinyxml2::XMLDocument *doc); static tinyxml2::XMLElement *ImageToXML(const FrameBufferImage *img, tinyxml2::XMLDocument *doc);

View File

@@ -133,43 +133,71 @@ SourceCallback *ResetGeometry::clone() const
return new ResetGeometry; return new ResetGeometry;
} }
SetAlpha::SetAlpha() : SourceCallback(), alpha_(0.f) SetAlpha::SetAlpha() : SourceCallback(), duration_(0.f), progress_(0.f), alpha_(0.f)
{ {
pos_ = glm::vec2(); start_ = glm::vec2();
step_ = glm::normalize(glm::vec2(1.f, 1.f)); // step in diagonal by default target_ = glm::vec2();
} }
SetAlpha::SetAlpha(float alpha) : SetAlpha() SetAlpha::SetAlpha(float alpha, float duration) : SetAlpha()
{ {
alpha_ = CLAMP(alpha, 0.f, 1.f); alpha_ = CLAMP(alpha, 0.f, 1.f);
duration_ = duration;
} }
void SetAlpha::update(Source *s, float) void SetAlpha::update(Source *s, float dt)
{ {
if (s && !s->locked()) { if (s && !s->locked()) {
// 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 // initial mixing view position
pos_ = glm::vec2(s->group(View::MIXING)->translation_); start_ = glm::vec2(s->group(View::MIXING)->translation_);
// step in diagonal by default
glm::vec2 step = glm::normalize(glm::vec2(1.f, 1.f));
// step in direction of source translation if possible // step in direction of source translation if possible
if ( glm::length(pos_) > DELTA_ALPHA) if ( glm::length(start_) > DELTA_ALPHA)
step_ = glm::normalize(pos_); step = glm::normalize(start_);
//
// target mixing view position
//
// special case Alpha = 0
if (alpha_ < DELTA_ALPHA) {
target_ = step;
}
// special case Alpha = 1
else if (alpha_ > 1.f - DELTA_ALPHA) {
target_ = step * 0.005f;
}
// general case
else {
// converge to reduce the difference of alpha using dichotomic algorithm
target_ = start_;
float delta = 1.f;
do {
target_ += step * (delta / 2.f);
delta = SourceCore::alphaFromCordinates(target_.x, target_.y) - alpha_;
} while (glm::abs(delta) > DELTA_ALPHA);
}
initialized_ = true; initialized_ = true;
} }
// perform operation // time passed
float delta = SourceCore::alphaFromCordinates(pos_.x, pos_.y) - alpha_; progress_ += dt;
// converge to reduce the difference of alpha using dichotomic algorithm // perform movement
if ( glm::abs(delta) > DELTA_ALPHA ){ if ( ABS(duration_) > 0.f)
pos_ += step_ * (delta / 2.f); s->group(View::MIXING)->translation_ = glm::vec3(start_ + (progress_/duration_) * target_, s->group(View::MIXING)->translation_.z);
s->group(View::MIXING)->translation_ = glm::vec3(pos_, s->group(View::MIXING)->translation_.z);
} // time-out
// reached alpha target if ( progress_ > duration_ ) {
else { // apply depth to target
s->group(View::MIXING)->translation_ = glm::vec3(target_, s->group(View::MIXING)->translation_.z);
// done
finished_ = true; finished_ = true;
} }
} }
else else
finished_ = true; finished_ = true;
@@ -378,6 +406,12 @@ SourceCallback *Play::reverse(Source *s) const
return new Play(s->playing()); return new Play(s->playing());
} }
void Play::accept(Visitor& v)
{
SourceCallback::accept(v);
v.visit(*this);
}
RePlay::RePlay() : SourceCallback() RePlay::RePlay() : SourceCallback()
{ {
} }

View File

@@ -12,16 +12,16 @@ public:
typedef enum { typedef enum {
CALLBACK_GENERIC = 0, CALLBACK_GENERIC = 0,
CALLBACK_ALPHA, CALLBACK_ALPHA = 1,
CALLBACK_LOOM, CALLBACK_LOOM = 2,
CALLBACK_GRAB, CALLBACK_GRAB = 3,
CALLBACK_RESIZE, CALLBACK_RESIZE = 4,
CALLBACK_TURN, CALLBACK_TURN = 5,
CALLBACK_DEPTH, CALLBACK_DEPTH = 6,
CALLBACK_PLAY, CALLBACK_PLAY = 7,
CALLBACK_REPLAY, CALLBACK_REPLAY = 8,
CALLBACK_RESETGEO, CALLBACK_RESETGEO = 9,
CALLBACK_LOCK CALLBACK_LOCK = 10
} CallbackType; } CallbackType;
static SourceCallback *create(CallbackType type); static SourceCallback *create(CallbackType type);
@@ -56,16 +56,20 @@ public:
class SetAlpha : public SourceCallback class SetAlpha : public SourceCallback
{ {
float duration_;
float progress_;
float alpha_; float alpha_;
glm::vec2 pos_; glm::vec2 start_;
glm::vec2 step_; glm::vec2 target_;
public: public:
SetAlpha (); SetAlpha ();
SetAlpha (float alpha); SetAlpha (float alpha, float duration = 0.f);
float value () const { return alpha_;} float value () const { return alpha_;}
void setValue (float a) { alpha_ = a; } void setValue (float a) { alpha_ = a; }
float duration () const { return duration_;}
void setDuration (float d) { duration_ = d; }
void update (Source *s, float) override; void update (Source *s, float) override;
void multiply (float factor) override; void multiply (float factor) override;
@@ -154,6 +158,7 @@ public:
SourceCallback *clone() const override; SourceCallback *clone() const override;
SourceCallback *reverse(Source *s) const override; SourceCallback *reverse(Source *s) const override;
CallbackType type () const override { return CALLBACK_PLAY; } CallbackType type () const override { return CALLBACK_PLAY; }
void accept (Visitor& v) override;
}; };
class RePlay : public SourceCallback class RePlay : public SourceCallback

View File

@@ -48,6 +48,7 @@ class Loom;
class Grab; class Grab;
class Resize; class Resize;
class Turn; class Turn;
class Play;
// Declares the interface for the visitors // Declares the interface for the visitors
@@ -101,6 +102,7 @@ public:
virtual void visit (Grab&) {} virtual void visit (Grab&) {}
virtual void visit (Resize&) {} virtual void visit (Resize&) {}
virtual void visit (Turn&) {} virtual void visit (Turn&) {}
virtual void visit (Play&) {}
}; };