AnimationManager.cpp 2.75 KB
Newer Older
's avatar
committed
1
#include "AnimationManager.h"
2
3
4
5

// --------------- STDLIB ---------------
// --------------- EXTERN ---------------
// --------------- INTERN ---------------
's avatar
committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <engine/Time.h>

namespace glare
{
	namespace core
	{

		AnimationManager::AnimationManager()
		{

		}

		AnimationManager::~AnimationManager()
		{

		}

		size_t AnimationManager::addAnimation(std::shared_ptr<Animation> animation)
		{
			m_pending_animations.push_back(animation);
			return m_pending_animations.size() - 1;
		}

		size_t AnimationManager::addAnimation(const Animation &animation)
		{
			std::shared_ptr<Animation> anim = std::make_shared<Animation>();
			memcpy(anim.get(), &animation, sizeof(Animation));
			return addAnimation(anim);
		}

		void AnimationManager::stopAnimation(std::shared_ptr<Animation> animation)
		{
			if (animation->on_update)
				animation->on_update(animation->current_time, animation->current_value);

			if (animation->on_cancel)
				animation->on_cancel();

			if (animation->on_finish)
				animation->on_finish();

			animation->mark_finished = true;
		}

		void AnimationManager::stopAnimation(unsigned animation_id)
		{
			Animation &animation = *m_pending_animations[animation_id];

			if (animation.on_update)
				animation.on_update(animation.current_time, animation.current_value);

			if (animation.on_cancel)
				animation.on_cancel();

			if (animation.on_finish)
				animation.on_finish();

			animation.mark_finished = true;
		}

		void AnimationManager::update()
		{
68
			const double delta = Time::deltaTime() * 1000.0;
's avatar
committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
			unsigned index = 0;
			for (auto it = m_pending_animations.begin(); it != m_pending_animations.end();)
			{
				handleAnimation(**it, delta, index++);

				if (it->operator->()->mark_finished)
				{
					it = m_pending_animations.erase(it);
				}
				else
				{
					++it;
				}
			}
		}

		void AnimationManager::handleAnimation(Animation &animation, double delta, unsigned index)
		{
			if (animation.mark_finished)
				return;

			animation.current_time += delta;

			if (animation.current_time > animation.start_delay)
			{
				if (!animation.has_started)
				{
					if (animation.on_start)
						animation.on_start();
					animation.has_started = true;
				}

101
				const double from_delay = animation.current_time - animation.start_delay;
's avatar
committed
102
103
104
105
106
107
108
109
110
111
112

				if (from_delay >= animation.duration)
				{
					animation.current_value = 1.0;
					animation.current_time = animation.duration + animation.start_delay;
					stopAnimation(index);
				}
				else
				{
					animation.current_value = from_delay / animation.duration;
					if (animation.on_update)
113
						animation.on_update(from_delay, animation.interpolator(animation.current_value));
's avatar
committed
114
115
116
117
118
				}
			}
		}
	}
}