feat: add native title screen flow
This commit is contained in:
1 parent
17567726c4
commit
98e852c980
8 files changed
+423
-1
No files matched your search
@@ -0,0 +1,11 @@
|
||||
#include "UI/PogoTitleScreenGameMode.h"
|
||||
|
||||
#include "UI/PogoTitleScreenPlayerController.h"
|
||||
|
||||
APogoTitleScreenGameMode::APogoTitleScreenGameMode()
|
||||
{
|
||||
PlayerControllerClass = APogoTitleScreenPlayerController::StaticClass();
|
||||
DefaultPawnClass = nullptr;
|
||||
HUDClass = nullptr;
|
||||
bStartPlayersAsSpectators = true;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/GameModeBase.h"
|
||||
#include "PogoTitleScreenGameMode.generated.h"
|
||||
|
||||
UCLASS()
|
||||
class POGO_API APogoTitleScreenGameMode : public AGameModeBase
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
APogoTitleScreenGameMode();
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "UI/PogoTitleScreenPlayerController.h"
|
||||
|
||||
#include "Blueprint/UserWidget.h"
|
||||
#include "UI/PogoTitleScreenWidget.h"
|
||||
|
||||
void APogoTitleScreenPlayerController::BeginPlay()
|
||||
{
|
||||
Super::BeginPlay();
|
||||
|
||||
if (!IsLocalPlayerController())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TitleScreenWidget = CreateWidget<UPogoTitleScreenWidget>(this, UPogoTitleScreenWidget::StaticClass());
|
||||
if (!TitleScreenWidget)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
TitleScreenWidget->AddToViewport(0);
|
||||
bShowMouseCursor = true;
|
||||
|
||||
FInputModeUIOnly InputMode;
|
||||
InputMode.SetWidgetToFocus(TitleScreenWidget->TakeWidget());
|
||||
InputMode.SetLockMouseToViewportBehavior(EMouseLockMode::DoNotLock);
|
||||
SetInputMode(InputMode);
|
||||
TitleScreenWidget->SetKeyboardFocus();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "PogoTitleScreenPlayerController.generated.h"
|
||||
|
||||
class UPogoTitleScreenWidget;
|
||||
|
||||
UCLASS()
|
||||
class POGO_API APogoTitleScreenPlayerController : public APlayerController
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
protected:
|
||||
virtual void BeginPlay() override;
|
||||
|
||||
private:
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UPogoTitleScreenWidget> TitleScreenWidget;
|
||||
};
|
||||
@@ -0,0 +1,232 @@
|
||||
#include "UI/PogoTitleScreenWidget.h"
|
||||
|
||||
#include "Blueprint/WidgetTree.h"
|
||||
#include "Brushes/SlateRoundedBoxBrush.h"
|
||||
#include "Components/AudioComponent.h"
|
||||
#include "Components/Button.h"
|
||||
#include "Components/CanvasPanel.h"
|
||||
#include "Components/CanvasPanelSlot.h"
|
||||
#include "Components/HorizontalBox.h"
|
||||
#include "Components/HorizontalBoxSlot.h"
|
||||
#include "Components/Image.h"
|
||||
#include "Components/Overlay.h"
|
||||
#include "Components/OverlaySlot.h"
|
||||
#include "Components/ScaleBox.h"
|
||||
#include "Components/TextBlock.h"
|
||||
#include "Input/Reply.h"
|
||||
#include "InputCoreTypes.h"
|
||||
#include "Kismet/GameplayStatics.h"
|
||||
#include "MediaPlayer.h"
|
||||
#include "MediaTexture.h"
|
||||
#include "Sound/SoundBase.h"
|
||||
#include "Styling/CoreStyle.h"
|
||||
|
||||
namespace PogoTitleScreen
|
||||
{
|
||||
const FLinearColor Petroleum(0.025f, 0.14f, 0.14f, 0.88f);
|
||||
const FLinearColor PetroleumHover(0.04f, 0.22f, 0.21f, 0.96f);
|
||||
const FLinearColor PetroleumPressed(0.02f, 0.10f, 0.10f, 0.98f);
|
||||
const FLinearColor Ivory(0.98f, 0.91f, 0.72f, 1.0f);
|
||||
const FLinearColor Vermilion(0.80f, 0.14f, 0.06f, 1.0f);
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::NativeOnInitialized()
|
||||
{
|
||||
Super::NativeOnInitialized();
|
||||
|
||||
MediaPlayer = NewObject<UMediaPlayer>(this, TEXT("TitleMediaPlayer"));
|
||||
MediaTexture = NewObject<UMediaTexture>(this, TEXT("TitleMediaTexture"));
|
||||
if (MediaPlayer && MediaTexture)
|
||||
{
|
||||
MediaPlayer->PlayOnOpen = true;
|
||||
MediaPlayer->SetLooping(true);
|
||||
MediaTexture->AutoClear = true;
|
||||
MediaTexture->ClearColor = PogoTitleScreen::Petroleum;
|
||||
MediaTexture->SetMediaPlayer(MediaPlayer);
|
||||
MediaTexture->UpdateResource();
|
||||
}
|
||||
|
||||
BuildLayout();
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::NativeConstruct()
|
||||
{
|
||||
Super::NativeConstruct();
|
||||
|
||||
StartMedia();
|
||||
StartMusic();
|
||||
SetKeyboardFocus();
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::NativeDestruct()
|
||||
{
|
||||
StopPlayback();
|
||||
Super::NativeDestruct();
|
||||
}
|
||||
|
||||
FReply UPogoTitleScreenWidget::NativeOnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent)
|
||||
{
|
||||
const FKey Key = InKeyEvent.GetKey();
|
||||
if (Key == EKeys::Enter || Key == EKeys::SpaceBar)
|
||||
{
|
||||
RequestPlay();
|
||||
return FReply::Handled();
|
||||
}
|
||||
|
||||
return Super::NativeOnKeyDown(InGeometry, InKeyEvent);
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::BuildLayout()
|
||||
{
|
||||
if (!WidgetTree)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UOverlay* RootOverlay = WidgetTree->ConstructWidget<UOverlay>(UOverlay::StaticClass(), TEXT("RootOverlay"));
|
||||
WidgetTree->RootWidget = RootOverlay;
|
||||
|
||||
UScaleBox* BackgroundScale = WidgetTree->ConstructWidget<UScaleBox>(UScaleBox::StaticClass(), TEXT("BackgroundScale"));
|
||||
BackgroundScale->SetStretch(EStretch::ScaleToFill);
|
||||
BackgroundScale->SetStretchDirection(EStretchDirection::Both);
|
||||
BackgroundScale->SetClipping(EWidgetClipping::ClipToBounds);
|
||||
|
||||
BackgroundImage = WidgetTree->ConstructWidget<UImage>(UImage::StaticClass(), TEXT("BackgroundImage"));
|
||||
FSlateBrush BackgroundBrush;
|
||||
BackgroundBrush.DrawAs = ESlateBrushDrawType::Image;
|
||||
BackgroundBrush.ImageSize = FVector2D(1280.0, 720.0);
|
||||
BackgroundBrush.SetResourceObject(MediaTexture);
|
||||
BackgroundImage->SetBrush(BackgroundBrush);
|
||||
BackgroundScale->SetContent(BackgroundImage);
|
||||
|
||||
UOverlaySlot* BackgroundSlot = RootOverlay->AddChildToOverlay(BackgroundScale);
|
||||
BackgroundSlot->SetHorizontalAlignment(HAlign_Fill);
|
||||
BackgroundSlot->SetVerticalAlignment(VAlign_Fill);
|
||||
|
||||
UCanvasPanel* Foreground = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), TEXT("Foreground"));
|
||||
UOverlaySlot* ForegroundSlot = RootOverlay->AddChildToOverlay(Foreground);
|
||||
ForegroundSlot->SetHorizontalAlignment(HAlign_Fill);
|
||||
ForegroundSlot->SetVerticalAlignment(VAlign_Fill);
|
||||
|
||||
PlayButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), TEXT("PlayButton"));
|
||||
|
||||
FButtonStyle ButtonStyle;
|
||||
ButtonStyle.SetNormal(FSlateRoundedBoxBrush(PogoTitleScreen::Petroleum, 10.0f, PogoTitleScreen::Ivory, 1.0f));
|
||||
ButtonStyle.SetHovered(FSlateRoundedBoxBrush(PogoTitleScreen::PetroleumHover, 10.0f, PogoTitleScreen::Ivory, 1.0f));
|
||||
ButtonStyle.SetPressed(FSlateRoundedBoxBrush(PogoTitleScreen::PetroleumPressed, 10.0f, PogoTitleScreen::Vermilion, 1.0f));
|
||||
ButtonStyle.SetDisabled(FSlateRoundedBoxBrush(PogoTitleScreen::PetroleumPressed, 10.0f, PogoTitleScreen::Ivory, 1.0f));
|
||||
ButtonStyle.SetNormalPadding(FMargin(24.0f, 12.0f));
|
||||
ButtonStyle.SetPressedPadding(FMargin(24.0f, 13.0f, 24.0f, 11.0f));
|
||||
PlayButton->SetStyle(ButtonStyle);
|
||||
|
||||
UHorizontalBox* LabelRow = WidgetTree->ConstructWidget<UHorizontalBox>(UHorizontalBox::StaticClass(), TEXT("PlayLabelRow"));
|
||||
const FSlateFontInfo LabelFont = FCoreStyle::GetDefaultFontStyle(TEXT("Bold"), 20);
|
||||
|
||||
auto AddLabel = [this, LabelRow, &LabelFont](const TCHAR* Text, const FLinearColor& Color, const FMargin& Padding)
|
||||
{
|
||||
UTextBlock* Label = WidgetTree->ConstructWidget<UTextBlock>();
|
||||
Label->SetText(FText::FromString(Text));
|
||||
Label->SetColorAndOpacity(FSlateColor(Color));
|
||||
Label->SetFont(LabelFont);
|
||||
UHorizontalBoxSlot* LabelSlot = LabelRow->AddChildToHorizontalBox(Label);
|
||||
LabelSlot->SetPadding(Padding);
|
||||
LabelSlot->SetVerticalAlignment(VAlign_Center);
|
||||
};
|
||||
|
||||
AddLabel(TEXT("◆"), PogoTitleScreen::Vermilion, FMargin(0.0f, 0.0f, 12.0f, 0.0f));
|
||||
AddLabel(TEXT("JOUER"), PogoTitleScreen::Ivory, FMargin(0.0f));
|
||||
AddLabel(TEXT("◆"), PogoTitleScreen::Vermilion, FMargin(12.0f, 0.0f, 0.0f, 0.0f));
|
||||
PlayButton->SetContent(LabelRow);
|
||||
|
||||
PlayButton->OnClicked.AddDynamic(this, &UPogoTitleScreenWidget::HandlePlayClicked);
|
||||
PlayButton->OnHovered.AddDynamic(this, &UPogoTitleScreenWidget::HandlePlayHovered);
|
||||
PlayButton->OnUnhovered.AddDynamic(this, &UPogoTitleScreenWidget::HandlePlayUnhovered);
|
||||
|
||||
UCanvasPanelSlot* PlaySlot = Foreground->AddChildToCanvas(PlayButton);
|
||||
PlaySlot->SetAnchors(FAnchors(0.5f, 1.0f));
|
||||
PlaySlot->SetAlignment(FVector2D(0.5f, 1.0f));
|
||||
PlaySlot->SetSize(FVector2D(220.0f, 64.0f));
|
||||
PlaySlot->SetPosition(FVector2D(0.0f, -36.0f));
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::StartMedia()
|
||||
{
|
||||
if (!MediaPlayer)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("Title screen media player is unavailable"));
|
||||
return;
|
||||
}
|
||||
|
||||
MediaPlayer->SetLooping(true);
|
||||
if (!MediaPlayer->OpenUrl(TEXT("./Movies/pogo.mp4")))
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("Title screen video could not be opened"));
|
||||
}
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::StartMusic()
|
||||
{
|
||||
USoundBase* Music = LoadObject<USoundBase>(nullptr, TEXT("/Game/Pogo/Audio/S_PogoTitleMusic.S_PogoTitleMusic"));
|
||||
if (!Music)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("Title screen music is unavailable"));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicComponent = UGameplayStatics::SpawnSound2D(this, Music, 0.65f, 1.0f, 0.0f, nullptr, false, false);
|
||||
if (!MusicComponent)
|
||||
{
|
||||
UE_LOG(LogTemp, Warning, TEXT("Title screen music could not be started"));
|
||||
}
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::StopPlayback()
|
||||
{
|
||||
if (MediaPlayer)
|
||||
{
|
||||
MediaPlayer->Close();
|
||||
}
|
||||
|
||||
if (MusicComponent)
|
||||
{
|
||||
MusicComponent->Stop();
|
||||
MusicComponent = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::RequestPlay()
|
||||
{
|
||||
if (bTransitionStarted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bTransitionStarted = true;
|
||||
if (PlayButton)
|
||||
{
|
||||
PlayButton->SetIsEnabled(false);
|
||||
}
|
||||
StopPlayback();
|
||||
UGameplayStatics::OpenLevel(this, FName(TEXT("/Game/ThirdPerson/Lvl_ThirdPerson")));
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::HandlePlayClicked()
|
||||
{
|
||||
RequestPlay();
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::HandlePlayHovered()
|
||||
{
|
||||
if (PlayButton)
|
||||
{
|
||||
PlayButton->SetRenderTranslation(FVector2D(0.0f, -4.0f));
|
||||
}
|
||||
}
|
||||
|
||||
void UPogoTitleScreenWidget::HandlePlayUnhovered()
|
||||
{
|
||||
if (PlayButton)
|
||||
{
|
||||
PlayButton->SetRenderTranslation(FVector2D::ZeroVector);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreMinimal.h"
|
||||
#include "Blueprint/UserWidget.h"
|
||||
#include "PogoTitleScreenWidget.generated.h"
|
||||
|
||||
class UAudioComponent;
|
||||
class UButton;
|
||||
class UImage;
|
||||
class UMediaPlayer;
|
||||
class UMediaTexture;
|
||||
|
||||
UCLASS()
|
||||
class POGO_API UPogoTitleScreenWidget : public UUserWidget
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
protected:
|
||||
virtual void NativeOnInitialized() override;
|
||||
virtual void NativeConstruct() override;
|
||||
virtual void NativeDestruct() override;
|
||||
virtual FReply NativeOnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent) override;
|
||||
|
||||
private:
|
||||
void BuildLayout();
|
||||
void StartMedia();
|
||||
void StartMusic();
|
||||
void StopPlayback();
|
||||
void RequestPlay();
|
||||
|
||||
UFUNCTION()
|
||||
void HandlePlayClicked();
|
||||
|
||||
UFUNCTION()
|
||||
void HandlePlayHovered();
|
||||
|
||||
UFUNCTION()
|
||||
void HandlePlayUnhovered();
|
||||
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UButton> PlayButton;
|
||||
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UImage> BackgroundImage;
|
||||
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UMediaPlayer> MediaPlayer;
|
||||
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UMediaTexture> MediaTexture;
|
||||
|
||||
UPROPERTY(Transient)
|
||||
TObjectPtr<UAudioComponent> MusicComponent;
|
||||
|
||||
bool bTransitionStarted = false;
|
||||
};
|
||||
@@ -17,8 +17,10 @@ public class pogo : ModuleRules
|
||||
"AIModule",
|
||||
"StateTreeModule",
|
||||
"GameplayStateTreeModule",
|
||||
"MediaAssets",
|
||||
"UMG",
|
||||
"Slate"
|
||||
"Slate",
|
||||
"SlateCore"
|
||||
});
|
||||
|
||||
PrivateDependencyModuleNames.AddRange(new string[] { });
|
||||
|
||||
Reference in new issue
Block a user