Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AnimationComponent.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Collections.Specialized;
6 using System.Linq;
7 using SiliconStudio.Core.Serialization.Converters;
8 using SiliconStudio.Paradox.DataModel;
9 using SiliconStudio.Paradox.EntityModel;
10 using SiliconStudio.Paradox.Games;
11 using SiliconStudio.Core;
12 using SiliconStudio.Core.Collections;
13 using SiliconStudio.Core.Serialization.Contents;
14 
15 namespace SiliconStudio.Paradox.Engine
16 {
17  /// <summary>
18  /// Add animation capabilities to an <see cref="Entity"/>. It will usually apply to <see cref="ModelComponent.ModelViewHierarchy"/>
19  /// </summary>
20  /// <remarks>
21  /// Data is stored as in http://altdevblogaday.com/2011/10/23/low-level-animation-part-2/.
22  /// </remarks>
23  [DataConverter(AutoGenerate = true, ContentReference = true)]
24  [DataContract("AnimationComponent")]
25  public sealed class AnimationComponent : EntityComponent
26  {
27  private readonly Dictionary<string, AnimationClip> animations;
28  private readonly TrackingCollection<PlayingAnimation> playingAnimations;
29  internal AnimationBlender Blender = new AnimationBlender();
30 
32 
34  {
35  animations = new Dictionary<string, AnimationClip>();
36  playingAnimations = new TrackingCollection<PlayingAnimation>();
37  playingAnimations.CollectionChanged += playingAnimations_CollectionChanged;
38  }
39 
40  void playingAnimations_CollectionChanged(object sender, TrackingCollectionChangedEventArgs e)
41  {
42  if (e.Action == NotifyCollectionChangedAction.Remove)
43  {
44  var item = (PlayingAnimation)e.Item;
45  var evaluator = item.Evaluator;
46  if (evaluator != null)
47  {
48  Blender.ReleaseEvaluator(evaluator);
49  item.Evaluator = null;
50  }
51  }
52  }
53 
54  [DataMemberConvert]
55  public Dictionary<string, AnimationClip> Animations
56  {
57  get { return animations; }
58  }
59 
60  /// <summary>
61  /// Plays right away the animation with the specified name, instantly removing all other blended animations.
62  /// </summary>
63  /// <param name="name">The animation name.</param>
64  public void Play(string name)
65  {
66  PlayingAnimations.Clear();
67  PlayingAnimations.Add(new PlayingAnimation(this, name) { CurrentTime = TimeSpan.Zero, Weight = 1.0f });
68  }
69 
70  /// <summary>
71  /// Crossfades to a new animation.
72  /// </summary>
73  /// <param name="name">The name.</param>
74  /// <param name="fadeTimeSpan">The fade time span.</param>
75  /// <exception cref="ArgumentException">name</exception>
76  public void Crossfade(string name, TimeSpan fadeTimeSpan)
77  {
78  if (!Animations.ContainsKey(name))
79  throw new ArgumentException("name");
80 
81  // Fade all animations
82  foreach (var otherPlayingAnimation in PlayingAnimations)
83  {
84  otherPlayingAnimation.WeightTarget = 0.0f;
85  otherPlayingAnimation.RemainingTime = fadeTimeSpan;
86  }
87 
88  // Blend to new animation
89  Blend(name, 1.0f, fadeTimeSpan);
90  }
91 
92  /// <summary>
93  /// Blends progressively a new animation.
94  /// </summary>
95  /// <param name="name">The name.</param>
96  /// <param name="desiredWeight">The desired weight.</param>
97  /// <param name="fadeTimeSpan">The fade time span.</param>
98  /// <exception cref="ArgumentException">name</exception>
99  public void Blend(string name, float desiredWeight, TimeSpan fadeTimeSpan)
100  {
101  if (!Animations.ContainsKey(name))
102  throw new ArgumentException("name");
103 
104  var playingAnimation = new PlayingAnimation(this, name) { CurrentTime = TimeSpan.Zero, Weight = 0.0f };
105  PlayingAnimations.Add(playingAnimation);
106 
107  if (fadeTimeSpan > TimeSpan.Zero)
108  {
109  playingAnimation.WeightTarget = desiredWeight;
110  playingAnimation.RemainingTime = fadeTimeSpan;
111  }
112  else
113  {
114  playingAnimation.Weight = desiredWeight;
115  }
116  }
117 
119  {
120  return new PlayingAnimation(this, name);
121  }
122 
123  [DataMemberIgnore]
124  public TrackingCollection<PlayingAnimation> PlayingAnimations
125  {
126  get { return playingAnimations; }
127  }
128 
129  public override PropertyKey DefaultKey
130  {
131  get { return Key; }
132  }
133  }
134 }
void Crossfade(string name, TimeSpan fadeTimeSpan)
Crossfades to a new animation.
Base class for converters to/from a data type.
Add animation capabilities to an Entity. It will usually apply to ModelComponent.ModelViewHierarchy ...
object Item
Gets the added or removed item (if dictionary, value only).
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
Performs animation blending. For now, all AnimationClip must target the same skeleton.
void Blend(string name, float desiredWeight, TimeSpan fadeTimeSpan)
Blends progressively a new animation.
Blend
Blend option. A blend option identifies the data source and an optional pre-blend operation...
Definition: Blend.cs:14
A class that represents a tag propety.
Definition: PropertyKey.cs:17
void Play(string name)
Plays right away the animation with the specified name, instantly removing all other blended animatio...