net.dreditor.input 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,307 +1,307 @@
1
- //Author: Benjamin "Sweden" Jillson : Discord: sweden_gaming For Project Eden's Garden
2
- using DREditor.Services;
3
- using System;
4
- using System.Linq;
5
- using UnityEngine;
6
- using UnityEngine.InputSystem;
7
-
8
- namespace DREditor.Input
9
- {
10
- /// <summary>
11
- ///
12
- /// </summary>
13
- [DefaultExecutionOrder(-1)]
14
- [ClassHeader(nameof(ServiceHolder<IInputManager>) + " for type: " + nameof(IInputManager) +
15
- "\n\n")]
16
- public class PlayerInputHandler : ServiceHolder<IInputManager>, IInputManager
17
- {
18
- /// <summary>
19
- ///
20
- /// </summary>
21
- [Tooltip("")]
22
- [SerializeField] protected InputActionAsset controls = null;
23
-
24
- /// <summary>
25
- ///
26
- /// </summary>
27
- [Tooltip("")]
28
- [SerializeField] protected InputActionReference cancel = null;
29
-
30
- /// <summary>
31
- ///
32
- /// </summary>
33
- [Tooltip("")]
34
- [SerializeField] protected InputActionReference leave = null;
35
-
36
-
37
- /// <summary>
38
- ///
39
- /// </summary>
40
- [Header("")]
41
- [Divider]
42
- [Tooltip("")]
43
- [SerializeField] protected bool debugInputControls = false;
44
-
45
-
46
- /// <summary>
47
- ///
48
- /// </summary>
49
- [Header("")]
50
- [Divider]
51
- [Tooltip("")]
52
- [SerializeField] protected string keyboardScheme = "Keyboard&Mouse";
53
-
54
- /// <summary>
55
- ///
56
- /// </summary>
57
- [Tooltip("")]
58
- [SerializeField] protected string gamepadScheme = "Gamepad";
59
-
60
-
61
- /// <summary>
62
- ///
63
- /// </summary>
64
- /// <remarks></remarks>
65
- protected DRControls Actions;
66
-
67
- /// <summary>
68
- ///
69
- /// </summary>
70
- protected PlayerInput playerInput;
71
-
72
- /// <summary>
73
- ///
74
- /// </summary>
75
- protected bool active = false;
76
-
77
- /// <summary>
78
- ///
79
- /// </summary>
80
- protected string currentScheme = "";
81
-
82
- /// <summary>
83
- ///
84
- /// </summary>
85
- protected readonly string NullName = "null";
86
-
87
- /// <summary>
88
- ///
89
- /// </summary>
90
- protected readonly string MouseName = "Mouse";
91
-
92
- /// <summary>
93
- ///
94
- /// </summary>
95
- protected readonly string SwitchName = "Switch";
96
-
97
-
98
- /// <inheritdoc/>
99
- protected override void Awake()
100
- {
101
- InitializePlayerInput();
102
-
103
- if (controls == null)
104
- UnityEngine.Debug.LogError($"ALERT: You MUST assign the {nameof(controls)} field!");
105
-
106
- base.Awake();
107
- }
108
-
109
- /// <summary>
110
- ///
111
- /// </summary>
112
- protected virtual void InitializePlayerInput()
113
- {
114
- if (playerInput != null)
115
- playerInput.neverAutoSwitchControlSchemes = true;
116
- else
117
- playerInput = (PlayerInput)FindAnyObjectByType(typeof(PlayerInput));
118
- }
119
-
120
- /// <inheritdoc/>
121
- protected override void InitializeService()
122
- {
123
- Actions ??= new();
124
-
125
- Enable();
126
-
127
- ServiceInitializer.HardInitialize<IInputManager>(this, dontDestroyOnLoad);
128
-
129
- if (ServiceLocator.GetService<IInputManager>() == this as IInputManager)
130
- ServiceLocator.Register(Actions);
131
- }
132
-
133
-
134
- /// <inheritdoc/>
135
- public virtual void SetSchemeByDevice(InputDevice device)
136
- {
137
- // This had to be added because it was possible for this gameobject to be destroyed and still call this function.
138
- if (ToString() == NullName)
139
- return;
140
-
141
- if (device != null)
142
- EvaluateSchemeByDevice(device);
143
- else
144
- UnityEngine.Debug.Log($"REPORT: {ToString()}: Device was Null");
145
- }
146
-
147
- /// <summary>
148
- ///
149
- /// </summary>
150
- /// <param name="device"></param>
151
- protected virtual void EvaluateSchemeByDevice(InputDevice device)
152
- {
153
- if (debugInputControls)
154
- UnityEngine.Debug.Log($"{ToString()}: Device is: {device.name} ");
155
-
156
- var scheme = InputControlScheme.FindControlSchemeForDevice(device, Actions.controlSchemes);
157
-
158
- if (currentScheme != gamepadScheme && device.name == "Core (Plus) Wired Controller")
159
- SetDeviceScheme(gamepadScheme);
160
-
161
- if (scheme != null && currentScheme == scheme.Value.name)
162
- return;
163
-
164
- if (scheme != null)
165
- SetDeviceScheme(scheme.Value.name);
166
- else if (device.name == MouseName && currentScheme != keyboardScheme)
167
- SetDeviceScheme(keyboardScheme);
168
- else if (device.name.Contains(SwitchName) && currentScheme != gamepadScheme)
169
- SetDeviceScheme(gamepadScheme);
170
- }
171
-
172
- /// <summary>
173
- ///
174
- /// </summary>
175
- /// <param name="scheme"></param>
176
- protected virtual void SetDeviceScheme(string scheme)
177
- {
178
- if (debugInputControls)
179
- UnityEngine.Debug.Log($"{ToString()}: Setting Scheme to: " + scheme);
180
-
181
- currentScheme = scheme;
182
-
183
- try
184
- {
185
- var bindingGroup = Actions.controlSchemes.First(x => x.name == scheme).bindingGroup;
186
-
187
- Actions.bindingMask = InputBinding.MaskByGroup(bindingGroup);
188
- }
189
- catch (Exception e)
190
- {
191
- UnityEngine.Debug.LogWarning("ALERT: Could not set scheme to: " + scheme);
192
-
193
- UnityEngine.Debug.LogWarning("ANALYSIS: " + e.ToString());
194
- }
195
- }
196
-
197
- /// <summary>
198
- ///
199
- /// </summary>
200
- /// <param name="device"></param>
201
- /// <param name="deviceChange"></param>
202
- protected virtual void InputSystemOnDeviceChange(InputDevice device, InputDeviceChange deviceChange)
203
- {
204
- switch (deviceChange)
205
- {
206
- case InputDeviceChange.Added:
207
- UnityEngine.Debug.Log("A new device has been added! Device is: " + device.displayName);
208
-
209
- //TODO: Change pairing from Keyboard to Xbox and dont allow/disable any other schemes
210
- //var bindingGroup = playerInputActions.controlSchemes.First(x => x.name == "Switch").bindingGroup;
211
- //playerInputActions.bindingMask = InputBinding.MaskByGroup(bindingGroup);
212
-
213
- SetSchemeByDevice(device);
214
-
215
- break;
216
-
217
- case InputDeviceChange.Removed:
218
-
219
- UnityEngine.Debug.Log(device.displayName + "has been removed!");
220
-
221
- break;
222
-
223
- case InputDeviceChange.Disconnected:
224
-
225
- UnityEngine.Debug.Log(device.displayName + " has been disconnected!");
226
-
227
- SetDeviceScheme(keyboardScheme);
228
-
229
- break;
230
-
231
- case InputDeviceChange.Reconnected:
232
- break;
233
- case InputDeviceChange.Enabled:
234
- break;
235
- case InputDeviceChange.Disabled:
236
- break;
237
- case InputDeviceChange.UsageChanged:
238
- break;
239
- case InputDeviceChange.ConfigurationChanged:
240
- break;
241
- }
242
- }
243
-
244
-
245
- /// <inheritdoc/>
246
- public virtual string GetCurrentScheme() => currentScheme;
247
-
248
-
249
- /// <inheritdoc/>
250
- protected override void OnDestroy()
251
- {
252
- base.OnDestroy();
253
-
254
- if (ServiceLocator.HasService<IInputManager>(out var input) && input == this as IInputManager)
255
- ServiceLocator.Unregister(Actions);
256
- }
257
-
258
-
259
-
260
- /// <inheritdoc/>
261
- public void Enable()
262
- {
263
- if (BlockedUpdatingActiveTo(true))
264
- return;
265
-
266
- InputSystem.onDeviceChange += InputSystemOnDeviceChange;
267
-
268
- Actions?.Enable();
269
-
270
- controls.Enable();
271
- }
272
-
273
- /// <inheritdoc/>
274
- public void Disable()
275
- {
276
- if (BlockedUpdatingActiveTo(false))
277
- return;
278
-
279
- InputSystem.onDeviceChange -= InputSystemOnDeviceChange;
280
-
281
- Actions?.Disable();
282
-
283
- controls.Disable();
284
- }
285
-
286
-
287
- protected virtual bool BlockedUpdatingActiveTo(bool active)
288
- {
289
- if (active == this.active)
290
- return true;
291
- else
292
- this.active = active;
293
-
294
- return false;
295
- }
296
-
297
-
298
- /// <inheritdoc/>
299
- public virtual bool UsingGamepadScheme() => GetCurrentScheme() == gamepadScheme;
300
-
301
- /// <inheritdoc/>
302
- public virtual InputAction GetLeave() => leave.action;
303
-
304
- /// <inheritdoc/>
305
- public virtual InputAction GetCancel() => cancel.action;
306
- }
1
+ //Author: Benjamin "Sweden" Jillson : Discord: sweden_gaming For Project Eden's Garden
2
+ using DREditor.Services;
3
+ using System;
4
+ using System.Linq;
5
+ using UnityEngine;
6
+ using UnityEngine.InputSystem;
7
+
8
+ namespace DREditor.Input
9
+ {
10
+ /// <summary>
11
+ ///
12
+ /// </summary>
13
+ [DefaultExecutionOrder(-1)]
14
+ [ClassHeader(nameof(ServiceHolder<IInputManager>) + " for type: " + nameof(IInputManager) +
15
+ "\n\n")]
16
+ public class PlayerInputHandler : ServiceHolder<IInputManager>, IInputManager
17
+ {
18
+ /// <summary>
19
+ ///
20
+ /// </summary>
21
+ [Tooltip("")]
22
+ [SerializeField] protected InputActionAsset controls = null;
23
+
24
+ /// <summary>
25
+ ///
26
+ /// </summary>
27
+ [Tooltip("")]
28
+ [SerializeField] protected InputActionReference cancel = null;
29
+
30
+ /// <summary>
31
+ ///
32
+ /// </summary>
33
+ [Tooltip("")]
34
+ [SerializeField] protected InputActionReference leave = null;
35
+
36
+
37
+ /// <summary>
38
+ ///
39
+ /// </summary>
40
+ [Header("")]
41
+ [Divider]
42
+ [Tooltip("")]
43
+ [SerializeField] protected bool debugInputControls = false;
44
+
45
+
46
+ /// <summary>
47
+ ///
48
+ /// </summary>
49
+ [Header("")]
50
+ [Divider]
51
+ [Tooltip("")]
52
+ [SerializeField] protected string keyboardScheme = "Keyboard&Mouse";
53
+
54
+ /// <summary>
55
+ ///
56
+ /// </summary>
57
+ [Tooltip("")]
58
+ [SerializeField] protected string gamepadScheme = "Gamepad";
59
+
60
+
61
+ /// <summary>
62
+ ///
63
+ /// </summary>
64
+ /// <remarks></remarks>
65
+ protected DRControls Actions;
66
+
67
+ /// <summary>
68
+ ///
69
+ /// </summary>
70
+ protected PlayerInput playerInput;
71
+
72
+ /// <summary>
73
+ ///
74
+ /// </summary>
75
+ protected bool active = false;
76
+
77
+ /// <summary>
78
+ ///
79
+ /// </summary>
80
+ protected string currentScheme = "";
81
+
82
+ /// <summary>
83
+ ///
84
+ /// </summary>
85
+ protected readonly string NullName = "null";
86
+
87
+ /// <summary>
88
+ ///
89
+ /// </summary>
90
+ protected readonly string MouseName = "Mouse";
91
+
92
+ /// <summary>
93
+ ///
94
+ /// </summary>
95
+ protected readonly string SwitchName = "Switch";
96
+
97
+
98
+ /// <inheritdoc/>
99
+ protected override void Awake()
100
+ {
101
+ InitializePlayerInput();
102
+
103
+ if (controls == null)
104
+ UnityEngine.Debug.LogError($"ALERT: You MUST assign the {nameof(controls)} field!");
105
+
106
+ base.Awake();
107
+ }
108
+
109
+ /// <summary>
110
+ ///
111
+ /// </summary>
112
+ protected virtual void InitializePlayerInput()
113
+ {
114
+ if (playerInput != null)
115
+ playerInput.neverAutoSwitchControlSchemes = true;
116
+ else
117
+ playerInput = (PlayerInput)FindAnyObjectByType(typeof(PlayerInput));
118
+ }
119
+
120
+ /// <inheritdoc/>
121
+ protected override void InitializeService()
122
+ {
123
+ Actions ??= new();
124
+
125
+ Enable();
126
+
127
+ ServiceInitializer.HardInitialize<IInputManager>(this, dontDestroyOnLoad);
128
+
129
+ if (ServiceLocator.GetService<IInputManager>() == this as IInputManager)
130
+ ServiceLocator.Register(Actions);
131
+ }
132
+
133
+
134
+ /// <inheritdoc/>
135
+ public virtual void SetSchemeByDevice(InputDevice device)
136
+ {
137
+ // This had to be added because it was possible for this gameobject to be destroyed and still call this function.
138
+ if (ToString() == NullName)
139
+ return;
140
+
141
+ if (device != null)
142
+ EvaluateSchemeByDevice(device);
143
+ else
144
+ UnityEngine.Debug.Log($"REPORT: {ToString()}: Device was Null");
145
+ }
146
+
147
+ /// <summary>
148
+ ///
149
+ /// </summary>
150
+ /// <param name="device"></param>
151
+ protected virtual void EvaluateSchemeByDevice(InputDevice device)
152
+ {
153
+ if (debugInputControls)
154
+ UnityEngine.Debug.Log($"{ToString()}: Device is: {device.name} ");
155
+
156
+ var scheme = InputControlScheme.FindControlSchemeForDevice(device, Actions.controlSchemes);
157
+
158
+ if (currentScheme != gamepadScheme && device.name == "Core (Plus) Wired Controller")
159
+ SetDeviceScheme(gamepadScheme);
160
+
161
+ if (scheme != null && currentScheme == scheme.Value.name)
162
+ return;
163
+
164
+ if (scheme != null)
165
+ SetDeviceScheme(scheme.Value.name);
166
+ else if (device.name == MouseName && currentScheme != keyboardScheme)
167
+ SetDeviceScheme(keyboardScheme);
168
+ else if (device.name.Contains(SwitchName) && currentScheme != gamepadScheme)
169
+ SetDeviceScheme(gamepadScheme);
170
+ }
171
+
172
+ /// <summary>
173
+ ///
174
+ /// </summary>
175
+ /// <param name="scheme"></param>
176
+ protected virtual void SetDeviceScheme(string scheme)
177
+ {
178
+ if (debugInputControls)
179
+ UnityEngine.Debug.Log($"{ToString()}: Setting Scheme to: " + scheme);
180
+
181
+ currentScheme = scheme;
182
+
183
+ try
184
+ {
185
+ var bindingGroup = Actions.controlSchemes.First(x => x.name == scheme).bindingGroup;
186
+
187
+ Actions.bindingMask = InputBinding.MaskByGroup(bindingGroup);
188
+ }
189
+ catch (Exception e)
190
+ {
191
+ UnityEngine.Debug.LogWarning("ALERT: Could not set scheme to: " + scheme);
192
+
193
+ UnityEngine.Debug.LogWarning("ANALYSIS: " + e.ToString());
194
+ }
195
+ }
196
+
197
+ /// <summary>
198
+ ///
199
+ /// </summary>
200
+ /// <param name="device"></param>
201
+ /// <param name="deviceChange"></param>
202
+ protected virtual void InputSystemOnDeviceChange(InputDevice device, InputDeviceChange deviceChange)
203
+ {
204
+ switch (deviceChange)
205
+ {
206
+ case InputDeviceChange.Added:
207
+ UnityEngine.Debug.Log("A new device has been added! Device is: " + device.displayName);
208
+
209
+ //TODO: Change pairing from Keyboard to Xbox and dont allow/disable any other schemes
210
+ //var bindingGroup = playerInputActions.controlSchemes.First(x => x.name == "Switch").bindingGroup;
211
+ //playerInputActions.bindingMask = InputBinding.MaskByGroup(bindingGroup);
212
+
213
+ SetSchemeByDevice(device);
214
+
215
+ break;
216
+
217
+ case InputDeviceChange.Removed:
218
+
219
+ UnityEngine.Debug.Log(device.displayName + "has been removed!");
220
+
221
+ break;
222
+
223
+ case InputDeviceChange.Disconnected:
224
+
225
+ UnityEngine.Debug.Log(device.displayName + " has been disconnected!");
226
+
227
+ SetDeviceScheme(keyboardScheme);
228
+
229
+ break;
230
+
231
+ case InputDeviceChange.Reconnected:
232
+ break;
233
+ case InputDeviceChange.Enabled:
234
+ break;
235
+ case InputDeviceChange.Disabled:
236
+ break;
237
+ case InputDeviceChange.UsageChanged:
238
+ break;
239
+ case InputDeviceChange.ConfigurationChanged:
240
+ break;
241
+ }
242
+ }
243
+
244
+
245
+ /// <inheritdoc/>
246
+ public virtual string GetCurrentScheme() => currentScheme;
247
+
248
+
249
+ /// <inheritdoc/>
250
+ protected override void OnDestroy()
251
+ {
252
+ base.OnDestroy();
253
+
254
+ if (ServiceLocator.HasService<IInputManager>(out var input) && input == this as IInputManager)
255
+ ServiceLocator.Unregister(Actions);
256
+ }
257
+
258
+
259
+
260
+ /// <inheritdoc/>
261
+ public void Enable()
262
+ {
263
+ if (BlockedUpdatingActiveTo(true))
264
+ return;
265
+
266
+ InputSystem.onDeviceChange += InputSystemOnDeviceChange;
267
+
268
+ Actions?.Enable();
269
+
270
+ controls.Enable();
271
+ }
272
+
273
+ /// <inheritdoc/>
274
+ public void Disable()
275
+ {
276
+ if (BlockedUpdatingActiveTo(false))
277
+ return;
278
+
279
+ InputSystem.onDeviceChange -= InputSystemOnDeviceChange;
280
+
281
+ Actions?.Disable();
282
+
283
+ controls.Disable();
284
+ }
285
+
286
+
287
+ protected virtual bool BlockedUpdatingActiveTo(bool active)
288
+ {
289
+ if (active == this.active)
290
+ return true;
291
+ else
292
+ this.active = active;
293
+
294
+ return false;
295
+ }
296
+
297
+
298
+ /// <inheritdoc/>
299
+ public virtual bool UsingGamepadScheme() => GetCurrentScheme() == gamepadScheme;
300
+
301
+ /// <inheritdoc/>
302
+ public virtual InputAction GetLeave() => leave.action;
303
+
304
+ /// <inheritdoc/>
305
+ public virtual InputAction GetCancel() => cancel.action;
306
+ }
307
307
  }
@@ -1,11 +1,11 @@
1
- fileFormatVersion: 2
2
- guid: 1f056034e0fa1e8489c0bf07951fd261
3
- MonoImporter:
4
- externalObjects: {}
5
- serializedVersion: 2
6
- defaultReferences: []
7
- executionOrder: 0
8
- icon: {instanceID: 0}
9
- userData:
10
- assetBundleName:
11
- assetBundleVariant:
1
+ fileFormatVersion: 2
2
+ guid: 1f056034e0fa1e8489c0bf07951fd261
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant: