From 5a3661c4d2ad7b89a6001d400e5a73261c6d1ab3 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 22 Nov 2026 02:31:51 +0000 Subject: [PATCH] fix: zoom the VR menu with the right stick again - down pulls it closer, up pushes it away (1-8 m, kept in Gothic.ini) The experimental branch had it (c2d9f34c9, VRMenuBillboard); main replaced that placement and the zoom was gone, so the save list was hard to read with the menu 2.5 m away. Now VRPlaceInFrontOfCamera on Gothic's menu (MenuHandler) zooms with the turning hand's stick (right, mirrored: left) and keeps the distance in Gothic.ini [GOTHIC_VR_ACCESSIBILITY] menuDistance. Teleporting is off while the menu is open, the same stick would aim it. The cinema screen, which uses the same placement, doesn't zoom. --- .../Adapters/UI/VRPlaceInFrontOfCamera.cs | 111 ++++++++++++++++++ Assets/Gothic-VR/Scripts/VRConstants.cs | 4 + 2 files changed, 115 insertions(+) diff --git a/Assets/Gothic-VR/Scripts/Adapters/UI/VRPlaceInFrontOfCamera.cs b/Assets/Gothic-VR/Scripts/Adapters/UI/VRPlaceInFrontOfCamera.cs index d16667f46..c5aa7f9fb 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/UI/VRPlaceInFrontOfCamera.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/UI/VRPlaceInFrontOfCamera.cs @@ -1,7 +1,12 @@ +using Gothic.Core.Adapters.UI.Menus; +using Gothic.Core.Logging; +using Gothic.Core.Services.Config; using Gothic.Core.Services.Context; +using Gothic.VR.Interaction.Ports; using Gothic.VR.Services.Context; using Reflex.Attributes; using UnityEngine; +using Logger = Gothic.Core.Logging.Logger; namespace Gothic.VR.Adapters.UI { @@ -9,27 +14,60 @@ namespace Gothic.VR.Adapters.UI /// Places this object once in front of the VR camera, upright at eye height and facing it, when enabled (e.g. main /// menu, video cinema screen). It doesn't follow head turns or movement afterwards. Only a teleport places it anew. /// For UI that follows the head, use VRFollowCamera. + /// On Gothic's menu (MenuHandler), the turning hand's stick zooms it: down pulls it closer, up pushes it away + /// (1-8 m) - far away the save list was hard to read. Teleporting is off meanwhile, the same stick would aim it. + /// The distance is kept in Gothic.ini (menuDistance). /// public class VRPlaceInFrontOfCamera : MonoBehaviour { // A bigger jump of the camera is a teleport (e.g. the hero is moved to the chapter start) - face it anew. private const float _teleportDistance = 5f; + private const float _minZoomDistance = 1f; + private const float _maxZoomDistance = 8f; + // Meters per second at a fully pushed stick. + private const float _zoomSpeed = 2.5f; + private const float _zoomStickDeadZone = 0.2f; + [Tooltip("Distance in front of the camera in meters.")] [SerializeField] private float _distance = 1.5f; [Inject] private readonly ContextInteractionService _contextInteractionService; + [Inject] private readonly IVrInput _vrInput; + [Inject] private readonly ConfigService _configService; private Transform _cameraTransform; private Vector3 _lastCameraPosition; + private Vector3 _placedForward = Vector3.forward; private bool _isPlaced; + private bool _isZoomable; + private bool _isZoomDirty; + private IVrRig _blockedTeleportRig; + private bool _wasTeleportEnabled; + + private void Awake() + { + // Only Gothic's menu zooms - not the video cinema's screen. + _isZoomable = GetComponent() != null; + } private void OnEnable() { _isPlaced = false; + if (_isZoomable) + ReadZoomDistance(); TryPlace(); } + private void OnDisable() + { + if (!_isZoomable) + return; + + PersistZoomDistance(); + RestoreTeleport(); + } + /// /// For objects created at runtime (no injection, no serialized values). Places it in front of the camera. /// @@ -43,6 +81,9 @@ public void Configure(Transform cameraTransform, float distance) private void LateUpdate() { + if (_isZoomable) + UpdateZoom(); + // The camera might not exist (or we're not injected yet) when enabled. Place it once we have it. if (_isPlaced && _cameraTransform != null && Vector3.Distance(_cameraTransform.position, _lastCameraPosition) <= _teleportDistance) @@ -62,9 +103,79 @@ private void TryPlace() forward = Vector3.forward; _lastCameraPosition = _cameraTransform.position; + _placedForward = forward; transform.SetPositionAndRotation(_lastCameraPosition + forward * _distance, Quaternion.LookRotation(forward, Vector3.up)); _isPlaced = true; + + if (_isZoomable) + BlockTeleport(); + } + + private void UpdateZoom() + { + if (_vrInput == null || !_isPlaced) + return; + + var stick = _vrInput.GetThumbstick(_vrInput.ControlLayout.TurnSide); + if (Mathf.Abs(stick.y) < _zoomStickDeadZone) + { + // The stick is released: keep what was set. + PersistZoomDistance(); + return; + } + + // Up pushes the menu away, down pulls it closer. + _distance = Mathf.Clamp(_distance + stick.y * _zoomSpeed * Time.unscaledDeltaTime, _minZoomDistance, + _maxZoomDistance); + transform.position = _lastCameraPosition + _placedForward * _distance; + _isZoomDirty = true; + } + + private void ReadZoomDistance() + { + if (_configService?.Gothic == null) + return; + + _distance = Mathf.Clamp(_configService.Gothic.GetFloat(VRConstants.IniNames.MenuDistance, _distance), + _minZoomDistance, _maxZoomDistance); + } + + private void PersistZoomDistance() + { + if (!_isZoomDirty) + return; + + _isZoomDirty = false; + _configService?.Gothic?.SetFloat(VRConstants.IniSectionAccessibility, VRConstants.IniNames.MenuDistance, + _distance); + Logger.Log($"[Menu] Zoomed to {_distance:0.0} m.", LogCat.VR); + } + + /// + /// The zooming stick is the teleporting one - while the menu is open it only zooms. Restored when it closes. + /// + private void BlockTeleport() + { + if (_blockedTeleportRig != null) + return; + + var rig = _contextInteractionService?.GetImpl()?.Rig; + if (rig == null) + return; + + _blockedTeleportRig = rig; + _wasTeleportEnabled = rig.TeleportEnabled; + rig.TeleportEnabled = false; + } + + private void RestoreTeleport() + { + if (_blockedTeleportRig == null) + return; + + _blockedTeleportRig.TeleportEnabled = _wasTeleportEnabled; + _blockedTeleportRig = null; } private bool TryResolveCamera() diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs index f102d56a8..64a0b4fc7 100644 --- a/Assets/Gothic-VR/Scripts/VRConstants.cs +++ b/Assets/Gothic-VR/Scripts/VRConstants.cs @@ -19,6 +19,10 @@ public static class IniNames // the right stick while the ring is open, or in the menu). public const string FavoritesRingZoom = "favoritesRingZoom"; + // The menu's distance in m (VRPlaceInFrontOfCamera on Gothic's menu), set with the turning hand's + // stick while the menu is open. + public const string MenuDistance = "menuDistance"; + // VrLedgeClimbMode: 0 = pull yourself up, 1 = fade onto the ledge. public const string LedgeClimbMode = "ledgeClimbMode";