Files
pogo/Source/pogo/UI/PogoTitleScreenWidget.cpp
T

243 lines
7.5 KiB
C++

#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 "FileMediaSource.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);
}
UPogoTitleScreenWidget::UPogoTitleScreenWidget(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
SetIsFocusable(true);
}
void UPogoTitleScreenWidget::NativeOnInitialized()
{
Super::NativeOnInitialized();
MediaPlayer = NewObject<UMediaPlayer>(this, TEXT("TitleMediaPlayer"));
MediaTexture = NewObject<UMediaTexture>(this, TEXT("TitleMediaTexture"));
VideoSource = NewObject<UFileMediaSource>(this, TEXT("TitleVideoSource"));
if (MediaPlayer && MediaTexture && VideoSource)
{
MediaPlayer->PlayOnOpen = true;
MediaPlayer->SetLooping(true);
MediaTexture->AutoClear = true;
MediaTexture->ClearColor = PogoTitleScreen::Petroleum;
MediaTexture->SetMediaPlayer(MediaPlayer);
MediaTexture->UpdateResource();
VideoSource->SetFilePath(TEXT("./Movies/pogo.mp4"));
}
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));
SetDesiredFocusWidget(PlayButton);
}
void UPogoTitleScreenWidget::StartMedia()
{
if (!MediaPlayer || !VideoSource)
{
UE_LOG(LogTemp, Warning, TEXT("Title screen media player is unavailable"));
return;
}
MediaPlayer->SetLooping(true);
if (!MediaPlayer->OpenSource(VideoSource))
{
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);
}
}