From b6e20e04b519e50909331f537df2ea6114a137ee Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 23 Jul 2016 19:16:28 +0200 Subject: General ui code improvements, moved layout related variables to ui::UI, added ColorPicker widget. --- src/ui/inputbox.cc | 107 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 38 deletions(-) (limited to 'src/ui/inputbox.cc') diff --git a/src/ui/inputbox.cc b/src/ui/inputbox.cc index 89e3243..a7565e7 100644 --- a/src/ui/inputbox.cc +++ b/src/ui/inputbox.cc @@ -4,6 +4,7 @@ the terms of the GNU General Public License version 2 */ +#include "ui/definitions.h" #include "ui/inputbox.h" #include "ui/paint.h" #include "auxiliary/functions.h" @@ -15,13 +16,14 @@ namespace ui InputBox::InputBox(Widget *parent) : Widget(parent) { - input_text.clear(); - input_pos = 0; - input_max = 512; + _text.clear(); + _pos = 0; + _max = 512; set_label("input"); set_background(false); set_border(false); + set_alignment(AlignLeft | AlignTop); } InputBox::~InputBox() @@ -30,58 +32,75 @@ InputBox::~InputBox() void InputBox::clear() { - input_text.clear(); - input_pos = 0; + _text.clear(); + _pos = 0; } void InputBox::set_text(std::string const &text) { - input_text.assign(text); - input_pos = input_text.size(); + _text.assign(text); + _pos = _text.size(); } void InputBox::set_text(const char *text) { if (text) - input_text.assign(text); + _text.assign(text); else - input_text.clear(); - input_pos = input_text.size(); + _text.clear(); + _pos = _text.size(); } void InputBox::set_prompt(const std::string &prompt) { - input_prompt.assign(prompt); + _prompt.assign(prompt); } void InputBox::set_prompt(const char *prompt) { if (!prompt) - input_prompt.clear(); + _prompt.clear(); else - input_prompt.assign(prompt); + _prompt.assign(prompt); } void InputBox::set_max(const size_t max) { - input_max = max; + _max = max; +} + +void InputBox::set_alignment(const unsigned int alignment) +{ + _alignment = alignment; } void InputBox::draw() { + size_t text_width = (size_t) floorf(width() / font()->width()); - size_t prompt_width = aux::text_length(input_prompt); + size_t prompt_width = aux::text_length(_prompt); math::Vector2f v(global_location()); Paint::set_color(palette()->text()); + + // Horizontal alignment + // FIXME horizontal alignment is not supported + + // Vertical alignment + if (alignment() & ui::AlignVCenter) { + v.get_y() += (height() - font()->height()) * 0.5f; + + } else if (alignment() & ui::AlignBottom) { + v.get_y() += height() - font()->height(); + } // draw the prompt if (prompt_width) { - Paint::draw_text(v, font(), input_prompt); + Paint::draw_text(v, font(), _prompt); v[0] += prompt_width * font()->width(); } Paint::set_color(palette()->text()); // draw the part before the cursor - std::string firstpart(input_text.substr(0, input_pos)); + std::string firstpart(_text.substr(0, _pos)); size_t draw_width = 0; const char *c = firstpart.c_str(); @@ -111,13 +130,13 @@ void InputBox::draw() // draw the part behind the cursor v[0] += draw_width * font()->width(); - if (input_pos < input_text.size()) { - if (input_pos > 1 && aux::is_color_code(input_text.c_str() + input_pos - 1)) { - Paint::set_system_color(input_text[input_pos]); + if (_pos < _text.size()) { + if (_pos > 1 && aux::is_color_code(_text.c_str() + _pos - 1)) { + Paint::set_system_color(_text[_pos]); } // limit to width std::string secondpart; - c = &input_text.c_str()[input_pos]; + c = &_text.c_str()[_pos]; while (*c && (draw_width <= (text_width - prompt_width - 1))) { if (aux::is_color_code(c)) { c++; @@ -144,58 +163,58 @@ void InputBox::draw() void InputBox::complete() { - core::CommandBuffer::complete(input_text, input_pos); + core::CommandBuffer::complete(_text, _pos); } bool InputBox::on_keypress(const int key, const unsigned int modifier) { switch (key) { case SDLK_HOME: - input_pos = 0; + _pos = 0; return true; break; case SDLK_END: - input_pos = input_text.size(); + _pos = _text.size(); return true; break; case SDLK_LEFT: - if (input_pos > 0) - input_pos--; + if (_pos > 0) + _pos--; return true; break; case SDLK_RIGHT: - if (input_pos < input_text.size()) - input_pos++; + if (_pos < _text.size()) + _pos++; return true; break; case SDLK_DELETE: - if (input_text.size() && input_pos < input_text.size()) { - input_text.erase(input_pos, 1); + if (_text.size() && _pos < _text.size()) { + _text.erase(_pos, 1); } return true; break; case SDLK_BACKSPACE: - if (input_text.size() && input_pos) { - input_text.erase(input_pos - 1, 1); - input_pos--; + if (_text.size() && _pos) { + _text.erase(_pos - 1, 1); + _pos--; } return true; break; default: if ((key >= 32) && (key < 175)) { - if (input_text.size() < input_max) { + if (_text.size() < _max) { // TODO bell sound - if (input_pos == input_text.size()) - input_text += (char)key; + if (_pos == _text.size()) + _text += (char)key; else - input_text.insert(input_pos, 1, (char)key); - input_pos++; + _text.insert(_pos, 1, (char)key); + _pos++; } return true; } @@ -204,11 +223,23 @@ bool InputBox::on_keypress(const int key, const unsigned int modifier) return false; } + bool InputBox::on_keyrelease(const int key, const unsigned int modifier) { return false; } +bool InputBox::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { + set_focus(); + return true; + } + + return false; +} + } -- cgit v1.2.3