Lock mouse to screen when showing Main Menu UI or Options Menu UI

This commit is contained in:
baz 2023-12-05 22:41:58 +00:00
parent 1647ab3ed1
commit 27120d731a
2 changed files with 27 additions and 0 deletions

View File

@ -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()

View File

@ -5,7 +5,9 @@
#include <Kismet/KismetSystemLibrary.h>
#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)