Tanoda
MethodRecording.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.IO;
11using System.Collections.Generic;
12using UnityEngine;
13#if UNITY_EDITOR
14using UnityEditor;
15#endif
16
17namespace Leap.Unity.Recording {
18
19 public abstract class MethodRecording : MonoBehaviour {
20 public Mode mode { get; private set; }
21
22 protected virtual void Awake() {
24 if (gameObject != null) {
26 }
27 };
28 }
29
30 public abstract float GetDuration();
31
32 public virtual void EnterRecordingMode() {
33 mode = Mode.Recording;
34 }
35
36 public virtual void ExitRecordingMode(string savePath) {
37 mode = Mode.None;
38 }
39
40 public virtual void EnterPlaybackMode() {
41 mode = Mode.Playback;
42 }
43
44 public abstract void SweepTime(float from, float to);
45
46 public enum Mode {
47 None = 0,
48 Recording = 1,
49 Playback = 2
50 }
51 }
52
53 public abstract class BasicMethodData<T> : ScriptableObject {
54 public List<float> times;
55 public List<T> args;
56 }
57
58 public abstract class BasicMethodRecording<T, K> : MethodRecording where T : BasicMethodData<K> {
59
60 public T data;
61
62 public override void EnterRecordingMode() {
63 base.EnterRecordingMode();
64 data = ScriptableObject.CreateInstance<T>();
65 }
66
67 public override void ExitRecordingMode(string savePath) {
68 base.ExitRecordingMode(savePath);
69#if UNITY_EDITOR
70 AssetDatabase.CreateAsset(data, savePath);
71#endif
72 }
73
74 public override sealed float GetDuration() {
75 if (data.times.Count == 0) {
76 return 0;
77 } else {
78 return data.times[data.times.Count - 1];
79 }
80 }
81
82 public override sealed void SweepTime(float from, float to) {
83 int startIndex = data.times.BinarySearch(from);
84 int endIndex = data.times.BinarySearch(to);
85
86 if (startIndex < 0) {
87 startIndex = ~startIndex;
88 }
89
90 if (endIndex < 0) {
91 endIndex = ~endIndex;
92 }
93
94 for (int i = startIndex; i < endIndex; i++) {
95 InvokeArgs(data.args[i]);
96 }
97 }
98
99 protected void SaveArgs(K state) {
100 if (data.times == null) data.times = new List<float>();
101 if (data.args == null) data.args = new List<K>();
102
104 data.args.Add(state);
105 }
106
107 protected abstract void InvokeArgs(K state);
108 }
109}
override void ExitRecordingMode(string savePath)
override sealed void SweepTime(float from, float to)
abstract void SweepTime(float from, float to)
virtual void ExitRecordingMode(string savePath)