-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathTween.c
420 lines (333 loc) · 11.1 KB
/
Tween.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2016-6-8
* Update : 2022-10-19
* Author : scott.cgi
*/
#include "Engine/Toolkit/Utils/Tween.h"
#include "Engine/Toolkit/Math/Math.h"
#include "Engine/Toolkit/Platform/Log.h"
#include "Engine/Toolkit/Utils/ArrayIntMap.h"
#include "Engine/Toolkit/Utils/ArrayQueue.h"
typedef struct
{
/**
* Target's actions queue, run each after over.
*/
ArrayQueue(TweenAction*) queue [1];
/**
* Target's current running actions.
*/
ArrayList(TweenAction*) current[1];
/**
* A queue action currently is running in current list.
*/
TweenAction* queueAction;
}
Tween;
static ArrayIntMap(tweenID, Tween*) tweenRunningMap[1] = AArrayIntMap_Init(Tween*, 25);
static ArrayList (Tween*) tweenCacheList [1] = AArrayList_Init (Tween*, 25);
static ArrayList (TweenAction*) actionCacheList[1] = AArrayList_Init (TweenAction*, 25);
static inline Tween* GetTween()
{
Tween* tween = AArrayList_Pop(tweenCacheList, Tween*);
if (tween == NULL)
{
tween = malloc(sizeof(Tween));
AArrayQueue->Init(sizeof(TweenAction*), tween->queue);
tween->queue->elementList->increase = 6;
AArrayList->Init(sizeof(TweenAction*), tween->current);
tween->current->increase = 6;
}
else
{
AArrayQueue->Clear(tween->queue);
AArrayList ->Clear(tween->current);
}
tween->queueAction = NULL;
return tween;
}
static TweenAction* GetAction()
{
TweenAction* action = AArrayList_Pop(actionCacheList, TweenAction*);
if (action == NULL)
{
action = malloc(sizeof(TweenAction));
AArrayList->InitWithCapacity(sizeof(TweenActionValue), 6, action->actionValueList);
action->actionValueList->increase = 6;
}
else
{
AArrayList->Clear(action->actionValueList);
}
AUserData_Init(action->userData);
action->curTime = 0.0f;
action->duration = 0.0f;
action->OnComplete = NULL;
action->isQueue = true;
action->target = NULL;
return action;
}
static TweenActionValue* AddTweenActionValue(TweenAction* action)
{
TweenActionValue* actionValue = AArrayList_GetPtrAdd(action->actionValueList, TweenActionValue);
actionValue->value = 0.0f;
actionValue->fromValue = 0.0f;
actionValue->toValue = 0.0f;
actionValue->OnGet = NULL;
actionValue->OnSet = NULL;
actionValue->isRelative = true;
actionValue->easeType = TweenEaseType_Smooth;
return actionValue;
}
static inline void SetActionValue(TweenAction* action)
{
for (int i = 0; i < action->actionValueList->size; ++i)
{
TweenActionValue* actionValue = AArrayList_GetPtr(action->actionValueList, i, TweenActionValue);
ALog_A
(
actionValue->OnGet != NULL && actionValue->OnSet != NULL,
"ATween SetActionValue action OnSet and OnGet must not NULL."
);
actionValue->fromValue = actionValue->OnGet(action->target);
if (actionValue->isRelative)
{
actionValue->toValue = actionValue->value + actionValue->fromValue;
}
else
{
actionValue->toValue = actionValue->value;
}
}
}
static void* RunActions(Array(TweenAction*)* actions, void* tweenID)
{
Tween* tween;
if (tweenID == NULL)
{
// not give tweenID, we use Tween ptr for it
tween = GetTween();
tweenID = tween;
AArrayIntMap_TryPut(tweenRunningMap, tweenID, tween);
}
else
{
int index = AArrayIntMap->GetIndex(tweenRunningMap, (intptr_t) tweenID);
if (index < 0)
{
tween = GetTween();
AArrayIntMap_InsertAt(tweenRunningMap, tweenID, -index - 1, tween);
}
else
{
tween = AArrayIntMap_GetAt(tweenRunningMap, index, Tween*);
}
}
//----------------------------------------------------------------------------------------------------------------------
for (int i = 0; i < actions->length; ++i)
{
TweenAction* action = AArray_Get(actions, i, TweenAction*);
if (action->isQueue)
{
AArrayQueue_Enqueue(tween->queue, action);
}
else
{
AArrayList_Add(tween->current, action);
SetActionValue(action);
}
}
return tweenID;
}
static void RemoveCurrentActionByIndex(Tween* tween, TweenAction* action, int index)
{
if (action == tween->queueAction)
{
tween->queueAction = NULL;
}
AArrayList->RemoveByLast(tween->current, index);
AArrayList_Add(actionCacheList, action);
}
static bool TryRemoveAction(void* tweenID, TweenAction* action)
{
Tween* tween = AArrayIntMap_Get(tweenRunningMap, tweenID, Tween*);
if (tween != NULL)
{
for (int i = 0; i < tween->current->size; ++i)
{
TweenAction* tweenAction = AArrayList_Get(tween->current, i, TweenAction*);
if (action == tweenAction)
{
RemoveCurrentActionByIndex(tween, action, i);
return true;
}
}
for (int i = tween->queue->headIndex; i < tween->queue->elementList->size; ++i)
{
TweenAction* tweenAction = AArrayList_Get(tween->queue->elementList, i, TweenAction*);
if (action == tweenAction)
{
AArrayQueue->RemoveAt(tween->queue, i);
AArrayList_Add(actionCacheList, action);
return true;
}
}
}
return false;
}
static bool TryRemoveAllActions(void* tweenID)
{
int index = AArrayIntMap->GetIndex(tweenRunningMap, (intptr_t) tweenID);
if (index >= 0)
{
Tween* tween = AArrayIntMap_GetAt(tweenRunningMap, index, Tween*);
for (int i = 0; i < tween->current->size; ++i)
{
AArrayList_Add(actionCacheList, AArrayList_Get(tween->current, i, TweenAction*));
}
AArrayList->Clear(tween->current);
TweenAction* action;
while ((action = AArrayQueue_Dequeue(tween->queue, TweenAction*)))
{
AArrayList_Add(actionCacheList, action);
}
// if queueAction not NULL it must be in tweenData->current
// so just set NULL
tween->queueAction = NULL;
return true;
}
return false;
}
static inline void SetActionComplete(TweenAction* action, bool isFireOnComplete)
{
for (int k = 0; k < action->actionValueList->size; ++k)
{
TweenActionValue* actionValue = AArrayList_GetPtr(action->actionValueList, k, TweenActionValue);
actionValue->OnSet
(
action->target,
actionValue->toValue
);
}
if (isFireOnComplete && action->OnComplete != NULL)
{
action->OnComplete(action);
}
}
static bool TryCompleteAllActions(void* tweenID, bool isFireOnComplete)
{
int index = AArrayIntMap->GetIndex(tweenRunningMap, (intptr_t) tweenID);
if (index >= 0)
{
Tween* tween = AArrayIntMap_GetAt(tweenRunningMap, index, Tween*);
for (int i = 0; i < tween->current->size; ++i)
{
TweenAction* action = AArrayList_Get(tween->current, i, TweenAction*);
SetActionComplete(action, isFireOnComplete);
AArrayList_Add(actionCacheList, action);
}
AArrayList->Clear(tween->current);
TweenAction* action;
while ((action = AArrayQueue_Dequeue(tween->queue, TweenAction*)))
{
SetActionComplete(action, isFireOnComplete);
AArrayList_Add(actionCacheList, action);
}
// if queueAction not NULL it must be in tweenData->current
// so just set NULL
tween->queueAction = NULL;
return true;
}
return false;
}
static bool HasAction(void* tweenID)
{
int index = AArrayIntMap->GetIndex(tweenRunningMap, (intptr_t) tweenID);
if (index >= 0)
{
Tween* tween = AArrayIntMap_GetAt(tweenRunningMap, index, Tween*);
if (tween->current->size > 0 || tween->queue->elementList->size > 0)
{
return true;
}
return false;
}
return false;
}
static void Update(float deltaSeconds)
{
for (int i = tweenRunningMap->elementList->size - 1; i > -1; --i)
{
Tween* tween = AArrayIntMap_GetAt(tweenRunningMap, i, Tween*);
// get a queue action to run
if (tween->queueAction == NULL)
{
tween->queueAction = AArrayQueue_Dequeue(tween->queue, TweenAction*);
if (tween->queueAction != NULL)
{
// add the running queue action into current list
AArrayList_Add(tween->current, tween->queueAction);
SetActionValue(tween->queueAction);
}
}
if (tween->current->size == 0)
{
// all actions complete so remove tweenData and push to cache
AArrayList_Add (tweenCacheList, tween);
AArrayIntMap->RemoveAt(tweenRunningMap, i);
continue;
}
for (int j = tween->current->size - 1; j > -1; --j)
{
TweenAction* action = AArrayList_Get(tween->current, j, TweenAction*);
if (action->curTime < action->duration)
{
for (int k = 0; k < action->actionValueList->size; ++k)
{
TweenActionValue* actionValue = AArrayList_GetPtr(action->actionValueList, k, TweenActionValue);
actionValue->OnSet
(
action->target,
actionValue->fromValue + (actionValue->toValue - actionValue->fromValue) *
ATweenEase->Easing[actionValue->easeType](action->curTime / action->duration)
);
}
action->curTime += deltaSeconds;
}
else
{
for (int k = 0; k < action->actionValueList->size; ++k)
{
TweenActionValue* actionValue = AArrayList_GetPtr(action->actionValueList, k, TweenActionValue);
actionValue->OnSet(action->target, actionValue->toValue);
}
// action complete
if (action->OnComplete != NULL)
{
action->OnComplete(action);
}
RemoveCurrentActionByIndex(tween, action, j);
}
}
}
}
struct ATween ATween[1] =
{{
GetAction,
AddTweenActionValue,
RunActions,
TryRemoveAllActions,
TryCompleteAllActions,
TryRemoveAction,
HasAction,
Update,
}};