Finalised new InputManagerHelper, which translates input based on the operating input system, new or old

Updated CardStack2D to have defined keyboard input or specific gamepad input over the older axisname for new input system.
pull/413/head
Simon (darkside) Jackson 2020-10-12 23:27:57 +01:00
parent 0b0d03cdb7
commit 38740bfef0
3 changed files with 305 additions and 94 deletions

View File

@ -4,9 +4,6 @@
/// </summary> /// </summary>
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
@ -25,8 +22,14 @@ public class CardStack2D : MonoBehaviour
[SerializeField] [SerializeField]
private int usedCardXPos = 1280; private int usedCardXPos = 1280;
[SerializeField] [SerializeField]
private KeyCode leftButton = KeyCode.LeftArrow;
[SerializeField]
private KeyCode rightButton = KeyCode.RightArrow;
[SerializeField]
private Transform[] cards = null; private Transform[] cards = null;
private int cardArrayOffset; private int cardArrayOffset;
private Vector3[] cardPositions; private Vector3[] cardPositions;
private int xPowerDifference; private int xPowerDifference;
@ -76,12 +79,12 @@ public class CardStack2D : MonoBehaviour
if (canUseHorizontalAxis) if (canUseHorizontalAxis)
{ {
///Controls for the cards. ///Controls for the cards.
if (UIExtensionsInputManager.GetAxisRaw("Horizontal") < 0 && cardArrayOffset > 0) if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") < 0 || UIExtensionsInputManager.GetKey(leftButton)) && cardArrayOffset > 0)
{ {
cardArrayOffset--; cardArrayOffset--;
StartCoroutine(ButtonCooldown()); StartCoroutine(ButtonCooldown());
} }
else if (UIExtensionsInputManager.GetAxisRaw("Horizontal") > 0 && cardArrayOffset < cards.Length - 1) else if ((UIExtensionsInputManager.GetAxisRaw("Horizontal") > 0 || UIExtensionsInputManager.GetKey(rightButton)) && cardArrayOffset < cards.Length - 1)
{ {
cardArrayOffset++; cardArrayOffset++;
StartCoroutine(ButtonCooldown()); StartCoroutine(ButtonCooldown());

View File

@ -1,66 +1,272 @@
/// Credit SimonDarksideJ /// Credit SimonDarksideJ
/// Sourced from: https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/348/menu-manager-does-not-work-with-the-new /// Sourced from: https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/348/menu-manager-does-not-work-with-the-new
using System;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
public static class UIExtensionsInputManager public static class UIExtensionsInputManager
{ {
#if !ENABLE_LEGACY_INPUT_MANAGER
private static bool[] mouseButtons = new bool[3] { false, false, false };
private static Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>();
private static Dictionary<String, bool> buttons = new Dictionary<String, bool>();
#endif
public static bool GetMouseButton(int button) public static bool GetMouseButton(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButton(button); return Input.GetMouseButton(button);
#else
if (Mouse.current == null)
{
return false;
}
return Mouse.current.leftButton.isPressed;
#endif
} }
public static bool GetMouseButtonDown(int button) public static bool GetMouseButtonDown(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButtonDown(button); return Input.GetMouseButtonDown(button);
#else
if (Mouse.current == null)
{
return false;
}
if (Mouse.current.leftButton.isPressed)
{
if (!mouseButtons[button])
{
mouseButtons[button] = true;
return true;
}
}
return false;
#endif
} }
public static bool GetMouseButtonUp(int button) public static bool GetMouseButtonUp(int button)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetMouseButtonUp(button); return Input.GetMouseButtonUp(button);
#else
if (Mouse.current == null)
{
return false;
}
if (mouseButtons[button] && !Mouse.current.leftButton.isPressed)
{
mouseButtons[button] = false;
return true;
}
return false;
#endif
} }
public static bool GetButton(string input) public static bool GetButton(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButton(input); return Input.GetButton(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input);
if (!buttons.ContainsKey(input))
{
buttons.Add(input, false);
}
return buttonPressed != null ? buttonPressed.isPressed : false;
#endif
}
private static ButtonControl GetButtonControlFromString(string input)
{
if (Gamepad.current == null)
{
return null;
}
switch (input)
{
case "Submit":
return Gamepad.current.aButton;
case "Cancel":
return Gamepad.current.bButton;
default:
return null;
}
} }
public static bool GetButtonDown(string input) public static bool GetButtonDown(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButtonDown(input); return Input.GetButtonDown(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input);
if (buttonPressed.isPressed)
{
if (!buttons.ContainsKey(input))
{
buttons.Add(input, false);
}
if (!buttons[input])
{
buttons[input] = true;
return true;
}
}
else
{
buttons[input] = false;
}
return false;
#endif
} }
public static bool GetButtonUp(string input) public static bool GetButtonUp(string input)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetButtonUp(input); return Input.GetButtonUp(input);
#else
ButtonControl buttonPressed = GetButtonControlFromString(input);
if (buttons[input] && !buttonPressed.isPressed)
{
buttons[input] = false;
return true;
}
return false;
#endif
} }
public static bool GetKey(KeyCode key) public static bool GetKey(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKey(key); return Input.GetKey(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (!keys.ContainsKey(key))
{
keys.Add(key, false);
}
return keyPressed != null ? keyPressed.isPressed : false;
#endif
}
private static KeyControl GetKeyControlFromKeyCode(KeyCode key)
{
if (Keyboard.current == null)
{
return null;
}
switch (key)
{
case KeyCode.Escape:
return Keyboard.current.escapeKey;
case KeyCode.KeypadEnter:
return Keyboard.current.numpadEnterKey;
case KeyCode.UpArrow:
return Keyboard.current.upArrowKey;
case KeyCode.DownArrow:
return Keyboard.current.downArrowKey;
case KeyCode.RightArrow:
return Keyboard.current.rightArrowKey;
case KeyCode.LeftArrow:
return Keyboard.current.leftArrowKey;
case KeyCode.LeftShift:
return Keyboard.current.leftShiftKey;
case KeyCode.Tab:
return Keyboard.current.tabKey;
default:
return null;
}
} }
public static bool GetKeyDown(KeyCode key) public static bool GetKeyDown(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKeyDown(key); return Input.GetKeyDown(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (keyPressed.isPressed)
{
if (!keys.ContainsKey(key))
{
keys.Add(key, false);
}
if (!keys[key])
{
keys[key] = true;
return true;
}
}
else
{
keys[key] = false;
}
return false;
#endif
} }
public static bool GetKeyUp(KeyCode key) public static bool GetKeyUp(KeyCode key)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKeyUp(key); return Input.GetKeyUp(key);
#else
KeyControl keyPressed = GetKeyControlFromKeyCode(key);
if (keys[key] && !keyPressed.isPressed)
{
keys[key] = false;
return true;
}
return false;
#endif
} }
public static float GetAxisRaw(string axis) public static float GetAxisRaw(string axis)
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetAxisRaw(axis); return Input.GetAxisRaw(axis);
#else
if (Gamepad.current == null)
{
return 0f;
}
switch (axis)
{
case "Horizontal":
return Gamepad.current.leftStick.x.ReadValue();
case "Vertical":
return Gamepad.current.leftStick.y.ReadValue();
}
return 0f;
#endif
} }
public static Vector3 MousePosition public static Vector3 MousePosition
{ {
get get
{ {
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.mousePosition; return Input.mousePosition;
#else
return Mouse.current.position.ReadValue();
#endif
} }
} }
} }

View File

@ -1,7 +1,8 @@
{ {
"name": "UnityUIExtensions", "name": "UnityUIExtensions",
"references": [ "references": [
"GUID:2bafac87e7f4b9b418d9448d219b01ab" "GUID:2bafac87e7f4b9b418d9448d219b01ab",
"GUID:75469ad4d38634e559750d17036d5f7c"
], ],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],
@ -10,5 +11,6 @@
"precompiledReferences": [], "precompiledReferences": [],
"autoReferenced": true, "autoReferenced": true,
"defineConstraints": [], "defineConstraints": [],
"versionDefines": [] "versionDefines": [],
"noEngineReferences": false
} }