Skip to content

Commit 0ba550d

Browse files
committed
Fixed smol bug
Rendering test text input focus fix
1 parent 34026b1 commit 0ba550d

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

examples/RenderingTest/application.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ namespace
164164
auto inputText = inputEnt.get<TTFText>();
165165
auto inputComp = inputEnt.get<TextInputComponent>();
166166

167+
ecsRef->attach<MouseLeftClickComponent>(bgEnt.entity, makeCallable<OnFocus>(OnFocus{inputEnt.id}) );
168+
167169
inputText->setColors(FIELD_TXT);
168170
inputComp->clearTextAfterEnter = false;
169171
inputComp->minWidth = static_cast<size_t>(width - FIELD_PAD_LEFT * 2.0f);

src/Engine/UI/textinput.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ namespace pg
2424
{
2525
auto text = entity->get<TextInputComponent>();
2626

27+
// Clamp cursor in case text was modified externally
28+
if (text->cursorPos > text->text.size())
29+
text->cursorPos = text->text.size();
30+
2731
// Insert text at cursor position instead of appending
2832
text->text.insert(text->cursorPos, event.text);
2933
text->cursorPos += event.text.size();
@@ -131,8 +135,6 @@ namespace pg
131135
{
132136
if (inputHandler->isKeyPressed(SDL_SCANCODE_LCTRL) or inputHandler->isKeyPressed(SDL_SCANCODE_RCTRL))
133137
{
134-
size_t eraseStart = text->cursorPos;
135-
136138
// Skip forward to next word boundary
137139
size_t eraseEnd = text->cursorPos;
138140
while (eraseEnd < text->text.size() and text->text[eraseEnd] != ' ')
@@ -144,7 +146,7 @@ namespace pg
144146
if (eraseEnd < text->text.size() and text->text[eraseEnd] == ' ')
145147
eraseEnd++;
146148

147-
text->text.erase(eraseStart, eraseEnd - eraseStart);
149+
text->text.erase(text->cursorPos, eraseEnd - text->cursorPos);
148150
}
149151
else
150152
{

src/Engine/UI/textinput.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace pg
3737
{
3838
DEFAULT_COMPONENT_MEMBERS(TextInputComponent)
3939

40-
TextInputComponent(StandardEvent event, const std::string& defaultText = "") : event(event), text(defaultText) { LOG_THIS_MEMBER("TextInputComponent"); }
40+
TextInputComponent(StandardEvent event, const std::string& defaultText = "") : event(event), text(defaultText), cursorPos(defaultText.size()) { LOG_THIS_MEMBER("TextInputComponent"); }
4141

4242
void setText(const std::string& text)
4343
{

0 commit comments

Comments
 (0)