2022-05-28 12:46:58 +00:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "ReturnToMainMenu.h"
|
|
|
|
#include "MultiplayerSessionsSubsystem.h"
|
2022-05-28 14:26:06 +00:00
|
|
|
#include "Blaster/Character/BlasterCharacter.h"
|
2022-05-28 12:46:58 +00:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:29:15 +00:00
|
|
|
if (ReturnButton && !ReturnButton->OnClicked.IsBound())
|
2022-05-28 12:46:58 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 13:29:15 +00:00
|
|
|
if (ReturnButton && ReturnButton->OnClicked.IsBound())
|
|
|
|
{
|
|
|
|
ReturnButton->OnClicked.RemoveDynamic(this, &UReturnToMainMenu::ReturnButtonClicked);
|
|
|
|
}
|
|
|
|
if (MultiplayerSessionsSubsystem && MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.IsBound())
|
|
|
|
{
|
|
|
|
MultiplayerSessionsSubsystem->MultiplayerOnDestroySessionComplete.RemoveDynamic(this, &UReturnToMainMenu::OnDestroySessionComplete);
|
|
|
|
}
|
2022-05-28 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UReturnToMainMenu::ReturnButtonClicked()
|
2022-05-28 14:26:06 +00:00
|
|
|
{
|
|
|
|
ReturnButton->SetIsEnabled(false);
|
|
|
|
|
|
|
|
if (const UWorld* World = GetWorld())
|
|
|
|
{
|
|
|
|
if (const APlayerController* FirstPlayerController = World->GetFirstPlayerController())
|
|
|
|
{
|
|
|
|
if (ABlasterCharacter* BlasterCharacter = Cast<ABlasterCharacter>(FirstPlayerController->GetPawn()))
|
|
|
|
{
|
|
|
|
BlasterCharacter->ServerLeaveGame();
|
|
|
|
if (!BlasterCharacter->OnLeftGame.IsBound())
|
|
|
|
{
|
|
|
|
BlasterCharacter->OnLeftGame.AddDynamic(this, &UReturnToMainMenu::OnPlayerLeftGame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReturnButton->SetIsEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UReturnToMainMenu::OnPlayerLeftGame()
|
2022-05-28 12:46:58 +00:00
|
|
|
{
|
|
|
|
if (MultiplayerSessionsSubsystem)
|
|
|
|
{
|
|
|
|
MultiplayerSessionsSubsystem->DestroySession();
|
2022-05-28 14:26:06 +00:00
|
|
|
}
|
2022-05-28 12:46:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|