Tanoda
MarkerController.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.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Playables;
13using UnityEngine.Timeline;
14
15namespace Leap.Unity.Recording {
16
17 [RequireComponent(typeof(PlayableDirector))]
18 public class MarkerController : MonoBehaviour {
19
20 private PlayableDirector _director;
21 private TimelineAsset _timeline;
22 private Dictionary<string, TimelineClip> _markers = new Dictionary<string, TimelineClip>();
23 private Dictionary<string, Action> _actions = new Dictionary<string, Action>();
24
25 private void Awake() {
26 _director = GetComponent<PlayableDirector>();
27 }
28
29 public bool TestMarker(string markerName, MarkerTest test) {
30 TimelineClip clip;
31 if (_markers.TryGetValue(markerName, out clip)) {
32 switch (test) {
33 case MarkerTest.BeforeMarkerStarts:
34 return _director.time < clip.start;
35 case MarkerTest.AfterMarkerStarts:
36 return _director.time > clip.start;
37 case MarkerTest.BeforeMarkerEnds:
38 return _director.time < clip.end;
39 case MarkerTest.AfterMarkerEnds:
40 return _director.time > clip.end;
41 case MarkerTest.InsideMarker:
42 return _director.time > clip.start && _director.time < clip.end;
43 case MarkerTest.OutsideMarker:
44 return _director.time < clip.start || _director.time > clip.end;
45 default:
46 throw new ArgumentException();
47 }
48 } else {
49 return false;
50 }
51 }
52
53 public void LoopAtMarker(string markerName) {
54 _actions[markerName] = Action.Loop;
55 }
56
57 public void PauseAtMarker(string markerName) {
58 _actions[markerName] = Action.Pause;
59 }
60
61 public void SkipMarker(string markerName) {
62 _actions[markerName] = Action.Skip;
63 }
64
65 public void ClearMarker(string markerName) {
66 if (_actions.ContainsKey(markerName) &&
67 _actions[markerName] == Action.Pause &&
68 TestMarker(markerName, MarkerTest.InsideMarker)) {
69 _director.Resume();
70 }
71
72 _actions[markerName] = Action.None;
73 }
74
75 private void Update() {
77
78 foreach (var action in _actions) {
79 TimelineClip clip;
80 if (!_markers.TryGetValue(action.Key, out clip)) {
81 continue;
82 }
83
84 switch (action.Value) {
85 case Action.Pause:
86 if (_director.time >= clip.start) {
87 _director.Pause();
88 }
89 break;
90 case Action.Loop:
91 if (_director.time >= clip.end) {
92 _director.time = clip.start;
93 }
94 break;
95 case Action.Skip:
96 if (_director.time >= clip.start && _director.time < clip.end) {
97 _director.time = clip.end;
98 }
99 break;
100 }
101 }
102 }
103
104 public void updateMarkersIfNeeded() {
105 if (_timeline == _director.playableAsset) {
106 return;
107 }
108
109 _timeline = _director.playableAsset as TimelineAsset;
110
111 _markers.Clear();
112 var timeline = _director.playableAsset as TimelineAsset;
113 for (int i = 0; i < timeline.outputTrackCount; i++) {
114 var track = timeline.GetOutputTrack(i);
115 if (track is MarkerTrack) {
116 foreach (var clip in track.GetClips()) {
117 var marker = clip.asset as MarkerClip;
118 _markers[marker.markerName] = clip;
119 }
120 }
121 }
122 }
123
124 private enum Action {
125 None,
126 Pause,
127 Loop,
128 Skip
129 }
130
131 public enum MarkerTest {
132 BeforeMarkerStarts,
133 AfterMarkerStarts,
134 BeforeMarkerEnds,
135 AfterMarkerEnds,
136 InsideMarker,
137 OutsideMarker
138 }
139 }
140}
void LoopAtMarker(string markerName)
bool TestMarker(string markerName, MarkerTest test)
void PauseAtMarker(string markerName)