Skip to content

Commit

Permalink
Tracking of what device currently is used by user
Browse files Browse the repository at this point in the history
  • Loading branch information
Javernaut committed Oct 1, 2023
1 parent 1750e37 commit 139a1b4
Show file tree
Hide file tree
Showing 9 changed files with 328 additions and 2 deletions.
41 changes: 39 additions & 2 deletions Assets/_Project/GameLoop/GameLoopScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,27 @@ PrefabInstance:
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_AddedComponents:
- targetCorrespondingSourceObject: {fileID: 3990949246573373962, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
insertIndex: -1
addedObject: {fileID: 632174591}
m_SourcePrefab: {fileID: 100100000, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
--- !u!1 &539813701 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 3990949246573373962, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
m_PrefabInstance: {fileID: 539813700}
m_PrefabAsset: {fileID: 0}
--- !u!114 &539813702 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 5418023924605014827, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
m_PrefabInstance: {fileID: 539813700}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 539813701}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &630880443 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 280762697159813980, guid: 6df863c29beac4db6b2200007b05a9f7, type: 3}
Expand All @@ -302,7 +321,7 @@ MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 8322018121757044840, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
m_PrefabInstance: {fileID: 539813700}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_GameObject: {fileID: 539813701}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: cf6abccb356d41ee9e0274415f97d35d, type: 3}
Expand All @@ -318,6 +337,24 @@ Transform:
m_CorrespondingSourceObject: {fileID: 1400456780297427256, guid: 6cd3234e06f9a4e109050805c30ed0e7, type: 3}
m_PrefabInstance: {fileID: 539813700}
m_PrefabAsset: {fileID: 0}
--- !u!114 &632174591
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 539813701}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0166d8ff8d905b048b2448179e1f5d11, type: 3}
m_Name:
m_EditorClassIdentifier:
_components:
- {fileID: 539813702}
_identifier:
_useSceneContext: 0
_context: {fileID: 0}
_bindType: 0
--- !u!114 &51240333591000278
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
164 changes: 164 additions & 0 deletions Assets/_Project/GameLoop/HUD/HUD.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,56 @@ MonoBehaviour:
m_ChildScaleWidth: 0
m_ChildScaleHeight: 0
m_ReverseArrangement: 0
--- !u!1 &5795697728839912656
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4148856328877213152}
- component: {fileID: 8039696141627453622}
m_Layer: 5
m_Name: ControlSchemePanel
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &4148856328877213152
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5795697728839912656}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 516796785892380289}
m_Father: {fileID: 3051852662065597076}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0, y: 0}
--- !u!114 &8039696141627453622
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5795697728839912656}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05571a11212840e781e5f2792498c258, type: 3}
m_Name:
m_EditorClassIdentifier:
schemeText: {fileID: 7663052755690123720}
--- !u!1 &7058914858491266573
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -96,6 +146,7 @@ RectTransform:
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 830127278184693476}
- {fileID: 4148856328877213152}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
Expand Down Expand Up @@ -324,6 +375,119 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &5673750045820612867
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 4148856328877213152}
m_Modifications:
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_Pivot.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_Pivot.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchorMax.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchorMax.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchorMin.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchorMin.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_SizeDelta.x
value: 326.14966
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_SizeDelta.y
value: 98.657715
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalRotation.x
value: -0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalRotation.y
value: -0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalRotation.z
value: -0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchoredPosition.x
value: 20
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_AnchoredPosition.y
value: 20
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8420783227783594487, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
propertyPath: m_Name
value: Text
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
--- !u!224 &516796785892380289 stripped
RectTransform:
m_CorrespondingSourceObject: {fileID: 5301073672966225794, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
m_PrefabInstance: {fileID: 5673750045820612867}
m_PrefabAsset: {fileID: 0}
--- !u!114 &7663052755690123720 stripped
MonoBehaviour:
m_CorrespondingSourceObject: {fileID: 2658694408559558859, guid: cf135742448d04d75a4f313efb3719f7, type: 3}
m_PrefabInstance: {fileID: 5673750045820612867}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &7560882373606036060
PrefabInstance:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public override void InstallBindings()
Container.Bind<LevelAnnouncement>().AsTransient();
Container.BindFactory<int, LevelAnnouncement, LevelAnnouncement.Factory>()
.FromComponentInNewPrefab(levelAnnouncementPrefab);

Container.BindInterfacesAndSelfTo<PlayerInputDeviceTracker>().AsSingle();
}
}
}
41 changes: 41 additions & 0 deletions Assets/_Project/Scripts/Platformio/Loop/ControlSchemePanel.cs
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")
};
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Platformio.Loop
{
public enum PlayerInputDeviceType
{
Keyboard,
PS,
Xbox
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 139a1b4

Please sign in to comment.