forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueAnimation.cs
More file actions
150 lines (117 loc) · 4.33 KB
/
Copy pathValueAnimation.cs
File metadata and controls
150 lines (117 loc) · 4.33 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.Experimental.UIElements.GraphView
{
internal interface IEasingCurve
{
float GetEffectiveProgress(float progress);
}
internal abstract class ValueAnimationBase
{
const int k_DefaultDuration = 200;
const int k_StepDuration = 5;
IVisualElementScheduler m_Schedule;
static Dictionary<Type, Func<object, object, float, object>> s_InterpolateFuncs;
public event Action finished;
protected abstract object startValue { get; }
protected abstract object endValue { get; }
public int duration { get; set; }
public IEasingCurve easingCurve { get; set; }
public float progress { get; private set; }
private float progressIncrement
{
get
{
if (duration > 0)
return (float)k_StepDuration / duration;
return float.MaxValue;
}
}
public bool running { get; private set; }
public ValueAnimationBase(IVisualElementScheduler schedule)
{
m_Schedule = schedule;
duration = k_DefaultDuration;
}
static ValueAnimationBase()
{
s_InterpolateFuncs = new Dictionary<Type, Func<object, object, float, object>>();
s_InterpolateFuncs[typeof(float)] = (a, b, interp) => Mathf.Lerp((float)a, (float)b, interp);
s_InterpolateFuncs[typeof(Vector2)] = (a, b, interp) => Vector2.Lerp((Vector2)a, (Vector2)b, interp);
s_InterpolateFuncs[typeof(Vector3)] = (a, b, interp) => Vector3.Lerp((Vector3)a, (Vector3)b, interp);
s_InterpolateFuncs[typeof(Rect)] = (a, b, interp) =>
{
Rect r1 = (Rect)a;
Rect r2 = (Rect)b;
return new Rect(Mathf.Lerp(r1.x, r2.x, interp)
, Mathf.Lerp(r1.y, r2.y, interp)
, Mathf.Lerp(r1.width, r2.width, interp)
, Mathf.Lerp(r1.height, r2.height, interp));
};
s_InterpolateFuncs[typeof(Color)] = (a, b, interp) => Color.Lerp((Color)a, (Color)b, interp);
}
public void Start()
{
if (running || m_Schedule == null)
return;
running = true;
UpdateValue();
m_Schedule.Execute(Step).StartingIn(0).Every(k_StepDuration).Until(() => progress >= 1f || !running);
}
public void Stop()
{
if (!running)
return;
running = false;
progress = 0;
if (finished != null)
finished();
}
void Step()
{
progress += progressIncrement;
UpdateValue();
if (progress >= 1.0f)
{
Stop();
}
}
void UpdateValue()
{
float effectiveProgress = easingCurve != null ? easingCurve.GetEffectiveProgress(progress) : progress;
object currentValue = Interpolate(startValue, endValue, effectiveProgress);
NotifyValueUpdated(currentValue);
}
protected abstract void NotifyValueUpdated(object value);
static object Interpolate(object a, object b, float interp)
{
Func<object, object, float, object> interpolateFunc = null;
if (s_InterpolateFuncs.TryGetValue(a.GetType(), out interpolateFunc))
{
return interpolateFunc(a, b, interp);
}
return a;
}
}
internal class ValueAnimation<T> : ValueAnimationBase
{
public event Action<T> valueUpdated;
public T from { get; set; }
public T to { get; set; }
protected override object startValue => from;
protected override object endValue => to;
public ValueAnimation(IVisualElementScheduler schedule) : base(schedule)
{
}
protected override void NotifyValueUpdated(object value)
{
if (valueUpdated != null)
valueUpdated((T)value);
}
}
}