409 lines
13 KiB
C#
409 lines
13 KiB
C#
using System;
|
|
using Rewired;
|
|
using Rewired.Data;
|
|
using UnityEngine;
|
|
|
|
public class UtilitiesInput : MonoBehaviour
|
|
{
|
|
public static InputManager rewiredInputManager = null;
|
|
|
|
public static Player rewiredPlayer = null;
|
|
|
|
public static bool isReelingIn = false;
|
|
|
|
public static bool isReelingOut = false;
|
|
|
|
public static Vector2 lookAxis = Vector2.zero;
|
|
|
|
public static Vector2 moveAxis = Vector2.zero;
|
|
|
|
public static Vector2 prevLookAxis = Vector2.zero;
|
|
|
|
public static Vector2 prevMoveAxis = Vector2.zero;
|
|
|
|
private static Vector2 tempVRMoveAxis = Vector2.zero;
|
|
|
|
private static Vector2 tempPrevVRMoveAxis = Vector2.zero;
|
|
|
|
public const float sticksBugValue = 1f;
|
|
|
|
public static bool showGamepadUI = false;
|
|
|
|
public static Controller controllerKeyboard;
|
|
|
|
public static Controller controllerMouse;
|
|
|
|
public static ControllerMap controllerMapKeyboard;
|
|
|
|
public static ControllerMap controllerMapMouse;
|
|
|
|
public static UserDataStore_PlayerPrefs userDataStore_PlayerPrefs = null;
|
|
|
|
public static bool blockInput = false;
|
|
|
|
private static float axisAsButtonThreshold = 0.5f;
|
|
|
|
private static float axisAsButtonThreshold_Inverted = 0.7f;
|
|
|
|
public static bool isLThumbstickUp = false;
|
|
|
|
public static bool isLThumbstickDown = false;
|
|
|
|
public static bool isLThumbstickLeft = false;
|
|
|
|
public static bool isLThumbstickRight = false;
|
|
|
|
public static bool isRThumbstickUp = false;
|
|
|
|
public static bool isRThumbstickDown = false;
|
|
|
|
public static bool isRThumbstickLeft = false;
|
|
|
|
public static bool isRThumbstickRight = false;
|
|
|
|
private void Awake()
|
|
{
|
|
rewiredPlayer = ReInput.players.GetPlayer(0);
|
|
controllerKeyboard = rewiredPlayer.controllers.GetController(ControllerType.Keyboard, 0);
|
|
controllerMouse = rewiredPlayer.controllers.GetController(ControllerType.Mouse, 0);
|
|
controllerMapKeyboard = rewiredPlayer.controllers.maps.GetMap(controllerKeyboard.type, controllerKeyboard.id, "Default", "Default");
|
|
controllerMapMouse = rewiredPlayer.controllers.maps.GetMap(controllerMouse.type, controllerMouse.id, "Default", "Default");
|
|
userDataStore_PlayerPrefs = UnityEngine.Object.FindObjectOfType<UserDataStore_PlayerPrefs>();
|
|
showGamepadUI = true;
|
|
ShowGamepadUI(false);
|
|
for (int i = 0; i < rewiredPlayer.controllers.joystickCount; i++)
|
|
{
|
|
Debug.Log("UtilitiesInput Controller " + i + ": " + rewiredPlayer.controllers.Joysticks[i].hardwareIdentifier + "; " + rewiredPlayer.controllers.Joysticks[i].hardwareName + "; " + rewiredPlayer.controllers.Joysticks[i].name + "; " + rewiredPlayer.controllers.Joysticks[i].type.ToString());
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
lookAxis = (moveAxis = Vector2.zero);
|
|
isReelingIn = (isReelingOut = false);
|
|
isLThumbstickUp = (isLThumbstickDown = (isLThumbstickLeft = (isLThumbstickRight = false)));
|
|
isRThumbstickUp = (isRThumbstickDown = (isRThumbstickLeft = (isRThumbstickRight = false)));
|
|
if ((bool)GameController.Instance && (bool)GameController.Instance.fishingPlayer)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
if (VRManager.Instance.vrReeling && (bool)GameController.Instance.fishingPlayer.currentHands)
|
|
{
|
|
if (GameController.Instance.fishingPlayer.currentHands.isFlyRig && !GameController.Instance.fishingPlayer.currentHands.fishingPlayer.fish)
|
|
{
|
|
isReelingIn = GameController.Instance.fishingPlayer.currentHands.vrRopeIsReelingIn;
|
|
}
|
|
else if ((bool)GameController.Instance.fishingPlayer.currentHands.reel)
|
|
{
|
|
isReelingIn = GameController.Instance.fishingPlayer.currentHands.reel.vrHandle_DiffAngle < -0.03f;
|
|
isReelingOut = GameController.Instance.fishingPlayer.currentHands.reel.vrHandle_DiffAngle > 0.03f;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isReelingIn = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, VRControllersManager.Instance.GetSecondaryController()) > 0.05f;
|
|
if (GameController.Instance.iceLevel)
|
|
{
|
|
isReelingOut = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, VRControllersManager.Instance.GetPrimaryController()) > 0.05f;
|
|
}
|
|
else
|
|
{
|
|
isReelingOut = false;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isReelingIn = GetButton("REEL_IN");
|
|
isReelingOut = GetButton("REEL_OUT");
|
|
}
|
|
if (!VRManager.Instance.IsControllersInput() || VRManager.Instance.playerRotateStyle == VRManager.PlayerRotateStyle.FREE || GameController.Instance.fishingPlayer.currentState == FishingPlayer.PlayerState.DRIVING_BOAT || GameController.Instance.fishingPlayer.currentState == FishingPlayer.PlayerState.WATCH_FISH)
|
|
{
|
|
lookAxis.x = GetAxisRaw("LOOK_HORIZONTAL");
|
|
lookAxis.y = GetAxisRaw("LOOK_VERTICAL");
|
|
}
|
|
if (!VRManager.IsVROn() || VRManager.Instance.playerWalkStyle == VRManager.PlayerWalkStyle.FREE || GameController.Instance.fishingPlayer.currentState == FishingPlayer.PlayerState.DRIVING_BOAT || GameController.Instance.fishingPlayer.currentState == FishingPlayer.PlayerState.ICE_FISHING || GameController.Instance.fishingPlayer.currentState == FishingPlayer.PlayerState.WATCH_FISH)
|
|
{
|
|
moveAxis.x = GetAxis("MOVE_HORIZONTAL");
|
|
moveAxis.y = GetAxis("MOVE_VERTICAL");
|
|
}
|
|
}
|
|
else if ((bool)TrophyRoomManager.Instance)
|
|
{
|
|
lookAxis.x = GetAxisRaw("LOOK_HORIZONTAL");
|
|
lookAxis.y = GetAxisRaw("LOOK_VERTICAL");
|
|
if (!VRManager.IsVROn() || VRManager.Instance.playerWalkStyle == VRManager.PlayerWalkStyle.FREE)
|
|
{
|
|
moveAxis.x = GetAxis("MOVE_HORIZONTAL");
|
|
moveAxis.y = GetAxis("MOVE_VERTICAL");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lookAxis.x = GetAxisRaw("LOOK_HORIZONTAL");
|
|
lookAxis.y = GetAxisRaw("LOOK_VERTICAL");
|
|
moveAxis.x = GetAxis("MOVE_HORIZONTAL");
|
|
moveAxis.y = GetAxis("MOVE_VERTICAL");
|
|
}
|
|
if ((lookAxis.x == -1f || lookAxis.x == 1f) && (lookAxis.y == -1f || lookAxis.y == 1f))
|
|
{
|
|
lookAxis = prevLookAxis;
|
|
}
|
|
if ((moveAxis.x == -1f || moveAxis.x == 1f) && (moveAxis.y == -1f || moveAxis.y == 1f))
|
|
{
|
|
moveAxis = prevMoveAxis;
|
|
}
|
|
if (VRManager.IsVROn() && !VRManager.Instance.useOculusSDKPublic && !VRManager.Instance.IsControllersInput())
|
|
{
|
|
tempVRMoveAxis.x = GetAxis("MOVE_HORIZONTAL");
|
|
tempVRMoveAxis.y = GetAxis("MOVE_VERTICAL");
|
|
isLThumbstickUp = tempPrevVRMoveAxis.y < axisAsButtonThreshold && tempVRMoveAxis.y >= axisAsButtonThreshold && Mathf.Abs(tempVRMoveAxis.x) < axisAsButtonThreshold_Inverted;
|
|
isRThumbstickLeft = prevLookAxis.x > 0f - axisAsButtonThreshold && lookAxis.x <= 0f - axisAsButtonThreshold && Mathf.Abs(lookAxis.y) < axisAsButtonThreshold_Inverted;
|
|
isRThumbstickRight = prevLookAxis.x < axisAsButtonThreshold && lookAxis.x >= axisAsButtonThreshold && Mathf.Abs(lookAxis.y) < axisAsButtonThreshold_Inverted;
|
|
tempPrevVRMoveAxis = tempVRMoveAxis;
|
|
}
|
|
prevLookAxis = lookAxis;
|
|
prevMoveAxis = moveAxis;
|
|
ShowGamepadUI(rewiredPlayer.controllers.joystickCount > 0);
|
|
}
|
|
|
|
public static float GetAxis(string axisName)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return VRInputManager.GetAxis(axisName);
|
|
}
|
|
return rewiredPlayer.GetAxis(axisName);
|
|
}
|
|
|
|
public static float GetAxisRaw(string axisName)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return VRInputManager.GetAxisRaw(axisName);
|
|
}
|
|
return rewiredPlayer.GetAxisRaw(axisName);
|
|
}
|
|
|
|
public static bool GetButton(string buttonName)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return VRInputManager.GetButton(buttonName);
|
|
}
|
|
return rewiredPlayer.GetButton(buttonName);
|
|
}
|
|
|
|
public static bool GetButtonDown(string buttonName)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return VRInputManager.GetButtonDown(buttonName);
|
|
}
|
|
return rewiredPlayer.GetButtonDown(buttonName);
|
|
}
|
|
|
|
public static bool GetButtonUp(string buttonName)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return VRInputManager.GetButtonUp(buttonName);
|
|
}
|
|
return rewiredPlayer.GetButtonUp(buttonName);
|
|
}
|
|
|
|
public static void SetVibration(int motorId, bool primaryController, float strength, float duration = -1f)
|
|
{
|
|
if ((bool)GlobalSettings.Instance && !GlobalSettings.Instance.playerSettings.gamepadVibration)
|
|
{
|
|
return;
|
|
}
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
OVRInput.SetControllerVibration(strength, strength, (!primaryController) ? VRControllersManager.Instance.GetSecondaryController() : VRControllersManager.Instance.GetPrimaryController());
|
|
if (duration > 0f)
|
|
{
|
|
LeanTween.delayedCall(duration, (Action)delegate
|
|
{
|
|
StopVibration(primaryController);
|
|
});
|
|
}
|
|
}
|
|
else if (duration >= 0f)
|
|
{
|
|
rewiredPlayer.SetVibration(motorId, strength, duration);
|
|
}
|
|
else
|
|
{
|
|
rewiredPlayer.SetVibration(motorId, strength);
|
|
}
|
|
}
|
|
|
|
public static void StopVibration(bool primaryController)
|
|
{
|
|
if ((bool)VRManager.Instance && VRManager.Instance.IsControllersInput())
|
|
{
|
|
OVRInput.SetControllerVibration(0f, 0f, (!primaryController) ? VRControllersManager.Instance.GetSecondaryController() : VRControllersManager.Instance.GetPrimaryController());
|
|
}
|
|
else
|
|
{
|
|
rewiredPlayer.StopVibration();
|
|
}
|
|
}
|
|
|
|
public static void StopVibration()
|
|
{
|
|
StopVibration(true);
|
|
StopVibration(false);
|
|
}
|
|
|
|
public static float GetVibration(int motorId)
|
|
{
|
|
if (VRManager.Instance.IsControllersInput())
|
|
{
|
|
return 1f;
|
|
}
|
|
return rewiredPlayer.GetVibration(motorId);
|
|
}
|
|
|
|
public static void ResetRewiredPrefs()
|
|
{
|
|
rewiredPlayer.controllers.maps.LoadDefaultMaps(ControllerType.Keyboard);
|
|
rewiredPlayer.controllers.maps.LoadDefaultMaps(ControllerType.Mouse);
|
|
controllerKeyboard = rewiredPlayer.controllers.GetController(ControllerType.Keyboard, 0);
|
|
controllerMouse = rewiredPlayer.controllers.GetController(ControllerType.Mouse, 0);
|
|
controllerMapKeyboard = rewiredPlayer.controllers.maps.GetMap(controllerKeyboard.type, controllerKeyboard.id, "Default", "Default");
|
|
controllerMapMouse = rewiredPlayer.controllers.maps.GetMap(controllerMouse.type, controllerMouse.id, "Default", "Default");
|
|
}
|
|
|
|
public static InputAction FindInputAction(string actionName)
|
|
{
|
|
for (int i = 0; i < ReInput.mapping.Actions.Count; i++)
|
|
{
|
|
InputAction inputAction = ReInput.mapping.Actions[i];
|
|
if (ReInput.mapping.Actions[i].name == actionName)
|
|
{
|
|
return ReInput.mapping.Actions[i];
|
|
}
|
|
}
|
|
Debug.LogError("FindInputAction not found: " + actionName);
|
|
return null;
|
|
}
|
|
|
|
public static string GetActionKeyName(string actionName, bool positive = true)
|
|
{
|
|
InputAction inputAction = FindInputAction(actionName);
|
|
return GetActionKeyName(inputAction.id, positive);
|
|
}
|
|
|
|
public static string GetActionKeyName(int actionId, bool positive = true)
|
|
{
|
|
Pole pole = ((!positive) ? Pole.Negative : Pole.Positive);
|
|
string text = string.Empty;
|
|
bool flag = false;
|
|
foreach (ActionElementMap item in controllerMapKeyboard.ElementMapsWithAction(actionId))
|
|
{
|
|
if (item.axisContribution == pole)
|
|
{
|
|
flag = true;
|
|
text = item.elementIdentifierName;
|
|
}
|
|
}
|
|
foreach (ActionElementMap item2 in controllerMapMouse.ElementMapsWithAction(actionId))
|
|
{
|
|
if (item2.axisContribution == pole)
|
|
{
|
|
flag = true;
|
|
text = item2.elementIdentifierName;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
Debug.LogError("GetActionKeyName not found: " + actionId);
|
|
}
|
|
else
|
|
{
|
|
switch (text)
|
|
{
|
|
case "None":
|
|
text = string.Empty;
|
|
break;
|
|
case "Left Mouse Button":
|
|
text = Utilities.GetTranslation("HUD_CONTROLS/MOUSE_LEFT");
|
|
break;
|
|
case "Right Mouse Button":
|
|
text = Utilities.GetTranslation("HUD_CONTROLS/MOUSE_RIGHT");
|
|
break;
|
|
case "Mouse Button 3":
|
|
text = Utilities.GetTranslation("HUD_CONTROLS/MOUSE_MIDDLE");
|
|
break;
|
|
case "Mouse Wheel":
|
|
text = Utilities.GetTranslation("HUD_CONTROLS/MOUSE_WHEEL");
|
|
break;
|
|
case "Space":
|
|
text = Utilities.GetTranslation("HUD_CONTROLS/SPACE");
|
|
break;
|
|
}
|
|
}
|
|
return text;
|
|
}
|
|
|
|
public static int GetActionElementMapId(int actionId, bool positive = true)
|
|
{
|
|
Pole pole = ((!positive) ? Pole.Negative : Pole.Positive);
|
|
foreach (ActionElementMap item in controllerMapKeyboard.ElementMapsWithAction(actionId))
|
|
{
|
|
if (item.axisContribution == pole)
|
|
{
|
|
return item.id;
|
|
}
|
|
}
|
|
foreach (ActionElementMap item2 in controllerMapMouse.ElementMapsWithAction(actionId))
|
|
{
|
|
if (item2.axisContribution == pole)
|
|
{
|
|
return item2.id;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static string GetMoveControls()
|
|
{
|
|
return GetActionKeyName("MOVE_VERTICAL") + "/" + GetActionKeyName("MOVE_VERTICAL", false) + "/" + GetActionKeyName("MOVE_HORIZONTAL", false) + "/" + GetActionKeyName("MOVE_HORIZONTAL");
|
|
}
|
|
|
|
public static void ShowGamepadUI(bool show)
|
|
{
|
|
if (showGamepadUI != show && (bool)MenuManager.Instance)
|
|
{
|
|
Debug.Log("ShowGamepadUI: " + show);
|
|
showGamepadUI = show;
|
|
if ((bool)MenuManager.Instance)
|
|
{
|
|
ShowGamepadUIParent(show, MenuManager.Instance.gameObject);
|
|
}
|
|
if ((bool)HUDManager.Instance)
|
|
{
|
|
ShowGamepadUIParent(show, HUDManager.Instance.gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void ShowGamepadUIParent(bool show, GameObject parent)
|
|
{
|
|
MultiTags[] componentsInChildren = parent.GetComponentsInChildren<MultiTags>(true);
|
|
if (componentsInChildren == null)
|
|
{
|
|
return;
|
|
}
|
|
for (int i = 0; i < componentsInChildren.Length; i++)
|
|
{
|
|
if (componentsInChildren[i].gameObject.HasTag("GAMEPAD_UI"))
|
|
{
|
|
componentsInChildren[i].gameObject.SetActive(show);
|
|
}
|
|
}
|
|
}
|
|
}
|