Commit 98030085 authored by Carl Findahl's avatar Carl Findahl 💬
Browse files

Create Pause Menu in C++ / BP

parent 1dcf9527
......@@ -73,3 +73,4 @@ PhysXTreeRebuildRate=10
DefaultBroadphaseSettings=(bUseMBPOnClient=False,bUseMBPOnServer=False,bUseMBPOuterBounds=False,MBPBounds=(Min=(X=0.000000,Y=0.000000,Z=0.000000),Max=(X=0.000000,Y=0.000000,Z=0.000000),IsValid=0),MBPOuterBounds=(Min=(X=0.000000,Y=0.000000,Z=0.000000),Max=(X=0.000000,Y=0.000000,Z=0.000000),IsValid=0),MBPNumSubdivs=2)
ChaosSettings=(DefaultThreadingModel=DedicatedThread,DedicatedThreadTickMode=VariableCappedWithTarget,DedicatedThreadBufferMode=Double)
......@@ -78,6 +78,9 @@ DoubleClickTime=0.200000
+ActionMappings=(ActionName="Interact",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=E)
+ActionMappings=(ActionName="UI_ToggleInventory",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=I)
+ActionMappings=(ActionName="Block",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=RightMouseButton)
+ActionMappings=(ActionName="PauseMenu",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Escape)
+ActionMappings=(ActionName="PauseMenu",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=Pause)
+ActionMappings=(ActionName="PauseMenu",bShift=False,bCtrl=False,bAlt=False,bCmd=False,Key=P)
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=W)
+AxisMappings=(AxisName="MoveForward",Scale=-1.000000,Key=S)
+AxisMappings=(AxisName="MoveForward",Scale=1.000000,Key=Up)
......
......@@ -99,11 +99,6 @@ void UVikingMainMenu::JoinGameHandler()
void UVikingMainMenu::ExitGameHandler()
{
// Get game instance
UVikingGameInstance* GameInstance = GetGameInstance<UVikingGameInstance>();
if (!ensure(GameInstance != nullptr)) return;
UE_LOG(LogTemp, Warning, TEXT("Exiting!"));
UKismetSystemLibrary::QuitGame(GetWorld(), nullptr, EQuitPreference::Quit, false);
}
......
// Carl, Tomas, Jon Gunnar, Leon - 2019
#include "VikingPauseMenu.h"
#include "Kismet/KismetSystemLibrary.h"
#include "GameFramework/PlayerController.h"
#include "VikingGameInstance.h"
#include "Engine/Engine.h"
#include "Button.h"
#include "Kismet/GameplayStatics.h"
bool UVikingPauseMenu::Initialize()
{
const bool bSuccess = Super::Initialize();
if (!bSuccess) { return bSuccess; }
// Set up bindings
ResumeButton->OnClicked.AddDynamic(this, &UVikingPauseMenu::HandleResumeGame);
MenuButton->OnClicked.AddDynamic(this, &UVikingPauseMenu::HandleMenu);
QuitButton->OnClicked.AddDynamic(this, &UVikingPauseMenu::HandleQuit);
// Change input mode of player to work with the Pause menu we just made
bIsFocusable = true;
APlayerController* Controller = GetOwningPlayer<APlayerController>();
if (!ensure(Controller != nullptr)) return false;
// UI Input Only
FInputModeUIOnly InputModeData{};
InputModeData.SetWidgetToFocus(TakeWidget());
InputModeData.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
// Perform operation on controller
Controller->SetInputMode(InputModeData);
Controller->bShowMouseCursor = true;
return true;
}
void UVikingPauseMenu::RemoveFromParent()
{
APlayerController* Controller = GetOwningPlayer<APlayerController>();
if (!ensure(Controller != nullptr)) return;
// Game Input Only
FInputModeGameOnly InputModeData{};
// Perform operation on controller
Controller->SetInputMode(InputModeData);
Controller->bShowMouseCursor = false;
Super::RemoveFromParent();
}
void UVikingPauseMenu::HandleResumeGame()
{
RemoveFromViewport();
}
void UVikingPauseMenu::HandleMenu()
{
// Get game instance
UVikingGameInstance* GameInstance = GetGameInstance<UVikingGameInstance>();
if (!ensure(GameInstance != nullptr)) return;
// Get engine if we are server, then browse to default map
if (GetOwningPlayer<APlayerController>()->HasAuthority())
{
UEngine* Engine = GameInstance->GetEngine();
if (!ensure(Engine != nullptr)) return;
Engine->BrowseToDefaultMap(*GameInstance->GetWorldContext());
}
// If client, then just open level locally
else
{
UGameplayStatics::OpenLevel(GetOwningPlayer<APlayerController>(), FName("MainMenu"));
}
}
void UVikingPauseMenu::HandleQuit()
{
UKismetSystemLibrary::QuitGame(GetWorld(), nullptr, EQuitPreference::Quit, false);
}
......@@ -6,9 +6,9 @@
#include "UObject/ConstructorHelpers.h"
#include "UI/VikingMainMenu.h"
UVikingGameInstance::UVikingGameInstance() {
UVikingGameInstance::UVikingGameInstance()
{
static ConstructorHelpers::FClassFinder<UUserWidget> MenuBPClass(TEXT("/Game/Viking/Blueprints/UI/WBP_MainMenu"));
if (MenuBPClass.Class == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Could not find Menu Class in C++ for GameInstance! Did you move or delete it?"));
......@@ -16,6 +16,15 @@ UVikingGameInstance::UVikingGameInstance() {
}
MenuClass = MenuBPClass.Class;
static ConstructorHelpers::FClassFinder<UUserWidget> PauseMenuBPClass(TEXT("/Game/Viking/Blueprints/UI/WBP_PauseMenu"));
if (PauseMenuBPClass.Class == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Could not find Menu Class in C++ for GameInstance! Did you move or delete it?"));
return;
}
PauseMenuClass = PauseMenuBPClass.Class;
}
void UVikingGameInstance::HostServer(bool bArenaMode)
......@@ -27,12 +36,12 @@ void UVikingGameInstance::HostServer(bool bArenaMode)
APlayerController* Controller = GetFirstLocalPlayerController();
if (!ensure(Controller != nullptr)) return;
// UI Input Only
// Game Input Only
FInputModeGameOnly InputModeData{};
// Perform operation on controller
Controller->SetInputMode(InputModeData);
Controller->bShowMouseCursor = true;
Controller->bShowMouseCursor = false;
if (bArenaMode)
{
......@@ -52,12 +61,12 @@ void UVikingGameInstance::JoinSever(const FString& hostIP) {
return;
}
// UI Input Only
// Game Input Only
FInputModeGameOnly InputModeData{};
// Perform operation on controller
Controller->SetInputMode(InputModeData);
Controller->bShowMouseCursor = true;
Controller->bShowMouseCursor = false;
Controller->ClientTravel(hostIP, ETravelType::TRAVEL_Absolute);
}
......@@ -70,3 +79,10 @@ void UVikingGameInstance::ShowMainMenu()
MainMenuWidget->ShowAndSetInputToUI();
}
void UVikingGameInstance::ShowPauseMenu()
{
UUserWidget* PauseMenuWidget = CreateWidget<UUserWidget>(this, PauseMenuClass);
if (!ensure(PauseMenuWidget != nullptr)) return;
PauseMenuWidget->AddToViewport(1);
}
// Carl, Tomas, Jon Gunnar, Leon - 2019
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "VikingPauseMenu.generated.h"
/**
* Pause (but not paused) Menu
*/
UCLASS()
class VIKING_API UVikingPauseMenu : public UUserWidget
{
GENERATED_BODY()
private:
/** Quit Button Binding */
UPROPERTY(meta = (BindWidget))
class UButton* ResumeButton;
/** Quit Button Binding */
UPROPERTY(meta = (BindWidget))
class UButton* MenuButton;
/** Quit Button Binding */
UPROPERTY(meta = (BindWidget))
class UButton* QuitButton;
public:
virtual bool Initialize() override;
virtual void RemoveFromParent() override;
private:
UFUNCTION()
void HandleResumeGame();
UFUNCTION()
void HandleMenu();
UFUNCTION()
void HandleQuit();
};
......@@ -17,6 +17,8 @@ class VIKING_API UVikingGameInstance : public UGameInstance
private:
TSubclassOf<class UUserWidget> MenuClass;
TSubclassOf<UUserWidget> PauseMenuClass;
public:
/** Function to host a default server on the first level, or an Arena game if the Arena flag is true */
UFUNCTION(Exec, BlueprintCallable, Category = "Server")
......@@ -29,4 +31,8 @@ public:
/** Show main menu */
UFUNCTION(Exec, BlueprintCallable, Category = "VikingGame")
void ShowMainMenu();
/** Show pause menu */
UFUNCTION(Exec, BlueprintCallable, Category = "VikingGame")
void ShowPauseMenu();
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment