1// SuperTux
2// Copyright (C) 2015 Hume2 <teratux.mail@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "gui/item_textfield.hpp"
18
19#include "supertux/colorscheme.hpp"
20#include "supertux/globals.hpp"
21#include "supertux/resources.hpp"
22#include "video/drawing_context.hpp"
23
24ItemTextField::ItemTextField(const std::string& text_, std::string* input_, int id_) :
25 MenuItem(text_, id_),
26 input(input_),
27 flickw(static_cast<int>(Resources::normal_font->get_text_width("_")))
28{
29}
30
31void
32ItemTextField::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) {
33 std::string r_input = *input;
34 bool fl = active && (int(g_real_time*2)%2);
35 if ( fl ) {
36 r_input += "_";
37 }
38 context.color().draw_text(Resources::normal_font, r_input,
39 Vector(pos.x + static_cast<float>(menu_width) - 16.0f - static_cast<float>(fl ? 0 : flickw),
40 pos.y - Resources::normal_font->get_height() / 2.0f),
41 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
42 context.color().draw_text(Resources::normal_font, get_text(),
43 Vector(pos.x + 16.0f,
44 pos.y - static_cast<float>(Resources::normal_font->get_height()) / 2.0f),
45 ALIGN_LEFT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
46}
47
48int
49ItemTextField::get_width() const {
50 return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + Resources::normal_font->get_text_width(*input) + 16.0f + static_cast<float>(flickw));
51}
52
53void
54ItemTextField::event(const SDL_Event& ev) {
55 if (ev.type == SDL_TEXTINPUT) {
56 *input += ev.text.text;
57 }
58}
59
60void
61ItemTextField::process_action(const MenuAction& action)
62{
63 if (action == MenuAction::REMOVE) {
64 if (input->length()) {
65 unsigned char last_char;
66 do {
67 last_char = *(--input->end());
68 input->resize(input->length() - 1);
69 if (input->length() == 0) {
70 break;
71 }
72 } while ( (last_char & 128) && !(last_char & 64) );
73 } else {
74 invalid_remove();
75 }
76 }
77}
78
79/* EOF */
80