-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakeyGoInput.cs
More file actions
197 lines (180 loc) · 4 KB
/
MakeyGoInput.cs
File metadata and controls
197 lines (180 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using UnityEngine;
using System.Collections;
/// <summary>
/// A simple script to handle input from Makey Makey Go.
/// Supports detecting multiple delayed clicks (eg. detect N clicks).
///
/// Author: Hao Fu
/// Created: 12/23/2015
///
/// How to Use This?
///
/// Set up the parameters for this script.
/// Regesiter for the MakeyGoInput.onClicks event in your behavior.
///
/// Example:
///
/// void MyHandler (int clicks) {
/// // clicks is the number of clicks detected
/// }
///
/// MakeyGoInput.onClicks += MyHandler; // in Start() or Awake()
///
/// </summary>
public class MakeyGoInput : MonoBehaviour {
//
// enum
//
/// <summary>
/// Input mode based on Makey Makey Go.
/// </summary>
public enum MODE {
NONE, // detect noting
SPACE, // keyboard space bar
CLICK, // mouse left click
EITHER // either one will do the trick!
}
//
// public variables
//
/// <summary>
/// Master watchdog for clicks detetion.
/// </summary>
public bool isEnable = true;
/// <summary>
/// Debug mode flag.
/// </summary>
public bool isDebug = false;
/// <summary>
/// Mode for input.
/// </summary>
public MODE mode = MODE.EITHER;
/// <summary>
/// If only detect single click.
/// </summary>
public bool isSingleClick = false;
/// <summary>
/// Time coroutine will wait for next click input.
/// </summary>
public float inputDelayTime = 0.2f;
//
// private variables
//
/// <summary>
/// Current delay time updated by coroutine.
/// </summary>
private float _currentDelayTime = 0f;
/// <summary>
/// Count of clicks accumulated.
/// </summary>
private int _clickCount = 0;
/// <summary>
/// Handle for delay coroutine.
/// </summary>
private Coroutine _clickDelayedRoutine = null;
//
// delegates and event
//
/// <summary>
/// On click delegate with number of clicks injected.
/// </summary>
public delegate void OnClicks (int clicks);
/// <summary>
/// Occurs when clicks detection finished.
/// </summary>
public static event OnClicks onClicks;
//
// Unity functions
//
// Use this for initialization
void Start () { }
// Update is called once per frame
void Update () {
// check click event routine
if (isEnable) {
ClickRoutine ();
}
}
//
// private routines
//
/// <summary>
/// Click detection loop in Update().
/// Fire click detection coroutine.
/// </summary>
private void ClickRoutine () {
bool is_click = false;
// switch mode
switch (mode) {
case MODE.NONE:
break;
case MODE.SPACE:
if (Input.GetKeyDown(KeyCode.Space)) {
is_click = true;
}
break;
case MODE.CLICK:
if (Input.GetKeyDown(KeyCode.Mouse0)) {
is_click = true;
}
break;
case MODE.EITHER:
if (Input.GetKeyDown(KeyCode.Space) ||
Input.GetKeyDown(KeyCode.Mouse0)) {
is_click = true;
}
break;
default:
break;
}
// check click detection
if (is_click) {
if (isSingleClick) {
// only detect single click, just fire
if (onClicks != null) {
_clickCount = 1;
onClicks(_clickCount);
_clickCount = 0;
}
// debug
if (isDebug) {
Debug.Log ("Single click: 1");
}
} else {
if (_clickDelayedRoutine == null) {
// if delay routine is null, start a new one
_currentDelayTime = inputDelayTime;
_clickCount = 1;
_clickDelayedRoutine = StartCoroutine(ClickDelayedRoutine());
} else {
// reset delay time and click count
_currentDelayTime = inputDelayTime;
_clickCount += 1;
}
}
}
}
/// <summary>
/// Click detection routine.
/// Wait for delayed click input.
/// </summary>
/// <returns>The delayed routine.</returns>
private IEnumerator ClickDelayedRoutine () {
// wait for delay tiem to run out
while (_currentDelayTime > 0f) {
_currentDelayTime -= Time.deltaTime;
yield return new WaitForEndOfFrame();
}
// debug
if (isDebug) {
Debug.Log ("Multiple clicks: " + _clickCount);
}
// call event
if (onClicks != null) {
onClicks(_clickCount);
_clickCount = 0;
}
// recycle
_clickDelayedRoutine = null;
}
}