Skip to content

Commit 3c27c06

Browse files
committed
Animation options for whole animation.
1 parent 364093d commit 3c27c06

File tree

5 files changed

+68
-32
lines changed

5 files changed

+68
-32
lines changed

src/apps/weblib/js-api/vizzu.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ interface AnimOption {
193193
}
194194

195195
interface AnimOptions {
196+
easing: Easing;
197+
duration: Duration;
198+
delay: Duration;
196199
style: AnimOption;
197200
title: AnimOption;
198201
enable: AnimOption;

src/chart/animator/options.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,43 @@
66
using namespace Vizzu;
77
using namespace Vizzu::Anim;
88

9+
void Options::Section::set(const std::string &param, const std::string &value)
10+
{
11+
if (param == "easing") easing = ::Anim::Easing(value);
12+
else if (param == "delay") delay = ::Anim::Duration(value);
13+
else if (param == "duration") duration = ::Anim::Duration(value);
14+
else throw std::logic_error("invalid animation parameter: " + param);
15+
}
16+
17+
void Options::Section::writeOver(::Anim::Options &option) const
18+
{
19+
if (easing.has_value()) option.easing = *easing;
20+
if (delay.has_value()) option.delay = *delay;
21+
if (duration.has_value()) option.duration = *duration;
22+
}
23+
924
void Options::set(const std::string &path,
1025
const std::string &value)
1126
{
1227
auto parts = Text::SmartString::split(path, '.');
1328

14-
if (parts.size() != 2)
15-
throw std::logic_error("invalid animation option: " + path);
16-
17-
auto sectionId = SectionId(parts[0]);
18-
auto &section = sections.at((int)sectionId);
19-
20-
auto option = parts[1];
21-
if (option == "easing")
22-
section.easing = ::Anim::Easing(value);
23-
else if (option == "delay")
24-
section.delay = ::Anim::Duration(value);
25-
else if (option == "duration")
26-
section.duration = ::Anim::Duration(value);
27-
else throw std::logic_error("invalid animation option: " + option);
29+
if (parts.size() == 1)
30+
{
31+
all.set(path, value);
32+
}
33+
else if (parts.size() == 2)
34+
{
35+
auto sectionId = SectionId(parts[0]);
36+
sections.at((int)sectionId).set(parts[1], value);
37+
}
38+
else throw std::logic_error("invalid animation option: " + path);
2839
}
2940

3041
::Anim::Options Options::get(SectionId sectionId,
3142
const ::Anim::Options &defaultOption) const
3243
{
3344
auto &section = sections.at((int)sectionId);
3445
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;
46+
section.writeOver(res);
3847
return res;
3948
}

src/chart/animator/options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ class Options
2222
std::optional<::Anim::Easing> easing;
2323
std::optional<::Anim::Duration> delay;
2424
std::optional<::Anim::Duration> duration;
25+
void set(const std::string &param, const std::string &value);
26+
void writeOver(::Anim::Options &option) const;
2527
};
2628

29+
Section all;
2730
std::array<Section, SectionId::EnumInfo::count()> sections;
2831

2932
void set(const std::string &path, const std::string &value);

src/chart/animator/planner.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void Planner::createPlan(const Diag::Diagram &source,
2525

2626
Morph::StyleMorphFactory(source.getStyle(), target.getStyle(),
2727
actual.getStyle(), *this,
28-
options.get(SectionId::style, ::Anim::Options(500ms)));
28+
options.get(SectionId::style, defOptions(500ms)));
2929

3030
if (source.getOptions()->title.get() != target.getOptions()->title.get())
3131
addElement(
@@ -34,60 +34,62 @@ void Planner::createPlan(const Diag::Diagram &source,
3434
source.getOptions()->title.ref(),
3535
target.getOptions()->title.ref(),
3636
actual.getOptions()->title.ref()),
37-
options.get(SectionId::title, ::Anim::Options(500ms)));
37+
options.get(SectionId::title, defOptions(500ms)));
3838

3939
if (anyMarker(
4040
[&](const auto &source, const auto &target) -> bool {
4141
return (bool)(source.enabled && !target.enabled);
4242
}))
43-
addMorph(SectionId::enable, ::Anim::Options(1s));
43+
addMorph(SectionId::enable, defOptions(1s));
4444

45-
if (needColor()) addMorph(SectionId::color, ::Anim::Options(500ms));
45+
if (needColor()) addMorph(SectionId::color, defOptions(500ms));
4646

4747
if (source.getOptions()->polar.get()
4848
!= target.getOptions()->polar.get()
4949
|| source.getOptions()->angle.get()
5050
!= target.getOptions()->angle.get())
51-
addMorph(SectionId::coordSystem, ::Anim::Options(1s));
51+
addMorph(SectionId::coordSystem, defOptions(1s));
5252

5353
const auto &src = source.getOptions()->shapeType.get();
5454
const auto &trg = target.getOptions()->shapeType.get();
5555
if((bool)src.getFactor(Diag::ShapeType::Circle) && src != trg)
56-
addMorph(SectionId::shape, ::Anim::Options(1s));
56+
addMorph(SectionId::shape, defOptions(1s));
5757

5858
if (positionMorphNeeded())
5959
{
60-
addMorph(SectionId::y, ::Anim::Options(1s));
61-
addMorph(SectionId::x, ::Anim::Options(1s));
60+
addMorph(SectionId::y, defOptions(1s));
61+
addMorph(SectionId::x, defOptions(1s));
6262
}
6363
else
6464
{
6565
if (verticalBeforeHorizontal())
6666
{
67-
if (needVertical()) addMorph(SectionId::y, ::Anim::Options(750ms));
67+
if (needVertical()) addMorph(SectionId::y,
68+
defOptions(1500ms, 0, 0.5));
6869
if (needHorizontal()) addMorph(SectionId::x,
69-
::Anim::Options(750ms, 750ms));
70+
defOptions(1500ms, 0.5, 0.5));
7071
}
7172
else
7273
{
73-
if (needHorizontal()) addMorph(SectionId::x, ::Anim::Options(750ms));
74+
if (needHorizontal()) addMorph(SectionId::x,
75+
defOptions(1500ms, 0, 0.5));
7476
if (needVertical()) addMorph(SectionId::y,
75-
::Anim::Options(750ms, 750ms));
77+
defOptions(1500ms, 0.5, 0.5));
7678
}
7779
}
7880

7981
if (!(bool)src.getFactor(Diag::ShapeType::Circle) && src != trg)
80-
addMorph(SectionId::shape, ::Anim::Options(1s));
82+
addMorph(SectionId::shape, defOptions(1s));
8183

8284
if (anyMarker(
8385
[&](const auto &source, const auto &target) {
8486
return (bool)(!source.enabled && target.enabled);
8587
}))
86-
addMorph(SectionId::enable, ::Anim::Options(1s));
88+
addMorph(SectionId::enable, defOptions(1s));
8789

8890
if (!source.getOptions()->polar.get()
8991
&& target.getOptions()->polar.get())
90-
addMorph(SectionId::coordSystem, ::Anim::Options(1s));
92+
addMorph(SectionId::coordSystem, defOptions(1s));
9193
}
9294

9395
void Planner::addMorph(SectionId sectionId, const ::Anim::Options &autoOptions)
@@ -199,3 +201,17 @@ bool Planner::needHorizontal() const
199201
|| source.size.x != target.size.x);
200202
});
201203
}
204+
205+
::Anim::Options Planner::defOptions(
206+
::Anim::Duration wholeDuration,
207+
double delayFactor,
208+
double durationFactor) const
209+
{
210+
::Anim::Options res(wholeDuration);
211+
options->all.writeOver(res);
212+
if (delayFactor > 0)
213+
res.delay = res.delay + res.duration * delayFactor;
214+
if (durationFactor != 1)
215+
res.duration = res.duration * durationFactor;
216+
return res;
217+
}

src/chart/animator/planner.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ class Planner : public ::Anim::Group
4646
bool needColor() const;
4747
bool needHorizontal() const;
4848
bool needVertical() const;
49+
::Anim::Options defOptions(
50+
::Anim::Duration wholeDuration,
51+
double delayFactor = 0,
52+
double durationFactor = 1) const;
4953
};
54+
5055
}
5156
}
5257

0 commit comments

Comments
 (0)