-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathC3DAnimationClip.cpp
724 lines (624 loc) · 17.5 KB
/
C3DAnimationClip.cpp
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#include "Base.h"
#include "C3DLayer.h"
#include "C3DAnimationClip.h"
#include "C3DAnimation.h"
#include "C3DAnimationChannel.h"
#include "C3DAnimationCurve.h"
#include "C3DActionListener.h"
namespace cocos3d
{
C3DAnimationClip::C3DAnimationClip(const std::string& id, C3DAnimation* animation, unsigned long startTime, unsigned long endTime)
: _id(id), _animation(animation), _startTime(startTime), _endTime(endTime), _duration(_endTime - _startTime),
_stateBits(0x00), _repeatCount(1.0f), _activeDuration(_duration * _repeatCount), _speed(1.0f), _timeStarted(0),
_elapsedTime(0), _blendWeight(1.0f), _crossFadeToClip(NULL), _crossFadeOutElapsed(0), _crossFadeOutDuration(0)
{
assert(0 <= startTime && startTime <= animation->_duration && 0 <= endTime && endTime <= animation->_duration);
_frameCount = ((float)_duration / animation->_duration) * animation->getFrameCount();
_actionEvents = NULL;
}
C3DAnimationClip::~C3DAnimationClip()
{
if (_actionEvents)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
C3DActionEvent* lEvt = *iter;
SAFE_DELETE(lEvt);
++iter;
}
SAFE_DELETE(_actionEvents);
}
}
const std::string& C3DAnimationClip::getID() const
{
return _id;
}
unsigned long C3DAnimationClip::getStartTime() const
{
return _startTime;
}
unsigned long C3DAnimationClip::getEndTime() const
{
return _endTime;
}
unsigned long C3DAnimationClip::getElaspedTime() const
{
return _elapsedTime;
}
void C3DAnimationClip::setRepeatCount(float repeatCount)
{
assert(repeatCount == 0 || repeatCount > 0.0f);
_repeatCount = repeatCount;
if (repeatCount == 0)
{
_activeDuration = _duration;
}
else
{
_activeDuration = _repeatCount * _duration;
}
}
float C3DAnimationClip::getRepeatCount() const
{
return _repeatCount;
}
unsigned long C3DAnimationClip::getDuration() const
{
return _duration;
}
void C3DAnimationClip::setSpeed(float speed)
{
_speed = speed;
}
float C3DAnimationClip::getSpeed() const
{
return _speed;
}
void C3DAnimationClip::setBlendWeight(float blendWeight)
{
_blendWeight = blendWeight;
}
float C3DAnimationClip::getBlendWeight() const
{
return _blendWeight;
}
bool C3DAnimationClip::isPlaying() const
{
return isState(CLIP_IS_PLAYING);
}
bool C3DAnimationClip::isPaused() const
{
return isState(CLIP_IS_PAUSED);
}
bool C3DAnimationClip::isResumed() const
{
return isState(CLIP_IS_RESTARTED);
}
void C3DAnimationClip::play()
{
if (isState(CLIP_IS_PLAYING))
{
// If paused, reset the bit and return.
if (isState(CLIP_IS_PAUSED))
{
resetState(CLIP_IS_PAUSED);
return;
}
// If the clip is set to be removed, reset the flag.
if (isState(CLIP_IS_MARKED_FOR_REMOVAL))
resetState(CLIP_IS_MARKED_FOR_REMOVAL);
setState(CLIP_IS_RESTARTED);
}
else
{
setState(CLIP_IS_PLAYING);
_animation->addRunClip(this);
}
_timeStarted = C3DLayer::getGameTime();
}
void C3DAnimationClip::crossFade(C3DAnimationClip* clip, unsigned long duration)
{
assert(clip);
if (clip->isState(CLIP_IS_FADING_OUT) && clip->_crossFadeToClip == this)
{
clip->resetState(CLIP_IS_FADING_OUT);
clip->_crossFadeToClip->resetState(CLIP_IS_FADING_IN);
SAFE_RELEASE(clip->_crossFadeToClip);
}
if (_crossFadeToClip)
{
SAFE_RELEASE(_crossFadeToClip);
}
// fade in
_crossFadeToClip = clip;
_crossFadeToClip->retain();
_crossFadeToClip->setState(CLIP_IS_FADING_IN);
_crossFadeToClip->_blendWeight = 0.0f;
//fade out
setState(CLIP_IS_FADING_OUT_STARTED);
setState(CLIP_IS_FADING_OUT);
_crossFadeOutElapsed = 0L;
_crossFadeOutDuration = duration;
if (!isState(CLIP_IS_PLAYING))
play();
_crossFadeToClip->play();
}
void C3DAnimationClip::stop()
{
_stateBits = CLIP_IS_MARKED_FOR_REMOVAL;
}
void C3DAnimationClip::pause()
{
_stateBits = CLIP_IS_PAUSED;
}
void C3DAnimationClip::resume()
{
_stateBits = CLIP_IS_RESTARTED;
}
unsigned short C3DAnimationClip::update(unsigned long elapsedTime, bool updatePose)
{
if(_repeatCount != 0)
{
return update_once(elapsedTime, updatePose);
}
else
{
return update_loop(elapsedTime, updatePose);
}
}
unsigned short C3DAnimationClip::update_loop(unsigned long elapsedTime, bool updatePose)
{
if (isState(CLIP_IS_PAUSED))
{
return CLIP_IS_PAUSED;
}
else if (isState(CLIP_IS_MARKED_FOR_REMOVAL))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_NONE;
return CLIP_IS_MARKED_FOR_REMOVAL;
}
else if(isState(CLIP_IS_RESTARTED))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_PLAYING;
return CLIP_IS_RESTARTED;
}
else if (!isState(CLIP_IS_STARTED))
{
_elapsedTime = (C3DLayer::getGameTime() - _timeStarted) * _speed;
setState(CLIP_IS_STARTED);
}
else if (isState(CLIP_IS_FADING_OUT))
{
if (isState(CLIP_IS_FADING_OUT_STARTED)) // Calculate elapsed time since the fade out begin.
{
_crossFadeOutElapsed = (C3DLayer::getGameTime() - _crossFadeToClip->_timeStarted) * abs(_speed);
resetState(CLIP_IS_FADING_OUT_STARTED);
}
else
{
_crossFadeOutElapsed += elapsedTime * abs(_speed);
}
if (_crossFadeOutElapsed < _crossFadeOutDuration)
{
_blendWeight = (float) (_crossFadeOutDuration - _crossFadeOutElapsed) / (float) _crossFadeOutDuration;
_crossFadeToClip->_blendWeight = (1.0f - _blendWeight);
}
else
{ // Fade is done.
_blendWeight = 0.0f;
_crossFadeToClip->_blendWeight = 1.0f;
resetState(CLIP_IS_STARTED);
resetState(CLIP_IS_FADING_OUT);
_crossFadeToClip->resetState(CLIP_IS_FADING_IN);
SAFE_RELEASE(_crossFadeToClip);
}
}
else
{
_elapsedTime += elapsedTime * _speed;
if (_repeatCount == 0 && _elapsedTime <= 0)
_elapsedTime = _activeDuration + _elapsedTime;
}
unsigned long currentTime = 0L;
int counter = 0;
// Check to see if clip is complete.
if (_repeatCount != 0 && ((_speed >= 0.0f && _elapsedTime >= (long) _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0L)))
{
resetState(CLIP_IS_STARTED);
if (_speed >= 0.0f)
{
// If _duration == 0, we have a "pose". Just set currentTime to 0.
if (_duration == 0)
{
currentTime = 0L;
}
else
{
currentTime = _activeDuration % _duration; // Get's the fractional part of the final repeat.
if (currentTime == 0L)
currentTime = _duration;
}
}
else
{
currentTime = 0L; // If we are negative speed, the end value should be 0.
}
}
else
{
// If _duration == 0, we have a "pose". Just set currentTime to 0.
if (_duration == 0)
currentTime = 0L;
else // Gets portion/fraction of the repeat.
{
currentTime = _elapsedTime % _duration;
counter = _elapsedTime/_duration;
}
}
// Notify any listeners of Animation events.
if (_actionEvents)
{
C3DActionEvent* evt;
if (_speed >= 0.0f)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
evt = *iter;
if(_elapsedTime == 0)
{
if( _elapsedTime>=((counter-1)*(long)_duration + (long)evt->_eventTime))
{
evt->_listener->action();
// evt->_finished = true;
}
}
else
{
if(currentTime == 0)
{
if( _elapsedTime>=((counter-1)*(long)_duration + (long)evt->_eventTime)/* && evt->_finished == false*/)
{
evt->_listener->action();
//evt->_finished = true;
}
}
else
{
if( _elapsedTime>=(counter*(long)_duration + (long)evt->_eventTime)/* && evt->_finished == false*/)
{
evt->_listener->action();
//evt->_finished = true;
}
}
}
iter++;
}
}
else
{
//
if(_actionEvents->empty())
return CLIP_IS_NONE;
std::list<C3DActionEvent*>::iterator iter = _actionEvents->end();
iter--;
while (iter != _actionEvents->begin())
{
evt = *iter;
if( _elapsedTime<=(long)evt->_eventTime && evt->_finished == false)
{
evt->_listener->action();
// evt->_finished = true;
}
iter--;
}
}
}
if (updatePose)
{
float percentComplete = (float)(_startTime + currentTime) / (float) _animation->_duration;
// Evaluate this clip.
C3DAnimationChannel* channel = NULL;
C3DBone* bone = NULL;
unsigned int channelCount = _animation->_channels.size();
float value[10];
for (unsigned int i = 0; i < channelCount; i++)
{
channel = _animation->_channels[i];
bone = channel->_bone;
// Evaluate the point on Curve
C3DAnimationCurve::InterpolationMode mode = _animation->getQuality() == C3DAnimation::High ? C3DAnimationCurve::Linear : C3DAnimationCurve::Near;
channel->getCurve()->evaluate(percentComplete, value, mode);
// Set the animation value on the target property.
bone->setAnimationValue(value, _blendWeight);
}
}
// When ended. Probably should move to it's own method so we can call it when the clip is ended early.
if (!isState(CLIP_IS_STARTED))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_NONE;
if (_actionEvents)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
// (*iter)->_finished = false;
iter++;
}
}
return CLIP_IS_MARKED_FOR_REMOVAL;
}
return CLIP_IS_PLAYING;
}
unsigned short C3DAnimationClip::update_once(unsigned long elapsedTime, bool updatePose)
{
if (isState(CLIP_IS_PAUSED))
{
return CLIP_IS_PAUSED;
}
else if (isState(CLIP_IS_MARKED_FOR_REMOVAL))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_NONE;
return CLIP_IS_MARKED_FOR_REMOVAL;
}
else if(isState(CLIP_IS_RESTARTED))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_PLAYING;
return CLIP_IS_RESTARTED;
}
else if (!isState(CLIP_IS_STARTED))
{
_elapsedTime = (C3DLayer::getGameTime() - _timeStarted) * _speed;
setState(CLIP_IS_STARTED);
}
else if (isState(CLIP_IS_FADING_OUT))
{
if (isState(CLIP_IS_FADING_OUT_STARTED)) // Calculate elapsed time since the fade out begin.
{
_crossFadeOutElapsed = (C3DLayer::getGameTime() - _crossFadeToClip->_timeStarted) * abs(_speed);
resetState(CLIP_IS_FADING_OUT_STARTED);
}
else
{
_crossFadeOutElapsed += elapsedTime * abs(_speed);
}
if (_crossFadeOutElapsed < _crossFadeOutDuration)
{
_blendWeight = (float) (_crossFadeOutDuration - _crossFadeOutElapsed) / (float) _crossFadeOutDuration;
_crossFadeToClip->_blendWeight = (1.0f - _blendWeight);
}
else
{ // Fade is done.
_blendWeight = 0.0f;
_crossFadeToClip->_blendWeight = 1.0f;
resetState(CLIP_IS_STARTED);
resetState(CLIP_IS_FADING_OUT);
_crossFadeToClip->resetState(CLIP_IS_FADING_IN);
SAFE_RELEASE(_crossFadeToClip);
}
}
else
{
_elapsedTime += elapsedTime * _speed;
if (_repeatCount == 0 && _elapsedTime <= 0)
_elapsedTime = _activeDuration + _elapsedTime;
}
unsigned long currentTime = 0L;
// Check to see if clip is complete.
if (_repeatCount != 0 && ((_speed >= 0.0f && _elapsedTime >= (long) _activeDuration) || (_speed <= 0.0f && _elapsedTime <= 0L)))
{
resetState(CLIP_IS_STARTED);
if (_speed >= 0.0f)
{
// If _duration == 0, we have a "pose". Just set currentTime to 0.
if (_duration == 0)
{
currentTime = 0L;
}
else
{
currentTime = _activeDuration % _duration; // Get's the fractional part of the final repeat.
if (currentTime == 0L)
currentTime = _duration;
}
}
else
{
currentTime = 0L; // If we are negative speed, the end value should be 0.
}
}
else
{
// If _duration == 0, we have a "pose". Just set currentTime to 0.
if (_duration == 0)
currentTime = 0L;
else // Gets portion/fraction of the repeat.
currentTime = _elapsedTime % _duration;
}
// Notify any listeners of Animation events.
if (_actionEvents)
{
C3DActionEvent* evt;
if (_speed >= 0.0f)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
evt = *iter;
if( _elapsedTime>=(long)evt->_eventTime && evt->_finished == false)
{
evt->_listener->action();
evt->_finished = true;
}
iter++;
}
}
else
{
//
if(_actionEvents->empty())
return CLIP_IS_NONE;
std::list<C3DActionEvent*>::iterator iter = _actionEvents->end();
iter--;
while (iter != _actionEvents->begin())
{
evt = *iter;
if( _elapsedTime<=(long)evt->_eventTime && evt->_finished == false)
{
evt->_listener->action();
evt->_finished = true;
}
iter--;
}
}
}
if (updatePose)
{
float percentComplete = (float)(_startTime + currentTime) / (float) _animation->_duration;
// Evaluate this clip.
C3DAnimationChannel* channel = NULL;
C3DBone* bone = NULL;
unsigned int channelCount = _animation->_channels.size();
float value[10];
for (unsigned int i = 0; i < channelCount; i++)
{
channel = _animation->_channels[i];
bone = channel->_bone;
// Evaluate the point on Curve
C3DAnimationCurve::InterpolationMode mode = _animation->getQuality() == C3DAnimation::High ? C3DAnimationCurve::Linear : C3DAnimationCurve::Near;
channel->getCurve()->evaluate(percentComplete, value, mode);
// Set the animation value on the target property.
bone->setAnimationValue(value, _blendWeight);
}
}
// When ended. Probably should move to it's own method so we can call it when the clip is ended early.
if (!isState(CLIP_IS_STARTED))
{
_blendWeight = 1.0f;
_stateBits = CLIP_IS_NONE;
if (_actionEvents)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
(*iter)->_finished = false;
iter++;
}
}
return CLIP_IS_MARKED_FOR_REMOVAL;
}
return CLIP_IS_PLAYING;
}
bool C3DAnimationClip::isState(unsigned short bit) const
{
return (_stateBits & bit) == bit;
}
void C3DAnimationClip::setState(unsigned short bit)
{
_stateBits |= bit;
}
void C3DAnimationClip::resetState(unsigned short bit)
{
_stateBits &= ~bit;
}
void C3DAnimationClip::addActionEvent(C3DActionListener* listener, unsigned long eventTime)
{
assert(listener);
assert(eventTime <= _activeDuration);
C3DActionEvent* actionEvent = new C3DActionEvent(listener, eventTime);
if (!_actionEvents)
{
_actionEvents = new std::list<C3DActionEvent*>;
_actionEvents->push_back(actionEvent);
}
else
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
if (eventTime < (*iter)->_eventTime)
{
iter = _actionEvents->insert(iter, actionEvent);
return;
}
iter++;
}
_actionEvents->push_back(actionEvent);
}
}
void C3DAnimationClip::addActionEvent(C3DActionEvent* actionEvent)
{
assert(actionEvent);
if (!_actionEvents)
{
_actionEvents = new std::list<C3DActionEvent*>;
_actionEvents->push_back(actionEvent);
}
else
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
if (actionEvent->_eventTime < (*iter)->_eventTime)
{
iter = _actionEvents->insert(iter, actionEvent);
return;
}
iter++;
}
_actionEvents->push_back(actionEvent);
}
}
void C3DAnimationClip::clearActionEvent()
{
if (_actionEvents)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
C3DActionEvent* lEvt = *iter;
SAFE_DELETE(lEvt);
++iter;
}
SAFE_DELETE(_actionEvents);
}
}
int C3DAnimationClip::getFrameCount()
{
return _frameCount;
}
void C3DAnimationClip::setCurrentFrame(int nFrame)
{
_elapsedTime = ((float)nFrame / _frameCount) * _duration;
}
int C3DAnimationClip::getCurrentFrame()
{
float tElapsed = ( _elapsedTime % _duration );
return tElapsed / _duration * _frameCount;
}
C3DAnimationClip* C3DAnimationClip::clone(C3DAnimation* animation)
{
C3DAnimationClip* clip = new C3DAnimationClip(_id.c_str(), animation, _startTime, _endTime);
clip->setSpeed(getSpeed());
clip->setRepeatCount(getRepeatCount());
if (_actionEvents)
{
std::list<C3DActionEvent*>::iterator iter = _actionEvents->begin();
while (iter != _actionEvents->end())
{
C3DActionEvent* srcEvent = *iter;
C3DActionEvent* objEvent = new C3DActionEvent(*srcEvent);
clip->addActionEvent(objEvent);
++iter;
}
}
clip->autorelease();
return clip;
}
}