-
Notifications
You must be signed in to change notification settings - Fork 33
/
Configuration.cs
295 lines (245 loc) · 9.45 KB
/
Configuration.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Contracts;
using UnityEngine;
namespace KSPAdvancedFlyByWire
{
public class ControllerConfiguration
{
public InputWrapper wrapper = InputWrapper.SDL;
public int controllerIndex = 0;
public bool isEnabled = true;
public List<ControllerPreset> presets = new List<ControllerPreset>();
public int currentPreset = 0;
public CurveType analogInputCurve = CurveType.XSquared;
public bool treatHatsAsButtons = false;
public float discreteActionStep = 1.00f;
public float incrementalActionSensitivity = 1.50f;
public float cameraSensitivity = 0.05f;
public bool manualDeadZones = false;
[XmlIgnore()]
public bool presetEditorOpen = false;
[XmlIgnore()]
public bool controllerConfigurationOpen = false;
public List<AxisConfiguration> axisConfigurations = null;
[XmlIgnore()]
public IController iface;
[XmlIgnore()]
public HashSet<Bitset> evaluatedDiscreteActionMasks = new HashSet<Bitset>();
public ControllerPreset GetCurrentPreset()
{
if (currentPreset >= presets.Count)
{
currentPreset = 0;
if (presets.Count == 0)
{
presets.Add(new ControllerPreset());
}
}
var preset = presets[currentPreset];
if(preset == null)
{
MonoBehaviour.print("Advanced Fly-By-Wire: null preset error");
}
return preset;
}
public void SetAnalogInputCurveType(CurveType type)
{
analogInputCurve = type;
if (iface != null)
{
iface.analogEvaluationCurve = CurveFactory.Instantiate(type);
}
}
public void SetManualDeadZones(bool state)
{
manualDeadZones = state;
if (iface != null)
{
iface.manualDeadZones = state;
}
}
}
public class Configuration
{
public List<ControllerConfiguration> controllers = new List<ControllerConfiguration>();
public void ActivateController(InputWrapper wrapper, int controllerIndex, IController.ButtonPressedCallback pressedCallback, IController.ButtonReleasedCallback releasedCallback)
{
foreach (var config in controllers)
{
if (config.wrapper == wrapper && config.controllerIndex == controllerIndex)
{
config.isEnabled = true;
return;
}
}
var controller = new ControllerConfiguration();
controller.wrapper = wrapper;
controller.controllerIndex = controllerIndex;
if (Utility.CheckXInputSupport() && wrapper == InputWrapper.XInput)
{
#if !LINUX
controller.iface = new XInputController(controller.controllerIndex);
#endif
}
else if (Utility.CheckSDLSupport() && wrapper == InputWrapper.SDL)
{
controller.iface = new SDLController(controller.controllerIndex);
}
/*else if (Utility.CheckSharpDXSupport() && wrapper == InputWrapper.DirectInput)
{
#if !LINUX
controller.iface = new DirectXController(controller.controllerIndex);
#endif
}*/
else if (wrapper == InputWrapper.KeyboardMouse)
{
controller.iface = new KeyboardMouseController();
}
else
{
// invalid configuration, bail out..
return;
}
controller.SetManualDeadZones(controller.manualDeadZones);
controller.iface.analogEvaluationCurve = CurveFactory.Instantiate(controller.analogInputCurve);
controller.iface.buttonPressedCallback = pressedCallback;
controller.iface.buttonReleasedCallback = releasedCallback;
controller.presets = DefaultControllerPresets.GetDefaultPresets(controller.iface);
controller.currentPreset = 0;
controllers.Add(controller);
ScreenMessages.PostScreenMessage("CONTROLLER: " + controller.iface.GetControllerName(), 1.0f, ScreenMessageStyle.UPPER_CENTER);
}
public void DeactivateController(InputWrapper wrapper, int controllerIndex)
{
for (int i = 0; i < controllers.Count; i++)
{
var config = controllers[i];
if (config.wrapper == wrapper && config.controllerIndex == controllerIndex)
{
config.isEnabled = false;
//controllers[i].iface = null;
//controllers.RemoveAt(i);
return;
}
}
}
public void OnPreSerialize()
{
foreach (ControllerConfiguration config in controllers)
{
foreach (var preset in config.presets)
{
preset.OnPreSerialize();
}
config.axisConfigurations = new List<AxisConfiguration>();
for (int i = 0; i < config.iface.GetAxesCount(); i++)
{
config.axisConfigurations.Add(config.iface.axisStates[i]);
}
config.manualDeadZones = config.iface.manualDeadZones;
}
}
public void OnPostDeserialize()
{
foreach (ControllerConfiguration config in controllers)
{
/// Catch exceptions from any one controller to prevent overwrite
/// of presets for all controllers.
try
{
if (Utility.CheckXInputSupport() && config.wrapper == InputWrapper.XInput)
{
#if !LINUX
config.iface = new XInputController(config.controllerIndex);
#endif
}
else if (Utility.CheckSDLSupport() && config.wrapper == InputWrapper.SDL)
{
config.iface = new SDLController(config.controllerIndex);
}
/* else if (Utility.CheckSharpDXSupport() && config.wrapper == InputWrapper.DirectInput)
{
#if !LINUX
config.iface = new DirectXController(config.controllerIndex);
#endif
}*/
else if (config.wrapper == InputWrapper.KeyboardMouse)
{
config.iface = new KeyboardMouseController();
}
config.iface.buttonPressedCallback = AdvancedFlyByWire.Instance.ButtonPressedCallback;
config.iface.buttonReleasedCallback = AdvancedFlyByWire.Instance.ButtonReleasedCallback;
config.evaluatedDiscreteActionMasks = new HashSet<Bitset>();
for (int i = 0; i < config.iface.GetAxesCount(); i++)
{
/// Protection from flaky SDL behaviour. SDL may report more axis
/// than was previously accounted for in the configuration file.
if (i < config.axisConfigurations.Count)
{
config.iface.axisStates[i] = config.axisConfigurations[i];
}
else
{
config.iface.axisStates[i] = new AxisConfiguration();
}
}
config.axisConfigurations = null;
foreach (var preset in config.presets)
{
preset.OnPostDeserialize();
}
config.iface.manualDeadZones = config.manualDeadZones;
}
catch { }
}
}
public ControllerConfiguration GetConfigurationByIController(IController controller)
{
foreach (ControllerConfiguration config in controllers)
{
if(config.iface == controller)
{
return config;
}
}
return null;
}
public ControllerConfiguration GetConfigurationByControllerType(InputWrapper wrapper, int controllerIndex)
{
foreach (ControllerConfiguration config in controllers)
{
if (config.wrapper == wrapper && config.controllerIndex == controllerIndex)
{
return config;
}
}
return null;
}
public static void Serialize(string filename, Configuration config)
{
var serializer = new XmlSerializer(typeof(Configuration));
using (var writer = new StreamWriter(filename))
{
config.OnPreSerialize();
serializer.Serialize(writer, config);
}
}
public static Configuration Deserialize(string filename)
{
var serializer = new XmlSerializer(typeof(Configuration));
try
{
using (var reader = new StreamReader(filename))
{
var config = (Configuration)serializer.Deserialize(reader);
config.OnPostDeserialize();
return config;
}
} catch {}
return null;
}
}
}