Skip to content

Commit 0d34b98

Browse files
committed
added cursor management
1 parent 95d21cd commit 0d34b98

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

Source/LuaMachine/LuaMachine.Build.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public LuaMachine(ReadOnlyTargetRules Target) : base(Target)
3838
"Engine",
3939
"Slate",
4040
"SlateCore",
41-
"UMG"
41+
"UMG",
42+
"InputCore"
4243
// ... add private dependencies that you statically link with here ...
4344
}
4445
);

Source/LuaMachine/Private/LuaMultiLineEditableTextBox.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ULuaMultiLineEditableTextBox::ULuaMultiLineEditableTextBox()
1919

2020
bIsReadonly = false;
2121
bHandleTab = true;
22+
bHandleArrows = true;
2223

2324
CodeStyle = FTextBlockStyle()
2425
.SetFont(WidgetStyle.Font)
@@ -70,13 +71,86 @@ FReply ULuaMultiLineEditableTextBox::OnKeyChar(const FGeometry& InGeometry, cons
7071

7172
FReply ULuaMultiLineEditableTextBox::OnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent)
7273
{
73-
if (bHandleTab && InKeyEvent.GetKeyCode() == 9)
74+
FKey Key = InKeyEvent.GetKey();
75+
if (bHandleTab && Key == EKeys::Tab)
7476
{
7577
return FReply::Handled();
7678
}
79+
80+
if (bHandleArrows)
81+
{
82+
if (Key == EKeys::Up)
83+
{
84+
MoveCursorUp();
85+
return FReply::Handled();
86+
}
87+
88+
if (Key == EKeys::Down)
89+
{
90+
MoveCursorDown();
91+
return FReply::Handled();
92+
}
93+
94+
if (Key == EKeys::Right)
95+
{
96+
MoveCursorRight();
97+
return FReply::Handled();
98+
}
99+
100+
if (Key == EKeys::Left)
101+
{
102+
MoveCursorLeft();
103+
return FReply::Handled();
104+
}
105+
}
106+
77107
return EditableTextBoxPtr->SMultiLineEditableTextBox::OnKeyDown(InGeometry, InKeyEvent);
78108
}
79109

110+
void ULuaMultiLineEditableTextBox::OnCursorMoved(const FTextLocation& Location)
111+
{
112+
CursorLocation = Location;
113+
}
114+
115+
int32 ULuaMultiLineEditableTextBox::GetCursorLine() const
116+
{
117+
return CursorLocation.GetLineIndex();
118+
}
119+
120+
int32 ULuaMultiLineEditableTextBox::GetCursorColumn() const
121+
{
122+
return CursorLocation.GetOffset();
123+
}
124+
125+
void ULuaMultiLineEditableTextBox::CursorGoTo(int32 Line, int32 Column)
126+
{
127+
if (Line < 0)
128+
Line = 0;
129+
if (Column < 0)
130+
Column = 0;
131+
return EditableTextBoxPtr->GoTo(FTextLocation(Line, Column));
132+
}
133+
134+
void ULuaMultiLineEditableTextBox::MoveCursorUp()
135+
{
136+
return CursorGoTo(CursorLocation.GetLineIndex() - 1, 0);
137+
}
138+
139+
void ULuaMultiLineEditableTextBox::MoveCursorDown()
140+
{
141+
return CursorGoTo(CursorLocation.GetLineIndex() + 1, 0);
142+
}
143+
144+
void ULuaMultiLineEditableTextBox::MoveCursorRight()
145+
{
146+
return CursorGoTo(CursorLocation.GetLineIndex(), CursorLocation.GetOffset() + 1);
147+
}
148+
149+
void ULuaMultiLineEditableTextBox::MoveCursorLeft()
150+
{
151+
return CursorGoTo(CursorLocation.GetLineIndex(), CursorLocation.GetOffset() - 1);
152+
}
153+
80154
void ULuaMultiLineEditableTextBox::SynchronizeProperties()
81155
{
82156
Super::SynchronizeProperties();
@@ -110,6 +184,8 @@ TSharedRef<SWidget> ULuaMultiLineEditableTextBox::RebuildWidget()
110184
.OnKeyCharHandler_UObject(this, &ULuaMultiLineEditableTextBox::OnKeyChar)
111185
.OnKeyDownHandler_UObject(this, &ULuaMultiLineEditableTextBox::OnKeyDown)
112186
.IsReadOnly(bIsReadonly)
187+
.AllowContextMenu(false)
188+
.OnCursorMoved_UObject(this, &ULuaMultiLineEditableTextBox::OnCursorMoved)
113189
.Style(&WidgetStyle);
114190

115191
return EditableTextBoxPtr.ToSharedRef();

Source/LuaMachine/Public/LuaMultiLineEditableTextBox.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,42 @@ class LUAMACHINE_API ULuaMultiLineEditableTextBox : public UTextLayoutWidget
5151
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Lua Code")
5252
bool bHandleTab;
5353

54-
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "GetText (Lua Code Box)"))
54+
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Lua Code")
55+
bool bHandleArrows;
56+
57+
UFUNCTION(BlueprintPure, Category = "Widget", meta = (DisplayName = "GetText (Lua Code Box)"))
5558
FText GetText() const;
5659

5760
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "SetText (Lua Code Box)"))
5861
void SetText(FText InText);
5962

63+
UFUNCTION(BlueprintPure, Category = "Widget", meta = (DisplayName = "GetCursorLine (Lua Code Box)"))
64+
int32 GetCursorLine() const;
65+
66+
UFUNCTION(BlueprintPure, Category = "Widget", meta = (DisplayName = "GetCursorColumn (Lua Code Box)"))
67+
int32 GetCursorColumn() const;
68+
69+
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "MoveCursorUp (Lua Code Box)"))
70+
void MoveCursorUp();
71+
72+
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "MoveCursorDown (Lua Code Box)"))
73+
void MoveCursorDown();
74+
75+
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "MoveCursorRight (Lua Code Box)"))
76+
void MoveCursorRight();
77+
78+
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "MoveCursorLeft (Lua Code Box)"))
79+
void MoveCursorLeft();
80+
81+
UFUNCTION(BlueprintCallable, Category = "Widget", meta = (DisplayName = "CursorGoTo (Lua Code Box)"))
82+
void CursorGoTo(int32 Line, int32 Column);
83+
6084
virtual FReply OnKeyChar(const FGeometry& InGeometry, const FCharacterEvent& InCharacterEvent);
6185

6286
virtual FReply OnKeyDown(const FGeometry& InGeometry, const FKeyEvent& InKeyEvent);
6387

88+
virtual void OnCursorMoved(const FTextLocation& Location);
89+
6490
virtual void SynchronizeProperties() override;
6591

6692
#if WITH_EDITOR
@@ -71,4 +97,6 @@ class LUAMACHINE_API ULuaMultiLineEditableTextBox : public UTextLayoutWidget
7197
virtual TSharedRef<SWidget> RebuildWidget() override;
7298

7399
TSharedPtr<SMultiLineEditableTextBox> EditableTextBoxPtr;
100+
101+
FTextLocation CursorLocation;
74102
};

0 commit comments

Comments
 (0)