107 lines
2.8 KiB
C++
107 lines
2.8 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "ReturnToMainMenu.h"
|
|
#include "MultiplayerSessionsSubsystem.h"
|
|
#include "Components/Button.h"
|
|
#include "GameFramework/GameModeBase.h"
|
|
|
|
void UReturnToMainMenu::MenuSetup()
|
|
{
|
|
AddToViewport();
|
|
SetVisibility(ESlateVisibility::Visible);
|
|
bIsFocusable = true;
|
|
|
|
if (const UWorld* World = GetWorld())
|
|
{
|
|
PlayerController = PlayerController == nullptr ? World->GetFirstPlayerController() : PlayerController;
|
|
if (PlayerController)
|
|
{
|
|
FInputModeGameAndUI InputModeData;
|
|
InputModeData.SetWidgetToFocus(TakeWidget());
|
|
PlayerController->SetInputMode(InputModeData);
|
|
PlayerController->SetShowMouseCursor(true);
|
|
}
|
|
}
|
|
|
|
if (ReturnButton && !ReturnButton->OnClicked.IsBound())
|
|
{
|
|
ReturnButton->OnClicked.AddDynamic(this, &UReturnToMainMenu::ReturnButtonClicked);
|
|
}
|
|
|
|
if (const UGameInstance* GameInstance = GetGameInstance())
|
|
{
|
|
MultiplayerSessionsSubsystem = GameInstance->GetSubsystem<UMultiplayerSessionsSubsystem>();
|
|
if (MultiplayerSessionsSubsystem)
|
|
{
|
|
MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.AddDynamic(this, &UReturnToMainMenu::OnDestroySessionComplete);
|
|
}
|
|
}
|
|
}
|
|
|
|
bool UReturnToMainMenu::Initialize()
|
|
{
|
|
if (!Super::Initialize())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void UReturnToMainMenu::MenuTearDown()
|
|
{
|
|
RemoveFromParent();
|
|
|
|
if (const UWorld* World = GetWorld())
|
|
{
|
|
PlayerController = PlayerController == nullptr ? World->GetFirstPlayerController() : PlayerController;
|
|
if (PlayerController)
|
|
{
|
|
const FInputModeGameOnly InputModeData;
|
|
PlayerController->SetInputMode(InputModeData);
|
|
PlayerController->SetShowMouseCursor(false);
|
|
}
|
|
}
|
|
if (ReturnButton && ReturnButton->OnClicked.IsBound())
|
|
{
|
|
ReturnButton->OnClicked.RemoveDynamic(this, &UReturnToMainMenu::ReturnButtonClicked);
|
|
}
|
|
if (MultiplayerSessionsSubsystem && MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.IsBound())
|
|
{
|
|
MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.RemoveDynamic(this, &UReturnToMainMenu::OnDestroySessionComplete);
|
|
}
|
|
}
|
|
|
|
void UReturnToMainMenu::ReturnButtonClicked()
|
|
{
|
|
if (MultiplayerSessionsSubsystem)
|
|
{
|
|
ReturnButton->SetIsEnabled(false);
|
|
MultiplayerSessionsSubsystem->DestroySession();
|
|
}
|
|
}
|
|
|
|
void UReturnToMainMenu::OnDestroySessionComplete(bool bWasSuccessful)
|
|
{
|
|
if (!bWasSuccessful)
|
|
{
|
|
ReturnButton->SetIsEnabled(true);
|
|
return;
|
|
}
|
|
if (const UWorld* World = GetWorld())
|
|
{
|
|
if (AGameModeBase* GameMode = World->GetAuthGameMode<AGameModeBase>())
|
|
{
|
|
GameMode->ReturnToMainMenuHost();
|
|
}
|
|
else
|
|
{
|
|
PlayerController = PlayerController == nullptr ? World->GetFirstPlayerController() : PlayerController;
|
|
if (PlayerController)
|
|
{
|
|
PlayerController->ClientReturnToMainMenuWithTextReason(FText());
|
|
}
|
|
}
|
|
}
|
|
} |