summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Tweening/Tween.cs
blob: ad292518c6d9700bf5f1c6136f912903d388bada (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Tweening
{
    public abstract class Tween<T> : Tween
        where T : struct
    {
        internal Tween(object target, float duration, float delay, TweenMember<T> member, T endValue)
            : base(target, duration, delay)
        {
            Member = member;
            _endValue = endValue;
        }

        public TweenMember<T> Member { get; }
        public override string MemberName => Member.Name;

        protected T _startValue;
        protected T _endValue;

        protected override void Initialize()
        {
            _startValue = Member.Value;
        }

        protected override void Swap()
        {
            _endValue = _startValue;
            Initialize();
        }
    }

    public abstract class Tween
    {
        internal Tween(object target, float duration, float delay)
        {
            Target = target;
            Duration = duration;
            Delay = delay;
            IsAlive = true;

            _remainingDelay = delay;
        }

        public object Target { get; }
        public abstract string MemberName { get; }
        public float Duration { get; }
        public float Delay { get; }
        public bool IsPaused { get; set; }
        public bool IsRepeating => _remainingRepeats != 0;
        public bool IsRepeatingForever => _remainingRepeats < 0;
        public bool IsAutoReverse { get; private set; }
        public bool IsAlive { get; private set; }
        public bool IsComplete { get; private set; }
        public float TimeRemaining => Duration - _elapsedDuration;
        public float Completion => MathHelper.Clamp(_completion, 0, 1);

        private Func<float, float> _easingFunction;
        private bool _isInitialized;
        private float _completion;
        private float _elapsedDuration;
        private float _remainingDelay;
        private float _repeatDelay;
        private int _remainingRepeats;
        private Action<Tween> _onBegin;
        private Action<Tween> _onEnd;

        public Tween Easing(Func<float, float> easingFunction) { _easingFunction = easingFunction; return this; }
        public Tween OnBegin(Action<Tween> action) { _onBegin = action; return this; }
        public Tween OnEnd(Action<Tween> action) { _onEnd = action; return this; }
        public Tween Pause() { IsPaused = true; return this; }
        public Tween Resume() { IsPaused = false; return this; }

        public Tween Repeat(int count, float repeatDelay = 0f)
        {
            _remainingRepeats = count;
            _repeatDelay = repeatDelay;
            return this;
        }

        public Tween RepeatForever(float repeatDelay = 0f)
        {
            _remainingRepeats = -1;
            _repeatDelay = repeatDelay;
            return this;
        }

        public Tween AutoReverse()
        {
            if (_remainingRepeats == 0)
                _remainingRepeats = 1;

            IsAutoReverse = true;
            return this;
        }

        protected abstract void Initialize();
        protected abstract void Interpolate(float n);
        protected abstract void Swap();

        public void Cancel()
        {
            _remainingRepeats = 0;
            IsAlive = false;
        }

        public void CancelAndComplete()
        {
            if (IsAlive)
            {
                _completion = 1;

                Interpolate(1);
                IsComplete = true;
                _onEnd?.Invoke(this);
            }

            Cancel();
        }

        public void Update(float elapsedSeconds)
        {
            if(IsPaused || !IsAlive)
                return;

            if (_remainingDelay > 0)
            {
                _remainingDelay -= elapsedSeconds;

                if (_remainingDelay > 0)
                    return;
            }

            if (!_isInitialized)
            {
                _isInitialized = true;
                Initialize();
                _onBegin?.Invoke(this);
            }

            if (IsComplete)
            {
                IsComplete = false;
                _elapsedDuration = 0;
                _onBegin?.Invoke(this);

                if (IsAutoReverse)
                    Swap();
            }

            _elapsedDuration += elapsedSeconds;

            var n = _completion = _elapsedDuration / Duration;

            if (_easingFunction != null)
                n = _easingFunction(n);

            if (_elapsedDuration >= Duration)
            {
                if (_remainingRepeats != 0)
                {
                    if(_remainingRepeats > 0)
                        _remainingRepeats--;

                    _remainingDelay = _repeatDelay;
                }
                else if (_remainingRepeats == 0)
                {
                    IsAlive = false;
                }

                n = _completion = 1;
                IsComplete = true;
            }

            Interpolate(n);

            if (IsComplete)
                _onEnd?.Invoke(this);
        }

    }
}