VrWorldInput.cs

This component manages VR input specifically for controlling UI scrolling. It detects which scrollable panel is hovered and translates right hand joystick movement into scrolling offset.

using Sandbox;
using Sandbox.UI;
using Sandbox.VR;
using System;

public sealed class VrWorldInput : Component
{
	[RequireComponent] private Sandbox.WorldInput WorldInput { get; set; }

	/// <summary>UI pixels scrolled per second at full right-stick deflection.</summary>
	[Property] public float ScrollSpeed { get; set; } = 600f;

	private const float HitSphereRadius = 0.5f;
	private const float ScrollDeadZone = 0.2f;
	private Panel _scrollTarget;
	private Vector2 _scrollPosition;

	/// <summary>Reserves the right stick for UI while either pointer hovers a scrollable panel.</summary>
	public static bool IsHoveringScrollable( Scene scene ) => FindHoveredScrollable( scene ).IsValid();

	private static Panel FindHoveredScrollable( Scene scene )
	{
		Panel leftTarget = null;
		foreach ( var input in scene.GetAll<VrWorldInput>() )
		{
			if ( !input.Active || input.IsProxy || !input.WorldInput.IsValid() || !input.WorldInput.Active )
				continue;

			// A hovered label or row belongs to its nearest scrollable ancestor.
			for ( var panel = input.WorldInput.Hovered; panel.IsValid() && panel.IsVisible; panel = panel.Parent )
			{
				var axes = GetScrollAxes( panel );
				if ( axes.x == 0f && axes.y == 0f )
					continue;

				if ( input.WorldInput.VRHandSource == VRHand.HandSources.Right )
					return panel;

				leftTarget = panel;
				break;
			}
		}

		return leftTarget;
	}

	private static Vector2 GetScrollAxes( Panel panel )
	{
		var style = panel.ComputedStyle;
		return new Vector2(
			(style?.OverflowX ?? style?.Overflow) == OverflowMode.Scroll && panel.ScrollSize.x > 0f ? 1f : 0f,
			(style?.OverflowY ?? style?.Overflow) == OverflowMode.Scroll && panel.ScrollSize.y > 0f ? 1f : 0f
		);
	}

	protected override void OnPreRender()
	{
		// Only the right-hand component applies the shared stick input, once per frame.
		if ( !Game.IsRunningInVR || IsProxy || !WorldInput.IsValid() || !WorldInput.Active
			|| WorldInput.VRHandSource != VRHand.HandSources.Right )
		{
			_scrollTarget = null;
			return;
		}

		var target = FindHoveredScrollable( Scene );
		if ( !target.IsValid() )
		{
			_scrollTarget = null;
			return;
		}

		if ( _scrollTarget != target || target.IsDragScrolling )
		{
			_scrollTarget = target;
			_scrollPosition = target.ScrollOffset;
		}

		if ( target.IsDragScrolling )
			return;

		var stick = Input.VR.RightHand.Joystick.Value;
		var axes = GetScrollAxes( target );
		var direction = new Vector2( ScrollAxis( stick.x ) * axes.x, -ScrollAxis( stick.y ) * axes.y );
		_scrollPosition += direction * ScrollSpeed * Time.Delta * target.ScaleToScreen;

		// Set an absolute position after input updates, avoiding duplicate native wheel scrolling.
		target.ScrollTo( _scrollPosition );
		_scrollPosition = target.ScrollOffset;
	}

	private static float ScrollAxis( float value ) =>
		MathF.Abs( value ) <= ScrollDeadZone ? 0f : MathF.Sign( value ) * (MathF.Abs( value ) - ScrollDeadZone) / (1f - ScrollDeadZone);

	protected override void OnDisabled() => _scrollTarget = null;

	protected override void OnUpdate()
	{
		var hovered = WorldInput.Hovered;
		if ( !hovered.IsValid() )
			return;

		var origin = WorldPosition;
		var ray = new Ray( origin, WorldRotation.Forward );

		if ( !TryGetHitPoint( hovered, ray, out var hitPoint ) )
			return;

		Gizmo.Draw.Color = Color.Cyan;
		Gizmo.Draw.Line( origin, hitPoint );
		Gizmo.Draw.SolidSphere( hitPoint, HitSphereRadius );
	}

	/// <summary>
	/// Nested hovered panels have no 3D transform; hit-test the root, then fall back to the panel plane.
	/// </summary>
	private static bool TryGetHitPoint( Sandbox.UI.Panel panel, Ray ray, out Vector3 hitPoint )
	{
		hitPoint = default;

		var root = panel.FindRootPanel();
		if ( root.IsValid() && root.RayToLocalPosition( ray, out _, out var distance ) && distance > 0f )
		{
			hitPoint = ray.Position + ray.Forward * distance;
			return true;
		}

		var go = panel.GameObject;
		if ( !go.IsValid() )
			return false;

		// LookAtCamera panels face the viewer; use a plane through the panel toward the ray origin.
		var planePos = go.WorldPosition;
		var planeNormal = (ray.Position - planePos).Normal;
		var denom = Vector3.Dot( ray.Forward, planeNormal );
		if ( denom.AlmostEqual( 0f ) )
			return false;

		var t = Vector3.Dot( planePos - ray.Position, planeNormal ) / denom;
		if ( t < 0f )
			return false;

		hitPoint = ray.Position + ray.Forward * t;
		return true;
	}
}