blaster/Source/Blaster/HUD/ReturnToMainMenu.cpp

131 lines
3.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "ReturnToMainMenu.h"
#include "MultiplayerSessionsSubsystem.h"
#include "Blaster/Character/BlasterCharacter.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()
{
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()
{
if (MultiplayerSessionsSubsystem)
{
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());
}
}
}
}