-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tracking of what device currently is used by user
- Loading branch information
Showing
9 changed files
with
328 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Assets/_Project/Scripts/Platformio/Loop/ControlSchemePanel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using TMPro; | ||
using UnityEngine; | ||
using Zenject; | ||
|
||
namespace Platformio.Loop | ||
{ | ||
public class ControlSchemePanel : MonoBehaviour | ||
{ | ||
[Inject] private readonly PlayerInputDeviceTracker _playerInputDeviceTracker; | ||
|
||
[SerializeField] private TextMeshProUGUI schemeText; | ||
|
||
private void Awake() | ||
{ | ||
SetNewDeviceType(_playerInputDeviceTracker.CurrentDevice); | ||
_playerInputDeviceTracker.OnPlayerInputDeviceType += SetNewDeviceType; | ||
} | ||
|
||
private void OnDestroy() | ||
{ | ||
_playerInputDeviceTracker.OnPlayerInputDeviceType -= SetNewDeviceType; | ||
} | ||
|
||
private void SetNewDeviceType(PlayerInputDeviceType newDeviceType) | ||
{ | ||
schemeText.text = GetDeviceNameBy(newDeviceType); | ||
} | ||
|
||
private string GetDeviceNameBy(PlayerInputDeviceType type) | ||
{ | ||
return type switch | ||
{ | ||
PlayerInputDeviceType.Keyboard => "Keyboard", | ||
PlayerInputDeviceType.PS => "Playstation", | ||
PlayerInputDeviceType.Xbox => "Xbox", | ||
_ => throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown PlayerInputDeviceType") | ||
}; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/_Project/Scripts/Platformio/Loop/ControlSchemePanel.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
Assets/_Project/Scripts/Platformio/Loop/PlayerInputDeviceTracker.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Linq; | ||
using UnityEngine.InputSystem; | ||
using UnityEngine.InputSystem.Users; | ||
using Zenject; | ||
|
||
namespace Platformio.Loop | ||
{ | ||
public class PlayerInputDeviceTracker : IInitializable, IDisposable | ||
{ | ||
[Inject] private readonly PlayerInput _playerInput; | ||
|
||
private PlayerInputDeviceType _currentDevice; | ||
|
||
public PlayerInputDeviceType CurrentDevice => _currentDevice; | ||
|
||
public delegate void PlayerInputDeviceTypeChanged(PlayerInputDeviceType newDeviceType); | ||
|
||
public event PlayerInputDeviceTypeChanged OnPlayerInputDeviceType; | ||
|
||
public void Initialize() | ||
{ | ||
_currentDevice = GetDeviceByUser(_playerInput.user); | ||
InputUser.onChange += OnInputUserChange; | ||
} | ||
|
||
private void OnInputUserChange(InputUser user, InputUserChange change, InputDevice device) | ||
{ | ||
if (change == InputUserChange.ControlSchemeChanged) { | ||
_currentDevice = GetDeviceByUser(user); | ||
OnPlayerInputDeviceType?.Invoke(_currentDevice); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
InputUser.onChange -= OnInputUserChange; | ||
} | ||
|
||
private PlayerInputDeviceType GetDeviceByUser(InputUser inputUser) | ||
{ | ||
var devices = inputUser.pairedDevices; | ||
|
||
if (devices.Any( | ||
item => | ||
item.name.Contains("DualSense") || | ||
item.name.Contains("DualShock") | ||
)) | ||
{ | ||
return PlayerInputDeviceType.PS; | ||
} | ||
|
||
if (devices.Any( | ||
item => item.name.Contains("Xbox")) | ||
) | ||
{ | ||
return PlayerInputDeviceType.Xbox; | ||
} | ||
|
||
// The fallback value is the keyboard | ||
return PlayerInputDeviceType.Keyboard; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/_Project/Scripts/Platformio/Loop/PlayerInputDeviceTracker.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Assets/_Project/Scripts/Platformio/Loop/PlayerInputDeviceType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Platformio.Loop | ||
{ | ||
public enum PlayerInputDeviceType | ||
{ | ||
Keyboard, | ||
PS, | ||
Xbox | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/_Project/Scripts/Platformio/Loop/PlayerInputDeviceType.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.