From b417df720584c101f3799874a0c836a543a8d0a8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 12 Oct 2008 14:55:10 +0000 Subject: user interface updates, work-in-progress --- src/ui/input.cc | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/ui/input.cc (limited to 'src/ui/input.cc') diff --git a/src/ui/input.cc b/src/ui/input.cc new file mode 100644 index 0000000..0c4ea32 --- /dev/null +++ b/src/ui/input.cc @@ -0,0 +1,174 @@ +/* + ui/input.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "ui/input.h" +#include "ui/paint.h" +#include "auxiliary/functions.h" +#include "core/core.h" +#include "render/render.h" + +namespace ui +{ + +Input::Input(Widget *parent) : Widget(parent) +{ + input_text.clear(); + input_pos = 0; + + set_label("input"); + set_background(false); + set_border(false); +} + +Input::~Input() +{ +} + +void Input::clear() +{ + input_text.clear(); + input_pos = 0; +} +void Input::set_text(std::string const &text) +{ + input_text.assign(text); + input_pos = input_text.size(); +} + +void Input::set_text(const char *text) +{ + if (text) + input_text.assign(text); + else + input_text.clear(); + input_pos = input_text.size(); +} + +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()); + + std::string firstpart(input_text.substr(0, input_pos)); + size_t draw_width = 0; + const char *c = firstpart.c_str(); + + while (*c) { + if (aux::is_color_code(c)) { + c++; + } else { + draw_width++; + } + c++; + } + + c = firstpart.c_str(); + while (*c && draw_width > text_width - 2) { + if (aux::is_color_code(c)) { + c++; + 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); + } + + // 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]); + } + c = input_text.c_str() + input_pos; + paint::text(v, size(), font(), std::string(c), AlignLeft | AlignVCenter); + } + + // draw cursor + if (has_input_focus() && (core::application()->time() - ::floorf(core::application()->time())) < 0.5f) { + std::string cursor("^B"); + cursor += (char) 11; + paint::text(v, size(), font(), cursor, AlignLeft | AlignVCenter); + } +} + +bool Input::on_keypress(const int key, const unsigned int modifier) +{ + switch (key) { + case SDLK_TAB: + core::CommandBuffer::complete(input_text, input_pos); + return true; + break; + + case SDLK_HOME: + input_pos = 0; + return true; + break; + + case SDLK_END: + input_pos = input_text.size(); + return true; + break; + + case SDLK_LEFT: + if (input_pos > 0) + input_pos--; + return true; + break; + + case SDLK_RIGHT: + if (input_pos < input_text.size()) + input_pos++; + return true; + break; + + case SDLK_DELETE: + if (input_text.size() && input_pos < input_text.size()) { + input_text.erase(input_pos, 1); + } + return true; + break; + + case SDLK_BACKSPACE: + if (input_text.size() && input_pos) { + input_text.erase(input_pos-1, 1); + input_pos--; + } + return true; + break; + + default: + if ((key >= 32) && (key <175)) { + if (input_pos == input_text.size()) + input_text += (char)key; + else + input_text.insert(input_pos, 1, (char)key); + input_pos++; + return true; + } + break; + } + + return false; +} +bool Input::on_keyrelease(const int key, const unsigned int modifier) +{ + return false; +} + +} + + -- cgit v1.2.3