yangyin
2025-03-27 b0de14c2670b9ff0079dacfb4b7457b438368f11
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
185
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Enum.Metro;
using System;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Animate.Metro
{
    #region AnimateAnimate
 
    public abstract class Animate<T> : IDisposable
    {
        public Action<T> Update { get; set; }
 
        public MethodInvoker Complete { get; set; }
 
        #region Internal Vars
        private DateTime _lastUpdateTime;
        private readonly Timer _animateTimer;
        private bool _reverse;
        #endregion
 
        #region Constructors
        public Animate(int updateInterval = 16)
        {
            _animateTimer = new Timer()
            {
                Interval = updateInterval,
                Enabled = false,
            };
            _animateTimer.Tick += OnFrameUpdate;
            _reverse = false;
            Alpha = 0.0;
        }
        #endregion
 
        #region Functions
        public void Setting(int duration, T initial, T end, EasingType easing = EasingType.Linear)
        {
            InitialValue = initial;
            EndValue = end;
            EasingType = easing;
            Duration = duration;
        }
 
        public void Start()
        {
            _reverse = false;
            Alpha = 0.0;
            Play();
        }
 
        public void Back()
        {
            _reverse = true;
            Alpha = 1.0;
            Play();
        }
 
        public void Start(int duration)
        {
            _reverse = false;
            Alpha = 0.0;
            Duration = duration;
            Play();
        }
 
        public void Back(int duration)
        {
            _reverse = true;
            Alpha = 1.0;
            Duration = duration;
            Play();
        }
 
        public void Reverse()
        {
            Reverse(!_reverse);
        }
 
        public void Reverse(bool val)
        {
            _reverse = val;
 
            if (!Active)
            {
                Play();
            }
        }
 
        public void Play()
        {
            _lastUpdateTime = DateTime.Now;
            Active = true;
            _animateTimer.Enabled = true;
            _animateTimer.Start();
        }
 
        public void Pause()
        {
            _animateTimer.Stop();
            _animateTimer.Enabled = false;
            Active = false;
        }
 
        public void Stop()
        {
            Pause();
            Alpha = _reverse ? 1.0 : 0.0;
        }
 
        public void Start(int duration, T initial, T end, EasingType easing = EasingType.Linear)
        {
            Setting(duration, initial, end, easing);
            Start();
        }
 
        public void Back(int duration, T initial, T end, EasingType easing = EasingType.Linear)
        {
            Setting(duration, initial, end, easing);
            Back();
        }
        #endregion
 
        #region Events
        private void OnFrameUpdate(object sender, EventArgs e)
        {
            DateTime updateTime = DateTime.Now;
            double elapsed;
 
            if (Duration == 0)
            {
                elapsed = 1.0;
            }
            else
            {
                elapsed = (updateTime - _lastUpdateTime).TotalMilliseconds / Duration;
            }
 
            _lastUpdateTime = updateTime;
            Alpha = Math.Max(0.0, Math.Min(Alpha + (_reverse ? -elapsed : elapsed), 1.0));
 
            Update?.Invoke(Value);
 
            if (Alpha is not 0.0 and not 1.0)
            {
                return;
            }
 
            Pause();
            Complete?.Invoke();
        }
        #endregion
 
        #region Properties
        public double Alpha { get; set; }
 
        public int Duration { get; set; }
 
        public T InitialValue { get; private set; }
 
        public abstract T Value { get; }
 
        public T EndValue { get; private set; }
 
        public EasingType EasingType { get; private set; }
 
        public bool Active { get; private set; }
 
        public object Tag { get; set; }
        #endregion
 
        #region Dispose
        void IDisposable.Dispose()
        {
            GC.SuppressFinalize(this);
            _animateTimer.Dispose();
        }
        #endregion
    }
 
    #endregion
}