comparison Orchestland/Assets/LeapMotion+OVR/SystemWipe/SystemWipeRecognizerListener.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 System;
3 using Leap.Util;
4
5 using System.Runtime.InteropServices;
6
7 public class SystemWipeArgs : EventArgs {
8 private SystemWipeInfo m_wipeInfo;
9
10 public SystemWipeInfo WipeInfo { get { return m_wipeInfo; } }
11
12 public SystemWipeArgs(SystemWipeInfo wipeInfo) {
13 m_wipeInfo = wipeInfo;
14 }
15 }
16
17 public class SystemWipeRecognizerListener : MonoBehaviour {
18
19 public event EventHandler<SystemWipeArgs> SystemWipeUpdate;
20
21 public static SystemWipeRecognizerListener Instance;
22
23 private SystemWipeInfo m_latestWipeInfo;
24
25 private bool m_wipeInfoDirty = false;
26
27 private object wipeInfoLock = new object();
28
29 /// <summary>
30 /// Singleton pattern
31 /// </summary>
32 void Awake() {
33 if ( Instance == null ) {
34 Instance = this;
35 }
36 else {
37 throw new Exception("Attempting to create multiple SystemWipeRecognizerListeners. Only the first recognizer will be listed.");
38 }
39 }
40
41 void Update() {
42 // This code is not used in synchronous querying
43 //
44 lock(wipeInfoLock) {
45 if( m_wipeInfoDirty) {
46 EventHandler<SystemWipeArgs> handler = SystemWipeUpdate;
47
48 if ( handler != null ) {
49 handler(this, new SystemWipeArgs(m_latestWipeInfo));
50 }
51 m_wipeInfoDirty = false;
52 }
53 }
54
55 // Synchronous access:
56 //
57
58 // Try to get latest swipe info
59 Leap.Util.SystemWipeInfo info = Leap.Util.SystemWipeRecognizerNative.GetNextSystemWipeInfo();
60
61 // If one exists...
62 if (info.Status != Leap.Util.Status.InfoQueueEmpty)
63 {
64 // then save the lastest one ast m_latestWipeInfo
65 while (info.Status != Leap.Util.Status.InfoQueueEmpty)
66 {
67 m_latestWipeInfo = info;
68 //Debug.Log("Swipe " + info.Status + " " + info.Direction + " " + info.Progress);
69 info = Leap.Util.SystemWipeRecognizerNative.GetNextSystemWipeInfo();
70 }
71
72 // Execute handler for one lastest info.
73 EventHandler<SystemWipeArgs> handler = SystemWipeUpdate;
74 if (handler != null) { handler(this, new SystemWipeArgs(m_latestWipeInfo)); }
75 }
76 }
77
78 void SystemWipeInfoCallback(Leap.Util.SystemWipeInfo info)
79 {
80 //Debug.Log("Swipe " + info.Status + " " + info.Direction + " " + info.Progress);
81 lock(wipeInfoLock) {
82 m_wipeInfoDirty = true;
83 m_latestWipeInfo = info;
84 }
85 }
86
87 // Called before the body's first Update() and, if you Disable the body it's called again before the first following Update().
88 void OnEnable()
89 {
90 // Callback delegate doesn't work as it is. We use synchronous querying in Update() instead.
91
92 //systemWipeInfoDelegate = new Leap.Util.SystemWipeRecognizerNative.CallbackSystemWipeInfoDelegate(SystemWipeInfoCallback);
93 //Leap.Util.SystemWipeRecognizerNative.SetSystemWipeRecognizerCallback(Marshal.GetFunctionPointerForDelegate(systemWipeInfoDelegate));
94
95 Leap.Util.SystemWipeRecognizerNative.EnableSystemWipeRecognizer();
96 }
97
98 // Called when the body is disabled. Also called upon body destruction.
99 void OnDisable()
100 {
101 Leap.Util.SystemWipeRecognizerNative.DisableSystemWipeRecognizer();
102 }
103
104 Leap.Util.SystemWipeRecognizerNative.CallbackSystemWipeInfoDelegate systemWipeInfoDelegate;
105 }