aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/Animation/FloatAnimation2.cs
blob: 1d5c9193f551ca484c444530fdca42b51d1dc029 (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
// Copyright (c) 2015-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System;

namespace vke
{
	public class FloatAnimation2 : Animation
	{

		public float TargetValue;
		protected float initialValue;
		int stepCount, currentStep;

		#region CTOR
		public FloatAnimation2(Object instance, string _propertyName, float Target, int _stepCount = 20)
			: base(instance, _propertyName)
		{

			TargetValue = Target;

			float value = getValue();
			initialValue = value;

			stepCount = _stepCount;
		}
		#endregion

		bool smooth = true;

		float smoothedStep (float step) => (-MathF.Cos (step * MathF.PI) + 1)/2.0f;

		public override void Process()
		{

			currentStep++;

			float t = (float)currentStep / (float)stepCount;
			if (smooth)
				t = smoothedStep (t);

			setValue (initialValue + t * (TargetValue - initialValue));

			if (currentStep < stepCount)
				return;

			setValue(TargetValue);
			AnimationList.Remove(this);
			RaiseAnimationFinishedEvent ();
		}

		public override string ToString ()
		{
			return string.Format ("{0}:->{1}:{2}",base.ToString(),TargetValue,stepCount);
		}
	}

}