Skip to content

Commit bd009a1

Browse files
committed
Enable text input for text input panels.
Was not explicitly enabled before (which should not have worked in SDL2 either), now is explicitly enabled when text panels have focus
1 parent b6bace7 commit bd009a1

File tree

6 files changed

+54
-0
lines changed

6 files changed

+54
-0
lines changed

Source/GUI/GUIInput.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "GUIInput.h"
12
#include "GUI.h"
23

34
using namespace RTE;
@@ -148,3 +149,14 @@ void GUIInput::Update() {
148149
int GUIInput::GetModifier() const {
149150
return m_Modifier;
150151
}
152+
153+
void GUIInput::StartTextInput() {
154+
m_TextInputActive++;
155+
}
156+
157+
void GUIInput::StopTextInput() {
158+
m_TextInputActive--;
159+
if (m_TextInputActive < 0) {
160+
m_TextInputActive = 0;
161+
}
162+
}

Source/GUI/GUIInput.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ namespace RTE {
122122
/// @param enableKeyJoyMouseCursor Whether the keyboard and joysticks also control the mouse or not.
123123
void SetKeyJoyMouseCursor(bool enableKeyJoyMouseCursor) { m_KeyJoyMouseCursor = enableKeyJoyMouseCursor; }
124124

125+
/// Enables receiving text input events.
126+
virtual void StartTextInput();
127+
128+
/// Disables receiving text input events.
129+
virtual void StopTextInput();
130+
125131
protected:
126132
enum Constants {
127133
KEYBOARD_BUFFER_SIZE = 256
@@ -132,6 +138,7 @@ namespace RTE {
132138
unsigned char m_ScanCodeState[KEYBOARD_BUFFER_SIZE];
133139
std::string m_TextInput;
134140
bool m_HasTextInput;
141+
int m_TextInputActive{0};
135142

136143
// Mouse button states
137144
// Order: Left, Middle, Right

Source/GUI/GUITextPanel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ void GUITextPanel::Draw(GUIScreen* Screen) {
140140
Screen->GetBitmap()->SetClipRect(nullptr);
141141
}
142142

143+
void GUITextPanel::OnGainFocus() {
144+
m_Manager->GetInputController()->StartTextInput();
145+
}
146+
147+
void GUITextPanel::OnLoseFocus() {
148+
m_Manager->GetInputController()->StopTextInput();
149+
}
150+
143151
void GUITextPanel::OnKeyPress(int KeyCode, int Modifier) {
144152
// TODO: Figure out what the "performance bitching" is.
145153
// Condition here to stop the compiler bitching about performance

Source/GUI/GUITextPanel.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ namespace RTE {
3535
/// @param Screen Screen class
3636
void Draw(GUIScreen* Screen) override;
3737

38+
/// Called when this panel gains focus.
39+
/// Start text input events.
40+
void OnGainFocus() override;
41+
42+
/// Called when this panel loses focus.
43+
/// Stops text input events.
44+
void OnLoseFocus() override;
45+
3846
/// Called when the mouse goes down on the panel
3947
/// @param X Mouse Position, Mouse Buttons, Modifier.
4048
void OnMouseDown(int X, int Y, int Buttons, int Modifier) override;
@@ -51,6 +59,7 @@ namespace RTE {
5159
/// @param KeyCode KeyCode, Modifier.
5260
void OnKeyPress(int KeyCode, int Modifier) override;
5361

62+
/// Called when text input is received
5463
void OnTextInput(std::string_view inputText) override;
5564

5665
/// Sets the text in the textpanel.

Source/GUI/Wrappers/GUIInputWrapper.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "GUI.h"
22
#include "GUIInputWrapper.h"
3+
#include "GUIInput.h"
4+
#include "SDL3/SDL_keyboard.h"
35
#include "WindowMan.h"
46
#include "FrameMan.h"
57
#include "UInputMan.h"
@@ -62,6 +64,18 @@ void GUIInputWrapper::Update() {
6264
m_MouseY = static_cast<int>(mousePos.GetY() / static_cast<float>(g_WindowMan.GetResMultiplier()));
6365
}
6466

67+
void GUIInputWrapper::StartTextInput() {
68+
GUIInput::StartTextInput();
69+
SDL_StartTextInput(g_WindowMan.GetWindow());
70+
}
71+
72+
void GUIInputWrapper::StopTextInput() {
73+
GUIInput::StopTextInput();
74+
if (m_TextInputActive <= 0) {
75+
SDL_StopTextInput(g_WindowMan.GetWindow());
76+
}
77+
}
78+
6579
void GUIInputWrapper::UpdateKeyboardInput(float keyElapsedTime) {
6680
// Clear the keyboard buffer, we need it to check for changes.
6781
memset(m_KeyboardBuffer, 0, sizeof(uint8_t) * GUIInput::Constants::KEYBOARD_BUFFER_SIZE);

Source/GUI/Wrappers/GUIInputWrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ namespace RTE {
2929
#pragma region Virtual Override Methods
3030
/// Updates the input.
3131
void Update() override;
32+
33+
void StartTextInput() override;
34+
35+
void StopTextInput() override;
3236
#pragma endregion
3337

3438
private:

0 commit comments

Comments
 (0)