summaryrefslogtreecommitdiff
path: root/examples/ThirdPartyLibs/Gwen/Anim.cpp
blob: 4094a492a9922a57521a5804170a4928d36d183b (plain)
1
2
3
4
5
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
68
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
	GWEN
	Copyright (c) 2010 Facepunch Studios
	See license in Gwen.h
*/

#include "Gwen/Anim.h"
#include "Gwen/Utility.h"
#include <math.h>

using namespace Gwen;

#ifndef GWEN_NO_ANIMATION

static Gwen::Anim::Animation::List g_Animations;
static Gwen::Anim::Animation::ChildList g_AnimationsListed;

void Gwen::Anim::Add(Gwen::Controls::Base* control, Animation* animation)
{
	animation->m_Control = control;

	g_Animations[control].push_back(animation);
}

void Gwen::Anim::Cancel(Gwen::Controls::Base* control)
{
	/* cannot use std::list iterator with algoryhtmns based on pointers
	struct AnimDeletePredicate 
	{
		AnimDeletePredicate( Gwen::Controls::Base* control )
		{
			this->control = control;
		}

		bool operator() ( Gwen::Anim::Animation* anim ) 
		{
			return anim->m_Control == control;
		}

		Gwen::Controls::Base* control;
	};

	std::remove_if ( g_Animations.begin(), g_Animations.end(), AnimDeletePredicate( control ) );
	*/
	Gwen::Anim::Animation::List::iterator iAnimations;
	if ((iAnimations = g_Animations.find(control)) != g_Animations.end())
	{
		Gwen::Anim::Animation::ChildList& ChildAnimationsForControl = iAnimations->second;
		Gwen::Anim::Animation::ChildList::iterator iAnimationChild = ChildAnimationsForControl.begin();
		if (iAnimationChild != ChildAnimationsForControl.end())
		{
			do
			{
				delete (*iAnimationChild);
			} while (++iAnimationChild != ChildAnimationsForControl.end());
		}
		g_Animations.erase(iAnimations);
	}
}

void Gwen::Anim::Think()
{
	Gwen::Anim::Animation::List::iterator it = g_Animations.begin();

	if (it != g_Animations.end())
	{
		Gwen::Anim::Animation::ChildList::iterator itChild;

		Gwen::Anim::Animation* anim;

		do
		{
			if ((itChild = it->second.begin()) != it->second.end())
			{
				do
				{
					anim = *itChild;

					anim->Think();

					if (anim->Finished())
					{
						itChild = it->second.erase(itChild);

						delete anim;
					}
					else
					{
						++itChild;
					}

				} while (itChild != it->second.end());
			}

		} while (++it != g_Animations.end());
	}
}

Gwen::Anim::TimedAnimation::TimedAnimation(float fLength, float fDelay, float fEase)
{
	m_fStart = Platform::GetTimeInSeconds() + fDelay;
	m_fEnd = m_fStart + fLength;
	m_fEase = fEase;
	m_bStarted = false;
	m_bFinished = false;
}

void Gwen::Anim::TimedAnimation::Think()
{
	if (m_bFinished) return;

	float fCurrent = Platform::GetTimeInSeconds();
	float fSecondsIn = fCurrent - m_fStart;
	if (fSecondsIn < 0.0f) return;

	if (!m_bStarted)
	{
		m_bStarted = true;
		OnStart();
	}

	float fDelta = fSecondsIn / (m_fEnd - m_fStart);
	if (fDelta < 0.0f) fDelta = 0.0f;
	if (fDelta > 1.0f) fDelta = 1.0f;

	Run(pow(fDelta, m_fEase));

	if (fDelta == 1.0f)
	{
		m_bFinished = true;
		OnFinish();
	}
}

bool Gwen::Anim::TimedAnimation::Finished()
{
	return m_bFinished;
}

#endif