RealtimeStyleTransferRuntime/Plugins/GameSubtitles/Source/Private/Widgets/SSubtitleDisplay.cpp

83 lines
2.2 KiB
C++
Raw Normal View History

2022-05-23 18:41:30 +00:00
// Copyright Epic Games, Inc. All Rights Reserved.
#include "Widgets/SSubtitleDisplay.h"
2022-09-13 07:18:28 +00:00
#include "Delegates/Delegate.h"
#include "Framework/Text/TextLayout.h"
#include "Internationalization/Text.h"
2022-05-23 18:41:30 +00:00
#include "Kismet/GameplayStatics.h"
2022-09-13 07:18:28 +00:00
#include "Layout/Children.h"
#include "Layout/Margin.h"
#include "Layout/Visibility.h"
#include "SubtitleManager.h"
#include "Widgets/Layout/SBorder.h"
#include "Widgets/Text/SRichTextBlock.h"
struct FSlateBrush;
2022-05-23 18:41:30 +00:00
void SSubtitleDisplay::Construct(const FArguments& InArgs)
{
if (!InArgs._ManualSubtitles.Get())
{
FSubtitleManagerSetSubtitleText& OnSetSubtitleText = FSubtitleManager::GetSubtitleManager()->OnSetSubtitleText();
OnSetSubtitleText.AddSP(this, &SSubtitleDisplay::HandleSubtitleChanged);
}
ChildSlot
[
SAssignNew(Background, SBorder)
.Visibility(EVisibility::Collapsed)
.Padding(FMargin(7.0, 5.0))
[
SAssignNew(TextDisplay, SRichTextBlock)
.TextStyle(InArgs._TextStyle)
.Justification(ETextJustify::Center)
.WrapTextAt(InArgs._WrapTextAt)
]
];
}
SSubtitleDisplay::~SSubtitleDisplay()
{
FSubtitleManager::GetSubtitleManager()->OnSetSubtitleText().RemoveAll(this);
}
void SSubtitleDisplay::SetTextStyle(const FTextBlockStyle& InTextStyle)
{
TextDisplay->SetTextStyle(InTextStyle);
}
void SSubtitleDisplay::SetBackgroundBrush(const FSlateBrush* InSlateBrush)
{
Background->SetBorderImage(InSlateBrush);
}
void SSubtitleDisplay::SetCurrentSubtitleText(const FText& InSubtitleText)
{
Background->SetVisibility(InSubtitleText.IsEmpty() ? EVisibility::Collapsed : EVisibility::HitTestInvisible);
TextDisplay->SetText(InSubtitleText);
}
bool SSubtitleDisplay::HasSubtitles() const
{
return !TextDisplay->GetText().IsEmpty();
}
void SSubtitleDisplay::SetWrapTextAt(const TAttribute<float>& InWrapTextAt)
{
TextDisplay->SetWrapTextAt(InWrapTextAt);
}
void SSubtitleDisplay::HandleSubtitleChanged(const FText& InSubtitleText)
{
if (UGameplayStatics::AreSubtitlesEnabled())
{
Background->SetVisibility(InSubtitleText.IsEmpty() ? EVisibility::Collapsed : EVisibility::HitTestInvisible);
TextDisplay->SetText(InSubtitleText);
}
else
{
Background->SetVisibility(EVisibility::Collapsed);
}
}