1 using System;
2 using System.Collections.Generic;
3 using OpenTK.Input;
4 
5 namespace SanYingInput
6 {
7 	/// <summary>
8 	/// The class that mediate between with the class of JoystickManager
9 	/// </summary>
10 	internal static class JoystickApi
11 	{
12 		/// <summary>
13 		/// Instance of the JoystickManager class
14 		/// </summary>
15 		private static JoystickManager Joystick = null;
16 
17 		/// <summary>
18 		/// Index of joy-stick currently chosen
19 		/// </summary>
20 		internal static int currentDevice { get; private set; }
21 
22 		/// <summary>
23 		/// State of the previous button
24 		/// </summary>
25 		internal static List<ButtonState> lastButtonState { get; private set; }
26 
27 		/// <summary>
28 		/// Function to initialize
29 		/// </summary>
Init()30 		internal static void Init()
31 		{
32 			Joystick = new JoystickManager();
33 			currentDevice = -1;
34 			lastButtonState = new List<ButtonState>();
35 		}
36 
37 		/// <summary>
38 		/// Function to enumerate joy-stick
39 		/// </summary>
EnumerateJoystick()40 		internal static void EnumerateJoystick()
41 		{
42 			JoystickManager.AttachedJoysticks = new JoystickManager.Joystick[] { };
43 			Joystick.RefreshJoysticks();
44 		}
45 
46 		/// <summary>
47 		/// Function to select joy-stick
48 		/// </summary>
49 		/// <param name="index">Index</param>
SelectJoystick(int index)50 		internal static void SelectJoystick(int index)
51 		{
52 			if (index < 0 || index >= JoystickManager.AttachedJoysticks.Length)
53 			{
54 				currentDevice = -1;
55 				return;
56 			}
57 			currentDevice = index;
58 		}
59 
60 		/// <summary>
61 		/// Function to update a state of chosen joy-stick
62 		/// </summary>
Update()63 		internal static void Update()
64 		{
65 			lastButtonState = GetButtonsState();
66 
67 			if (currentDevice < 0 || currentDevice >= JoystickManager.AttachedJoysticks.Length || !OpenTK.Input.Joystick.GetCapabilities(currentDevice).IsConnected)
68 			{
69 				currentDevice = -1;
70 				return;
71 			}
72 
73 			JoystickManager.AttachedJoysticks[currentDevice].Poll();
74 		}
75 
76 		/// <summary>
77 		/// Function to acquire a GUID of chosen joy-stick
78 		/// </summary>
79 		/// <returns>GUID of chosen joy-stick</returns>
GetGuid()80 		internal static Guid GetGuid()
81 		{
82 
83 			if (currentDevice < 0 || currentDevice >= JoystickManager.AttachedJoysticks.Length)
84 			{
85 				currentDevice = -1;
86 				return new Guid();
87 			}
88 
89 			return JoystickManager.AttachedJoysticks[currentDevice].Guid;
90 		}
91 
92 		/// <summary>
93 		/// Function to acquire the state of the button of chosen joy-stick
94 		/// </summary>
95 		/// <returns>State of the button of chosen joy-stick</returns>
GetButtonsState()96 		internal static List<ButtonState> GetButtonsState()
97 		{
98 			List<ButtonState> buttonsState = new List<ButtonState>();
99 
100 			if (currentDevice < 0 || currentDevice >= JoystickManager.AttachedJoysticks.Length)
101 			{
102 				currentDevice = -1;
103 				return buttonsState;
104 			}
105 
106 			for (int i = 0; i < JoystickManager.AttachedJoysticks[currentDevice].ButtonCount(); i++)
107 			{
108 				buttonsState.Add(JoystickManager.GetButton(currentDevice, i));
109 			}
110 			return buttonsState;
111 		}
112 
113 		/// <summary>
114 		/// Function to acquire state of axial of chosen joy-stick
115 		/// </summary>
116 		/// <returns>State of axial of chosen joy-stick</returns>
GetAxises()117 		internal static List<double> GetAxises()
118 		{
119 			List<double> axises = new List<double>();
120 
121 			if (currentDevice < 0 || currentDevice >= JoystickManager.AttachedJoysticks.Length)
122 			{
123 				currentDevice = -1;
124 				return axises;
125 			}
126 
127 			for (int i = 0; i < JoystickManager.AttachedJoysticks[currentDevice].ButtonCount(); i++)
128 			{
129 				axises.Add(JoystickManager.GetAxis(currentDevice, i));
130 			}
131 			return axises;
132 		}
133 	}
134 }
135