Skip to content

Commit

Permalink
Group UI ports into a unified system, fix inventory and add pause men…
Browse files Browse the repository at this point in the history
…u and in-game UI
  • Loading branch information
RedBigz committed Nov 17, 2024
1 parent 394bd7d commit a7dc311
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 43 deletions.
40 changes: 40 additions & 0 deletions TABGVR/Patches/UI/InventoryUIPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using HarmonyLib;
using Landfall.Network;
using TABGVR.Util;
using UnityEngine;
using UnityEngine.UI;

namespace TABGVR.Patches.UI;

[HarmonyPatch(typeof(InventoryUI), nameof(InventoryUI.Start))]
public static class InventoryUIPatch
{
/// <summary>
/// Patches the inventory to make it visible in world space.
/// </summary>
public static bool Prefix(InventoryUI __instance)
{
__instance.transform.SetParent(
global::Player.localPlayer.m_cameraMovement.transform.Find("CameraRotationX"));

__instance.transform.localPosition = Vector3.forward;
__instance.transform.localRotation = Quaternion.identity;

// *inventory*

// shebang it
UIPorter.Shebang(__instance.transform.Find("Inventory").gameObject);

// fix header being weird af
__instance.characterRT.transform.parent.Find("Header").GetComponent<RectTransform>().localPosition =
new Vector3(0, 620, 0);

// *game ui*
__instance.gameUI.Start();
var gameUI = __instance.transform.Find("GameUI").gameObject;
gameUI.AddComponent<RectMask2D>(); // hide stuff that's normally offscreen
UIPorter.Shebang(gameUI);

return true;
}
}
41 changes: 0 additions & 41 deletions TABGVR/Patches/UIPatch.cs

This file was deleted.

6 changes: 5 additions & 1 deletion TABGVR/Player/Controllers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
using UnityEngine;
using UnityEngine.SpatialTracking;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;

namespace TABGVR.Player;

public static class Controllers
{
public static GameObject LeftHand, RightHand, Head;

public static Floor VRFloor;

/// <summary>
/// <see cref="InputDevice" />s of each hand.
/// </summary>
public static InputDevice LeftHandXR, RightHandXR;


/// <summary>
/// Left hand Vector3 in relative space.
Expand All @@ -35,6 +37,8 @@ public static void Setup()
Head = new GameObject("TABGVR_HMD");
LeftHand = new GameObject("TABGVR_LeftHand");
RightHand = new GameObject("TABGVR_RightHand");

VRFloor = new GameObject("Floor").AddComponent<Floor>(); // For UI tracking

var headDriver = Head.AddComponent<TrackedPoseDriver>();
headDriver.deviceType = TrackedPoseDriver.DeviceType.GenericXRDevice;
Expand Down
14 changes: 14 additions & 0 deletions TABGVR/Player/Floor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;

namespace TABGVR.Player;

public class Floor : MonoBehaviour
{
public void Update()
{
transform.position = Camera.current.transform.position - Controllers.Head.transform.position;
}
}
40 changes: 39 additions & 1 deletion TABGVR/Player/VRControls.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using JetBrains.Annotations;
using Landfall.TABG.UI;
using TABGVR.Util;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;

namespace TABGVR.Player;

Expand All @@ -17,6 +21,8 @@ public class VRControls : MonoBehaviour
private bool _leftTriggered;
private bool _rightTriggered;

private bool _menuButtonPressed;

[CanBeNull] private Pickup currentPickup;
private HaxInput haxInput;
private InputHandler inputHandler;
Expand Down Expand Up @@ -44,6 +50,10 @@ private void Start()

private void Update()
{
// update interactor visibility
if (global::Player.usingInterface != UIPorter.InteractorVisuals)
UIPorter.InteractorVisuals = global::Player.usingInterface;

if (movementHandler.death.dead) return;

Controllers.RightHandXR.TryGetFeatureValue(CommonUsages.primary2DAxis, out var rightJoystick);
Expand All @@ -61,6 +71,26 @@ private void Update()
Controllers.RightHandXR.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out var rightClick);
Controllers.LeftHandXR.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out var leftClick);

Controllers.LeftHandXR.TryGetFeatureValue(CommonUsages.menuButton, out var menuButtonPressed);

if (menuButtonPressed && !_menuButtonPressed)
{
var menuTransitions = InventoryUI.instance.gameObject.GetComponent<MenuTransitions>();

switch (MenuState.CurrentMenuState)
{
case MenuState.TABGMenuState.Main:
menuTransitions.GoToEscape();
break;
case MenuState.TABGMenuState.Escape:
case MenuState.TABGMenuState.Options:
menuTransitions.GoToMain();
break;
}
}

_menuButtonPressed = menuButtonPressed;

if (leftClick && !inputHandler.isSpringting)
inputHandler.isSpringting = true;

Expand All @@ -70,7 +100,13 @@ private void Update()
{
if (!_rightTriggered)
{
if (weaponHandler.rightWeapon) weaponHandler.PressAttack(true, false);
if (UIPorter.UIRightHand.GetComponent<XRRayInteractor>()
.TryGetCurrentUIRaycastResult(out var uiRaycast))
{
var handler = uiRaycast.gameObject.GetComponent<IPointerClickHandler>();
handler?.OnPointerClick(new PointerEventData(EventSystem.current));
}
else if (weaponHandler.rightWeapon) weaponHandler.PressAttack(true, false);
else PickupInteract();
}
else if (weaponHandler.rightWeapon)
Expand Down Expand Up @@ -109,6 +145,8 @@ private void Update()
weaponHandler.leftWeapon?.gun.ReloadGun();
}

if (yButton && !_yButtonPressed) InventoryUI.ToggleInventoryState();

_aButtonPressed = aButton;
_bButtonPressed = bButton;
_xButtonPressed = xButton;
Expand Down
141 changes: 141 additions & 0 deletions TABGVR/Util/UIPorter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Linq;
using JetBrains.Annotations;
using TABGVR.Player;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.XR.Interaction.Toolkit.UI;

namespace TABGVR.Util;

internal static class UIPorter
{
private static bool _eventSystemSetUp;

internal static GameObject UILeftHand;
internal static GameObject UIRightHand;

[CanBeNull] private static XRInteractorLineVisual _uiLeftHandVisual;
[CanBeNull] private static XRInteractorLineVisual _uiRightHandVisual;

internal static bool InteractorVisuals
{
get
{
if (_uiLeftHandVisual is null || _uiRightHandVisual is null) return false;

return _uiLeftHandVisual.enabled;
}

set
{
if (_uiLeftHandVisual is null || _uiRightHandVisual is null) return;

_uiLeftHandVisual.enabled = value;
_uiRightHandVisual.enabled = value;
}
}

internal static void SetupInteractors(XRNode node)
{
if (UILeftHand && node == XRNode.LeftHand) return;
if (UIRightHand && node == XRNode.RightHand) return;

GameObject interactionController;

switch (node)
{
case XRNode.LeftHand:
UILeftHand ??= new GameObject("TABGVR_UILeftHand");
interactionController = UILeftHand;
break;
case XRNode.RightHand:
UIRightHand ??= new GameObject("TABGVR_UIRightHand");
interactionController = UIRightHand;
break;
default:
throw new ArgumentOutOfRangeException(nameof(node), node, null);
}

var controller = interactionController.AddComponent<XRController>();
var interactor = interactionController.AddComponent<XRRayInteractor>();
var lineVisual = interactionController.AddComponent<XRInteractorLineVisual>();
var lineRenderer = interactionController.GetComponent<LineRenderer>();

switch (node)
{
case XRNode.LeftHand:
_uiLeftHandVisual = lineVisual;
break;
case XRNode.RightHand:
_uiRightHandVisual = lineVisual;
break;
}

interactor.rayOriginTransform.localEulerAngles = node switch
{
XRNode.LeftHand => new Vector3(60, 347, 90),
XRNode.RightHand => new Vector3(60, 347, 270),
_ => throw new ArgumentOutOfRangeException(nameof(node), node, null)
};

lineVisual.lineBendRatio = 1;
lineVisual.invalidColorGradient = new Gradient()
{
mode = GradientMode.Blend,
alphaKeys =
new[] { new GradientAlphaKey(0.1f, 0), new GradientAlphaKey(0.1f, 1) },
colorKeys =
new[]
{
new GradientColorKey(Color.white, 0),
new GradientColorKey(Color.white, 1)
}
};

lineRenderer.material = new Material(Shader.Find("Sprites/Default"));

controller.controllerNode = node;

interactionController.transform.SetParent(Controllers.VRFloor.transform, false);
// InteractorMover.Interactors.Add(interactor);
}

internal static void SetupEventSystem()
{
if (_eventSystemSetUp) return;

var eventSystem = SceneManager.GetActiveScene().GetRootGameObjects().First((o => o.name == "MapObjects"))
.transform.Find("EventSystem").gameObject;

eventSystem.AddComponent<XRUIInputModule>();
eventSystem.GetComponent<StandaloneInputModule>().enabled = false;

_eventSystemSetUp = true;
}

internal static void SetupCanvas(GameObject canvas)
{
var canvasComponent = canvas.GetComponent<Canvas>();
canvasComponent.renderMode = RenderMode.WorldSpace;
canvasComponent.worldCamera = Camera.main;

var rectTransform = canvas.GetComponent<RectTransform>();
rectTransform.localPosition = Vector3.zero;
rectTransform.localRotation = Quaternion.identity;
rectTransform.localScale = Vector3.one * 0.0008f; // i don't know why this specific number works, but it does ¯\_(ツ)_/¯

canvas.AddComponent<TrackedDeviceGraphicRaycaster>();
}

internal static void Shebang(GameObject canvas)
{
SetupEventSystem();
SetupInteractors(XRNode.LeftHand);
SetupInteractors(XRNode.RightHand);
SetupCanvas(canvas);
}
}

0 comments on commit a7dc311

Please sign in to comment.