comparison Orchestland/Assets/OVR/Scripts/Util/OVRGamepadController.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 /************************************************************************************
2
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.
4
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
6 you may not use the Oculus VR Rift SDK except in compliance with the License,
7 which is provided at the time of installation or download, or which
8 otherwise accompanies this software in either electronic or hard copy form.
9
10 You may obtain a copy of the License at
11
12 http://www.oculusvr.com/licenses/LICENSE-3.2
13
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ************************************************************************************/
21
22 using System;
23 using System.Runtime.InteropServices;
24 using UnityEngine;
25
26 /// <summary>
27 /// OVRGamepadController is an interface class to a gamepad controller.
28 /// </summary>
29 public class OVRGamepadController : MonoBehaviour
30 {
31 /// <summary> An axis on the gamepad. </summary>
32 public enum Axis
33 {
34 None = -1,
35 LeftXAxis = 0,
36 LeftYAxis,
37 RightXAxis,
38 RightYAxis,
39 LeftTrigger,
40 RightTrigger,
41 Max,
42 };
43
44 /// <summary> A button on the gamepad. </summary>
45 public enum Button
46 {
47 None = -1,
48 A = 0,
49 B,
50 X,
51 Y,
52 Up,
53 Down,
54 Left,
55 Right,
56 Start,
57 Back,
58 LStick,
59 RStick,
60 LeftShoulder,
61 RightShoulder,
62 Max
63 };
64
65 /// <summary>
66 /// The default Unity input name for each gamepad Axis.
67 /// </summary>
68 public static string[] DefaultAxisNames = new string[(int)Axis.Max]
69 {
70 "Left_X_Axis",
71 "Left_Y_Axis",
72 "Right_X_Axis",
73 "Right_Y_Axis",
74 "LeftTrigger",
75 "RightTrigger",
76 };
77
78 /// <summary>
79 /// The default Unity input name for each gamepad Button.
80 /// </summary>
81 public static string[] DefaultButtonNames = new string[(int)Button.Max]
82 {
83 "Button A",
84 "Button B",
85 "Button X",
86 "Button Y",
87 "Up",
88 "Down",
89 "Left",
90 "Right",
91 "Start",
92 "Back",
93 "LStick",
94 "RStick",
95 "LeftShoulder",
96 "RightShoulder",
97 };
98
99 public static int[] DefaultButtonIds = new int[(int)Button.Max]
100 {
101 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13
102 };
103
104 /// <summary>
105 /// The current Unity input names for all gamepad axes.
106 /// </summary>
107 public static string[] AxisNames = null;
108
109 /// <summary>
110 /// The current Unity input names for all gamepad buttons.
111 /// </summary>
112 public static string[] ButtonNames = null;
113
114 static OVRGamepadController()
115 {
116 SetAxisNames(DefaultAxisNames);
117 SetButtonNames(DefaultButtonNames);
118 }
119
120 /// <summary>
121 /// Sets the current names for all gamepad axes.
122 /// </summary>
123 public static void SetAxisNames(string[] axisNames)
124 {
125 AxisNames = axisNames;
126 }
127
128 /// <summary>
129 /// Sets the current Unity input names for all gamepad buttons.
130 /// </summary>
131 /// <param name="buttonNames">Button names.</param>
132 public static void SetButtonNames(string[] buttonNames)
133 {
134 ButtonNames = buttonNames;
135 }
136
137 /// <summary> Handles an axis read event. </summary>
138 public delegate float ReadAxisDelegate(Axis axis);
139
140 /// <summary> Handles an button read event. </summary>
141 public delegate bool ReadButtonDelegate(Button button);
142
143 /// <summary> Occurs when an axis has been read. </summary>
144 public static ReadAxisDelegate ReadAxis = DefaultReadAxis;
145
146 /// <summary> Occurs when a button has been read. </summary>
147 public static ReadButtonDelegate ReadButton = DefaultReadButton;
148
149 #if (!UNITY_ANDROID || UNITY_EDITOR)
150 private static bool GPC_Available = false;
151
152 //-------------------------
153 // Public access to plugin functions
154
155 /// <summary>
156 /// GPC_Initialize.
157 /// </summary>
158 /// <returns><c>true</c>, if c_ initialize was GPed, <c>false</c> otherwise.</returns>
159 public static bool GPC_Initialize()
160 {
161 if (!OVRManager.instance.isSupportedPlatform)
162 return false;
163 return OVR_GamepadController_Initialize();
164 }
165
166 /// <summary>
167 /// GPC_Destroy
168 /// </summary>
169 /// <returns><c>true</c>, if c_ destroy was GPed, <c>false</c> otherwise.</returns>
170 public static bool GPC_Destroy()
171 {
172 if (!OVRManager.instance.isSupportedPlatform)
173 return false;
174 return OVR_GamepadController_Destroy();
175 }
176
177 /// <summary>
178 /// GPC_Update
179 /// </summary>
180 /// <returns><c>true</c>, if c_ update was GPed, <c>false</c> otherwise.</returns>
181 public static bool GPC_Update()
182 {
183 if (!OVRManager.instance.isSupportedPlatform)
184 return false;
185 return OVR_GamepadController_Update();
186 }
187 #endif
188
189 /// <summary>
190 /// GPC_GetAxis
191 /// The default delegate for retrieving axis info.
192 /// </summary>
193 /// <returns>The current value of the axis.</returns>
194 /// <param name="axis">Axis.</param>
195 public static float DefaultReadAxis(Axis axis)
196 {
197 #if UNITY_ANDROID && !UNITY_EDITOR
198 return Input.GetAxis(AxisNames[(int)axis]);
199 #else
200 return OVR_GamepadController_GetAxis((int)axis);
201 #endif
202 }
203
204 /// <summary>
205 /// Returns the current value of the given Axis.
206 /// </summary>
207 public static float GPC_GetAxis(Axis axis)
208 {
209 if (ReadAxis == null)
210 return 0f;
211 return ReadAxis(axis);
212 }
213
214 public static void SetReadAxisDelegate(ReadAxisDelegate del)
215 {
216 ReadAxis = del;
217 }
218
219 /// <summary>
220 /// Uses XInput to check if the given Button is down.
221 /// </summary>
222 public static bool DefaultReadButton(Button button)
223 {
224 #if UNITY_ANDROID && !UNITY_EDITOR
225 return Input.GetButton(ButtonNames[(int)button]);
226 #else
227 return OVR_GamepadController_GetButton((int)button);
228 #endif
229 }
230
231 /// <summary>
232 /// Returns true if the given Button is down.
233 /// </summary>
234 public static bool GPC_GetButton(Button button)
235 {
236 if (ReadButton == null)
237 return false;
238 return ReadButton(button);
239 }
240
241 public static void SetReadButtonDelegate(ReadButtonDelegate del)
242 {
243 ReadButton = del;
244 }
245
246 /// <summary>
247 /// Returns true if the gamepad controller is available.
248 /// </summary>
249 public static bool GPC_IsAvailable()
250 {
251 #if !UNITY_ANDROID || UNITY_EDITOR
252 return GPC_Available;
253 #else
254 return true;
255 #endif
256 }
257
258 void GPC_Test()
259 {
260 // Axis test
261 Debug.Log(string.Format("LT:{0:F3} RT:{1:F3} LX:{2:F3} LY:{3:F3} RX:{4:F3} RY:{5:F3}",
262 GPC_GetAxis(Axis.LeftTrigger), GPC_GetAxis(Axis.RightTrigger),
263 GPC_GetAxis(Axis.LeftXAxis), GPC_GetAxis(Axis.LeftYAxis),
264 GPC_GetAxis(Axis.RightXAxis), GPC_GetAxis(Axis.RightYAxis)));
265
266 // Button test
267 Debug.Log(string.Format("A:{0} B:{1} X:{2} Y:{3} U:{4} D:{5} L:{6} R:{7} SRT:{8} BK:{9} LS:{10} RS:{11} L1:{12} R1:{13}",
268 GPC_GetButton(Button.A), GPC_GetButton(Button.B),
269 GPC_GetButton(Button.X), GPC_GetButton(Button.Y),
270 GPC_GetButton(Button.Up), GPC_GetButton(Button.Down),
271 GPC_GetButton(Button.Left), GPC_GetButton(Button.Right),
272 GPC_GetButton(Button.Start), GPC_GetButton(Button.Back),
273 GPC_GetButton(Button.LStick), GPC_GetButton(Button.RStick),
274 GPC_GetButton(Button.LeftShoulder), GPC_GetButton(Button.RightShoulder)));
275 }
276
277 #if !UNITY_ANDROID || UNITY_EDITOR
278 void Start()
279 {
280 GPC_Available = GPC_Initialize();
281 }
282
283 void Update()
284 {
285 GPC_Available = GPC_Update();
286 }
287
288 void OnDestroy()
289 {
290 GPC_Destroy();
291 GPC_Available = false;
292 }
293
294 public const string LibOVR = "OculusPlugin";
295
296 [DllImport(LibOVR, CallingConvention = CallingConvention.Cdecl)]
297 public static extern bool OVR_GamepadController_Initialize();
298 [DllImport(LibOVR, CallingConvention = CallingConvention.Cdecl)]
299 public static extern bool OVR_GamepadController_Destroy();
300 [DllImport(LibOVR, CallingConvention = CallingConvention.Cdecl)]
301 public static extern bool OVR_GamepadController_Update();
302 [DllImport(LibOVR, CallingConvention = CallingConvention.Cdecl)]
303 public static extern float OVR_GamepadController_GetAxis(int axis);
304 [DllImport(LibOVR, CallingConvention = CallingConvention.Cdecl)]
305 public static extern bool OVR_GamepadController_GetButton(int button);
306 #endif
307 }