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_intfield.hpp"
18
19#include "supertux/colorscheme.hpp"
20#include "supertux/globals.hpp"
21#include "supertux/resources.hpp"
22#include "video/drawing_context.hpp"
23
24ItemIntField::ItemIntField(const std::string& text_, int* input_, int id_) :
25 MenuItem(text_, id_),
26 number(input_),
27 input(std::to_string(*input_)),
28 flickw(static_cast<int>(Resources::normal_font->get_text_width("_")))
29{
30}
31
32void
33ItemIntField::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) {
34 std::string r_input = input;
35 bool fl = active && (int(g_real_time*2)%2);
36 if ( fl ) {
37 r_input += "_";
38 }
39 context.color().draw_text(Resources::normal_font, r_input,
40 Vector(pos.x + static_cast<float>(menu_width) - 16 - static_cast<float>(fl ? 0 : flickw),
41 pos.y - Resources::normal_font->get_height() / 2.0f),
42 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
43 context.color().draw_text(Resources::normal_font, get_text(),
44 Vector(pos.x + 16.0f,
45 pos.y - Resources::normal_font->get_height() / 2.0f),
46 ALIGN_LEFT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
47}
48
49int
50ItemIntField::get_width() const {
51 return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + Resources::normal_font->get_text_width(input)) + 16 + flickw;
52}
53
54void
55ItemIntField::event(const SDL_Event& ev) {
56 if (ev.type == SDL_TEXTINPUT) {
57 std::string txt = ev.text.text;
58 for (auto& c : txt) {
59 add_char(c);
60 }
61 }
62}
63
64void
65ItemIntField::add_char(char c) {
66 if (c == '-') {
67 if (input.length() && input != "0") {
68 *number *= -1;
69 input = std::to_string(*number);
70 } else {
71 input = "-";
72 }
73 }
74
75 if (c < '0' || c > '9') {
76 return;
77 }
78
79 input.push_back(c);
80 try {
81 int new_number = std::stoi(input);
82 *number = new_number;
83 } catch (...) {
84 input = std::to_string(*number);
85 }
86}
87
88void
89ItemIntField::process_action(const MenuAction& action) {
90 if (action == MenuAction::REMOVE && input.length()) {
91 unsigned char last_char;
92 do {
93 last_char = *(--input.end());
94 input.resize(input.length() - 1);
95 if (input.length() == 0) {
96 break;
97 }
98 } while ( (last_char & 128) && !(last_char & 64) );
99 if (input.length() && input != "-") {
100 *number = std::stoi(input);
101 } else {
102 *number = 0;
103 }
104 }
105}
106
107/* EOF */
108