126 lines
3.4 KiB
C++
126 lines
3.4 KiB
C++
// © 2025 Naked People Team. All Rights Reserved.
|
|
|
|
|
|
#include "MainMenuWidget.h"
|
|
|
|
#include "ConfirmModalWidget.h"
|
|
#include "Components/Button.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Kismet/KismetSystemLibrary.h"
|
|
#include "NakedDesire/SaveGame/SaveSubsystem.h"
|
|
#include "Widgets/CommonActivatableWidgetContainer.h"
|
|
|
|
#define LOCTEXT_NAMESPACE "MainMenu"
|
|
|
|
UMainMenuWidget::UMainMenuWidget()
|
|
{
|
|
// Sensible default for the slice; override per-asset on WBP_MainMenu.
|
|
GameplayMap = TSoftObjectPtr<UWorld>(FSoftObjectPath(TEXT("/Game/Test/Maps/L_TestCity.L_TestCity")));
|
|
}
|
|
|
|
void UMainMenuWidget::NativeOnActivated()
|
|
{
|
|
Super::NativeOnActivated();
|
|
|
|
// Continue is only available when a save exists in the active slot.
|
|
bool bHasSave = false;
|
|
if (const UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this))
|
|
{
|
|
if (const USaveSubsystem* SaveSubsystem = GameInstance->GetSubsystem<USaveSubsystem>())
|
|
bHasSave = SaveSubsystem->DoesSaveExist();
|
|
}
|
|
ContinueButton->SetIsEnabled(bHasSave);
|
|
|
|
ContinueButton->OnClicked.AddUniqueDynamic(this, &UMainMenuWidget::OnContinueClicked);
|
|
NewGameButton->OnClicked.AddUniqueDynamic(this, &UMainMenuWidget::OnNewGameClicked);
|
|
SettingsButton->OnClicked.AddUniqueDynamic(this, &UMainMenuWidget::OnSettingsClicked);
|
|
CreditsButton->OnClicked.AddUniqueDynamic(this, &UMainMenuWidget::OnCreditsClicked);
|
|
QuitButton->OnClicked.AddUniqueDynamic(this, &UMainMenuWidget::OnQuitClicked);
|
|
}
|
|
|
|
void UMainMenuWidget::OnContinueClicked()
|
|
{
|
|
OpenGameplayLevel();
|
|
}
|
|
|
|
void UMainMenuWidget::OnNewGameClicked()
|
|
{
|
|
if (!ConfirmModalClass || !ModalStack)
|
|
{
|
|
HandleNewGameConfirmed();
|
|
return;
|
|
}
|
|
|
|
UConfirmModalWidget* Modal = ModalStack->AddWidget<UConfirmModalWidget>(ConfirmModalClass);
|
|
if (!Modal)
|
|
return;
|
|
|
|
Modal->Setup(
|
|
LOCTEXT("NewGameTitle", "New Game"),
|
|
LOCTEXT("NewGameBody", "Start a new game? Any existing progress will be lost."));
|
|
Modal->OnConfirmed.AddUObject(this, &UMainMenuWidget::HandleNewGameConfirmed);
|
|
}
|
|
|
|
void UMainMenuWidget::OnSettingsClicked()
|
|
{
|
|
if (SettingsWidgetClass && ModalStack)
|
|
ModalStack->AddWidget(SettingsWidgetClass);
|
|
}
|
|
|
|
void UMainMenuWidget::OnCreditsClicked()
|
|
{
|
|
if (CreditsWidgetClass && ModalStack)
|
|
ModalStack->AddWidget(CreditsWidgetClass);
|
|
}
|
|
|
|
void UMainMenuWidget::OnQuitClicked()
|
|
{
|
|
if (!ConfirmModalClass || !ModalStack)
|
|
{
|
|
HandleQuitConfirmed();
|
|
return;
|
|
}
|
|
|
|
UConfirmModalWidget* Modal = ModalStack->AddWidget<UConfirmModalWidget>(ConfirmModalClass);
|
|
if (!Modal)
|
|
return;
|
|
|
|
Modal->Setup(
|
|
LOCTEXT("QuitTitle", "Quit Game"),
|
|
LOCTEXT("QuitBody", "Quit to desktop?"));
|
|
Modal->OnConfirmed.AddUObject(this, &UMainMenuWidget::HandleQuitConfirmed);
|
|
}
|
|
|
|
void UMainMenuWidget::HandleNewGameConfirmed()
|
|
{
|
|
if (UGameInstance* GameInstance = UGameplayStatics::GetGameInstance(this))
|
|
{
|
|
if (USaveSubsystem* SaveSubsystem = GameInstance->GetSubsystem<USaveSubsystem>())
|
|
SaveSubsystem->StartNewGame();
|
|
}
|
|
|
|
OpenGameplayLevel();
|
|
}
|
|
|
|
void UMainMenuWidget::HandleQuitConfirmed()
|
|
{
|
|
UKismetSystemLibrary::QuitGame(this, GetOwningPlayer(), EQuitPreference::Quit, false);
|
|
}
|
|
|
|
void UMainMenuWidget::OpenGameplayLevel()
|
|
{
|
|
if (GameplayMap.IsNull())
|
|
return;
|
|
|
|
if (APlayerController* PC = UGameplayStatics::GetPlayerController(GetWorld(), 0))
|
|
{
|
|
PC->SetShowMouseCursor(false);
|
|
|
|
const FInputModeGameOnly InputMode;
|
|
PC->SetInputMode(InputMode);
|
|
}
|
|
|
|
UGameplayStatics::OpenLevelBySoftObjectPtr(this, GameplayMap);
|
|
}
|
|
|
|
#undef LOCTEXT_NAMESPACE |