From 13dcef668f890b152461fd541afa49c08f68f6c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20A?= Date: Sun, 19 May 2024 22:14:50 -0400 Subject: [PATCH] IGCSDOF: Go back to the original position after an EndSession. (#74) * (igcsdof) remove unnecessary buffer. * (igcsdof) fix: go back to the original positon. * Move the check of EndSession to UnityIGCSConnector class --- src/Cinematic/UnityIGCSConnector.cs | 30 +++++++++++++++++++++++------ src/UI/Panels/FreeCamPanel.cs | 1 - 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/Cinematic/UnityIGCSConnector.cs b/src/Cinematic/UnityIGCSConnector.cs index 791e55c..c6eda72 100644 --- a/src/Cinematic/UnityIGCSConnector.cs +++ b/src/Cinematic/UnityIGCSConnector.cs @@ -29,6 +29,9 @@ namespace CinematicUnityExplorer.Cinematic // Store the initial position when a session start in IGCSDof. Mono.CSharp.Tuple position = null; + // When we end a session, we need to make sure to go back to the position when the session ends. + private Mono.CSharp.Tuple endSessionPosition = null; + private readonly bool isValid = false; private bool _isActive = false; public bool IsActive => isValid && _isActive; @@ -42,21 +45,21 @@ namespace CinematicUnityExplorer.Cinematic private readonly Queue commands = new(); private IntPtr CameraStatus = IntPtr.Zero; - // In order to avoid allocations on every Update call, we create this buffer to allocate once - // and copy from here the CameraStatus (because Marshal.Copy requires a buffer, urgh). - private readonly byte[] CameraStatusBuffer = new byte[] { 0x0 }; public void UpdateFreecamStatus(bool enabled) { if (CameraStatus == IntPtr.Zero) return; - - CameraStatusBuffer[0] = enabled ? (byte)0x1 : (byte)0x0; - Marshal.Copy(CameraStatusBuffer, 0, CameraStatus, 1); + + Marshal.WriteByte(CameraStatus, enabled ? (byte)0x1 : (byte)0x0); } public void ExecuteCameraCommand(Camera cam) { var transform = cam.transform; + + // Check whether we should go back to the original position despite being active or not + this.ShouldMoveToOriginalPosition(transform); + if (!_isActive || position == null) { position = new(transform.position, transform.rotation); @@ -89,8 +92,23 @@ namespace CinematicUnityExplorer.Cinematic _isActive = true; } + // At the EndSession, since we have a queue system, we have to have a special check when the session ends and + // then move the camera back to the original position, because the queue gets cleaned as soon as the session + // ends. + public void ShouldMoveToOriginalPosition(Transform transform) + { + if (!isValid) return; + if (endSessionPosition == null) return; + + transform.position = endSessionPosition.Item1; + transform.rotation = endSessionPosition.Item2; + + endSessionPosition = null; + } + private void EndSession() { + endSessionPosition = position; position = null; _isActive = false; diff --git a/src/UI/Panels/FreeCamPanel.cs b/src/UI/Panels/FreeCamPanel.cs index 2d802b1..28142a7 100644 --- a/src/UI/Panels/FreeCamPanel.cs +++ b/src/UI/Panels/FreeCamPanel.cs @@ -685,7 +685,6 @@ namespace UnityExplorer.UI.Panels FreeCamPanel.EndFreecam(); return; } - Transform transform = FreeCamPanel.ourCamera.transform; if (!FreeCamPanel.blockFreecamMovementToggle.isOn && !FreeCamPanel.cameraPathMover.playingPath && FreeCamPanel.connector?.IsActive != true) {