Fix typewriter text size calculation being wrong just after first construction

This commit is contained in:
Steve Streeting 2023-04-11 12:24:07 +01:00
parent 35f8bdf35e
commit 0c92bcbaa8
2 changed files with 40 additions and 7 deletions

View File

@ -110,15 +110,37 @@ void UTypewriterTextWidget::PlayLine(const FText& InLine, float Speed)
bHasFinishedPlaying = false;
LineText->SetText(FText());
if (bFirstPlayLine)
{
// Delay the very first PlayLine after construction, CalculateWrappedString is not reliable until a couple
// of UI geometry updates. At first the geometry is 0, then it's just wrong, and then finally it settles.
FTimerHandle TempHandle;
FTimerDelegate DelayDelegate;
DelayDelegate.BindUObject(this, &ThisClass::StartPlayLine);
TimerManager.SetTimer(TempHandle, DelayDelegate, 0.2f, false);
}
else
{
StartPlayLine();
}
}
}
void UTypewriterTextWidget::StartPlayLine()
{
CalculateWrappedString();
FTimerDelegate Delegate;
Delegate.BindUObject(this, &ThisClass::PlayNextLetter);
FTimerManager& TimerManager = GetWorld()->GetTimerManager();
TimerManager.SetTimer(LetterTimer, Delegate, LetterPlayTime/CurrentPlaySpeed, true);
SetVisibility(ESlateVisibility::SelfHitTestInvisible);
}
bFirstPlayLine = false;
}
void UTypewriterTextWidget::SkipToLineEnd()
@ -137,6 +159,13 @@ void UTypewriterTextWidget::SkipToLineEnd()
OnLineFinishedPlaying();
}
void UTypewriterTextWidget::NativeConstruct()
{
Super::NativeConstruct();
bFirstPlayLine = true;
}
void UTypewriterTextWidget::PlayNextLetter()
{
// Incorporate pauses as a multiple of play timer (may not be exact but close enough)

View File

@ -109,6 +109,8 @@ public:
const FString& GetCurrentRunName() const { return CurrentRunName; }
protected:
virtual void NativeConstruct() override;
/// Called when on or more letters are added, if subclasses want to override
UFUNCTION(BlueprintImplementableEvent, Category = "Typewriter")
void OnPlayLetter();
@ -128,6 +130,7 @@ private:
void CalculateWrappedString();
FString CalculateSegments(FString* OutCurrentRunName);
void StartPlayLine();
UPROPERTY()
FText CurrentLine;
@ -159,4 +162,5 @@ private:
FTimerHandle LetterTimer;
float CurrentPlaySpeed = 1;
float PauseTime = 0;
bool bFirstPlayLine = true;
};