Skip to content

Commit

Permalink
Maroon-100 First Person Mode locking mouse cursor (#567)
Browse files Browse the repository at this point in the history
* Main Menu.pc: Remove first person player prefab and instead add just a Camera

* First person handler lock mouse

* Menus should now handle unlocking mouse cursor

* MainMenu scene: fix merge regression, use camera instead of first person player

* WebGL now listens to pointerlockchange event

* Fix to prevent unwanted pause

* Fix PC build not working
  • Loading branch information
FlorianGlawogger authored Feb 19, 2025
1 parent 3b7e056 commit 5b1c267
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 89 deletions.
14 changes: 14 additions & 0 deletions unity/Assets/Maroon/GlobalEntities/WebGlReceiver/WebGlReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class WebGlReceiver : MonoBehaviour, GlobalEntity

public WebGlDataEvent OnIncomingData = new WebGlDataEvent();

public UnityEvent OnPauseRequest = new UnityEvent();

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Properties, Getters and Setters
public string MostRecentData { get; private set; }
Expand Down Expand Up @@ -72,5 +74,17 @@ public void GetDataFromJavaScript(string data)
MostRecentData = data;
OnIncomingData.Invoke(data);
}


/// <summary>
/// Called from Javascript code when Escape is pressed.
/// Useful because Input.GetKeyDown(KeyCode.Escape) might not always be caught,
/// because browser unlocks mouse cursor when pressing Escape while cursor is locked
/// </summary>
public void PauseRequest()
{
Debug.Log("Unity received a PauseRequest from Javascript");
OnPauseRequest?.Invoke();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ private void Start()
{
m_PlayerCharacterController = GetComponent<ModeFirstPerson>();

/*
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
*/
}

private void LateUpdate()
Expand All @@ -55,7 +53,6 @@ private void Update()

public bool CanProcessInput()
{
return true; // TODO
return Cursor.lockState == CursorLockMode.Locked;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
"references": [
"GUID:f9b0aa2a96663864693b204ee0977e65",
"GUID:833d205176b3e444a810dbf875342968",
"GUID:30817c1a0e6d646d99c048fc403f5979",
"GUID:e720aa64e3f58fb4880566a322584340",
"GUID:325984b52e4128546bc7558552f8b1d2",
"GUID:72872094b21c16e48b631b2224833d49",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:7bdbd1a0d606bb444b8111edbc9d16cb"
"GUID:56f15dd651d5d3a44bbfc9ca89629921"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
35 changes: 31 additions & 4 deletions unity/Assets/Maroon/reusableGui/Menu/PrefabsBase/scrMenu.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
using UnityEngine;
#if UNITY_WEBGL
// For WebGlReceiver
using Maroon.GlobalEntities;
#endif
using UnityEngine;

public class scrMenu : MonoBehaviour
{
// #################################################################################################################
// Members

private CursorLockMode cursorLockModeBeforeMenu = CursorLockMode.None;
private bool cursorVisibleBeforeMenu = true;

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Open/Close

Expand Down Expand Up @@ -52,6 +59,13 @@ private void Start()
{
this.OpenMenu();
}

#if UNITY_WEBGL
if (this.EnableEscKey)
{
WebGlReceiver.Instance.OnPauseRequest.AddListener(() => OpenMenu());
}
#endif
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand All @@ -75,19 +89,32 @@ public void Update()

public void OpenMenu()
{
if (IsOpen)
return;

// Save cursor state and ensure cursor is useable
cursorLockModeBeforeMenu = Cursor.lockState;
cursorVisibleBeforeMenu = Cursor.visible;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;

this.Canvas.SetActive(true);
this.IsOpen = true;
}

public void CloseMenu()
{
this.Canvas.SetActive(false);
this.IsOpen = false;

if(this.RemoveExtraColumnsOnClose)
if (this.RemoveExtraColumnsOnClose)
{
this.RemoveAllMenuColumnsButFirst();
}

// Restore cursor state
Cursor.visible = cursorVisibleBeforeMenu;
Cursor.lockState = cursorLockModeBeforeMenu;
cursorLockModeBeforeMenu = CursorLockMode.None;
cursorVisibleBeforeMenu = true;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ private void OnClickMainMenu()
{
SceneManager.Instance.LoadSceneRequest(this.targetMainMenuSceneVR);
}

else
{
SceneManager.Instance.LoadSceneRequest(this.targetMainMenuScenePC);
}
this.Menu.CloseMenu();

Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}

private void OnClickResume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public class scrInfoSignEnterScene : MonoBehaviour
public void EnterScene()
{
Debug.Log("Enter Scene : " + this.targetScene);

if (!(PlatformManager.Instance.CurrentPlatformIsVR))
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}

Maroon.GlobalEntities.SceneManager.Instance.LoadSceneRequest(this.targetScene);
}

Expand Down
Loading

0 comments on commit 5b1c267

Please sign in to comment.