Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ template <> struct System<WIDGET_RENDER_TEXT_INPUT> {
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)
);
Expand All @@ -86,19 +88,10 @@ template <> struct System<WIDGET_RENDER_TEXT_INPUT> {
if (!cursorVisible)
return;

const int textX = guiPos.pos_.x + theme_.padding_ * 2;
const int cursorChars =
IRMath::clamp(textInput.cursorPos_, 0, static_cast<int>(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_
Expand Down
42 changes: 38 additions & 4 deletions engine/prefabs/irreden/render/widget_text_layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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).
Expand Down
54 changes: 54 additions & 0 deletions test/render/gui_layout_audit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IRComponents::C_WidgetTextInput>(id);
const auto &widget = data<C_Widget>(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<IRComponents::C_WidgetTextInput>(id);
auto &widget = IREntity::getComponent<C_Widget>(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);
Expand Down
Loading