-
Notifications
You must be signed in to change notification settings - Fork 9
/
DialogueBox.cpp
261 lines (213 loc) · 6.5 KB
/
DialogueBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) Sam Bloomberg
#include "DialogueBox.h"
#include "Engine/Font.h"
#include "Styling/SlateStyle.h"
#include "Widgets/Text/SRichTextBlock.h"
#include "TimerManager.h"
TSharedRef<SWidget> UDialogueTextBlock::RebuildWidget()
{
// Copied from URichTextBlock::RebuildWidget
UpdateStyleData();
TArray< TSharedRef< class ITextDecorator > > CreatedDecorators;
CreateDecorators(CreatedDecorators);
TextMarshaller = FRichTextLayoutMarshaller::Create(CreateMarkupParser(), CreateMarkupWriter(), CreatedDecorators, StyleInstance.Get());
MyRichTextBlock =
SNew(SRichTextBlock)
.TextStyle(bOverrideDefaultStyle ? &DefaultTextStyleOverride : &DefaultTextStyle)
.Marshaller(TextMarshaller)
.CreateSlateTextLayout(
FCreateSlateTextLayout::CreateWeakLambda(this, [this] (SWidget* InOwner, const FTextBlockStyle& InDefaultTextStyle) mutable
{
TextLayout = FSlateTextLayout::Create(InOwner, InDefaultTextStyle);
return StaticCastSharedPtr<FSlateTextLayout>(TextLayout).ToSharedRef();
}));
return MyRichTextBlock.ToSharedRef();
}
UDialogueBox::UDialogueBox(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
bHasFinishedPlaying = true;
}
void UDialogueBox::PlayLine(const FText& InLine)
{
check(GetWorld());
FTimerManager& TimerManager = GetWorld()->GetTimerManager();
TimerManager.ClearTimer(LetterTimer);
CurrentLine = InLine;
CurrentLetterIndex = 0;
CachedLetterIndex = 0;
CurrentSegmentIndex = 0;
MaxLetterIndex = 0;
Segments.Empty();
CachedSegmentText.Empty();
if (CurrentLine.IsEmpty())
{
if (IsValid(LineText))
{
LineText->SetText(FText::GetEmpty());
}
bHasFinishedPlaying = true;
OnLineFinishedPlaying();
SetVisibility(ESlateVisibility::Hidden);
}
else
{
if (IsValid(LineText))
{
LineText->SetText(FText::GetEmpty());
}
bHasFinishedPlaying = false;
FTimerDelegate Delegate;
Delegate.BindUObject(this, &ThisClass::PlayNextLetter);
TimerManager.SetTimer(LetterTimer, Delegate, LetterPlayTime, true);
SetVisibility(ESlateVisibility::SelfHitTestInvisible);
}
}
void UDialogueBox::SkipToLineEnd()
{
FTimerManager& TimerManager = GetWorld()->GetTimerManager();
TimerManager.ClearTimer(LetterTimer);
CurrentLetterIndex = MaxLetterIndex - 1;
if (IsValid(LineText))
{
LineText->SetText(FText::FromString(CalculateSegments()));
}
bHasFinishedPlaying = true;
OnLineFinishedPlaying();
}
void UDialogueBox::PlayNextLetter()
{
if (Segments.IsEmpty())
{
CalculateWrappedString();
}
FString WrappedString = CalculateSegments();
// TODO: How do we keep indexing of text i18n-friendly?
if (CurrentLetterIndex < MaxLetterIndex)
{
if (IsValid(LineText))
{
LineText->SetText(FText::FromString(WrappedString));
}
OnPlayLetter();
++CurrentLetterIndex;
}
else
{
if (IsValid(LineText))
{
LineText->SetText(FText::FromString(CalculateSegments()));
}
FTimerManager& TimerManager = GetWorld()->GetTimerManager();
TimerManager.ClearTimer(LetterTimer);
FTimerDelegate Delegate;
Delegate.BindUObject(this, &ThisClass::SkipToLineEnd);
TimerManager.SetTimer(LetterTimer, Delegate, EndHoldTime, false);
}
}
// TODO: Need to recalculate this + CalculateSegments when the text box gets resized.
void UDialogueBox::CalculateWrappedString()
{
if (IsValid(LineText) && LineText->GetTextLayout().IsValid())
{
TSharedPtr<FSlateTextLayout> Layout = LineText->GetTextLayout();
TSharedPtr<FRichTextLayoutMarshaller> Marshaller = LineText->GetTextMarshaller();
const FGeometry& TextBoxGeometry = LineText->GetCachedGeometry();
const FVector2D TextBoxSize = TextBoxGeometry.GetLocalSize();
Layout->SetWrappingWidth(TextBoxSize.X);
Marshaller->SetText(CurrentLine.ToString(), *Layout.Get());
Layout->UpdateIfNeeded();
bool bHasWrittenText = false;
for (const FTextLayout::FLineView& View: Layout->GetLineViews())
{
const FTextLayout::FLineModel& Model = Layout->GetLineModels()[View.ModelIndex];
for (TSharedRef<ILayoutBlock> Block : View.Blocks)
{
TSharedRef<IRun> Run = Block->GetRun();
FDialogueTextSegment Segment;
Run->AppendTextTo(Segment.Text, Block->GetTextRange());
// HACK: For some reason image decorators (and possibly other decorators that don't
// have actual text inside them) result in the run containing a zero width space instead of
// nothing. This messes up our checks for whether the text is empty or not, which doesn't
// have an effect on image decorators but might cause issues for other custom ones.
if (Segment.Text.Len() == 1 && Segment.Text[0] == 0x200B)
{
Segment.Text.Empty();
}
Segment.RunInfo = Run->GetRunInfo();
Segments.Add(Segment);
// A segment with a named run should still take up time for the typewriter effect.
MaxLetterIndex += FMath::Max(Segment.Text.Len(), Segment.RunInfo.Name.IsEmpty() ? 0 : 1);
if (!Segment.Text.IsEmpty() || !Segment.RunInfo.Name.IsEmpty())
{
bHasWrittenText = true;
}
}
if (bHasWrittenText)
{
Segments.Add(FDialogueTextSegment{TEXT("\n")});
++MaxLetterIndex;
}
}
Layout->SetWrappingWidth(0);
LineText->SetText(LineText->GetText());
}
else
{
Segments.Add(FDialogueTextSegment{CurrentLine.ToString()});
MaxLetterIndex = Segments[0].Text.Len();
}
}
FString UDialogueBox::CalculateSegments()
{
FString Result = CachedSegmentText;
int32 Idx = CachedLetterIndex;
while (Idx <= CurrentLetterIndex && CurrentSegmentIndex < Segments.Num())
{
const FDialogueTextSegment& Segment = Segments[CurrentSegmentIndex];
if (!Segment.RunInfo.Name.IsEmpty())
{
Result += FString::Printf(TEXT("<%s"), *Segment.RunInfo.Name);
if (!Segment.RunInfo.MetaData.IsEmpty())
{
for (const TTuple<FString, FString>& MetaData : Segment.RunInfo.MetaData)
{
Result += FString::Printf(TEXT(" %s=\"%s\""), *MetaData.Key, *MetaData.Value);
}
}
if (Segment.Text.IsEmpty())
{
Result += TEXT("/>");
++Idx; // This still takes up an index for the typewriter effect.
}
else
{
Result += TEXT(">");
}
}
bool bIsSegmentComplete = true;
if (!Segment.Text.IsEmpty())
{
int32 LettersLeft = CurrentLetterIndex - Idx + 1;
bIsSegmentComplete = LettersLeft >= Segment.Text.Len();
LettersLeft = FMath::Min(LettersLeft, Segment.Text.Len());
Idx += LettersLeft;
Result += Segment.Text.Mid(0, LettersLeft);
if (!Segment.RunInfo.Name.IsEmpty())
{
Result += TEXT("</>");
}
}
if (bIsSegmentComplete)
{
CachedLetterIndex = Idx;
CachedSegmentText = Result;
++CurrentSegmentIndex;
}
else
{
break;
}
}
return Result;
}