1 #include "animation/linear_animation_instance.hpp"
2 #include "animation/linear_animation.hpp"
3 #include "animation/loop.hpp"
8 LinearAnimationInstance::LinearAnimationInstance(
9 const LinearAnimation* animation) :
10 m_Animation(animation),
11 m_Time(animation->enableWorkArea()
12 ? (float)animation->workStart() / animation->fps()
15 m_LastTotalTime(0.0f),
21 bool LinearAnimationInstance::advance(float elapsedSeconds)
23 const LinearAnimation& animation = *m_Animation;
24 m_Time += elapsedSeconds * animation.speed() * m_Direction;
25 m_LastTotalTime = m_TotalTime;
26 m_TotalTime += elapsedSeconds;
28 int fps = animation.fps();
30 float frames = m_Time * fps;
32 int start = animation.enableWorkArea() ? animation.workStart() : 0;
34 animation.enableWorkArea() ? animation.workEnd() : animation.duration();
35 int range = end - start;
37 bool keepGoing = true;
41 switch (animation.loop())
44 if (m_Direction == 1 && frames > end)
47 m_SpilledTime = (frames - end) / fps;
49 m_Time = frames / fps;
52 else if (m_Direction == -1 && frames < start)
55 m_SpilledTime = (start - frames) / fps;
57 m_Time = frames / fps;
62 if (m_Direction == 1 && frames >= end)
64 m_SpilledTime = (frames - end) / fps;
65 frames = m_Time * fps;
66 frames = start + std::fmod(frames - start, range);
68 m_Time = frames / fps;
71 else if (m_Direction == -1 && frames <= start)
74 m_SpilledTime = (start - frames) / fps;
75 frames = m_Time * fps;
76 frames = end - std::abs(std::fmod(start - frames, range));
77 m_Time = frames / fps;
84 if (m_Direction == 1 && frames >= end)
86 m_SpilledTime = (frames - end) / fps;
88 frames = end + (end - frames);
89 m_Time = frames / fps;
92 else if (m_Direction == -1 && frames < start)
94 m_SpilledTime = (start - frames) / fps;
96 frames = start + (start - frames);
97 m_Time = frames / fps;
102 // we're within the range, we can stop fixing. We do this in
103 // a loop to fix conditions when time has advanced so far
104 // that we've ping-ponged back and forth a few times in a
105 // single frame. We want to accomodate for this in cases
106 // where animations are not advanced on regular intervals.
117 void LinearAnimationInstance::time(float value)
124 // Make sure to keep last and total in relative lockstep so state machines
125 // can track change even when setting time.
126 auto diff = m_TotalTime - m_LastTotalTime;
128 int start = (m_Animation->enableWorkArea() ? m_Animation->workStart() : 0) *
130 m_TotalTime = value - start;
131 m_LastTotalTime = m_TotalTime - diff;
133 // leaving this RIGHT now. but is this required? it kinda messes up
134 // playing things backwards and seeking. what purpose does it solve?