02eb90c78c677656501723af9cca33a18f8e394e
[supertux.git] / src / gui / menu_item.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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/menu_item.hpp"
18
19 #include "supertux/resources.hpp"
20 #include "supertux/timer.hpp"
21 #include "video/font.hpp"
22 #include <stdio.h>
23
24 static const float FLICK_CURSOR_TIME   = 0.5f;
25
26 MenuItem::MenuItem(MenuItemKind _kind, int _id) :
27   kind(_kind),
28   id(_id),
29   toggled(),
30   text(),
31   input(),
32   help(),
33   list(),
34   selected(),
35   target_menu(),
36   input_flickering()
37 {
38   toggled = false;
39   selected = false;
40   target_menu = 0;
41 }
42
43 void
44 MenuItem::change_text(const  std::string& text_)
45 {
46   text = text_;
47 }
48
49 void
50 MenuItem::change_input(const  std::string& text_)
51 {
52   input = text_;
53 }
54
55 void
56 MenuItem::set_help(const std::string& help_text)
57 {
58   std::string overflow;
59   help = Resources::normal_font->wrap_to_width(help_text, 600, &overflow);
60   while (!overflow.empty())
61   {
62     help += "\n";
63     help += Resources::normal_font->wrap_to_width(overflow, 600, &overflow);
64   }
65 }
66
67 std::string
68 MenuItem::get_input_with_symbol(bool active_item)
69 {
70   if(!active_item) {
71     input_flickering = true;
72   } else {
73     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
74   }
75
76   char str[1024];
77   if(input_flickering)
78     snprintf(str, sizeof(str), "%s ",input.c_str());
79   else
80     snprintf(str, sizeof(str), "%s_",input.c_str());
81
82   std::string string = str;
83
84   return string;
85 }
86
87 /* EOF */