-
Notifications
You must be signed in to change notification settings - Fork 9
/
DialogueBox.h
109 lines (82 loc) · 2.84 KB
/
DialogueBox.h
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
// Copyright (c) Sam Bloomberg
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Components/RichTextBlock.h"
#include "Framework/Text/RichTextLayoutMarshaller.h"
#include "Framework/Text/SlateTextLayout.h"
#include "DialogueBox.generated.h"
/**
* A text block that exposes more information about text layout.
*/
UCLASS()
class UDialogueTextBlock : public URichTextBlock
{
GENERATED_BODY()
public:
FORCEINLINE TSharedPtr<FSlateTextLayout> GetTextLayout() const
{
return TextLayout;
}
FORCEINLINE TSharedPtr<FRichTextLayoutMarshaller> GetTextMarshaller() const
{
return TextMarshaller;
}
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
private:
TSharedPtr<FSlateTextLayout> TextLayout;
TSharedPtr<FRichTextLayoutMarshaller> TextMarshaller;
};
struct FDialogueTextSegment
{
FString Text;
FRunInfo RunInfo;
};
UCLASS()
class FLAME_API UDialogueBox : public UUserWidget
{
GENERATED_BODY()
public:
UDialogueBox(const FObjectInitializer& ObjectInitializer);
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
TObjectPtr<UDialogueTextBlock> LineText;
// The amount of time between printing individual letters (for the "typewriter" effect).
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Dialogue Box")
float LetterPlayTime = 0.025f;
// The amount of time to wait after finishing the line before actually marking it completed.
// This helps prevent accidentally progressing dialogue on short lines.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Dialogue Box")
float EndHoldTime = 0.15f;
UFUNCTION(BlueprintCallable, Category = "Dialogue Box")
void PlayLine(const FText& InLine);
UFUNCTION(BlueprintCallable, Category = "Dialogue Box")
void GetCurrentLine(FText& OutLine) const { OutLine = CurrentLine; }
UFUNCTION(BlueprintCallable, Category = "Dialogue Box")
bool HasFinishedPlayingLine() const { return bHasFinishedPlaying; }
UFUNCTION(BlueprintCallable, Category = "Dialogue Box")
void SkipToLineEnd();
protected:
UFUNCTION(BlueprintImplementableEvent, Category = "Dialogue Box")
void OnPlayLetter();
UFUNCTION(BlueprintImplementableEvent, Category = "Dialogue Box")
void OnLineFinishedPlaying();
private:
void PlayNextLetter();
void CalculateWrappedString();
FString CalculateSegments();
UPROPERTY()
FText CurrentLine;
TArray<FDialogueTextSegment> Segments;
// The section of the text that's already been printed out and won't ever change.
// This lets us cache some of the work we've already done. We can't cache absolutely
// everything as the last few characters of a string may change if they're related to
// a named run that hasn't been completed yet.
FString CachedSegmentText;
int32 CachedLetterIndex = 0;
int32 CurrentSegmentIndex = 0;
int32 CurrentLetterIndex = 0;
int32 MaxLetterIndex = 0;
uint32 bHasFinishedPlaying : 1;
FTimerHandle LetterTimer;
};