Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/VRControllersManager.cs
2026-02-21 16:45:37 +08:00

486 lines
18 KiB
C#

using System.Collections;
using System.Collections.Generic;
using BitStrap;
using UnityEngine;
public class VRControllersManager : MonoBehaviour
{
private static VRControllersManager instance;
public OvrAvatar ovrAvatar;
public int textSize = 10;
public bool textOutline = true;
public bool textFacingCamera = true;
public float lineScale = 0.3f;
[Header("Hands")]
public Transform handRight;
public Transform handLeft;
[ReadOnly]
public bool handsVisible = true;
public bool isLeftHanded;
[Header("Controllers")]
public VRController currentRightController;
public VRController currentLeftController;
[Space(10f)]
public VRController viveRightController;
public VRController viveLeftController;
public VRController touchRightController;
public VRController touchLeftController;
public VRController indexRightController;
public VRController indexLeftController;
public VRController wmrRightController;
public VRController wmrLeftController;
public VRController touchSRightController;
public VRController touchSLeftController;
public VRController cosmosRightController;
public VRController cosmosLeftController;
public List<VRController> allControllers = new List<VRController>();
[Header("Test")]
public Transform throwObject;
public float throwForceMultiplier = 1f;
public Transform trackingSpace;
public Transform helpLine_01;
public Transform helpLine_02;
public Transform reelHandTarget;
public Transform reelHandHelper;
public Transform reelRotateParent;
public float handToReelDistanceCatch = 0.3f;
public float handToReelDistanceRelease = 0.5f;
public float handReelSpeedMultiplier = 1f;
public Vector2 handReelSpeedMinMax = Vector2.one;
public float handReelNoFishSpeedMultiplier = 1f;
public Vector2 handReelNoFishSpeedMinMax = Vector2.one;
public float handLineSpeedMultiplier = 1.5f;
public FishingReel testReel;
public static VRControllersManager Instance
{
get
{
return instance;
}
}
private VRControllersManager()
{
}
private void Awake()
{
if (instance == null)
{
instance = this;
Object.DontDestroyOnLoad(base.gameObject);
}
else
{
Object.DestroyImmediate(base.gameObject);
}
}
private void Start()
{
if (!VRManager.IsVROn())
{
base.gameObject.SetActive(false);
}
else
{
Initialize();
}
}
private void Update()
{
RefreshCurrentControllers();
}
private void LateUpdate()
{
handRight.localPosition = Vector3.zero;
handRight.localRotation = Quaternion.identity;
handLeft.localPosition = Vector3.zero;
handLeft.localRotation = Quaternion.identity;
}
public IEnumerator WriteControllersDebug()
{
while (true)
{
if ((bool)ovrAvatar)
{
Debug.Log("-- OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch): " + OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch));
Debug.Log("ovrAvatar.transform.position + OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch): " + (ovrAvatar.transform.position + OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch)));
if ((bool)ovrAvatar.rightControllerAnchor)
{
Debug.Log("ovrAvatar.rightControllerAnchor.position: " + ovrAvatar.rightControllerAnchor.position);
}
if ((bool)ovrAvatar.ControllerRight)
{
Debug.Log("ovrAvatar.ControllerRight.transform.position: " + ovrAvatar.ControllerRight.transform.position);
}
}
yield return new WaitForSeconds(10f);
}
}
public void Initialize()
{
allControllers.Add(viveRightController);
allControllers.Add(viveLeftController);
allControllers.Add(touchRightController);
allControllers.Add(touchLeftController);
allControllers.Add(wmrRightController);
allControllers.Add(wmrLeftController);
allControllers.Add(indexRightController);
allControllers.Add(indexLeftController);
allControllers.Add(touchSRightController);
allControllers.Add(touchSLeftController);
allControllers.Add(cosmosRightController);
allControllers.Add(cosmosLeftController);
foreach (VRController allController in allControllers)
{
allController.transform.localPosition = Vector3.zero;
}
if (ovrAvatar == null)
{
ChangeAvatar(VRManager.Instance.ovrAvatar);
}
}
public void ShowHandsQuick(bool show)
{
foreach (VRController allController in allControllers)
{
allController.modelParent.SetActive(show && VRManager.Instance.IsControllersInput());
allController.infoParent.SetActive(show && VRManager.Instance.IsControllersInput());
}
}
public void ShowHands(bool show)
{
handsVisible = show;
ShowHandsQuick(show);
}
public OVRInput.Controller GetPrimaryController()
{
return isLeftHanded ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch;
}
public OVRInput.Controller GetSecondaryController()
{
return (!isLeftHanded) ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch;
}
public bool IsLeftHanded()
{
return isLeftHanded;
}
public void SetLeftHanded(bool leftHand)
{
isLeftHanded = leftHand;
}
[Button]
public void ToggleLeftHanded()
{
isLeftHanded = !isLeftHanded;
if ((bool)HUDManager.Instance)
{
HUDManager.Instance.UpdateControls();
}
}
[Button]
public void ChangeAvatar(OvrAvatar avatar)
{
ovrAvatar = avatar;
if ((bool)ovrAvatar)
{
OVRCameraRig componentInParent = ovrAvatar.GetComponentInParent<OVRCameraRig>();
if ((bool)handRight && (bool)componentInParent && (bool)componentInParent.rightHandAnchor)
{
handRight.parent = ovrAvatar.GetComponentInParent<OVRCameraRig>().rightHandAnchor;
handRight.localPosition = Vector3.zero;
handRight.localRotation = Quaternion.identity;
}
if ((bool)handLeft && (bool)componentInParent && (bool)componentInParent.leftHandAnchor)
{
handLeft.parent = ovrAvatar.GetComponentInParent<OVRCameraRig>().leftHandAnchor;
handLeft.localPosition = Vector3.zero;
handLeft.localRotation = Quaternion.identity;
}
RefreshControllersType();
HideAllInfo();
if (ovrAvatar == VRManager.Instance.ovrAvatar)
{
Instance.UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.PrimaryIndexTrigger, Utilities.GetTranslation("GUI/BTN_CHOOSE"));
Instance.UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.PrimaryIndexTrigger, Utilities.GetTranslation("GUI/BTN_CHOOSE"));
}
else if ((bool)HUDManager.Instance)
{
HUDManager.Instance.UpdateControls();
}
}
}
public void RefreshControllersType()
{
if (!(viveLeftController == null))
{
RefreshCurrentControllers();
OVRInput.OpenVRController controllerType = VRManager.GetControllerType(false);
OVRInput.OpenVRController controllerType2 = VRManager.GetControllerType(true);
viveLeftController.gameObject.SetActive(controllerType == OVRInput.OpenVRController.ViveController);
viveRightController.gameObject.SetActive(controllerType2 == OVRInput.OpenVRController.ViveController);
touchLeftController.gameObject.SetActive(controllerType == OVRInput.OpenVRController.OculusTouch);
touchRightController.gameObject.SetActive(controllerType2 == OVRInput.OpenVRController.OculusTouch);
wmrLeftController.gameObject.SetActive(controllerType == OVRInput.OpenVRController.WindowsMRController);
wmrRightController.gameObject.SetActive(controllerType2 == OVRInput.OpenVRController.WindowsMRController);
indexLeftController.gameObject.SetActive(controllerType == OVRInput.OpenVRController.IndexController);
indexRightController.gameObject.SetActive(controllerType2 == OVRInput.OpenVRController.IndexController);
touchSLeftController.gameObject.SetActive(false);
touchSRightController.gameObject.SetActive(false);
cosmosLeftController.gameObject.SetActive(controllerType == OVRInput.OpenVRController.CosmosController);
cosmosRightController.gameObject.SetActive(controllerType2 == OVRInput.OpenVRController.CosmosController);
}
}
public void RefreshCurrentControllers()
{
if ((!(currentLeftController == null) && !(currentRightController == null)) || (OVRInput.GetActiveController() != OVRInput.Controller.Touch && OVRInput.GetActiveController() != OVRInput.Controller.LTouch && OVRInput.GetActiveController() != OVRInput.Controller.RTouch))
{
return;
}
OVRInput.OpenVRController controllerType = VRManager.GetControllerType(false);
OVRInput.OpenVRController controllerType2 = VRManager.GetControllerType(true);
if (controllerType == OVRInput.OpenVRController.ViveController || controllerType2 == OVRInput.OpenVRController.ViveController)
{
currentLeftController = viveLeftController;
currentRightController = viveRightController;
Debug.Log("VRControllersManager Connected ViveController");
}
else if (controllerType == OVRInput.OpenVRController.OculusTouch || controllerType2 == OVRInput.OpenVRController.OculusTouch)
{
currentLeftController = touchLeftController;
currentRightController = touchRightController;
if (!VRManager.useOculusSDK)
{
currentLeftController.transform.localPosition = new Vector3(currentLeftController.modelParent.transform.localPosition.x, 0f, currentLeftController.modelParent.transform.localPosition.z);
currentLeftController.transform.localEulerAngles = new Vector3(0f - currentLeftController.modelParent.transform.localEulerAngles.x, currentLeftController.transform.localEulerAngles.y, currentLeftController.transform.localEulerAngles.z);
currentRightController.transform.localPosition = new Vector3(currentRightController.modelParent.transform.localPosition.x, 0f, currentRightController.modelParent.transform.localPosition.z);
currentRightController.transform.localEulerAngles = new Vector3(0f - currentRightController.modelParent.transform.localEulerAngles.x, currentRightController.transform.localEulerAngles.y, currentRightController.transform.localEulerAngles.z);
}
Debug.Log("VRControllersManager Connected OculusTouch");
}
else if (controllerType == OVRInput.OpenVRController.WindowsMRController || controllerType2 == OVRInput.OpenVRController.WindowsMRController)
{
currentLeftController = wmrLeftController;
currentRightController = wmrRightController;
Debug.Log("VRControllersManager Connected WindowsMRController");
}
else if (controllerType == OVRInput.OpenVRController.IndexController || controllerType2 == OVRInput.OpenVRController.IndexController)
{
currentLeftController = indexLeftController;
currentRightController = indexRightController;
Debug.Log("VRControllersManager Connected IndexController");
}
else if (controllerType == OVRInput.OpenVRController.OculusTouchS || controllerType2 == OVRInput.OpenVRController.OculusTouchS)
{
currentLeftController = touchSLeftController;
currentRightController = touchSRightController;
Debug.Log("VRControllersManager Connected OculusTouchS");
}
else if (controllerType == OVRInput.OpenVRController.CosmosController || controllerType2 == OVRInput.OpenVRController.CosmosController)
{
currentLeftController = cosmosLeftController;
currentRightController = cosmosRightController;
Debug.Log("VRControllersManager Connected Cosmos");
}
else
{
currentLeftController = viveLeftController;
currentRightController = viveRightController;
Debug.Log("VRControllersManager Connected Unknown");
}
currentLeftController.rodHoldTransform.localScale = new Vector3(-1f, 1f, 1f);
currentRightController.fishingNetHoldTransform.localScale = new Vector3(-1f, 1f, 1f);
if (!GameController.Instance)
{
Instance.UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.PrimaryIndexTrigger, Utilities.GetTranslation("GUI/BTN_CHOOSE"));
Instance.UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.PrimaryIndexTrigger, Utilities.GetTranslation("GUI/BTN_CHOOSE"));
VRManager.Instance.ChangeUIInputStyle(VRManager.UIInputStyle.POINTER);
}
}
[Button]
public void ButtonsInfoTest()
{
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.PrimaryIndexTrigger, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.PrimaryHandTrigger, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Start, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.One, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Two, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.PrimaryThumbstick, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Up, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Down, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Left, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetPrimaryController(), OVRInput.Button.Right, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.PrimaryIndexTrigger, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.PrimaryHandTrigger, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Start, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.One, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Two, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.PrimaryThumbstick, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Up, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Down, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Left, "Button " + Random.Range(1, 666));
UpdateButtonInfo(GetSecondaryController(), OVRInput.Button.Right, "Button " + Random.Range(1, 666));
}
public VRController GetVRController(OVRInput.Controller controller)
{
return (controller != OVRInput.Controller.LTouch) ? currentRightController : currentLeftController;
}
public void UpdateButtonInfo(VRInputManager.ActionDefinition actionDefinition, string infoText)
{
if (actionDefinition != null)
{
List<VRInputManager.ButtonDefinition> list = null;
list = ((VRManager.GetControllerType(false) != OVRInput.OpenVRController.ViveController && VRManager.GetControllerType(true) != OVRInput.OpenVRController.ViveController) ? ((VRManager.GetControllerType(false) != OVRInput.OpenVRController.OculusTouch && VRManager.GetControllerType(true) != OVRInput.OpenVRController.OculusTouch) ? ((VRManager.GetControllerType(false) != OVRInput.OpenVRController.WindowsMRController && VRManager.GetControllerType(true) != OVRInput.OpenVRController.WindowsMRController) ? ((VRManager.GetControllerType(false) != OVRInput.OpenVRController.IndexController && VRManager.GetControllerType(true) != OVRInput.OpenVRController.IndexController) ? ((VRManager.GetControllerType(false) != OVRInput.OpenVRController.CosmosController && VRManager.GetControllerType(true) != OVRInput.OpenVRController.CosmosController) ? actionDefinition.viveButtons : actionDefinition.cosmosButtons) : actionDefinition.indexButtons) : actionDefinition.wmrButtons) : actionDefinition.oculusButtons) : actionDefinition.viveButtons);
if (list != null && list.Count > 0)
{
UpdateButtonInfo(list[0].GetController(), list[0].button, infoText);
}
}
}
public void UpdateButtonInfo(OVRInput.Controller controller, OVRInput.Button button, string infoText)
{
VRController vRController = GetVRController(controller);
if (vRController == null)
{
Debug.LogError("Controller not found: " + controller);
}
else
{
vRController.UpdateInfo(button, infoText);
}
}
public void HideAllInfo()
{
foreach (VRController allController in allControllers)
{
allController.HideAllInfo();
}
}
public void ThrowTestUpdate()
{
if (OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger, Instance.GetSecondaryController()))
{
throwObject.parent = null;
throwObject.GetComponent<Rigidbody>().isKinematic = false;
throwObject.GetComponent<Rigidbody>().AddForce(trackingSpace.rotation * OVRInput.GetLocalControllerVelocity(Instance.GetSecondaryController()) * throwForceMultiplier);
throwObject.GetComponent<Rigidbody>().angularVelocity = OVRInput.GetLocalControllerAngularVelocity(Instance.GetSecondaryController());
}
else if (OVRInput.GetDown(OVRInput.Button.PrimaryThumbstick, Instance.GetSecondaryController()))
{
throwObject.parent = touchLeftController.throwObjectTransform;
throwObject.localPosition = Vector3.zero;
throwObject.GetComponent<Rigidbody>().isKinematic = true;
throwObject.GetComponent<Rigidbody>().velocity = Vector3.zero;
throwObject.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
public void ReelTestStart()
{
helpLine_01.parent.parent = reelRotateParent;
helpLine_01.parent.localPosition = Vector3.zero;
helpLine_01.parent.localEulerAngles = new Vector3(0f, -90f, 0f);
}
public void ReelTestUpdate()
{
Transform reelCatchTransform = touchLeftController.reelCatchTransform;
float num = Vector3.Distance(helpLine_01.position, reelCatchTransform.position);
if (reelHandHelper == null)
{
reelHandHelper = new GameObject("ReelHandHelper").transform;
}
reelHandHelper.position = reelCatchTransform.position;
reelHandHelper.parent = reelRotateParent;
reelHandHelper.localPosition = new Vector3(reelHandHelper.localPosition.x, reelHandHelper.localPosition.y, 0f);
helpLine_02.LookAt(reelHandHelper);
reelHandHelper.parent = reelCatchTransform;
if (num < handToReelDistanceCatch)
{
helpLine_02.GetComponentInChildren<Renderer>().material.color = Color.yellow;
float num2 = 0f - helpLine_02.localEulerAngles.x;
if (helpLine_02.localEulerAngles.y < 80f && helpLine_02.localEulerAngles.y > -80f)
{
num2 = helpLine_02.localEulerAngles.x + 180f;
}
else if (helpLine_02.localEulerAngles.x > 0f)
{
num2 = 360f - helpLine_02.localEulerAngles.x;
}
if (num2 > 360f)
{
num2 -= 360f;
}
helpLine_01.parent.parent = reelRotateParent.parent;
reelRotateParent.localEulerAngles = new Vector3(0f, 0f, num2);
helpLine_01.parent.parent = reelRotateParent;
}
else
{
helpLine_02.GetComponentInChildren<Renderer>().material.color = Color.red;
}
}
}