diff --git a/Content/Blueprints/HUD/WBP_ReturnToMainMenu.uasset b/Content/Blueprints/HUD/WBP_ReturnToMainMenu.uasset new file mode 100644 index 0000000..ba38ea8 Binary files /dev/null and b/Content/Blueprints/HUD/WBP_ReturnToMainMenu.uasset differ diff --git a/Source/Blaster/Blaster.Build.cs b/Source/Blaster/Blaster.Build.cs index 79aa606..28c004b 100644 --- a/Source/Blaster/Blaster.Build.cs +++ b/Source/Blaster/Blaster.Build.cs @@ -8,7 +8,7 @@ public class Blaster : ModuleRules { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; - PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Niagara" }); + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "Niagara", "MultiplayerSessions", "OnlineSubsystem", "OnlineSubsystemSteam", "OnlineSubsystemEOS" }); PrivateDependencyModuleNames.AddRange(new string[] { }); diff --git a/Source/Blaster/HUD/ReturnToMainMenu.cpp b/Source/Blaster/HUD/ReturnToMainMenu.cpp new file mode 100644 index 0000000..415ab5e --- /dev/null +++ b/Source/Blaster/HUD/ReturnToMainMenu.cpp @@ -0,0 +1,99 @@ +// 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.AddDynamic(this, &UReturnToMainMenu::ReturnButtonClicked); + } + + if (const UGameInstance* GameInstance = GetGameInstance()) + { + MultiplayerSessionsSubsystem = GameInstance->GetSubsystem(); + 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); + } + } +} + +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()) + { + GameMode->ReturnToMainMenuHost(); + } + else + { + PlayerController = PlayerController == nullptr ? World->GetFirstPlayerController() : PlayerController; + if (PlayerController) + { + PlayerController->ClientReturnToMainMenuWithTextReason(FText()); + } + } + } +} \ No newline at end of file diff --git a/Source/Blaster/HUD/ReturnToMainMenu.h b/Source/Blaster/HUD/ReturnToMainMenu.h new file mode 100644 index 0000000..d1601c1 --- /dev/null +++ b/Source/Blaster/HUD/ReturnToMainMenu.h @@ -0,0 +1,38 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Blueprint/UserWidget.h" +#include "ReturnToMainMenu.generated.h" + +/** + * + */ +UCLASS() +class BLASTER_API UReturnToMainMenu : public UUserWidget +{ + GENERATED_BODY() + +public: + void MenuSetup(); + void MenuTearDown(); + +protected: + virtual bool Initialize() override; + UFUNCTION() + void OnDestroySessionComplete(bool bWasSuccessful); +private: + + UPROPERTY(meta = (BindWidget)) + class UButton* ReturnButton; + + UFUNCTION() + void ReturnButtonClicked(); + + UPROPERTY() + class UMultiplayerSessionsSubsystem* MultiplayerSessionsSubsystem; + + UPROPERTY() + class APlayerController* PlayerController; +};