Mercurial > hg > Game > Games
comparison Orchestland/Assets/LeapMotion+OVR/SystemWipe/QuickSwitcher.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 using UnityEngine; | |
2 using UnityEngine.UI; | |
3 using System.Collections; | |
4 using System.Collections.Generic; | |
5 | |
6 public class QuickSwitcher : MonoBehaviour { | |
7 | |
8 public bool m_enabled = false; | |
9 [SerializeField] | |
10 private HandController m_handController; | |
11 [SerializeField] | |
12 private LeapCameraAlignment m_cameraAlignment; | |
13 [SerializeField] | |
14 private float m_minProgressToStartTransition; | |
15 [SerializeField] | |
16 private float m_fractionToLockTransition; | |
17 [SerializeField] | |
18 private Vector3 m_wipeOutPosition; | |
19 [SerializeField] | |
20 private List<LeapImageRetriever> m_imageRetriever; | |
21 | |
22 private Vector3 m_startPosition; | |
23 | |
24 private enum TransitionState { ON, OFF, MANUAL, TWEENING }; | |
25 private TransitionState m_currentTransitionState; | |
26 // Know what the last locked state was so we know what we're transitioning to. | |
27 private TransitionState m_lastLockedState; | |
28 | |
29 // Where are we transitioning to and from | |
30 private Vector3 m_from; | |
31 private Vector3 m_to; | |
32 | |
33 private delegate void TweenCompleteDelegate(); | |
34 | |
35 // Use this for initialization | |
36 void Start () { | |
37 m_startPosition = transform.localPosition; | |
38 m_from = m_startPosition; | |
39 m_to = m_wipeOutPosition; | |
40 m_lastLockedState = TransitionState.ON; | |
41 SystemWipeRecognizerListener.Instance.SystemWipeUpdate += onWipeUpdate; | |
42 TweenToOffPosition(); | |
43 } | |
44 | |
45 private void onWipeUpdate(object sender, SystemWipeArgs eventArgs) { | |
46 if ( !m_enabled ) { return; } | |
47 | |
48 string debugLine = "Debug"; | |
49 if ( eventArgs.WipeInfo.Status == Leap.Util.Status.SwipeAbort ) { | |
50 debugLine += " | Abort"; | |
51 // If the user aborts, tween back to original location | |
52 if ( m_lastLockedState == TransitionState.ON ) { | |
53 TweenToOnPosition(); | |
54 } | |
55 else { | |
56 TweenToOffPosition(); | |
57 } | |
58 } | |
59 | |
60 if ( m_currentTransitionState == TransitionState.MANUAL ) { | |
61 debugLine += " | Manual Control"; | |
62 float fraction = Mathf.Clamp01(eventArgs.WipeInfo.Progress); | |
63 | |
64 debugLine += ": " + fraction; | |
65 transform.localPosition = Vector3.Lerp(m_from, m_to, fraction); | |
66 | |
67 // If we're sure of the gesture, just go make the transition | |
68 if ( fraction >= m_fractionToLockTransition ) { | |
69 debugLine += " | Transition Cofirmed"; | |
70 if ( m_lastLockedState == TransitionState.OFF ) { | |
71 TweenToOnPosition(); | |
72 } | |
73 else { | |
74 TweenToOffPosition(); | |
75 } | |
76 } | |
77 } | |
78 else if ( m_currentTransitionState == TransitionState.TWEENING ) { | |
79 debugLine += " | Currently Tweening"; | |
80 //Debug.Log(debugLine); | |
81 return; | |
82 } | |
83 else { // We're either on or off | |
84 debugLine += " | Locked"; | |
85 if ( eventArgs.WipeInfo.Progress >= m_minProgressToStartTransition ) { | |
86 debugLine += " | Go To Manual"; | |
87 m_currentTransitionState = TransitionState.MANUAL; | |
88 } | |
89 } | |
90 | |
91 //Debug.Log(debugLine); | |
92 } | |
93 | |
94 private void onOnPosition() { | |
95 //Debug.Log("onOnPosition"); | |
96 m_currentTransitionState = TransitionState.ON; | |
97 m_lastLockedState = TransitionState.ON; | |
98 m_from = m_startPosition; | |
99 m_to = m_wipeOutPosition; | |
100 m_handController.gameObject.SetActive(false); | |
101 if (m_cameraAlignment != null) | |
102 m_cameraAlignment.enabled = true; | |
103 } | |
104 | |
105 private void onOffPosition() { | |
106 //Debug.Log("onOffPosition"); | |
107 m_currentTransitionState = TransitionState.OFF; | |
108 m_lastLockedState = TransitionState.OFF; | |
109 m_from = m_wipeOutPosition; | |
110 m_to = m_startPosition; | |
111 if ( m_imageRetriever != null ) { | |
112 foreach (LeapImageRetriever image in m_imageRetriever) { | |
113 image.enabled = false; | |
114 } | |
115 } | |
116 else { | |
117 Debug.LogError("No image retreiver on: " + gameObject.name); | |
118 } | |
119 m_handController.gameObject.SetActive(true); | |
120 if (m_cameraAlignment != null) | |
121 m_cameraAlignment.enabled = false; | |
122 } | |
123 | |
124 public void TweenToOnPosition() { | |
125 //Debug.Log("tweenToOnPosition"); | |
126 if ( m_imageRetriever != null ) { | |
127 foreach (LeapImageRetriever image in m_imageRetriever) { | |
128 image.enabled = true; | |
129 } | |
130 } | |
131 StopAllCoroutines(); | |
132 StartCoroutine(doPositionTween(0.0f, 0.1f, onOnPosition)); | |
133 } | |
134 | |
135 public void TweenToOffPosition() { | |
136 // Debug.Log("tweenToOffPosition"); | |
137 StopAllCoroutines(); | |
138 StartCoroutine(doPositionTween(1.0f, 0.1f, onOffPosition)); | |
139 } | |
140 | |
141 public void TweenToPosition(float fraction, float time = 0.4f) { | |
142 m_currentTransitionState = TransitionState.TWEENING; | |
143 StopAllCoroutines(); | |
144 StartCoroutine(doPositionTween(fraction, time)); | |
145 } | |
146 | |
147 private IEnumerator doPositionTween(float goalPercent, float transitionTime, TweenCompleteDelegate onComplete = null) { | |
148 // Debug.Log("doPositionTween: " + goalPercent); | |
149 float startTime = Time.time; | |
150 | |
151 Vector3 from = transform.localPosition; | |
152 Vector3 to = Vector3.Lerp(m_startPosition, m_wipeOutPosition, goalPercent); | |
153 | |
154 while ( true ) { | |
155 float fraction = Mathf.Clamp01((Time.time - startTime)/transitionTime); | |
156 // Debug.Log("Tween step: " + fraction); | |
157 | |
158 transform.localPosition = Vector3.Lerp(from, to, fraction); | |
159 if (m_cameraAlignment != null) | |
160 m_cameraAlignment.tween = fraction; | |
161 | |
162 // Kick out of the loop if we're done | |
163 if ( fraction == 1 ) { | |
164 break; | |
165 } else { // otherwise continue | |
166 yield return 1; | |
167 } | |
168 } | |
169 | |
170 if ( onComplete != null ) { | |
171 onComplete(); | |
172 } | |
173 } | |
174 } | |
175 |