From 97fca172fd51270cebd5b722f861a6c753bd4d2a Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 12 Oct 2008 17:51:55 +0000 Subject: inputbox respects text width --- src/ui/input.cc | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ui/input.cc b/src/ui/input.cc index 0c4ea32..5e000d2 100644 --- a/src/ui/input.cc +++ b/src/ui/input.cc @@ -52,11 +52,11 @@ void Input::draw() draw_background(); draw_border(); - using namespace render; size_t text_width = (size_t) width() / font()->width(); math::Vector2f v(global_location()); paint::color(palette()->foreground()); + // draw the part before the cursor std::string firstpart(input_text.substr(0, input_pos)); size_t draw_width = 0; const char *c = firstpart.c_str(); @@ -74,14 +74,13 @@ void Input::draw() while (*c && draw_width > text_width - 2) { if (aux::is_color_code(c)) { c++; - Text::setcolor(*c); + render::Text::setcolor(*c); } else { draw_width--; } c++; } - // draw the part before the cursor if (*c) { paint::text(v, size(), font(), std::string(c), AlignLeft | AlignVCenter); } @@ -89,15 +88,26 @@ void Input::draw() // draw the part behind the cursor v.x += draw_width * font()->width(); if (input_pos < input_text.size()) { - // FIXME limit to width if (input_pos > 1 && aux::is_color_code(input_text.c_str() + input_pos -1)) { - Text::setcolor(input_text[input_pos]); + render::Text::setcolor(input_text[input_pos]); } - c = input_text.c_str() + input_pos; - paint::text(v, size(), font(), std::string(c), AlignLeft | AlignVCenter); + // limit to width + std::string secondpart; + c = &input_text.c_str()[input_pos]; + while (*c && (draw_width <= (text_width - 2))) { + if (aux::is_color_code(c)) { + c++; + } else { + draw_width++; + secondpart += *c; + } + c++; + } + + paint::text(v, size(), font(), secondpart, AlignLeft | AlignVCenter); } - // draw cursor + // draw the cursor if (has_input_focus() && (core::application()->time() - ::floorf(core::application()->time())) < 0.5f) { std::string cursor("^B"); cursor += (char) 11; -- cgit v1.2.3