-
Notifications
You must be signed in to change notification settings - Fork 21
/
ActionClip.cs
769 lines (649 loc) · 24 KB
/
ActionClip.cs
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Sirenix.OdinInspector;
namespace Slate
{
[Attachable(typeof(ActionTrack))]
///Clips are added in CutsceneTracks to make stuff happen
abstract public class ActionClip : SerializedMonoBehaviour, IDirectable, IKeyable
{
[SerializeField] [HideInInspector] private float _startTime;
[SerializeField] [HideInInspector] private AnimationDataCollection _animationData;
public IDirector root
{
get { return parent != null ? parent.root : null; }
}
public IDirectable parent { get; private set; }
public GameObject actor
{
get { return parent != null ? parent.actor : null; }
}
IEnumerable<IDirectable> IDirectable.children
{
get { return null; }
}
///All animated parameters are stored within this collection object
public AnimationDataCollection animationData
{
get { return _animationData; }
private set { _animationData = value; }
}
///...
public float startTime
{
get { return _startTime; }
set
{
if (_startTime != value)
{
_startTime = Mathf.Max(value, 0);
blendIn = Mathf.Clamp(blendIn, 0, length - blendOut);
blendOut = Mathf.Clamp(blendOut, 0, length - blendIn);
}
}
}
///...
public float endTime
{
get { return startTime + length; }
set
{
if (startTime + length != value)
{
length = Mathf.Max(value - startTime, 0);
blendOut = Mathf.Clamp(blendOut, 0, length - blendIn);
blendIn = Mathf.Clamp(blendIn, 0, length - blendOut);
}
}
}
///...
public bool isActive
{
get { return parent != null ? parent.isActive && isValid : false; }
}
///...
public bool isCollapsed
{
get { return parent != null ? parent.isCollapsed : false; }
}
//...
public bool isLocked
{
get { return parent != null ? parent.isLocked : false; }
}
///The length of the clip.
///Override for scalable clips.
virtual public float length
{
get { return 0; }
set { }
}
///The blend in value of the clip. A value of zero means instant.
///Override for blendable in clips.
virtual public float blendIn
{
get { return 0; }
set { }
}
///The blend out value of the clip. A value of zero means instant.
///Override for blendable out clips.
virtual public float blendOut
{
get { return 0; }
set { }
}
///Should the clip be able to cross-blend between other clips of the same type?
virtual public bool canCrossBlend
{
get { return false; }
}
///A short summary.
///Overide this to show something specific in the action clip in the editor.
virtual public string info
{
get
{
var nameAtt = this.GetType().RTGetAttribute<NameAttribute>(true);
if (nameAtt != null)
{
return nameAtt.name;
}
return this.GetType().Name.SplitCamelCase();
}
}
///Is everything ok for the clip to work?
virtual public bool isValid
{
get { return actor != null; }
}
virtual public TransformSpace defaultTransformSpace
{
get { return TransformSpace.CutsceneSpace; }
}
//An array of property/field paths that will be possible to animate.
//By default all properties/fields in the actionclip class with an [AnimatableParameter] attribute will be used.
[System.NonSerialized] private string[] _cachedAnimParamPaths;
private string[] animatedParameterPaths
{
get
{
return _cachedAnimParamPaths != null
? _cachedAnimParamPaths
: _cachedAnimParamPaths = AnimationDataUtility.GetAnimatableMemberPaths(this);
}
}
//If the params target is not this, registration of parameters should be handled manually
private bool handleParametersRegistrationManually
{
get { return !ReferenceEquals(animatedParametersTarget, this); }
}
///The target instance of the animated properties/fields.
///By default the instance of THIS action clip is used.
///Do NOT override if you don't know why! :)
virtual public object animatedParametersTarget
{
get { return this; }
}
///The interpolation to use when blending parameters. Only relevant when useWeightInParameters is true.
virtual public EaseType animatedParametersInterpolation
{
get { return EaseType.Linear; }
}
///Whether or not clip weight will be used in parameters automatically.
virtual public bool useWeightInParameters
{
get { return false; }
}
///Does the clip has any parameters?
public bool hasParameters
{
get { return animationData != null && animationData.isValid; }
}
///Does the clip has any active parameters?
public bool hasActiveParameters
{
get
{
if (!hasParameters || !isValid)
{
return false;
}
for (var i = 0; i < animationData.animatedParameters.Count; i++)
{
if (animationData.animatedParameters[i].enabled)
{
return true;
}
}
return false;
}
}
bool IDirectable.Initialize()
{
return OnInitialize();
}
void IDirectable.Enter()
{
SetAnimParamsSnapshot();
OnEnter();
}
void IDirectable.Update(float time, float previousTime)
{
UpdateAnimParams(time, previousTime);
OnUpdate(time, previousTime);
}
void IDirectable.Exit()
{
OnExit();
}
void IDirectable.ReverseEnter()
{
OnReverseEnter();
}
void IDirectable.Reverse()
{
RestoreAnimParamsSnapshot();
OnReverse();
}
void IDirectable.RootEnabled()
{
OnRootEnabled();
}
void IDirectable.RootDisabled()
{
OnRootDisabled();
}
void IDirectable.RootUpdated(float time, float previousTime)
{
OnRootUpdated(time, previousTime);
}
void IDirectable.RootDestroyed()
{
OnRootDestroyed();
}
#if UNITY_EDITOR
void IDirectable.DrawGizmos(bool selected)
{
if (selected && actor != null && isValid)
{
OnDrawGizmosSelected();
}
}
private Dictionary<MemberInfo, Attribute[]> paramsAttributes = new Dictionary<MemberInfo, Attribute[]>();
void IDirectable.SceneGUI(bool selected)
{
if (!selected || actor == null || !isValid)
{
return;
}
if (hasParameters)
{
for (var i = 0; i < animationData.animatedParameters.Count; i++)
{
var animParam = animationData.animatedParameters[i];
if (!animParam.isValid || animParam.animatedType != typeof(Vector3))
{
continue;
}
var m = animParam.GetMemberInfo();
Attribute[] attributes = null;
if (!paramsAttributes.TryGetValue(m, out attributes))
{
attributes = (Attribute[]) m.GetCustomAttributes(false);
paramsAttributes[m] = attributes;
}
ITransformRefParameter link = null;
var animAtt =
attributes.FirstOrDefault(a => a is AnimatableParameterAttribute) as
AnimatableParameterAttribute;
if (animAtt != null)
{
//only in case parameter has been added manualy. Probably never.
if (!string.IsNullOrEmpty(animAtt.link))
{
try
{
link = (GetType().GetField(animAtt.link).GetValue(this) as ITransformRefParameter);
}
catch (Exception exc)
{
Debug.LogException(exc);
}
}
}
if (link == null || link.useAnimation)
{
var space = link != null ? link.space : defaultTransformSpace;
var posHandleAtt =
attributes.FirstOrDefault(a => a is PositionHandleAttribute) as PositionHandleAttribute;
if (posHandleAtt != null)
{
DoParameterPositionHandle(animParam, space);
}
var rotHandleAtt =
attributes.FirstOrDefault(a => a is RotationHandleAttribute) as RotationHandleAttribute;
if (rotHandleAtt != null)
{
var posProp = this.GetType().RTGetFieldOrProp(rotHandleAtt.positionPropertyName);
var posVal = posProp != null
? (Vector3) posProp.RTGetFieldOrPropValue(this)
: default(Vector3);
DoParameterRotationHandle(animParam, space, posVal);
}
var trajAtt =
attributes.FirstOrDefault(a => a is ShowTrajectoryAttribute) as ShowTrajectoryAttribute;
if (trajAtt != null && animParam.enabled)
{
CurveEditor3D.Draw3DCurve(animParam, this, GetSpaceTransform(space), length / 2, length);
}
}
}
}
OnSceneGUI();
}
protected bool DoParameterPositionHandle(AnimatedParameter animParam, TransformSpace space)
{
return SceneGUIUtility.DoParameterPositionHandle(this, animParam, space);
}
protected bool DoParameterRotationHandle(AnimatedParameter animParam, TransformSpace space, Vector3 position)
{
return SceneGUIUtility.DoParameterRotationHandle(this, animParam, space, position);
}
protected bool DoVectorPositionHandle(TransformSpace space, ref Vector3 position)
{
return SceneGUIUtility.DoVectorPositionHandle(this, space, ref position);
}
protected bool DoVectorRotationHandle(TransformSpace space, Vector3 position, ref Vector3 euler)
{
return SceneGUIUtility.DoVectorRotationHandle(this, space, position, ref euler);
}
#endif
///After creation
public void PostCreate(IDirectable parent)
{
this.parent = parent;
CreateAnimationDataCollection();
OnCreate();
}
//Validate the clip
public void Validate()
{
OnAfterValidate();
}
public void Validate(IDirector root, IDirectable parent)
{
this.parent = parent;
hideFlags = HideFlags.HideInHierarchy;
ValidateAnimParams();
OnAfterValidate();
}
///HOOKS
///----------------------------------------------------------------------------------------------
virtual protected bool OnInitialize()
{
return true;
}
virtual protected void OnEnter()
{
}
virtual protected void OnUpdate(float time, float previousTime)
{
OnUpdate(time);
}
virtual protected void OnUpdate(float time)
{
}
virtual protected void OnExit()
{
}
virtual protected void OnReverse()
{
}
virtual protected void OnReverseEnter()
{
}
virtual protected void OnDrawGizmosSelected()
{
}
virtual protected void OnSceneGUI()
{
}
virtual protected void OnCreate()
{
}
virtual protected void OnAfterValidate()
{
}
virtual protected void OnRootEnabled()
{
}
virtual protected void OnRootDisabled()
{
}
virtual protected void OnRootUpdated(float time, float previousTime)
{
}
virtual protected void OnRootDestroyed()
{
}
///----------------------------------------------------------------------------------------------
///SHORTCUTS
///----------------------------------------------------------------------------------------------
///Is the root time within clip time range? A helper method.
public bool RootTimeWithinRange()
{
return IDirectableExtensions.IsRootTimeWithinClip(this);
}
///Transforms a point in specified space
public Vector3 TransformPosition(Vector3 point, TransformSpace space)
{
return IDirectableExtensions.TransformPosition(this, point, space);
}
///Inverse Transforms a point in specified space
public Vector3 InverseTransformPosition(Vector3 point, TransformSpace space)
{
return IDirectableExtensions.InverseTransformPosition(this, point, space);
}
///Transform an euler rotation in specified space and into a quaternion
public Quaternion TransformRotation(Vector3 euler, TransformSpace space)
{
return IDirectableExtensions.TransformRotation(this, euler, space);
}
///Trasnform a quaternion rotation in specified space and into an euler rotation
public Vector3 InverseTransformRotation(Quaternion rot, TransformSpace space)
{
return IDirectableExtensions.InverseTransformRotation(this, rot, space);
}
///Returns the final actor position in specified Space (InverseTransform Space)
public Vector3 ActorPositionInSpace(TransformSpace space)
{
return IDirectableExtensions.ActorPositionInSpace(this, space);
}
///Returns the transform object used for specified Space transformations. Null if World Space.
public Transform GetSpaceTransform(TransformSpace space, GameObject actorOverride = null)
{
return IDirectableExtensions.GetSpaceTransform(this, space, actorOverride);
}
///Returns the previous clip in parent track
public ActionClip GetPreviousClip()
{
return this.GetPreviousSibling<ActionClip>();
}
///Returns the next clip in parent track
public ActionClip GetNextClip()
{
return this.GetNextSibling<ActionClip>();
}
///The current clip weight based on blend properties and based on root current time.
public float GetClipWeight()
{
return GetClipWeight(root.currentTime - startTime);
}
///The weight of the clip at specified local time based on its blend properties.
public float GetClipWeight(float time)
{
return GetClipWeight(time, this.blendIn, this.blendOut);
}
///The weight of the clip at specified local time based on provided override blend in/out properties
public float GetClipWeight(float time, float blendInOut)
{
return GetClipWeight(time, blendInOut, blendInOut);
}
public float GetClipWeight(float time, float blendIn, float blendOut)
{
return this.GetWeight(time, blendIn, blendOut);
}
///----------------------------------------------------------------------------------------------
public void TryMatchSubClipLength()
{
if (this is ISubClipContainable)
{
length = (this as ISubClipContainable).subClipLength / (this as ISubClipContainable).subClipSpeed;
}
}
///Try set the clip length to match previous subclip loop if this contains a subclip at all.
public void TryMatchPreviousSubClipLoop()
{
if (this is ISubClipContainable)
{
length = (this as ISubClipContainable).GetPreviousLoopLocalTime();
}
}
///Try set the clip length to match next subclip loop if this contains a subclip at all.
public void TryMatchNexSubClipLoop()
{
if (this is ISubClipContainable)
{
var targetLength = (this as ISubClipContainable).GetNextLoopLocalTime();
var nextClip = GetNextClip();
if (nextClip == null || startTime + targetLength <= nextClip.startTime)
{
length = targetLength;
}
}
}
//...
string GetParameterName<T, TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> func)
{
return ReflectionTools.GetMemberPath(func);
}
///Get the AnimatedParameter of name. The name is usually the same as the field/property name that [AnimatableParameter] is used on.
public AnimatedParameter GetParameter<T, TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> func)
{
return GetParameter(GetParameterName(func));
}
///Get the AnimatedParameter of name. The name is usually the same as the field/property name that [AnimatableParameter] is used on.
public AnimatedParameter GetParameter(string paramName)
{
return animationData != null ? animationData.GetParameterOfName(paramName) : null;
}
///Enable/Disable an AnimatedParameter of name
public void SetParameterEnabled<T, TResult>(System.Linq.Expressions.Expression<Func<T, TResult>> func,
bool enabled)
{
SetParameterEnabled(GetParameterName(func), enabled);
}
///Enable/Disable an AnimatedParameter of name
public void SetParameterEnabled(string paramName, bool enabled)
{
var animParam = GetParameter(paramName);
if (animParam != null)
{
animParam.SetEnabled(enabled, root.currentTime - startTime);
}
}
///Re-Init/Reset all existing animated parameters
public void ResetAnimatedParameters()
{
if (animationData != null)
{
animationData.Reset();
}
}
//Creates the animation data collection out of the fields/properties marked with [AnimatableParameter] attribute
void CreateAnimationDataCollection()
{
if (handleParametersRegistrationManually)
{
return;
}
if (animatedParameterPaths != null && animatedParameterPaths.Length != 0)
{
animationData = new AnimationDataCollection(this, this.GetType(), animatedParameterPaths, null);
}
}
//Validate the animation parameters vs the animation data collection to be synced, adding or removing as required.
void ValidateAnimParams()
{
if (animationData != null)
{
animationData.Validate(this);
}
//we don't need validation in runtime
if (Application.isPlaying)
{
return;
}
if (handleParametersRegistrationManually)
{
return;
}
if (animatedParameterPaths == null || animatedParameterPaths.Length == 0)
{
animationData = null;
return;
}
//try append new
for (var i = 0; i < animatedParameterPaths.Length; i++)
{
var memberPath = animatedParameterPaths[i];
if (!string.IsNullOrEmpty(memberPath))
{
animationData.TryAddParameter(this, this.GetType(), memberPath, null);
}
}
//cleanup
foreach (var animParam in animationData.animatedParameters.ToArray())
{
if (!animParam.isValid)
{
animationData.RemoveParameter(animParam);
continue;
}
if (!animatedParameterPaths.Contains(animParam.parameterName))
{
animationData.RemoveParameter(animParam);
continue;
}
}
}
//Set an animation snapshot for all parameters
void SetAnimParamsSnapshot()
{
if (hasParameters)
{
animationData.SetVirtualTransformParent(GetSpaceTransform(defaultTransformSpace));
animationData.SetSnapshot();
}
}
//Update the animation parameters, setting their evaluated values
void UpdateAnimParams(float time, float previousTime)
{
if (hasParameters)
{
var ease = 1f;
if (useWeightInParameters)
{
var clipWeight = GetClipWeight(time);
ease = animatedParametersInterpolation == EaseType.Linear
? clipWeight
: Easing.Ease(animatedParametersInterpolation, 0, 1, clipWeight);
}
animationData.Evaluate(time, previousTime, ease);
}
}
//Restore the animation snapshot on all parameters
void RestoreAnimParamsSnapshot()
{
if (hasParameters)
{
animationData.RestoreSnapshot();
}
}
///----------------------------------------------------------------------------------------------
///---------------------------------------UNITY EDITOR-------------------------------------------
#if UNITY_EDITOR
//Unity callback.
protected void OnValidate()
{
OnEditorValidate();
}
///Show clip GUI contents
public void ShowClipGUI(Rect rect)
{
OnClipGUI(rect);
}
///This is called outside of the clip for UI on the the left/right sides of the clip.
public void ShowClipGUIExternal(Rect left, Rect right)
{
OnClipGUIExternal(left, right);
}
///Override for extra clip GUI contents.
virtual protected void OnClipGUI(Rect rect)
{
}
///Override for extra clip GUI contents outside of clip.
virtual protected void OnClipGUIExternal(Rect left, Rect right)
{
}
///Override to validate things in editor.
virtual protected void OnEditorValidate()
{
}
///----------------------------------------------------------------------------------------------
#endif
}
}