From 27120d731a3da649dc3a49f04b5699c8f0f49eeb Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 5 Dec 2023 22:41:58 +0000 Subject: [PATCH] Lock mouse to screen when showing Main Menu UI or Options Menu UI --- Source/Nakatomi/MainMenuUIWidget.cpp | 17 +++++++++++++++++ Source/Nakatomi/OptionsUIWidget.cpp | 10 ++++++++++ 2 files changed, 27 insertions(+) diff --git a/Source/Nakatomi/MainMenuUIWidget.cpp b/Source/Nakatomi/MainMenuUIWidget.cpp index 8aaabf6..28c6d39 100644 --- a/Source/Nakatomi/MainMenuUIWidget.cpp +++ b/Source/Nakatomi/MainMenuUIWidget.cpp @@ -3,6 +3,7 @@ #include "MainMenuUIWidget.h" +#include "Blueprint/WidgetBlueprintLibrary.h" #include "Kismet/GameplayStatics.h" void UMainMenuUIWidget::NativeConstruct() @@ -28,6 +29,14 @@ void UMainMenuUIWidget::NativeConstruct() { QuitButton->OnClicked.AddUniqueDynamic(this, &UMainMenuUIWidget::QuitButtonOnClicked); } + + if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) + { + UWidgetBlueprintLibrary::SetInputMode_UIOnlyEx(PlayerController, this, EMouseLockMode::LockAlways); + PlayerController->bShowMouseCursor = true; + } + + } void UMainMenuUIWidget::NewGameButtonOnClicked() @@ -37,6 +46,14 @@ void UMainMenuUIWidget::NewGameButtonOnClicked() { UGameplayStatics::OpenLevelBySoftObjectPtr(GetWorld(), NewGameLevel); } + + if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) + { + PlayerController->bShowMouseCursor = false; + UWidgetBlueprintLibrary::SetInputMode_GameOnly(PlayerController); + } + + SetIsFocusable(false); } void UMainMenuUIWidget::LoadGameButtonOnClicked() diff --git a/Source/Nakatomi/OptionsUIWidget.cpp b/Source/Nakatomi/OptionsUIWidget.cpp index 18a5cf1..c0e84ca 100644 --- a/Source/Nakatomi/OptionsUIWidget.cpp +++ b/Source/Nakatomi/OptionsUIWidget.cpp @@ -5,7 +5,9 @@ #include +#include "Blueprint/WidgetBlueprintLibrary.h" #include "GameFramework/GameUserSettings.h" +#include "Kismet/GameplayStatics.h" void UOptionsUIWidget::NativeConstruct() { @@ -69,6 +71,14 @@ void UOptionsUIWidget::NativeConstruct() { ResetToDefaultsButton->OnClicked.AddUniqueDynamic(this, &UOptionsUIWidget::ResetToDefaultsButtonOnClicked); } + + if (APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0)) + { + UWidgetBlueprintLibrary::SetInputMode_UIOnlyEx(PlayerController, this, EMouseLockMode::LockAlways); + PlayerController->bShowMouseCursor = true; + } + + SetIsFocusable(true); } void UOptionsUIWidget::SetReturnScreen(UUserWidget* userWidget)