-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamControllerPlugin.cs
More file actions
313 lines (268 loc) · 10.7 KB
/
SteamControllerPlugin.cs
File metadata and controls
313 lines (268 loc) · 10.7 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using UnityEngine;
using System;
using System.Collections;
namespace com.github.lhervier.ksp
{
[KSPAddon(KSPAddon.Startup.PSystemSpawn, true)]
public class SteamControllerPlugin : MonoBehaviour
{
// <summary>
// Logger
// </summary>
private static SteamControllerLogger LOGGER = new SteamControllerLogger();
// <summary>
// Delay before applying an action set (in frames)
// </summary>
private static int DELAY = 10;
// ==================================================================================
// <summary>
// Message indicating when on Steam Controller action set changes
// </summary>
private ScreenMessage screenMessage;
// <summary>
// Previous action set (so we don't display the message when the value has not changed)
// </summary>
private KSPActionSets prevActionSet;
// <summary>
// Connection Daemon to the steam controller
// </summary>
private SteamControllerDaemon connectionDaemon;
// <summary>
// Delayed Action daemon
// </summary>
private DelayedActionDaemon delayedActionDaemon;
// ===============================================================================
// Unity initialization
// ===============================================================================
// <summary>
// Make our plugin survive between scene loading
// </summary>
protected void Awake()
{
LOGGER.Log("Awaked");
DontDestroyOnLoad(this);
}
// <summary>
// Start of the plugin
// </summary>
protected void Start()
{
// Attach to delayed action daemon
this.delayedActionDaemon = gameObject.AddComponent<DelayedActionDaemon>();
LOGGER.Log("Delayed Actions Daemon attached");
// Prepare screen message
this.screenMessage = new ScreenMessage(
string.Empty,
5f,
ScreenMessageStyle.UPPER_RIGHT
);
LOGGER.Log("Status message ready");
// Attach to connection Daemon
this.StartCoroutine(
this.WaitForControllerDaemon(
() => {
this.connectionDaemon = SteamControllerDaemon.Instance;
this.connectionDaemon.OnControllerConnected.Add(this.OnControllerConnected);
this.connectionDaemon.OnControllerDisconnected.Add(this.OnControllerDisconnected);
LOGGER.Log("Controller Events attached");
// When a controller is already connected
if( this.connectionDaemon.ControllerConnected )
{
this.OnControllerConnected();
}
}
)
);
LOGGER.Log("Started");
}
// <summary>
// Plugin destroyed
// </summary>
public void OnDestroy()
{
this.connectionDaemon.OnControllerDisconnected.Remove(OnControllerDisconnected);
this.connectionDaemon.OnControllerConnected.Remove(OnControllerConnected);
Destroy(this.delayedActionDaemon);
LOGGER.Log("Destroyed");
}
// <summary>
// Wait for the controller daemon to be available
// </summary>
// <param name="next">Action to execute once the daemon is ready</param>
private IEnumerator WaitForControllerDaemon(Action next)
{
LOGGER.Log("Waiting for Controller Daemon");
while( SteamControllerDaemon.Instance == null )
{
yield return null;
}
LOGGER.Log("Controller Daemon found");
next();
}
// ====================================================================================
// <summary>
// Trigger an action set change
// </summary>
public void TriggerActionSetChange()
{
this.delayedActionDaemon.TriggerDelayedAction(this._TriggerActionSetChange, DELAY);
}
private void _TriggerActionSetChange()
{
this._SetActionSet(this.ComputeActionSet());
}
// <summary>
// Cancel an action set change
// </summary>
private void CancelActionSetChange()
{
this.delayedActionDaemon.CancelDelayedAction(this._TriggerActionSetChange);
}
// <summary>
// Change action set NOW
// </summary>
public void SetActionSet(KSPActionSets actionSet)
{
this.CancelActionSetChange();
this._SetActionSet(actionSet);
}
private void _SetActionSet(KSPActionSets actionSet)
{
if( !this.connectionDaemon.ControllerConnected ) {
return;
}
if( actionSet == this.prevActionSet )
{
return;
}
this.connectionDaemon.setActionSet(actionSet);
this.screenMessage.message = "Controller: " + actionSet.GetLabel() + ".";
ScreenMessages.PostScreenMessage(this.screenMessage);
this.prevActionSet = actionSet;
}
// <summary>
// Compute the action set to use, depending on the KSP context
// </summary>
private KSPActionSets ComputeActionSet()
{
if( HighLogic.LoadedSceneIsFlight )
{
if( MapView.MapIsEnabled )
{
return KSPActionSets.Map;
}
if( FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.isEVA ) // FlightGlobals.ActiveVessel is null when loading a saved game
{
return KSPActionSets.EVA;
}
FlightUIMode mode = FlightUIModeController.Instance.Mode;
switch( mode )
{
case FlightUIMode.STAGING:
case FlightUIMode.MANEUVER_EDIT: // May not happen has editing maneuvre is only available in map view
case FlightUIMode.MANEUVER_INFO:
return KSPActionSets.Flight;
case FlightUIMode.DOCKING:
return KSPActionSets.Docking;
case FlightUIMode.MAPMODE: // Seems to called alone (without another event juste next) only when in tracking station
return KSPActionSets.Map;
}
}
else if( HighLogic.LoadedScene == GameScenes.TRACKSTATION )
{
return KSPActionSets.Map;
}
else if( HighLogic.LoadedSceneIsEditor)
{
return KSPActionSets.Editor;
}
else if( HighLogic.LoadedScene == GameScenes.MISSIONBUILDER )
{
return KSPActionSets.Editor;
}
return KSPActionSets.Menu;
}
// ==============================================================================
// Connection/disconnection events of controller
// ==============================================================================
// <summary>
// New controller connected
// </summary>
private void OnControllerConnected()
{
// Hooks to KSP
GameEvents.onLevelWasLoadedGUIReady.Add(OnLevelWasLoadedGUIReady);
GameEvents.onGamePause.Add(OnGamePause);
GameEvents.onGameUnpause.Add(OnGameUnpause);
GameEvents.OnFlightUIModeChanged.Add(OnFlightUIModeChanged);
GameEvents.OnMapEntered.Add(OnMapEntered);
GameEvents.onVesselChange.Add(OnVesselChange);
LOGGER.Log("KSP hooks created");
// Trigger an action set change to load the right action set
this.TriggerActionSetChange();
}
// <summary>
// Controller disconnected
// </summary>
private void OnControllerDisconnected()
{
// Canceling eventual action set change
this.CancelActionSetChange();
// Unhooks to KSP
GameEvents.onLevelWasLoadedGUIReady.Remove(OnLevelWasLoadedGUIReady);
GameEvents.onGamePause.Remove(OnGamePause);
GameEvents.onGameUnpause.Remove(OnGameUnpause);
GameEvents.OnFlightUIModeChanged.Remove(OnFlightUIModeChanged);
GameEvents.OnMapEntered.Remove(OnMapEntered);
GameEvents.onVesselChange.Remove(OnVesselChange);
LOGGER.Log("KSP hooks removed");
}
// ========================================================================================
// KSP Events
// ========================================================================================
// <summary>
// A new scene has been loaded
// </summary>
protected void OnLevelWasLoadedGUIReady(GameScenes scn)
{
this.TriggerActionSetChange();
}
// <summary>
// Will be fired when pause main menu is displayed, but also when entering
// astronaut complex, R&D, Mission Control or administration building.
// </summary>
protected void OnGamePause()
{
this.SetActionSet(KSPActionSets.Menu);
}
// <summary>
// Will be fired when game is unpaused, but also when leaving
// astronaut complex, R&D, Mission Control or administration building
// </summary>
protected void OnGameUnpause()
{
this.SetActionSet(this.ComputeActionSet());
}
// <summary>
// User toggle the flightUI buttons (staging, docking, maps or maneuvre)
// </summary>
protected void OnFlightUIModeChanged(FlightUIMode mode)
{
this.TriggerActionSetChange();
}
// <summary>
// Map mode entered (mainly in tracking station)
// </summary>
protected void OnMapEntered()
{
this.SetActionSet(KSPActionSets.Map);
}
// <summary>
// Vessel changed
// </summary>
protected void OnVesselChange(Vessel ves)
{
this.TriggerActionSetChange();
}
}
}