Skip to content

Commit

Permalink
feat(ui): attched inspector to aim location (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Dec 16, 2023
1 parent 8c4fb9b commit b7ef8db
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 6 deletions.
2 changes: 0 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ While the mod is in a playable state, it is still in early development. Some fea
- Ability to switch primary hand
- Ability to yeet paladin down the waterfall
- Hand crank using uhhhhh... hands?
- Machine info not visible when targeting with hand. (#12)
- Crafting queue not visible. (#11)

=== Known Issues
- The players position is not synced correctly with the game when using roomscale. The characters position is in the center of the playspace. This might cause issues with collisions and other things. Also map shows the player looking in the wrong direction. (#14)
Expand Down
47 changes: 47 additions & 0 deletions plugin/src/camera/patch/builder/player_inspector_patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using Plugin.Input;
using UnityEngine;

namespace TechtonicaVR.VRCamera.Patch.Builder;

[HarmonyPatch]
public class PlayerInspectorPatch
{
private const int EXPECTED_INSPECTOR_UPDATE_PATCH_COUNT = 1;

static MethodInfo raycastMethod = typeof(Physics).GetMethod(nameof(Physics.Raycast), [typeof(Vector3), typeof(Vector3), typeof(RaycastHit).MakeByRefType(), typeof(float), typeof(int)]);
static MethodInfo raycastPatchMethod = typeof(PlayerInspectorPatch).GetMethod(nameof(PatchedRaycast));

[HarmonyPatch(typeof(PlayerInspector), nameof(PlayerInspector.LateUpdate))]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> InspectorUpdateTranspiler(IEnumerable<CodeInstruction> instructions)
{
var patchCnt = 0;
foreach (var instruction in instructions)
{
if (instruction.Calls(raycastMethod))
{
yield return new CodeInstruction(OpCodes.Call, raycastPatchMethod);
patchCnt++;
}
else
{
yield return instruction;
}
}

if (patchCnt != EXPECTED_INSPECTOR_UPDATE_PATCH_COUNT)
{
Plugin.Logger.LogError($"[PlayerInspector.LateUpdate] Patch count mismatch: {patchCnt} != {EXPECTED_INSPECTOR_UPDATE_PATCH_COUNT}");
}
}

public static bool PatchedRaycast(Vector3 _origin, Vector3 _direction, out RaycastHit hitInfo, float maxDistance, int layerMask)
{
return Physics.Raycast(SteamVRInputMapper.rightHandObject.transform.position, -SteamVRInputMapper.rightHandObject.transform.up, out hitInfo, maxDistance, layerMask);
}

}
16 changes: 14 additions & 2 deletions plugin/src/camera/patch/target_raycast_patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TargetRaycastPatch
{

public static Transform cursorTlc;
public static Transform inspectorTlc;

[HarmonyPrefix]
[HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.UpdateAimingRaycasts))]
Expand Down Expand Up @@ -48,10 +49,21 @@ public static bool UpdateAimingRaycastsPostfix(PlayerFirstPersonController __ins
[HarmonyPatch(typeof(CursorDotUI), nameof(PlayerFirstPersonController.LateUpdate))]
public static void LateUpdatePostfix(CursorDotUI __instance)
{
if (cursorTlc != null && Player.instance.fpcontroller.hasCamHit && VRCameraManager.mainCamera != null)
if (!Player.instance.fpcontroller.hasCamHit || VRCameraManager.mainCamera == null)
{
return;
}

var camHit = Player.instance.fpcontroller.camHit;

if (cursorTlc != null)
{
var camHit = Player.instance.fpcontroller.camHit;
MathyStuff.PositionCanvasInWorld(cursorTlc.gameObject, VRCameraManager.mainCamera, camHit.point);
}

if (inspectorTlc != null)
{
MathyStuff.PositionCanvasInWorld(inspectorTlc.gameObject, VRCameraManager.mainCamera, camHit.point);
}
}
}
1 change: 1 addition & 0 deletions plugin/src/main_game_patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void Start()
new DialoguePopupPatch(),
new DisableComponentPatch<OutlinePostProcess>(),
new CursorCanvasPatch(),
new InspectorCanvasPatch(),
new CompassPatch(),
new MapPatch(),
new CraftingQueuePatch(),
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/patch/main_game/ui/crafting_queue_patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override bool Apply(CraftingQueueGrid component)
trackedCanvas.offset = new Vector3(-0.08f, 0.06f, -0.1f);
trackedCanvas.showDistance = 0.3f;
trackedCanvas.noTransform = true;
tlc.localPosition = new Vector3(-17.7141f, 17.7794f, 0f);
trackedCanvas.tlcLocalPosition = new Vector3(-17.7141f, 17.7794f, 0f);
tlc.localScale = new Vector3(-0.1f, 0.1f, 0.1f);
for (int i = 0; i < tlc.childCount; i++)
{
Expand Down
36 changes: 36 additions & 0 deletions plugin/src/patch/main_game/ui/inspector_canvas_patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using TechtonicaVR.VRCamera.Patch;
using UnityEngine;

namespace TechtonicaVR.Patches.MainGame.UI;

class InspectorCanvasPatch : GameComponentPatch<InspectorUI>

{
protected override bool Apply(InspectorUI gameObject)
{
var tlc = gameObject.transform.GetChild(0);
if (tlc == null)
{
return false;
}

var sharedInspector = tlc.transform.GetChild(0);
if (sharedInspector == null)
{
return false;
}
sharedInspector.transform.localPosition = new Vector3(23.8832f, -21.9059f, 0);

var scannableInspector = tlc.gameObject.transform.GetChild(1).transform.GetChild(0);
if (scannableInspector == null)
{
return false;
}
scannableInspector.transform.localPosition = new Vector3(211.4383f, -22.6601f, 0);


TargetRaycastPatch.inspectorTlc = tlc;

return true;
}
}
4 changes: 3 additions & 1 deletion plugin/src/ui/hand_tracked_canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class HandTrackedCanvas : MonoBehaviour
public RectTransform rectTransform;
public Transform rectTransformOverride;

Check warning on line 14 in plugin/src/ui/hand_tracked_canvas.cs

View workflow job for this annotation

GitHub Actions / build

Field 'HandTrackedCanvas.rectTransformOverride' is never assigned to, and will always have its default value null
public Vector3 transformOverride = Vector3.zero;
public Vector3 tlcLocalPosition = Vector3.zero;

public bool noTransform = false;
public float showDistance;

Expand All @@ -33,7 +35,7 @@ private void Start()

canvas.gameObject.transform.parent = anchor.transform;
canvas.gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localPosition = Vector3.zero;
gameObject.transform.localPosition = tlcLocalPosition;
}

private void Update()
Expand Down

0 comments on commit b7ef8db

Please sign in to comment.