diff --git a/docs/pr-screenshots/codex/editor-text-input-viewport/after.png b/docs/pr-screenshots/codex/editor-text-input-viewport/after.png new file mode 100644 index 0000000000..a466c10a5f Binary files /dev/null and b/docs/pr-screenshots/codex/editor-text-input-viewport/after.png differ diff --git a/docs/pr-screenshots/codex/editor-text-input-viewport/before.png b/docs/pr-screenshots/codex/editor-text-input-viewport/before.png new file mode 100644 index 0000000000..1482af1aa5 Binary files /dev/null and b/docs/pr-screenshots/codex/editor-text-input-viewport/before.png differ diff --git a/engine/prefabs/irreden/render/systems/system_widget_render_text_input.hpp b/engine/prefabs/irreden/render/systems/system_widget_render_text_input.hpp index 01134bcb9f..179b4e4231 100644 --- a/engine/prefabs/irreden/render/systems/system_widget_render_text_input.hpp +++ b/engine/prefabs/irreden/render/systems/system_widget_render_text_input.hpp @@ -68,9 +68,11 @@ template <> struct System { scratch_ ); + const auto viewport = + IRPrefab::WidgetText::textInputViewport(widget, textInput, guiPos.pos_, theme_); IRPrefab::WidgetText::queue( textCmds_, - IRPrefab::WidgetText::textInputText(widget, textInput, guiPos.pos_, theme_), + viewport.text_, canvas_->size_, IRPrefab::Widget::detail::stateText(theme_, widget) ); @@ -86,19 +88,10 @@ template <> struct System { if (!cursorVisible) return; - const int textX = guiPos.pos_.x + theme_.padding_ * 2; - const int cursorChars = - IRMath::clamp(textInput.cursorPos_, 0, static_cast(textInput.text_.size())); - const int cursorX = IRMath::min( - textX + cursorChars * IRRender::kGlyphStepX, - guiPos.pos_.x + widget.size_.x - theme_.textInputCursorWidth_ - ); - const int cursorH = IRRender::kGlyphHeight; - const int cursorY = guiPos.pos_.y + (widget.size_.y - cursorH) / 2; IRRender::fillRect( *canvas_, - IRMath::ivec2(cursorX, cursorY), - IRMath::ivec2(theme_.textInputCursorWidth_, cursorH), + viewport.cursor_.pos_, + viewport.cursor_.size_, theme_.textInputCursor_, IRRender::kWidgetBorderDistance, scratch_ diff --git a/engine/prefabs/irreden/render/widget_text_layout.hpp b/engine/prefabs/irreden/render/widget_text_layout.hpp index 10ee972012..cd79c56308 100644 --- a/engine/prefabs/irreden/render/widget_text_layout.hpp +++ b/engine/prefabs/irreden/render/widget_text_layout.hpp @@ -217,18 +217,52 @@ inline TextRun numberFieldText( return run; } -inline TextRun textInputText( +struct TextInputViewport { + TextRun text_; + GuiRect cursor_; +}; + +// A single-line window follows the insertion point without modifying the value. +// Reserve cursor width even while unfocused so focus and blinking do not reflow it. +inline TextInputViewport textInputViewport( const IRComponents::C_Widget &widget, const IRComponents::C_WidgetTextInput &textInput, IRMath::ivec2 pos, const IRPrefab::Widget::WidgetTheme &theme ) { - return leftCentered( - textInput.text_, - IRMath::ivec2(pos.x + theme.padding_ * 2, pos.y), + const int width = IRMath::max(0, widget.size_.x); + const int inset = IRMath::min(theme.padding_ * 2, width / 2); + const int innerWidth = width - inset * 2; + const int cursorWidth = IRMath::min(theme.textInputCursorWidth_, innerWidth); + const int capacity = (innerWidth - cursorWidth) / IRRender::kGlyphStepX; + const int length = static_cast(textInput.text_.size()); + const int cursor = IRMath::clamp(textInput.cursorPos_, 0, length); + const int first = IRMath::clamp(cursor - capacity / 2, 0, IRMath::max(0, length - capacity)); + TextInputViewport viewport; + viewport.text_ = leftCentered( + std::string_view(textInput.text_).substr(first, capacity), + IRMath::ivec2(pos.x + inset, pos.y), widget.size_.y, IRRender::TextSize::LARGE ); + const int cursorHeight = IRMath::min(IRRender::kGlyphHeight, IRMath::max(0, widget.size_.y)); + viewport.cursor_ = { + IRMath::ivec2( + pos.x + inset + (cursor - first) * IRRender::kGlyphStepX, + pos.y + (widget.size_.y - cursorHeight) / 2 + ), + IRMath::ivec2(cursorWidth, cursorHeight) + }; + return viewport; +} + +inline TextRun textInputText( + const IRComponents::C_Widget &widget, + const IRComponents::C_WidgetTextInput &textInput, + IRMath::ivec2 pos, + const IRPrefab::Widget::WidgetTheme &theme +) { + return textInputViewport(widget, textInput, pos, theme).text_; } // Item `index` drawn on visible row `row` (0 = the top row). diff --git a/test/render/gui_layout_audit_test.cpp b/test/render/gui_layout_audit_test.cpp index a812df4fad..c61f89446e 100644 --- a/test/render/gui_layout_audit_test.cpp +++ b/test/render/gui_layout_audit_test.cpp @@ -463,6 +463,60 @@ TEST_F(GuiLayoutAudit, TextInputAndListRows) { ); } +TEST_F(GuiLayoutAudit, TextInputViewportFollowsCursorWithoutChangingValue) { + const std::string value = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const auto id = IRPrefab::Widget::makeTextInput(ivec2(10, 10), ivec2(50, 14), value); + auto &input = IREntity::getComponent(id); + const auto &widget = data(id); + const IRPrefab::GuiRect bounds{ivec2(10, 10), widget.size_}; + struct Case { + int cursor_; + std::string_view visible_; + int cursorX_; + }; + for (const auto &test : + {Case{26, "WXYZ", 50}, + Case{13, "LMNO", 34}, + Case{0, "ABCD", 18}, + Case{-1, "ABCD", 18}, + Case{100, "WXYZ", 50}}) { + SCOPED_TRACE(test.cursor_); + input.cursorPos_ = test.cursor_; + const auto viewport = + IRPrefab::WidgetText::textInputViewport(widget, input, bounds.pos_, theme()); + EXPECT_EQ(viewport.text_.text_, test.visible_); + EXPECT_EQ(viewport.cursor_.pos_.x, test.cursorX_); + EXPECT_TRUE(IRPrefab::contains(bounds, viewport.cursor_)); + EXPECT_TRUE(IRPrefab::contains(bounds, queued(viewport.text_))); + expectRuns(id, {queued(viewport.text_)}); + EXPECT_TRUE(report(kCanvas).empty()) << describe(report(kCanvas)); + EXPECT_EQ(IRPrefab::Widget::textInputValue(id), value); + } +} + +TEST_F(GuiLayoutAudit, TextInputViewportHandlesEmptyShortAndNarrowFields) { + const auto id = IRPrefab::Widget::makeTextInput(ivec2(10, 10), ivec2(50, 14), "HI"); + auto &input = IREntity::getComponent(id); + auto &widget = IREntity::getComponent(id); + auto viewport = IRPrefab::WidgetText::textInputViewport(widget, input, ivec2(10, 10), theme()); + EXPECT_EQ(viewport.text_.text_, "HI"); + EXPECT_EQ(viewport.cursor_.pos_.x, 34); + IRPrefab::Widget::setTextInputValue(id, ""); + viewport = IRPrefab::WidgetText::textInputViewport(widget, input, ivec2(10, 10), theme()); + EXPECT_TRUE(viewport.text_.text_.empty()); + EXPECT_EQ(viewport.cursor_.pos_.x, 18); + IRPrefab::Widget::setTextInputValue(id, "LONG VALUE"); + for (int width : {0, 1, 10, 18}) { + SCOPED_TRACE(width); + widget.size_.x = width; + viewport = IRPrefab::WidgetText::textInputViewport(widget, input, ivec2(10, 10), theme()); + EXPECT_TRUE(viewport.text_.text_.empty()); + EXPECT_GE(viewport.cursor_.pos_.x, 10); + EXPECT_LE(viewport.cursor_.pos_.x + viewport.cursor_.size_.x, 10 + width); + EXPECT_EQ(IRPrefab::Widget::textInputValue(id), "LONG VALUE"); + } +} + TEST_F(GuiLayoutAudit, DropdownHeaderAndOpenStripRows) { const IREntity::EntityId id = IRPrefab::Widget::makeDropdown(ivec2(10, 10), ivec2(100, 18), {"ONE", "TWO"}, 1, 18);