yangyin
2025-01-13 36616eb44dc36a4e6e3e7a7540310cb850218ea9
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
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Enum.Poison;
using System;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Animate.Poison
{
    #region AnimateAnimate
 
    public delegate void AnimationAction();
    public delegate bool AnimationFinishedEvaluator();
 
    public abstract class Animate
    {
        public event EventHandler AnimationCompleted;
        private void OnAnimationCompleted()
        {
            AnimationCompleted?.Invoke(this, EventArgs.Empty);
        }
 
        private DelayedCall timer;
        private Control targetControl;
 
        private AnimationAction actionHandler;
        private AnimationFinishedEvaluator evaluatorHandler;
 
        protected TransitionType transitionType;
        protected int counter;
        protected int startTime;
        protected int targetTime;
 
        public bool IsCompleted
        {
            get
            {
                if (timer != null)
                {
                    return !timer.IsWaiting;
                }
 
                return true;
            }
        }
        public bool IsRunning
        {
            get
            {
                if (timer != null)
                {
                    return timer.IsWaiting;
                }
 
                return false;
            }
        }
 
        public void Cancel()
        {
            if (IsRunning)
            {
                timer.Cancel();
            }
        }
 
        protected void Start(Control control, TransitionType transitionType, int duration, AnimationAction actionHandler)
        {
            Start(control, transitionType, duration, actionHandler, null);
        }
 
        protected void Start(Control control, TransitionType transitionTypee, int duration, AnimationAction actionHandlerr, AnimationFinishedEvaluator evaluatorHandlerr)
        {
            targetControl = control;
            transitionType = transitionTypee;
            actionHandler = actionHandlerr;
            evaluatorHandler = evaluatorHandlerr;
 
            counter = 0;
            startTime = 0;
            targetTime = duration;
 
            timer = DelayedCall.Start(DoAnimation, duration);
        }
 
        private void DoAnimation()
        {
            if (evaluatorHandler == null || evaluatorHandler.Invoke())
            {
                OnAnimationCompleted();
            }
            else
            {
                actionHandler.Invoke();
                counter++;
 
                timer.Start();
            }
        }
 
        protected int MakeTransition(float t, float b, float d, float c)
        {
            switch (transitionType)
            {
                case TransitionType.Linear:
                    // simple linear tweening - no easing 
                    return (int)((c * t / d) + b);
                case TransitionType.EaseInQuad:
                    // quadratic (t^2) easing in - accelerating from zero velocity
                    return (int)((c * (t /= d) * t) + b);
                case TransitionType.EaseOutQuad:
                    // quadratic (t^2) easing out - decelerating to zero velocity
                    return (int)((-c * (t /= d) * (t - 2)) + b);
                case TransitionType.EaseInOutQuad:
                    // quadratic easing in/out - acceleration until halfway, then deceleration
                    if ((t /= d / 2) < 1)
                    {
                        return (int)((c / 2 * t * t) + b);
                    }
                    else
                    {
                        return (int)((-c / 2 * (((--t) * (t - 2)) - 1)) + b);
                    }
                case TransitionType.EaseInCubic:
                    // cubic easing in - accelerating from zero velocity
                    return (int)((c * (t /= d) * t * t) + b);
 
                case TransitionType.EaseOutCubic:
                    // cubic easing in - accelerating from zero velocity
                    return (int)((c * (((t = (t / d) - 1) * t * t) + 1)) + b);
                case TransitionType.EaseInOutCubic:
                    // cubic easing in - accelerating from zero velocity
                    if ((t /= d / 2) < 1)
                    {
                        return (int)((c / 2 * t * t * t) + b);
                    }
                    else
                    {
                        return (int)((c / 2 * (((t -= 2) * t * t) + 2)) + b);
                    }
                case TransitionType.EaseInQuart:
                    // quartic easing in - accelerating from zero velocity
                    return (int)((c * (t /= d) * t * t * t) + b);
                case TransitionType.EaseInExpo:
                    // exponential (2^t) easing in - accelerating from zero velocity
                    if (t == 0)
                    {
                        return (int)b;
                    }
                    else
                    {
                        return (int)((c * Math.Pow(2, 10 * ((t / d) - 1))) + b);
                    }
                case TransitionType.EaseOutExpo:
                    // exponential (2^t) easing out - decelerating to zero velocity
                    if (t == d)
                    {
                        return (int)(b + c);
                    }
                    else
                    {
                        return (int)((c * (-Math.Pow(2, -10 * t / d) + 1)) + b);
                    }
                default:
                    return 0;
            }
        }
    }
 
    #endregion
}