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)
{
float a = 0.f;
xmlCurrent_->QueryFloatAttribute("alpha", &a);
c.setValue(a);
float d = 0.f;
xmlCurrent_->QueryFloatAttribute("duration", &d);
c.setDuration(d);
}
void SessionLoader::visit (SetDepth &c)

View File

@@ -71,6 +71,7 @@ public:
void visit (Grab&) override;
void visit (Resize&) override;
void visit (Turn&) override;
void visit (Play&) override;
static void XMLToNode(const tinyxml2::XMLElement *xml, Node &n);
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());
}
void SessionVisitor::visit (Play &c)
{
xmlCurrent_->SetAttribute("play", c.value());
}
void SessionVisitor::visit (SetAlpha &c)
{
xmlCurrent_->SetAttribute("alpha", c.value());
xmlCurrent_->SetAttribute("duration", c.duration());
}
void SessionVisitor::visit (SetDepth &c)

View File

@@ -77,6 +77,7 @@ public:
void visit (Grab&) override;
void visit (Resize&) override;
void visit (Turn&) override;
void visit (Play&) override;
static tinyxml2::XMLElement *NodeToXML(const Node &n, 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;
}
SetAlpha::SetAlpha() : SourceCallback(), alpha_(0.f)
SetAlpha::SetAlpha() : SourceCallback(), duration_(0.f), progress_(0.f), alpha_(0.f)
{
pos_ = glm::vec2();
step_ = glm::normalize(glm::vec2(1.f, 1.f)); // step in diagonal by default
start_ = glm::vec2();
target_ = glm::vec2();
}
SetAlpha::SetAlpha(float alpha) : SetAlpha()
SetAlpha::SetAlpha(float alpha, float duration) : SetAlpha()
{
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()) {
// set start position on first run or upon call of reset()
if (!initialized_){
// initial position
pos_ = glm::vec2(s->group(View::MIXING)->translation_);
// initial mixing view position
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
if ( glm::length(pos_) > DELTA_ALPHA)
step_ = glm::normalize(pos_);
if ( glm::length(start_) > DELTA_ALPHA)
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;
}
// perform operation
float delta = SourceCore::alphaFromCordinates(pos_.x, pos_.y) - alpha_;
// time passed
progress_ += dt;
// converge to reduce the difference of alpha using dichotomic algorithm
if ( glm::abs(delta) > DELTA_ALPHA ){
pos_ += step_ * (delta / 2.f);
s->group(View::MIXING)->translation_ = glm::vec3(pos_, s->group(View::MIXING)->translation_.z);
}
// reached alpha target
else {
// perform movement
if ( ABS(duration_) > 0.f)
s->group(View::MIXING)->translation_ = glm::vec3(start_ + (progress_/duration_) * target_, s->group(View::MIXING)->translation_.z);
// time-out
if ( progress_ > duration_ ) {
// apply depth to target
s->group(View::MIXING)->translation_ = glm::vec3(target_, s->group(View::MIXING)->translation_.z);
// done
finished_ = true;
}
}
else
finished_ = true;
@@ -378,6 +406,12 @@ SourceCallback *Play::reverse(Source *s) const
return new Play(s->playing());
}
void Play::accept(Visitor& v)
{
SourceCallback::accept(v);
v.visit(*this);
}
RePlay::RePlay() : SourceCallback()
{
}

View File

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

View File

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