forked from Cities2Modding/FirstPersonCamera
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraInput.cs
More file actions
504 lines (430 loc) · 18 KB
/
Copy pathCameraInput.cs
File metadata and controls
504 lines (430 loc) · 18 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
using FirstPersonCamera.Helpers;
using FirstPersonCameraContinued.DataModels;
using FirstPersonCameraContinued.Enums;
using FirstPersonCameraContinued.MonoBehaviours;
using FirstPersonCameraContinued.Systems;
using Game.Input;
using Game.Rendering;
using Game.SceneFlow;
using Game.UI.InGame;
using Game.Vehicles;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;
namespace FirstPersonCameraContinued
{
/// <summary>
/// Translates input to events and amends the data model
/// </summary>
public class CameraInput
{
private List<InputAction> TemporaryActions
{
get;
set;
} = new List<InputAction>();
/// <summary>
/// Event that occurs when the system is toggled on or off
/// </summary>
public Action OnToggle
{
get;
set;
}
/// <summary>
/// Event for when an entity is focused/followed
/// </summary>
public Action OnFollow
{
get;
set;
}
/// <summary>
/// Event for when an entity is unfocused/unfollowed
/// </summary>
public Action OnUnfollow
{
get;
set;
}
/// <summary>
/// Enables the highlighting UI and raycasting for entity
/// selection.
/// </summary>
public Action<bool> OnToggleSelectionMode
{
get;
set;
}
private readonly CameraDataModel _model;
private static bool firstGameSpeedChangeEvent = true;
private static bool gameIsPausedState;
private InputBarrier shortcutsProxyMapBarrier;
internal CameraInput(CameraDataModel model)
{
_model = model;
Configure();
}
/// <summary>
/// Configure key shortcuts
/// </summary>
private void Configure()
{
/* old method
var action = new InputAction("ToggleFPSController");
action.AddCompositeBinding("ButtonWithOneModifier")
.With("Modifier", "<Keyboard>/alt")
.With("Button", "<Keyboard>/f");
action.performed += (a) => Toggle();
action.Enable();
*/
// Create the input action
var action = new InputAction("FPSController_Movement", binding: "<Gamepad>/leftStick");
action.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/w") // W key for up
.With("Down", "<Keyboard>/s") // S key for down
.With("Left", "<Keyboard>/a") // A key for left
.With("Right", "<Keyboard>/d"); // D key for right
action.performed += ctx =>
{
_model.Movement = ctx.ReadValue<Vector2>();
if (_model.Mode == CameraMode.Follow)
OnUnfollow?.Invoke();
};
action.canceled += ctx => _model.Movement = float2.zero;
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action (offset height +)
action = new InputAction("FPSController_HeightUp");
action.AddBinding("<Keyboard>/r");
action.performed += ctx =>
{
if (_model.Mode == CameraMode.Follow)
{
_model.HeightOffset += 0.25f;
}
else
{
_model.HeightOffset += 1.0f;
}
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action (offset height -)
action = new InputAction("FPSController_HeightDown");
action.AddBinding("<Keyboard>/f");
action.performed += ctx =>
{
if (!Keyboard.current.altKey.isPressed)
{
if (_model.Mode == CameraMode.Follow)
{
_model.HeightOffset -= 0.25f;
}
else
{
_model.HeightOffset -= 1.0f;
}
}
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action (offset height +)
action = new InputAction("FPSController_NextRandom");
action.AddBinding("<Keyboard>/enter");
action.performed += ctx =>
{
EntryInfo entryInfo = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraSystem>().EntryInfo;
FirstPersonCameraUISystem firstPersonCameraUISystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraUISystem>();
if (entryInfo.RandomFollow)
{
if (entryInfo.RandomMode == RandomMode.Cim)
{
firstPersonCameraUISystem.EnterFollowRandomCim(false);
}
else if (entryInfo.RandomMode == RandomMode.Vehicle)
{
firstPersonCameraUISystem.EnterFollowRandomVehicle(false);
}
else if (entryInfo.RandomMode == RandomMode.Transit)
{
firstPersonCameraUISystem.EnterFollowRandomTransit(false);
}
else if (entryInfo.RandomMode == RandomMode.Bicycle)
{
firstPersonCameraUISystem.EnterFollowRandomBicycle(false);
}
}
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action - move follow camera offset
action = new InputAction("FPSController_MovementFollow", binding: "<Gamepad>/rightStick");
action.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/upArrow") // W key for up
.With("Down", "<Keyboard>/downArrow") // S key for down
.With("Left", "<Keyboard>/leftArrow") // A key for left
.With("Right", "<Keyboard>/rightArrow"); // D key for right
action.performed += ctx =>
{
if (_model.Mode == CameraMode.Follow)
{
_model.PositionFollowOffset += new float2(ctx.ReadValue<Vector2>().x * 0.5f, ctx.ReadValue<Vector2>().y * 0.5f);
Mod.log.Info("_model.PositionFollowOffset: " + _model.PositionFollowOffset);
}
};
action.canceled += ctx => _model.Movement = float2.zero;
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action (show pip window)
action = new InputAction("FPSController_HeightDown");
action.AddBinding("<Keyboard>/p");
action.performed += ctx =>
{
TogglePiPWindow();
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
// Create the input action (toggle zoom effect)
action = new InputAction("FPSController_HeightDown");
action.AddBinding("<Keyboard>/z");
action.performed += ctx =>
{
World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraSystem>().ToggleZoom();
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
//zoom in out pip window
action = new InputAction("FPSController_ScrollWheel", binding: "<Mouse>/scroll/y");
action.performed += ctx =>
{
var _pipSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraPIPSystem>();
if (_pipSystem.IsPiPWindow())
{
if (!Keyboard.current.ctrlKey.isPressed)
{
float scrollValue = ctx.ReadValue<float>() * 0.02f;
float targetOffset = _pipSystem.adjustableCameraOffset + scrollValue;
_pipSystem.adjustableCameraOffset = targetOffset;
}
else
{
float scrollValue = ctx.ReadValue<float>() * 0.02f;
float newSize = _pipSystem.m_PipSize + scrollValue * 0.01f;
Mod.log.Info("newSize: " + newSize);
_pipSystem.m_PipSize = newSize;
_pipSystem.UpdatePiPSize();
}
}
};
action.Disable();
// We only want these actions to occur whilst the controller is active
TemporaryActions.Add(action);
action = new InputAction("FPSController_MousePosition", binding: "<Mouse>/delta");
action.performed += ctx => _model.Look = ctx.ReadValue<Vector2>();
action.canceled += ctx => _model.Look = float2.zero;
action.Disable();
TemporaryActions.Add(action);
action = new InputAction("FPSController_Sprint");
action.AddBinding("<Keyboard>/leftShift");
action.performed += ctx => _model.IsSprinting = true;
action.canceled += ctx => _model.IsSprinting = false;
action.Disable();
TemporaryActions.Add(action);
action = new InputAction("FPSController_RightClick", binding: "<Mouse>/rightButton");
action.performed += ctx => RightClick(true);
action.canceled += ctx => RightClick(false);
action.Disable();
TemporaryActions.Add(action);
// have space bar listening?
action = new InputAction("FPSController_Space");
action.AddBinding("<Keyboard>/space");
action.performed += (a) => ManualPauseResume();
action.Disable();
TemporaryActions.Add(action);
action = new InputAction("FPSController_Escape", binding: "<Keyboard>/escape");
action.performed += ctx =>
{
// if (_model.Mode == CameraMode.Follow)
// OnUnfollow?.Invoke();
Disable();
OnToggle?.Invoke();
_model.HeightOffset = 0.0f;
_model.PositionFollowOffset = new float2(0f, 0f);
firstGameSpeedChangeEvent = true;
Mod.log.Info("FPSController_Escape");
};
action.Disable();
TemporaryActions.Add(action);
}
private static void TogglePiPWindow()
{
var _pipSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraPIPSystem>();
if (_pipSystem.IsPiPWindow())
{
_pipSystem.DestroyPiPWindow();
}
else
{
_pipSystem.CreatePiPWindow();
StaticCoroutine.Start(StartPiPToast());
}
}
static IEnumerator StartPiPToast()
{
yield return new WaitForEndOfFrame();
ToastTextFPC.DestroyAll();
GameObject toastObj = new GameObject("toastPiPZoom");
ToastTextFPC toastComponent = toastObj.AddComponent<ToastTextFPC>();
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastPIPScrollZoom", out string scrollZoomText);
toastComponent.Initialize(scrollZoomText, 2f);
yield break;
}
/// <summary>
/// Enable the camera input listeners
/// </summary>
public void Enable()
{
StaticCoroutine.Start(StartToast());
if (_model.FollowEntity != Entity.Null)
{
Mod.log.Info("CameraMode.Follow");
_model.Mode = CameraMode.Follow;
}
else
{
Mod.log.Info("CameraMode.Manual?");
_model.Mode = CameraMode.Manual;
}
foreach (var action in TemporaryActions)
action.Enable();
//block main game keybindings
ProxyActionMap shortcutsProxyMap = InputManager.instance.FindActionMap("Shortcuts");
HashSet<string> allow = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"Speed 1",
"Speed 2",
"Speed 3"
};
List<ProxyAction> actionsToBlock = shortcutsProxyMap.actions.Where(kv => !allow.Contains(kv.Key)).Select(kv => kv.Value).ToList();
shortcutsProxyMapBarrier = new InputBarrier(
"Disable Shortcuts Map",
actionsToBlock,
InputManager.DeviceType.Keyboard | InputManager.DeviceType.Gamepad,
blocked: true
);
if (Mod.FirstPersonModSettings != null && Mod.FirstPersonModSettings.ShowPIPOnEnter)
{
TogglePiPWindow();
}
}
public void SetEntity(Entity entity)
{
_model.FollowEntity = entity;
}
/// <summary>
/// Disable the camera input listeners
/// </summary>
private void Disable()
{
foreach (var action in TemporaryActions)
action.Disable();
shortcutsProxyMapBarrier.blocked = false;
shortcutsProxyMapBarrier.Dispose();
}
/// <summary>
/// Toggle the camera input listeners
/// </summary>
public void Toggle()
{
if (_model.IsTransitioningIn || _model.IsTransitioningOut)
return;
if (_model.Mode != CameraMode.Disabled)
Disable();
else
Enable();
OnToggle?.Invoke();
}
/// <summary>
/// Right click event for follow mechanics
/// </summary>
/// <param name="isDown"></param>
private void RightClick(bool isDown)
{
if (!isDown && _model.Mode != CameraMode.Disabled)
OnFollow?.Invoke();
OnToggleSelectionMode?.Invoke(isDown);
}
public void InvokeOnFollow()
{
OnFollow?.Invoke();
}
private void ManualPauseResume()
{
StaticCoroutine.Start(CheckIsPaused());
}
static IEnumerator CheckIsPaused()
{
//hacks? space bar registered before otherwise?
yield return new WaitForEndOfFrame();
Mod.log.Info("ran CheckIsPaused()");
var _timeUISystem = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<TimeUISystem>();
var setSimulationPausedMethod = typeof(TimeUISystem).GetMethod("SetSimulationPaused", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (firstGameSpeedChangeEvent)
{
gameIsPausedState = _timeUISystem.IsPaused();
firstGameSpeedChangeEvent = false;
}
if (gameIsPausedState)
{
setSimulationPausedMethod.Invoke(_timeUISystem, new object[] { true });
gameIsPausedState = false;
}
else
{
setSimulationPausedMethod.Invoke(_timeUISystem, new object[] { false });
gameIsPausedState = true;
}
yield break;
}
static IEnumerator StartToast()
{
yield return new WaitForEndOfFrame();
GameObject toastTextFPC = new GameObject("toastTextFPC");
ToastTextFPC toastComponent = toastTextFPC.AddComponent<ToastTextFPC>();
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastTextEnter", out string entryText);
EntryInfo entryInfo = World.DefaultGameObjectInjectionWorld.GetExistingSystemManaged<FirstPersonCameraSystem>().EntryInfo;
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastPIPWindow", out string pipWindowText);
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastZoom", out string zoomText);
if (entryInfo.RandomFollow)
{
if (entryInfo.RandomMode == RandomMode.Cim)
{
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastTextRandomModeCimEnter", out string randomModeText);
toastComponent.Initialize(entryText + "\n" + randomModeText + "\n" + pipWindowText + "\n" + zoomText);
}
else {
GameManager.instance.localizationManager.activeDictionary.TryGetValue("FirstPersonCameraContinued.ToastTextRandomModeVehicleEnter", out string randomModeText);
toastComponent.Initialize(entryText + "\n" + randomModeText + "\n" + pipWindowText + "\n" + zoomText);
}
}
else
{
toastComponent.Initialize(entryText + "\n" + pipWindowText + "\n" + zoomText);
}
yield break;
}
}
}