1 // Copyright 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include "AnimationWidget.h"
5 #include <imgui.h>
6 
AnimationWidget(std::string name,std::shared_ptr<AnimationManager> animationManager)7 AnimationWidget::AnimationWidget(
8     std::string name, std::shared_ptr<AnimationManager> animationManager)
9     : name(name), animationManager(animationManager)
10 {
11   lastUpdated = std::chrono::system_clock::now();
12   time = animationManager->getTimeRange().lower;
13   animationManager->update(time);
14 }
15 
~AnimationWidget()16 AnimationWidget::~AnimationWidget() {
17   animationManager.reset();
18 }
19 
update()20 void AnimationWidget::update()
21 {
22   auto &timeRange = animationManager->getTimeRange();
23   auto now = std::chrono::system_clock::now();
24   if (play) {
25     time += std::chrono::duration<float>(now - lastUpdated).count() * speedup;
26     if (time > timeRange.upper)
27       if (loop) {
28         const float d = timeRange.size();
29         time =
30             d == 0.f ? timeRange.lower : timeRange.lower + std::fmod(time, d);
31       } else {
32         time = timeRange.lower;
33         play = false;
34       }
35   }
36   animationManager->update(time, shutter);
37   lastUpdated = now;
38 }
39 
40 // update UI and process any UI events
addAnimationUI()41 void AnimationWidget::addAnimationUI()
42 {
43   auto &timeRange = animationManager->getTimeRange();
44   auto &animations = animationManager->getAnimations();
45   ImGui::Begin(name.c_str());
46 
47   if (ImGui::Button(play ? "Pause" : "Play ")) {
48     play = !play;
49     lastUpdated = std::chrono::system_clock::now();
50   }
51 
52   bool modified = play;
53 
54   ImGui::SameLine();
55   if (ImGui::SliderFloat("time", &time, timeRange.lower, timeRange.upper))
56     modified = true;
57 
58   ImGui::SameLine();
59   ImGui::Checkbox("Loop", &loop);
60 
61   { // simulate log slider
62     static float exp = 0.f;
63     ImGui::SliderFloat("speedup: ", &exp, -3.f, 3.f);
64     speedup = std::pow(10.0f, exp);
65     ImGui::SameLine();
66     ImGui::Text("%.*f", std::max(0, int(1.99f - exp)), speedup);
67   }
68 
69   if (ImGui::SliderFloat("shutter", &shutter, 0.0f, timeRange.size()))
70     modified = true;
71 
72   for (auto &a : animations)
73     ImGui::Checkbox(a.name.c_str(), &a.active);
74 
75   ImGui::Spacing();
76   ImGui::End();
77 
78   if (modified)
79     update();
80 }
81