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_floatfield.hpp"
18
19#include "supertux/colorscheme.hpp"
20#include "supertux/globals.hpp"
21#include "supertux/resources.hpp"
22#include "video/drawing_context.hpp"
23
24ItemFloatField::ItemFloatField(const std::string& text_, float* 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 has_comma(true)
30{
31 // removing all redundant zeros at the end
32 for (auto i = input.end() - 1; i != input.begin(); --i) {
33 char c = *i;
34 if (c == '.') {
35 input.resize(input.length() - 1);
36 has_comma = false;
37 }
38 if (c != '0') {
39 break;
40 }
41 input.resize(input.length() - 1);
42 }
43}
44
45void
46ItemFloatField::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) {
47 std::string r_input = input;
48 bool fl = active && (int(g_real_time*2)%2);
49 if ( fl ) {
50 r_input += "_";
51 }
52 context.color().draw_text(Resources::normal_font, r_input,
53 Vector(pos.x + static_cast<float>(menu_width) - 16.0f - static_cast<float>(fl ? 0 : flickw),
54 pos.y - Resources::normal_font->get_height() / 2.0f),
55 ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::field_color);
56 context.color().draw_text(Resources::normal_font, get_text(),
57 Vector(pos.x + 16.0f,
58 pos.y - Resources::normal_font->get_height() / 2.0f),
59 ALIGN_LEFT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
60}
61
62int
63ItemFloatField::get_width() const {
64 return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + Resources::normal_font->get_text_width(input)) + 16 + flickw;
65}
66
67void
68ItemFloatField::event(const SDL_Event& ev) {
69 if (ev.type == SDL_TEXTINPUT) {
70 std::string txt = ev.text.text;
71 for (auto i = txt.begin(); i != txt.end(); ++i) {
72 add_char(*i);
73 }
74 }
75}
76
77void
78ItemFloatField::add_char(char c) {
79 if (c == '-') {
80 if (input.length() && input != "0") {
81 *number *= -1;
82 if (*input.begin() == '-') {
83 input.erase(input.begin());
84 } else {
85 input.insert(input.begin(),'-');
86 }
87 } else {
88 input = "-";
89 }
90 } else if (!has_comma && (c == '.' || c == ',')) {
91 if (!input.length()) {
92 input = "0.";
93 } else {
94 input.push_back('.');
95 }
96 has_comma = true;
97 }
98
99 if (c < '0' || c > '9') {
100 return;
101 }
102
103 input.push_back(c);
104 try {
105 float new_number = std::stof(input);
106 *number = new_number;
107 } catch (...) {
108 input = std::to_string(*number);
109 }
110}
111
112void
113ItemFloatField::process_action(const MenuAction& action) {
114 if (action == MenuAction::REMOVE && input.length()) {
115 unsigned char last_char;
116 do {
117 last_char = *(--input.end());
118 input.resize(input.length() - 1);
119 if (input.length() == 0) {
120 break;
121 }
122 if (last_char == '.') {
123 has_comma = false;
124 }
125 } while ( (last_char & 128) && !(last_char & 64) );
126 if (input.length() && input != "-") {
127 try {
128 *number = std::stof(input);
129 }
130 catch(...) {
131 input = std::to_string(*number);
132 }
133 } else {
134 *number = 0;
135 }
136 }
137}
138
139/* EOF */
140