-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cs
More file actions
189 lines (157 loc) · 4.73 KB
/
InputManager.cs
File metadata and controls
189 lines (157 loc) · 4.73 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
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.XInput;
using UnityEngine.InputSystem.DualShock;
namespace CCC.Runtime
{
/// <summary>
/// Manages input device detection and provides centralized access to input-related functionality
/// including device type changes, haptic feedback, and sprite asset management for different input devices.
/// </summary>
public class InputManager : MonoBehaviour
{
#region Serialized Properties
[field: SerializeField, Tooltip("Path to the Xbox controller button sprite asset in Resources folder")]
public string xBoxSpriteAsset = "Textures/Xbox Buttons";
[field: SerializeField, Tooltip("Path to the DualShock controller button sprite asset in Resources folder")]
public string dualShockSpriteAsset = "Textures/DualShock Buttons";
[field: SerializeField, Tooltip("Path to the keyboard button sprite asset in Resources folder")]
public string keyboardSpriteAsset = "Textures/Keyboard Buttons";
#endregion
#region Private Fields
private DeviceType _deviceType = DeviceType.Keyboard;
private Haptics _haptics;
#endregion
#region Properties
/// <summary>
/// Gets or sets the haptic feedback controller for the current input device.
/// </summary>
/// <value>The Haptics instance for managing controller vibration and feedback.</value>
public Haptics Haptics
{
get => _haptics ??= new Haptics(this);
set => _haptics = value;
}
/// <summary>
/// Gets the current input device type and automatically updates when the device changes.
/// </summary>
/// <value>The currently detected input device type.</value>
public DeviceType CurrentDeviceType
{
get
{
var currDevice = GetDeviceType();
if (_deviceType != currDevice)
CurrentDeviceType = currDevice;
return currDevice;
}
private set
{
if (_deviceType == value) return;
DeviceTypeChanged?.Invoke(_deviceType = value);
}
}
#endregion
#region Singleton Implementation
/// <summary>
/// The singleton instance of the InputManager.
/// </summary>
/// <value>The global InputManager instance.</value>
public static InputManager Instance { get; private set; }
#endregion
#region Events
/// <summary>
/// Event triggered when the input device type changes.
/// </summary>
public event Action<DeviceType> DeviceTypeChanged;
/// <summary>
/// Event triggered when the current input device is disconnected.
/// </summary>
public event Action DeviceDisconnected;
#endregion
#region Event Functions
/// <summary>
/// Initializes the singleton instance and subscribes to input device change events.
/// </summary>
private void Awake()
{
if (Instance != null)
throw new Exception("Multiple InputManager instances");
Instance = this;
InputSystem.onDeviceChange += OnUserChange;
}
/// <summary>
/// Initializes the current device type and haptic feedback system.
/// </summary>
private void Start()
{
CurrentDeviceType = GetDeviceType();
Haptics = new Haptics(this);
}
/// <summary>
/// Cleans up event subscriptions and disposes of haptic resources.
/// </summary>
private void OnDestroy()
{
InputSystem.onDeviceChange -= OnUserChange;
Haptics?.Dispose();
}
#endregion
#region Private Methods
/// <summary>
/// Handles input device changes and updates the current device type accordingly.
/// </summary>
/// <param name="device">The input device that changed.</param>
/// <param name="change">The type of change that occurred.</param>
private void OnUserChange(InputDevice device, InputDeviceChange change)
{
switch (change)
{
case InputDeviceChange.Added:
CurrentDeviceType = GetDeviceType();
break;
case InputDeviceChange.Removed:
CurrentDeviceType = GetDeviceType();
goto case InputDeviceChange.Disconnected;
case InputDeviceChange.Disconnected:
DeviceDisconnected?.Invoke();
break;
}
}
/// <summary>
/// Determines the current input device type based on the active gamepad.
/// </summary>
/// <returns>The detected input device type.</returns>
private static DeviceType GetDeviceType()
{
return Gamepad.current switch
{
XInputController => DeviceType.XboxController,
DualShockGamepad => DeviceType.DualShock,
_ => DeviceType.Keyboard
};
}
#endregion
#region Types
/// <summary>
/// Represents different types of input devices supported by the InputManager.
/// </summary>
public enum DeviceType
{
/// <summary>
/// PlayStation DualShock controller.
/// </summary>
DualShock,
/// <summary>
/// Xbox controller.
/// </summary>
XboxController,
/// <summary>
/// Keyboard and mouse input.
/// </summary>
Keyboard,
}
#endregion
}
}