diff --git a/Scripts/tests/test_title_screen.py b/Scripts/tests/test_title_screen.py new file mode 100644 index 0000000..40d82ab --- /dev/null +++ b/Scripts/tests/test_title_screen.py @@ -0,0 +1,58 @@ +from pathlib import Path +import unittest + +ROOT = Path(__file__).resolve().parents[2] + + +def path(relative): + return ROOT / relative + + +def read(relative): + source_path = path(relative) + return source_path.read_text(encoding="utf-8") if source_path.exists() else "" + + +class TitleScreenSourceTests(unittest.TestCase): + def test_media_modules_are_linked(self): + build = read("Source/pogo/pogo.Build.cs") + self.assertIn('"MediaAssets"', build) + self.assertIn('"SlateCore"', build) + + def test_controller_owns_ui_input_mode(self): + source = read("Source/pogo/UI/PogoTitleScreenPlayerController.cpp") + self.assertIn("CreateWidget", source) + self.assertIn("FInputModeUIOnly", source) + self.assertIn("bShowMouseCursor = true", source) + + def test_widget_uses_approved_media_and_layout(self): + source = read("Source/pogo/UI/PogoTitleScreenWidget.cpp") + self.assertIn('TEXT("./Movies/pogo.mp4")', source) + self.assertIn("SetLooping(true)", source) + self.assertIn("EStretch::ScaleToFill", source) + self.assertIn("FAnchors(0.5f, 1.0f)", source) + self.assertIn("0.65f", source) + + def test_play_is_idempotent_and_opens_gameplay(self): + source = read("Source/pogo/UI/PogoTitleScreenWidget.cpp") + self.assertIn("if (bTransitionStarted)", source) + self.assertIn("bTransitionStarted = true", source) + self.assertLess( + source.index("if (bTransitionStarted)"), + source.index("bTransitionStarted = true"), + ) + self.assertIn( + 'FName(TEXT("/Game/ThirdPerson/Lvl_ThirdPerson"))', source + ) + self.assertIn("EKeys::Enter", source) + self.assertIn("EKeys::SpaceBar", source) + + def test_failure_paths_do_not_disable_play(self): + source = read("Source/pogo/UI/PogoTitleScreenWidget.cpp") + self.assertIn("void UPogoTitleScreenWidget::RequestPlay", source) + before_request = source[: source.index("void UPogoTitleScreenWidget::RequestPlay")] + self.assertNotIn("PlayButton->SetIsEnabled(false)", before_request) + + +if __name__ == "__main__": + unittest.main() diff --git a/Source/pogo/UI/PogoTitleScreenGameMode.cpp b/Source/pogo/UI/PogoTitleScreenGameMode.cpp new file mode 100644 index 0000000..422800e --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenGameMode.cpp @@ -0,0 +1,11 @@ +#include "UI/PogoTitleScreenGameMode.h" + +#include "UI/PogoTitleScreenPlayerController.h" + +APogoTitleScreenGameMode::APogoTitleScreenGameMode() +{ + PlayerControllerClass = APogoTitleScreenPlayerController::StaticClass(); + DefaultPawnClass = nullptr; + HUDClass = nullptr; + bStartPlayersAsSpectators = true; +} diff --git a/Source/pogo/UI/PogoTitleScreenGameMode.h b/Source/pogo/UI/PogoTitleScreenGameMode.h new file mode 100644 index 0000000..ec836bd --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenGameMode.h @@ -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(); +}; diff --git a/Source/pogo/UI/PogoTitleScreenPlayerController.cpp b/Source/pogo/UI/PogoTitleScreenPlayerController.cpp new file mode 100644 index 0000000..88eda72 --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenPlayerController.cpp @@ -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(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(); +} diff --git a/Source/pogo/UI/PogoTitleScreenPlayerController.h b/Source/pogo/UI/PogoTitleScreenPlayerController.h new file mode 100644 index 0000000..9a4cdac --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenPlayerController.h @@ -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 TitleScreenWidget; +}; diff --git a/Source/pogo/UI/PogoTitleScreenWidget.cpp b/Source/pogo/UI/PogoTitleScreenWidget.cpp new file mode 100644 index 0000000..c9e775a --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenWidget.cpp @@ -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(this, TEXT("TitleMediaPlayer")); + MediaTexture = NewObject(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::StaticClass(), TEXT("RootOverlay")); + WidgetTree->RootWidget = RootOverlay; + + UScaleBox* BackgroundScale = WidgetTree->ConstructWidget(UScaleBox::StaticClass(), TEXT("BackgroundScale")); + BackgroundScale->SetStretch(EStretch::ScaleToFill); + BackgroundScale->SetStretchDirection(EStretchDirection::Both); + BackgroundScale->SetClipping(EWidgetClipping::ClipToBounds); + + BackgroundImage = WidgetTree->ConstructWidget(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::StaticClass(), TEXT("Foreground")); + UOverlaySlot* ForegroundSlot = RootOverlay->AddChildToOverlay(Foreground); + ForegroundSlot->SetHorizontalAlignment(HAlign_Fill); + ForegroundSlot->SetVerticalAlignment(VAlign_Fill); + + PlayButton = WidgetTree->ConstructWidget(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::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(); + 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(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); + } +} diff --git a/Source/pogo/UI/PogoTitleScreenWidget.h b/Source/pogo/UI/PogoTitleScreenWidget.h new file mode 100644 index 0000000..a3ec4a6 --- /dev/null +++ b/Source/pogo/UI/PogoTitleScreenWidget.h @@ -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 PlayButton; + + UPROPERTY(Transient) + TObjectPtr BackgroundImage; + + UPROPERTY(Transient) + TObjectPtr MediaPlayer; + + UPROPERTY(Transient) + TObjectPtr MediaTexture; + + UPROPERTY(Transient) + TObjectPtr MusicComponent; + + bool bTransitionStarted = false; +}; diff --git a/Source/pogo/pogo.Build.cs b/Source/pogo/pogo.Build.cs index 09909a1..c23595e 100644 --- a/Source/pogo/pogo.Build.cs +++ b/Source/pogo/pogo.Build.cs @@ -17,8 +17,10 @@ public class pogo : ModuleRules "AIModule", "StateTreeModule", "GameplayStateTreeModule", + "MediaAssets", "UMG", - "Slate" + "Slate", + "SlateCore" }); PrivateDependencyModuleNames.AddRange(new string[] { });