Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AnimationCurve.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;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Runtime.InteropServices;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Collections;
10 using SiliconStudio.Core.Serialization;
11 using SiliconStudio.Paradox.Effects;
12 using SiliconStudio.Paradox.Graphics;
13 using SiliconStudio.Paradox.Internals;
16 
17 namespace SiliconStudio.Paradox.DataModel
18 {
19  /// <summary>
20  /// Untyped base class for animation curves.
21  /// </summary>
22  [DataContract(Inherited = true)]
23  public abstract class AnimationCurve
24  {
25  /// <summary>
26  /// Gets or sets the interpolation type.
27  /// </summary>
28  /// <value>
29  /// The interpolation type.
30  /// </value>
31  public AnimationCurveInterpolationType InterpolationType { get; set; }
32 
33  /// <summary>
34  /// Gets the type of keyframe values.
35  /// </summary>
36  /// <value>
37  /// The type of keyframe values.
38  /// </value>
39  public abstract Type ElementType { get; }
40 
41  /// <summary>
42  /// Gets the size of keyframe values.
43  /// </summary>
44  /// <value>
45  /// The size of keyframe values.
46  /// </value>
47  public abstract int ElementSize { get; }
48 
49  public abstract IReadOnlyList<CompressedTimeSpan> Keys { get; }
50 
51  /// <summary>
52  /// Writes a new value at the end of the curve (used for building curves).
53  /// It should be done in increasing order as it will simply add a new key at the end of <see cref="AnimationCurve{T}.KeyFrames"/>.
54  /// </summary>
55  /// <param name="newTime">The new time.</param>
56  /// <param name="location">The location.</param>
57  public abstract void AddValue(CompressedTimeSpan newTime, IntPtr location);
58 
59  protected AnimationCurve()
60  {
61  InterpolationType = AnimationCurveInterpolationType.Linear;
62  }
63  }
64 
65  /// <summary>
66  /// Typed class for animation curves.
67  /// </summary>
68  /// <typeparam name="T"></typeparam>
69  public class AnimationCurve<T> : AnimationCurve
70  {
71  /// <summary>
72  /// Gets or sets the key frames.
73  /// </summary>
74  /// <value>
75  /// The key frames.
76  /// </value>
77  public FastList<KeyFrameData<T>> KeyFrames { get; set; }
78 
79  /// <inheritdoc/>
80  public override Type ElementType
81  {
82  get { return typeof(T); }
83  }
84 
85  /// <inheritdoc/>
86  public override int ElementSize
87  {
88  get { return Utilities.UnsafeSizeOf<T>(); }
89  }
90 
91  /// <inheritdoc/>
92  public override IReadOnlyList<CompressedTimeSpan> Keys
93  {
94  get { return new LambdaReadOnlyCollection<KeyFrameData<T>, CompressedTimeSpan>(KeyFrames, x => x.Time); }
95  }
96 
97  public AnimationCurve()
98  {
99  KeyFrames = new FastList<KeyFrameData<T>>();
100  }
101 
102  /// <summary>
103  /// Find key index.
104  /// </summary>
105  /// <param name="time"></param>
106  /// <returns></returns>
108  {
109  // Simple binary search
110  int start = 0;
111  int end = KeyFrames.Count - 1;
112  while (start <= end)
113  {
114  int middle = start + ((end - start) >> 1);
115  var middleTime = KeyFrames[middle].Time;
116 
117  if (middleTime == time)
118  {
119  return middle;
120  }
121  if (middleTime < time)
122  {
123  start = middle + 1;
124  }
125  else
126  {
127  end = middle - 1;
128  }
129  }
130  return start;
131  }
132 
133  /// <inheritdoc/>
134  public override void AddValue(CompressedTimeSpan newTime, IntPtr location)
135  {
136  T value;
137  Utilities.UnsafeReadOut(location, out value);
138  KeyFrames.Add(new KeyFrameData<T> { Time = (CompressedTimeSpan)newTime, Value = value });
139  }
140  }
141 }
AnimationCurveInterpolationType
Describes how a curve should be interpolated.
SiliconStudio.Paradox.Input.Keys Keys
int FindKeyIndex(CompressedTimeSpan time)
Find key index.
SiliconStudio.Core.Mathematics.Quaternion Quaternion
Untyped base class for animation curves.
SiliconStudio.Core.Mathematics.Vector3 Vector3
override void AddValue(CompressedTimeSpan newTime, IntPtr location)
Writes a new value at the end of the curve (used for building curves). It should be done in increasin...