2 // ComponentStateCaptor.cs
5 // Created by Benjamin Sherratt on 04/05/2021.
6 // Copyright © 2021 SKFX Ltd. All rights reserved.
10 using System.Collections.Generic;
13 namespace JurassicTween.Internal {
14 public struct ComponentState {
15 public float[] values;
17 public ComponentState(float[] values) {
21 public struct ComponentStateDelta {
26 public Range(float start, float end) {
32 public Dictionary<string, Range> rangeByAnimationKey;
34 public ComponentStateDelta(Dictionary<string, Range> rangeByAnimationKey) {
35 this.rangeByAnimationKey = rangeByAnimationKey;
39 public interface IComponentStateGetter {
40 ComponentState GetComponentState(Component component);
43 public interface IComponentStateDeltaGenerator {
44 ComponentStateDelta GenerateComponentStateDelta(ComponentState startState, ComponentState endState);
47 public interface IComponentStateCaptor : IComponentStateGetter, IComponentStateDeltaGenerator {
50 public static class ComponentStateCaptor {
51 static Dictionary<Type, IComponentStateCaptor> stateCaptorByType;
53 public static IComponentStateCaptor GetStateCaptor(this Component component) {
54 Type componentType = component.GetType();
56 if (stateCaptorByType == null) {
57 stateCaptorByType = new Dictionary<Type, IComponentStateCaptor>();
58 CreateUnityInternalCaptors();
61 IComponentStateCaptor stateCaptor;
62 if (stateCaptorByType.ContainsKey(componentType)) {
63 stateCaptor = stateCaptorByType[componentType];
65 stateCaptor = new GenericComponentStateCaptor(componentType);
66 stateCaptorByType[componentType] = stateCaptor;
72 static void CreateUnityInternalCaptors() {
73 // Add the custom captors for Unity internals...
74 stateCaptorByType[typeof(Transform)] = new TransformComponentStateCaptor();