Extracted functions

This commit is contained in:
lumpn 2023-04-20 13:15:09 +08:00
parent e41fbbeb89
commit 4805f49ccb
2 changed files with 52 additions and 45 deletions

View File

@ -120,54 +120,19 @@ void UTypewriterTextWidget::PlayNextLinePart(float Speed)
CalculateWrappedString(RemainingLinePart); CalculateWrappedString(RemainingLinePart);
// TODO Jonas: Promote maxLines to variable. Might want maxLines = 1 for text before choices. // TODO Jonas: Promote MaxLines to variable. Might want MaxLines = 1 for text before choices.
int maxLines = 3; int MaxLines = 3;
if (NumberOfLines > maxLines) if (NumberOfLines > MaxLines)
{ {
int numLetters = 0; int MaxLength = CalculateMaxLength(MaxLines);
int currentLine = 1; int TerminatorIndex = FindLastTerminator(RemainingLinePart, MaxLength);
for (int i = 0; i < Segments.Num(); i++) int Length = TerminatorIndex + 1;
{ const FString& FirstLinePart = RemainingLinePart.Left(Length);
const auto& segment = Segments[i];
// TODO Jonas: Mark line break segments in CalculateWrappedString instead.
if (segment.Text.Equals(FString(TEXT("\n"))))
{
currentLine++;
if (currentLine > maxLines)
{
break;
}
}
else
{
numLetters += segment.Text.Len();
}
}
// TODO Jonas: Find lesser terminators like commas or spaces if no sentence terminator can be found. CalculateWrappedString(FirstLinePart);
int lastTerminator = RemainingLinePart.FindLastCharByPredicate(IsSentenceTerminator, numLetters);
if (lastTerminator != INDEX_NONE)
{
int count = lastTerminator + 1;
const FString& shortenedString = RemainingLinePart.Left(count);
// TODO Jonas: Clean up RemainingLinePart.RightChopInline(Length);
CurrentRunName = ""; bHasMoreLineParts = true;
CurrentLetterIndex = 0;
CachedLetterIndex = 0;
CurrentSegmentIndex = 0;
MaxLetterIndex = 0;
NumberOfLines = 0;
CombinedTextHeight = 0;
CurrentPlaySpeed = Speed;
Segments.Empty();
CachedSegmentText.Empty();
CalculateWrappedString(shortenedString);
RemainingLinePart.RightChopInline(count);
bHasMoreLineParts = true;
}
} }
FTimerDelegate Delegate; FTimerDelegate Delegate;
@ -243,6 +208,43 @@ bool UTypewriterTextWidget::IsSentenceTerminator(TCHAR Letter)
return Letter == '.' || Letter == '!' || Letter == '?'; return Letter == '.' || Letter == '!' || Letter == '?';
} }
int UTypewriterTextWidget::FindLastTerminator(const FString& CurrentLineString, int Count)
{
// TODO Jonas: Find lesser terminators like commas or spaces if no sentence terminator can be found.
int TerminatorIndex = CurrentLineString.FindLastCharByPredicate(IsSentenceTerminator, Count);
if (TerminatorIndex != INDEX_NONE)
{
return TerminatorIndex;
}
return Count;
}
int UTypewriterTextWidget::CalculateMaxLength(int MaxNumberOfLines)
{
int MaxLength = 0;
int CurrentNumberOfLines = 1;
for (int i = 0; i < Segments.Num(); i++)
{
const FTypewriterTextSegment& Segment = Segments[i];
// TODO Jonas: Mark line break segments as such in CalculateWrappedString instead.
if (Segment.Text.Equals(FString(TEXT("\n"))))
{
CurrentNumberOfLines++;
if (CurrentNumberOfLines > MaxNumberOfLines)
{
break;
}
}
else
{
MaxLength += Segment.Text.Len();
}
}
return MaxLength;
}
void UTypewriterTextWidget::CalculateWrappedString(const FString& CurrentLineString) void UTypewriterTextWidget::CalculateWrappedString(const FString& CurrentLineString)
{ {
// Rich Text views give you: // Rich Text views give you:
@ -252,6 +254,9 @@ void UTypewriterTextWidget::CalculateWrappedString(const FString& CurrentLineStr
// - The newlines we add are the only newlines in the output so that's the number of lines // - The newlines we add are the only newlines in the output so that's the number of lines
// If we've got here, that means the text isn't empty so 1 line at least // If we've got here, that means the text isn't empty so 1 line at least
NumberOfLines = 1; NumberOfLines = 1;
MaxLetterIndex = 0;
CombinedTextHeight = 0;
Segments.Empty();
if (IsValid(LineText) && LineText->GetTextLayout().IsValid()) if (IsValid(LineText) && LineText->GetTextLayout().IsValid())
{ {
TSharedPtr<FSlateTextLayout> Layout = LineText->GetTextLayout(); TSharedPtr<FSlateTextLayout> Layout = LineText->GetTextLayout();

View File

@ -131,7 +131,9 @@ protected:
private: private:
void PlayNextLetter(); void PlayNextLetter();
static bool IsSentenceTerminator(TCHAR Letter); static bool IsSentenceTerminator(TCHAR Letter);
static int FindLastTerminator(const FString& CurrentLineString, int Count);
int CalculateMaxLength(int MaxNumberOfLines);
void CalculateWrappedString(const FString& CurrentLineString); void CalculateWrappedString(const FString& CurrentLineString);
FString CalculateSegments(FString* OutCurrentRunName); FString CalculateSegments(FString* OutCurrentRunName);