Skip to content

Commit 2bcd4e7

Browse files
committed
Animator options connected to animator planner.
1 parent f44618c commit 2bcd4e7

File tree

9 files changed

+110
-60
lines changed

9 files changed

+110
-60
lines changed

src/chart/animator/animator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void Animator::init(Diag::DiagramPtr diagram)
2828
}
2929

3030
void Animator::animate(const Diag::DiagramPtr &diagram,
31-
Options &&/*options*/,
31+
Options &&options,
3232
OnComplete onThisCompletes)
3333
{
3434
if (isRunning()) throw std::logic_error("animation already in progress");
@@ -41,7 +41,7 @@ void Animator::animate(const Diag::DiagramPtr &diagram,
4141
onComplete = onThisCompletes;
4242
target->detachOptions();
4343
prepareActual();
44-
createPlan(*source, *target, *actual);
44+
createPlan(*source, *target, *actual, options);
4545
::Anim::Control::reset();
4646
::Anim::Control::play();
4747
}

src/chart/animator/morph.cpp

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ AbstractMorph::AbstractMorph(
1919
actual(actual)
2020
{}
2121

22+
std::unique_ptr<AbstractMorph> AbstractMorph::create(SectionId sectionId,
23+
const Diagram &source,
24+
const Diagram &target,
25+
Diagram &actual)
26+
{
27+
switch(sectionId) {
28+
case SectionId::EnumType::color:
29+
return std::make_unique<Color>(source, target, actual);
30+
case SectionId::EnumType::enable:
31+
return std::make_unique<Enable>(source, target, actual);
32+
case SectionId::EnumType::x:
33+
return std::make_unique<Horizontal>(source, target, actual);
34+
case SectionId::EnumType::y:
35+
return std::make_unique<Vertical>(source, target, actual);
36+
case SectionId::EnumType::shape:
37+
return std::make_unique<Shape>(source, target, actual);
38+
case SectionId::EnumType::coordSystem:
39+
return std::make_unique<CoordinateSystem>
40+
(source, target, actual);
41+
default: throw std::logic_error("invalid animation section");
42+
}
43+
}
44+
2245
void AbstractMorph::transform(double factor)
2346
{
2447
transform(source, target, actual, factor);
@@ -33,9 +56,9 @@ void AbstractMorph::transform(double factor)
3356
}
3457
}
3558

36-
void CoordinateSystem::transform(const Options &source,
37-
const Options &target,
38-
Options &actual,
59+
void CoordinateSystem::transform(const Diag::Options &source,
60+
const Diag::Options &target,
61+
Diag::Options &actual,
3962
double factor) const
4063
{
4164
actual.polar.set(interpolate(source.polar.get(), target.polar.get(), factor));
@@ -50,9 +73,9 @@ void Enable::transform(const Marker &source,
5073
actual.enabled = interpolate(source.enabled, target.enabled, factor);
5174
}
5275

53-
void Shape::transform(const Options &source,
54-
const Options &target,
55-
Options &actual,
76+
void Shape::transform(const Diag::Options &source,
77+
const Diag::Options &target,
78+
Diag::Options &actual,
5679
double factor) const
5780
{
5881
actual.shapeType.set(interpolate(source.shapeType.get(),
@@ -82,9 +105,9 @@ void Horizontal::transform(const Diagram &source,
82105
interpolate(source.anyAxisSet, target.anyAxisSet, factor);
83106
}
84107

85-
void Horizontal::transform(const Options &source,
86-
const Options &target,
87-
Options &actual,
108+
void Horizontal::transform(const Diag::Options &source,
109+
const Diag::Options &target,
110+
Diag::Options &actual,
88111
double factor) const
89112
{
90113
auto sourceIsConnecting = Vizzu::Diag::isConnecting(source.shapeType.get().type());
@@ -183,9 +206,9 @@ void Morph::Color::transform(const Diagram &source,
183206
factor);
184207
}
185208

186-
void Morph::Color::transform(const Options &source,
187-
const Options &target,
188-
Options &actual,
209+
void Morph::Color::transform(const Diag::Options &source,
210+
const Diag::Options &target,
211+
Diag::Options &actual,
189212
double factor) const
190213
{
191214
actual.legend.set(interpolate(source.legend.get(),

src/chart/animator/morph.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifndef MORPH_H
22
#define MORPH_H
33

4+
#include <memory>
5+
46
#include "base/anim/element.h"
57
#include "base/math/ratio.h"
68
#include "base/math/interpolation.h"
79
#include "chart/options/options.h"
810
#include "chart/generator/diagram.h"
911

12+
#include "options.h"
13+
1014
namespace Vizzu
1115
{
1216
namespace Anim
@@ -26,6 +30,8 @@ class AbstractMorph : public ::Anim::IElement
2630
virtual ~AbstractMorph()
2731
{}
2832

33+
static std::unique_ptr<AbstractMorph> create(SectionId sectionId,
34+
const Dia &source, const Dia &target, Dia &actual);
2935
void transform(double factor) override;
3036
virtual std::string name() const = 0;
3137
virtual void transform(const Dia&, const Dia&, Dia&, double) const {}

src/chart/animator/options.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ void Options::set(const std::string &path,
2626
section.duration = ::Anim::Duration(value);
2727
else throw std::logic_error("invalid animation option: " + option);
2828
}
29+
30+
::Anim::Options Options::get(SectionId sectionId,
31+
const ::Anim::Options &defaultOption) const
32+
{
33+
auto &section = sections.at((int)sectionId);
34+
auto res = defaultOption;
35+
if (section.easing.has_value()) res.easing = *section.easing;
36+
if (section.delay.has_value()) res.delay = *section.delay;
37+
if (section.duration.has_value()) res.duration = *section.duration;
38+
return res;
39+
}

src/chart/animator/options.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Anim
1313
{
1414

1515
class Enum(SectionId)
16-
(style,title,enable,color,coordsys,shape,vertical,horizontal);
16+
(style,title,enable,color,coordSystem,shape,y,x);
1717

1818
class Options
1919
{
@@ -27,6 +27,9 @@ class Options
2727
std::array<Section, SectionId::EnumInfo::count()> sections;
2828

2929
void set(const std::string &path, const std::string &value);
30+
31+
::Anim::Options get(SectionId sectionId,
32+
const ::Anim::Options &defaultOption) const;
3033
};
3134

3235
}

src/chart/animator/planner.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@
77

88
using namespace Vizzu;
99
using namespace Vizzu::Anim;
10-
using namespace Vizzu::Diag;
1110
using namespace std::literals::chrono_literals;
1211

1312
void Planner::createPlan(const Diag::Diagram &source,
1413
const Diag::Diagram &target,
15-
Diag::Diagram &actual)
14+
Diag::Diagram &actual,
15+
const Options &options)
1616
{
17+
typedef SectionId::EnumType SectionId;
18+
1719
this->source = &source;
1820
this->target = &target;
1921
this->actual = &actual;
22+
this->options = &options;
2023

2124
::Anim::Group::clear();
2225

2326
Morph::StyleMorphFactory(source.getStyle(), target.getStyle(),
24-
actual.getStyle(), *this);
27+
actual.getStyle(), *this,
28+
options.get(SectionId::style, ::Anim::Options(500ms)));
2529

2630
if (source.getOptions()->title.get() != target.getOptions()->title.get())
2731
addElement(
@@ -30,67 +34,67 @@ void Planner::createPlan(const Diag::Diagram &source,
3034
source.getOptions()->title.ref(),
3135
target.getOptions()->title.ref(),
3236
actual.getOptions()->title.ref()),
33-
::Anim::Options(500ms));
37+
options.get(SectionId::title, ::Anim::Options(500ms)));
3438

3539
if (anyMarker(
3640
[&](const auto &source, const auto &target) -> bool {
3741
return (bool)(source.enabled && !target.enabled);
3842
}))
39-
addMorph<Morph::Enable>(1s);
43+
addMorph(SectionId::enable, ::Anim::Options(1s));
4044

41-
if (needColor()) addMorph<Morph::Color>(500ms);
45+
if (needColor()) addMorph(SectionId::color, ::Anim::Options(500ms));
4246

4347
if (source.getOptions()->polar.get()
4448
!= target.getOptions()->polar.get()
4549
|| source.getOptions()->angle.get()
4650
!= target.getOptions()->angle.get())
47-
addMorph<Morph::CoordinateSystem>(1s);
51+
addMorph(SectionId::coordSystem, ::Anim::Options(1s));
4852

4953
const auto &src = source.getOptions()->shapeType.get();
5054
const auto &trg = target.getOptions()->shapeType.get();
51-
if((bool)src.getFactor(ShapeType::Circle) && src != trg)
52-
addMorph<Morph::Shape>(1s);
55+
if((bool)src.getFactor(Diag::ShapeType::Circle) && src != trg)
56+
addMorph(SectionId::shape, ::Anim::Options(1s));
5357

5458
if (positionMorphNeeded())
5559
{
56-
addMorph<Morph::Vertical>(1s);
57-
addMorph<Morph::Horizontal>(1s);
60+
addMorph(SectionId::y, ::Anim::Options(1s));
61+
addMorph(SectionId::x, ::Anim::Options(1s));
5862
}
5963
else
6064
{
6165
if (verticalBeforeHorizontal())
6266
{
63-
if (needVertical()) addMorph<Morph::Vertical>(750ms);
64-
if (needHorizontal()) addMorph<Morph::Horizontal>(750ms, 750ms);
67+
if (needVertical()) addMorph(SectionId::y, ::Anim::Options(750ms));
68+
if (needHorizontal()) addMorph(SectionId::x,
69+
::Anim::Options(750ms, 750ms));
6570
}
6671
else
6772
{
68-
if (needHorizontal()) addMorph<Morph::Horizontal>(750ms);
69-
if (needVertical()) addMorph<Morph::Vertical>(750ms, 750ms);
73+
if (needHorizontal()) addMorph(SectionId::x, ::Anim::Options(750ms));
74+
if (needVertical()) addMorph(SectionId::y,
75+
::Anim::Options(750ms, 750ms));
7076
}
7177
}
7278

73-
if (!(bool)src.getFactor(ShapeType::Circle) && src != trg)
74-
addMorph<Morph::Shape>(1s);
79+
if (!(bool)src.getFactor(Diag::ShapeType::Circle) && src != trg)
80+
addMorph(SectionId::shape, ::Anim::Options(1s));
7581

7682
if (anyMarker(
7783
[&](const auto &source, const auto &target) {
7884
return (bool)(!source.enabled && target.enabled);
7985
}))
80-
addMorph<Morph::Enable>(1s);
86+
addMorph(SectionId::enable, ::Anim::Options(1s));
8187

8288
if (!source.getOptions()->polar.get()
8389
&& target.getOptions()->polar.get())
84-
addMorph<Morph::CoordinateSystem>(1s);
90+
addMorph(SectionId::coordSystem, ::Anim::Options(1s));
8591
}
8692

87-
template <typename M>
88-
void Planner::addMorph(std::chrono::nanoseconds duration,
89-
std::chrono::nanoseconds delay)
93+
void Planner::addMorph(SectionId sectionId, const ::Anim::Options &autoOptions)
9094
{
9195
addElement(
92-
std::move(std::make_unique<M>(*source, *target, *actual)),
93-
::Anim::Options(duration, delay)
96+
Morph::AbstractMorph::create(sectionId, *source, *target, *actual),
97+
options->get(sectionId, autoOptions)
9498
);
9599
}
96100

@@ -112,20 +116,20 @@ bool Planner::anyMarker(
112116
bool Planner::positionMorphNeeded() const
113117
{
114118
return Diag::canOverlap(
115-
(ShapeType::Type)source->getOptions()->shapeType.get())
119+
(Diag::ShapeType::Type)source->getOptions()->shapeType.get())
116120
|| Diag::canOverlap(
117-
(ShapeType::Type)target->getOptions()->shapeType.get());
121+
(Diag::ShapeType::Type)target->getOptions()->shapeType.get());
118122
}
119123

120124
bool Planner::needColor() const
121125
{
122126
return source->anySelected != target->anySelected
123127
|| source->getOptions()->legend.get()
124128
!= target->getOptions()->legend.get()
125-
|| source->discreteAxises.at(Scale::Type::Color)
126-
!= target->discreteAxises.at(Scale::Type::Color)
127-
|| source->discreteAxises.at(Scale::Type::Lightness)
128-
!= target->discreteAxises.at(Scale::Type::Lightness)
129+
|| source->discreteAxises.at(Diag::Scale::Type::Color)
130+
!= target->discreteAxises.at(Diag::Scale::Type::Color)
131+
|| source->discreteAxises.at(Diag::Scale::Type::Lightness)
132+
!= target->discreteAxises.at(Diag::Scale::Type::Lightness)
129133
|| anyMarker(
130134
[&](const auto &source, const auto &target)
131135
{
@@ -139,16 +143,16 @@ size_t Planner::discreteCount(const Diag::Diagram *diagram,
139143
Diag::Scale::Type type) const
140144
{
141145
return diagram->getOptions()->getScales()
142-
.at(Scales::Id{type, Scales::Index{0}})
146+
.at(Diag::Scales::Id{type, Diag::Scales::Index{0}})
143147
.discretesIds().size();
144148
}
145149

146150
bool Planner::verticalBeforeHorizontal() const
147151
{
148-
auto srcXcnt = discreteCount(source, Scale::X);
149-
auto srcYcnt = discreteCount(source, Scale::Y);
150-
auto trgXcnt = discreteCount(target, Scale::X);
151-
auto trgYcnt = discreteCount(target, Scale::Y);
152+
auto srcXcnt = discreteCount(source, Diag::Scale::X);
153+
auto srcYcnt = discreteCount(source, Diag::Scale::Y);
154+
auto trgXcnt = discreteCount(target, Diag::Scale::X);
155+
auto trgYcnt = discreteCount(target, Diag::Scale::Y);
152156

153157
if ((trgYcnt != srcYcnt) || (trgXcnt != srcXcnt))
154158
{

src/chart/animator/planner.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "base/anim/group.h"
1010
#include "chart/generator/diagram.h"
1111

12+
#include "options.h"
13+
1214
namespace Vizzu
1315
{
1416
namespace Anim
@@ -23,17 +25,16 @@ class Planner : public ::Anim::Group
2325

2426
void createPlan(const Diag::Diagram &source,
2527
const Diag::Diagram &target,
26-
Diag::Diagram &actual);
28+
Diag::Diagram &actual,
29+
const Options &options);
2730

2831
private:
2932
const Diag::Diagram *source;
3033
const Diag::Diagram *target;
3134
Diag::Diagram *actual;
35+
const Options *options;
3236

33-
template <typename M>
34-
void addMorph(std::chrono::nanoseconds duration,
35-
std::chrono::nanoseconds delay = std::chrono::nanoseconds(0));
36-
37+
void addMorph(SectionId sectionId, const ::Anim::Options &autoOptions);
3738
bool anyMarker(const std::function<bool(const Diag::Marker &,
3839
const Diag::Marker &)> &compare) const;
3940

src/chart/animator/styles.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ StyleMorphFactory::StyleMorphFactory(
2929
const Styles::Chart &source,
3030
const Styles::Chart &target,
3131
Styles::Chart &actual,
32-
::Anim::Group &group) :
33-
group(group)
32+
::Anim::Group &group,
33+
const ::Anim::Options &options) :
34+
group(group), options(options)
3435
{
3536
pSource = reinterpret_cast<const std::byte *>(&source);
3637
pTarget = reinterpret_cast<const std::byte *>(&target);
@@ -64,8 +65,7 @@ StyleMorphFactory &StyleMorphFactory::operator()(T &value,
6465
auto morph = std::make_unique<StyleMorph<T>>
6566
(source, target, value);
6667

67-
group.addElement(std::move(morph),
68-
::Anim::Options(std::chrono::milliseconds(500)));
68+
group.addElement(std::move(morph), options);
6969
}
7070
}
7171
return *this;

src/chart/animator/styles.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class StyleMorphFactory
2121
StyleMorphFactory(const Styles::Chart &source,
2222
const Styles::Chart &target,
2323
Styles::Chart &actual,
24-
::Anim::Group &group);
24+
::Anim::Group &group,
25+
const ::Anim::Options &options);
2526

2627
template <typename T>
2728
StyleMorphFactory &operator()(T &value, const char *);
@@ -31,6 +32,7 @@ class StyleMorphFactory
3132
const std::byte *pSource;
3233
const std::byte *pTarget;
3334
::Anim::Group &group;
35+
const ::Anim::Options &options;
3436
};
3537

3638
}

0 commit comments

Comments
 (0)