summaryrefslogtreecommitdiff
path: root/Assets/UI_Extension/Scripts/Animation/Tween/TweenAnimation.cs
blob: 644155246531ba72b902218895f9ca26646e07d1 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// by chai
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace TweenAnimation
{
    // tween播放过程中产生事件
    [Serializable]
    public class TweenEvent
    {
        public string name; // 事件名
        public float time; // 事件触发时间
        public UnityEvent eventHandler; // 回调函数
#if UNITY_EDITOR
        [NonSerialized]
        public bool unfold = false;
#endif
        public TweenEvent()
        {
            name = "New Event";
            time = 0;
            eventHandler = null;
        }
    }

    public class TweenAnimation : MonoBehaviour
    {
        public enum EventTriggeredDirection
        {
            None = 0, 
            Forward = 1, 
            Backward = 2,
        }

        public enum PlaybackStyle
        {
            Once, 
            Loop, 
            PingPong,
        }

        public string description = "No Description";

        [SerializeReference]
        public List<TweenModule> modules;

        public float duration;

        public PlaybackStyle playbackStyle;

        public EventTriggeredDirection eventTriggeredDirection;

        // 回放次数,0是不限制,默认是0
        public int playbackLimit;

        // 是否触发事件
        public bool triggerEvents;

        // 只触发一次事件
        public bool triggerOnce;

        // 脚本中处理 ITweenEventHandler
        public bool scriptHandler;

        public List<TweenEvent> eventList;

        public TweenAnimation()
        {
            this.playbackStyle = PlaybackStyle.Loop;
            this.playbackLimit = 0;
            this.duration = 1;
            this.modules = new List<TweenModule>();
            this.triggerOnce = false;
        }

        public void AddModule(TweenModule module)
        {
            if (modules == null)
                modules = new List<TweenModule>();
            modules.Add(module);
        }

        public void RemoveModule(TweenModule module)
        {
            if (modules == null)
                return;
            if (modules.Contains(module))
                modules.Remove(module);
        }

        public void AddEvent(TweenEvent e)
        {
            if (eventList == null)
                eventList = new List<TweenEvent>();
            eventList.Add(e);
        }

        public void RemoveEvent(TweenEvent e)
        {
            if (eventList == null)
                return;
            eventList.Remove(e);
        }

        /// <summary>
        /// 根据时间执行tween
        /// </summary>
        /// <param name="time">real time</param>
        public void Process(float time)
        {
            if (modules == null)
                return;

            time = ApplyLimit(time);
            float logicTime = HandleTime(time);

            for (int i = 0; i < modules.Count; i ++)
            {
                TweenModule module = modules[i];
                if (!module.enabled)
                    continue;
                module.OnUpdate(logicTime);
            }
        }

        float HandleOnce(float time)
        {
            time = Mathf.Clamp(time, 0, duration);
            return time;
        }

        float HandleLoop(float time)
        {
            time = time % duration;
            return time;
        }

        float HandlePingpong(float time)
        {
            if (time <= duration)
                return time;
            float remain = time % duration;
            int round = (int)(time / duration);
            bool bOdd = (round & 1) == 1;
            if (bOdd)
                time = duration - remain;
            else
                time = remain;

            return time;
        }

        // 返回[0,duration]范围内的逻辑时间
        public float HandleTime(float time)
        {
            float logicTime = time;
            if (this.playbackStyle == PlaybackStyle.Once)
                logicTime = HandleOnce(time);
            else if (this.playbackStyle == PlaybackStyle.Loop)
                logicTime = HandleLoop(time);
            else if (this.playbackStyle == PlaybackStyle.PingPong)
                logicTime = HandlePingpong(time);
            return logicTime;
        }

        public float ApplyLimit(float time)
        {
            if (playbackLimit <= 0)
                return time;
            if (playbackStyle == PlaybackStyle.Loop)
                time = Mathf.Clamp(time, 0, playbackLimit * duration);
            else if(playbackStyle == PlaybackStyle.PingPong)
                time = Mathf.Clamp(time, 0, playbackLimit * duration);
            return time;
        }

#if UNITY_EDITOR
        public void UpdateEditor(float time)
        {
            Process(time);
        }

        // 返回0-duration内的时间,用负数代表从右往左
        public float GetIdentifiedTime(float t)
        {
            int round = (int)(t / duration);
            float time = HandleTime(t);
            if(playbackStyle == PlaybackStyle.PingPong)
            {
                bool bOdd = (round & 1) == 1;
                if(bOdd)
                    time = -time;
            }
            return time;
        }

#endif

    }

}