Skip to content
Open
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
28 changes: 27 additions & 1 deletion src/gui/gui2_textentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ GuiTextEntry::~GuiTextEntry()
SDL_StopTextInput();
}

float GuiTextEntry::getLineSpacing() const {
const auto& front = front_style->get(getState());
return front.font->getLineSpacing(32) * text_size / float(32);
}

void GuiTextEntry::onDraw(sp::RenderTarget& renderer)
{
const auto& back = back_style->get(getState());
Expand All @@ -34,9 +39,30 @@ void GuiTextEntry::onDraw(sp::RenderTarget& renderer)
if (shown_text.empty()) shown_text = " ";
sp::Rect text_rect(rect.position.x + 16, rect.position.y, rect.size.x - 32, rect.size.y);
auto prepared = front.font->prepare(shown_text, 32, text_size, {255,255,255,255}, text_rect.size, multiline ? sp::Alignment::TopLeft : sp::Alignment::CenterLeft, sp::Font::FlagClip);
auto linespacing = front.font->getLineSpacing(32) * text_size / float(32);

if (multiline) {
// ensure the text fills the available space as much as possible
auto min_y = std::numeric_limits<float>::infinity();
auto max_y = -min_y;
for(auto& d : prepared.data) {
if (d.position.y < min_y)
min_y = d.position.y;
if (d.position.y > max_y)
max_y = d.position.y;
}
auto clipped_from_top = -min_y - render_offset.y;
auto space_at_bottom = rect.size.y - max_y - render_offset.y - linespacing * 0.3f;

if (space_at_bottom > 0 && clipped_from_top > 0) {
// the text goes off the top of the box but doesn't reach the bottom;
// scroll until it either stops going off the top, or reaches the bottom
render_offset.y += std::min(space_at_bottom, clipped_from_top);
}
}

for(auto& d : prepared.data)
d.position += render_offset;
auto linespacing = front.font->getLineSpacing(32) * text_size / float(32);

float start_x = -1;
int selection_min = std::min(selection_start, selection_end);
Expand Down
4 changes: 3 additions & 1 deletion src/gui/gui2_textentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ class GuiTextEntry : public GuiElement
GuiTextEntry* upCallback(func_t func);
GuiTextEntry* downCallback(func_t func);

float getLineSpacing() const;

void setCursorPosition(int offset);
protected:
int getTextOffsetForPosition(glm::vec2 position);
void runChangeCallback();
};

#endif//GUI2_TEXTENTRY_H
#endif//GUI2_TEXTENTRY_H
8 changes: 6 additions & 2 deletions src/menus/luaConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ void LuaConsole::addLog(const string& message)
if (!console->is_open) {
console->message_show_timers.emplace_back();
console->message_show_timers.back().start(5.0f);
console->top->layout.size.y = std::min(450.0f, 15.0f + console->message_show_timers.size() * 15.0f);
auto linespace = console->log->getLineSpacing();
console->top->layout.size.y = std::min(450.0f, (0.3f + console->message_show_timers.size()) * linespace);
console->top->show();
console->top->setEnable(false);
}
}

Expand All @@ -91,6 +93,7 @@ void LuaConsole::update(float delta)
top->layout.size.y = 450;
message_show_timers.clear();
top->show();
top->setEnable(true);
entry->show();
}
}
Expand All @@ -99,7 +102,8 @@ void LuaConsole::update(float delta)
if (message_show_timers.empty()) {
top->hide();
} else {
top->layout.size.y = std::min(450.0f, 15.0f + message_show_timers.size() * 15.0f);
auto linespace = console->log->getLineSpacing();
top->layout.size.y = std::min(450.0f, (0.3f + message_show_timers.size()) * linespace);
}
}
}
Expand Down
Loading