Skip to content

Commit

Permalink
fix: map mouse keycode into mousebutton of the new input system
Browse files Browse the repository at this point in the history
  • Loading branch information
MSchmoecker committed Dec 14, 2023
1 parent 63c68c4 commit a1000cb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
21 changes: 18 additions & 3 deletions JotunnLib/Managers/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Jotunn.Configs;
using Jotunn.Utils;
using UnityEngine;
using UnityEngine.InputSystem.LowLevel;

namespace Jotunn.Managers
{
Expand Down Expand Up @@ -274,13 +275,27 @@ private void RegisterCustomInputs(ZInput self)
}
else if (btn.Key != KeyCode.None)
{
self.AddButton(btn.Name, InputUtils.KeyCodeToKey(btn.Key), btn.RepeatDelay, btn.RepeatInterval);
if (InputUtils.TryKeyCodeToMouseButton(btn.Key, out MouseButton mouseButton))
{
self.AddButton(btn.Name, mouseButton, btn.RepeatDelay, btn.RepeatInterval);
}
else
{
self.AddButton(btn.Name, InputUtils.KeyCodeToKey(btn.Key), btn.RepeatDelay, btn.RepeatInterval);
}
}
else if (btn.Shortcut.MainKey != KeyCode.None)
{
self.AddButton(btn.Name, InputUtils.KeyCodeToKey(btn.Shortcut.MainKey), btn.RepeatDelay, btn.RepeatInterval);
if (InputUtils.TryKeyCodeToMouseButton(btn.Shortcut.MainKey, out MouseButton mouseButton))
{
self.AddButton(btn.Name, mouseButton, btn.RepeatDelay, btn.RepeatInterval);
}
else
{
self.AddButton(btn.Name, InputUtils.KeyCodeToKey(btn.Shortcut.MainKey), btn.RepeatDelay, btn.RepeatInterval);
}
}

if (btn.GamepadButton != GamepadButton.None)
{
var joyBtnName = $"Joy!{btn.Name}";
Expand Down
31 changes: 31 additions & 0 deletions JotunnLib/Utils/InputUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ public static Key KeyCodeToKey(KeyCode key)
}
}

/// <summary>
/// Tries to convert a <see cref="KeyCode"/> KeyCode to a InputSystem <see cref="UnityEngine.InputSystem.LowLevel.MouseButton"/>
/// </summary>
/// <param name="key"></param>
/// <param name="mouseButton"></param>
/// <returns></returns>
public static bool TryKeyCodeToMouseButton(KeyCode key, out UnityEngine.InputSystem.LowLevel.MouseButton mouseButton)
{
switch (key)
{
case KeyCode.Mouse0:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Left;
return true;
case KeyCode.Mouse1:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Right;
return true;
case KeyCode.Mouse2:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Middle;
return true;
case KeyCode.Mouse3:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Forward;
return true;
case KeyCode.Mouse4:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Back;
return true;
default:
mouseButton = UnityEngine.InputSystem.LowLevel.MouseButton.Left;
return false;
}
}

/// <summary>
/// Translates a <see cref="GamepadButton"/> to its <see cref="KeyCode"/> value
/// </summary>
Expand Down

0 comments on commit a1000cb

Please sign in to comment.