-
Notifications
You must be signed in to change notification settings - Fork 33
/
IController.cs
178 lines (142 loc) · 5.19 KB
/
IController.cs
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
using System;
using System.Collections.Generic;
namespace KSPAdvancedFlyByWire
{
public enum InputWrapper
{
XInput = 0,
SDL = 1,
KeyboardMouse = 2,
DirectInput = 3
}
public abstract class IController
{
public delegate void ButtonPressedCallback(IController controller, int button, FlightCtrlState state);
public delegate void ButtonReleasedCallback(IController controller, int button, FlightCtrlState state);
public static List<KeyValuePair<InputWrapper, KeyValuePair<int, string>>> EnumerateAllControllers()
{
List<KeyValuePair<InputWrapper, KeyValuePair<int, string>>> controllers = new List<KeyValuePair<InputWrapper, KeyValuePair<int, string>>>();
if (Utility.CheckXInputSupport())
{
#if !LINUX
foreach (var controllerName in XInputController.EnumerateControllers())
{
controllers.Add(new KeyValuePair<InputWrapper, KeyValuePair<int, string>>(InputWrapper.XInput, controllerName));
}
#endif
}
/*
if (Utility.CheckSharpDXSupport())
{
#if !LINUX
foreach (var controllerName in DirectXController.EnumerateControllers())
{
controllers.Add(new KeyValuePair<InputWrapper, KeyValuePair<int, string>>(InputWrapper.DirectInput, controllerName));
}
#endif
}
*/
if (Utility.CheckSDLSupport())
{
foreach (var controllerName in SDLController.EnumerateControllers())
{
controllers.Add(new KeyValuePair<InputWrapper, KeyValuePair<int, string>>(InputWrapper.SDL, controllerName));
}
}
controllers.Add(new KeyValuePair<InputWrapper, KeyValuePair<int, string>>(InputWrapper.KeyboardMouse, new KeyValuePair<int, string>(0, "Keyboard & Mouse")));
return controllers;
}
public ButtonPressedCallback buttonPressedCallback = null;
public ButtonReleasedCallback buttonReleasedCallback = null;
public Curve analogEvaluationCurve = new Curve();
public bool manualDeadZones = false;
public bool[] buttonStates;
public AxisConfiguration[] axisStates;
public Bitset lastUpdateMask;
public void InitializeStateArrays(int buttons, int axes)
{
buttonStates = new bool[buttons];
axisStates = new AxisConfiguration[axes];
for (int i = 0; i < axes; i++)
{
axisStates[i] = new AxisConfiguration();
}
}
public abstract string GetControllerName();
public virtual void Update(FlightCtrlState state)
{
for (int i = 0; i < GetButtonsCount(); i++)
{
if (GetButtonState(i) && !buttonStates[i])
{
buttonStates[i] = true;
if (buttonPressedCallback != null)
{
buttonPressedCallback(this, i, state);
}
}
else if (!GetButtonState(i) && buttonStates[i])
{
buttonStates[i] = false;
if (buttonReleasedCallback != null)
{
buttonReleasedCallback(this, i, state);
}
}
}
lastUpdateMask = GetButtonsMask();
}
public abstract int GetButtonsCount();
public abstract string GetButtonName(int id);
public abstract int GetAxesCount();
public abstract string GetAxisName(int id);
public abstract bool GetButtonState(int button);
public float GetAxisState(int id)
{
return axisStates[id].Rescale(GetRawAxisState(id), analogEvaluationCurve, manualDeadZones);
}
public abstract float GetRawAxisState(int analogInput);
public Bitset GetButtonsMask()
{
int buttonsCount = GetButtonsCount();
Bitset mask = new Bitset(buttonsCount);
for (int i = 0; i < buttonsCount; i++)
{
if (GetButtonState(i))
{
mask.Set(i);
}
}
return mask;
}
public virtual string ConvertMaskToName(Bitset mask, bool hasAxis = false, int axisIndex = 0)
{
string result = "";
bool isFirst = true;
for (int i = 0; i < GetButtonsCount(); i++)
{
if (mask.Get(i))
{
if (!isFirst)
{
result += " + ";
}
else
{
isFirst = false;
}
result += GetButtonName(i);
}
}
if (hasAxis)
{
if (!isFirst)
{
result += " + ";
}
result += GetAxisName(axisIndex);
}
return result;
}
}
}