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_stringselect.hpp"
18 
19 #include "gui/menu_manager.hpp"
20 #include "supertux/colorscheme.hpp"
21 #include "supertux/resources.hpp"
22 #include "video/drawing_context.hpp"
23 #include "video/surface.hpp"
24 
ItemStringSelect(const std::string & text,const std::vector<std::string> & list_,int * selected_,int id)25 ItemStringSelect::ItemStringSelect(const std::string& text, const std::vector<std::string>& list_, int* selected_, int id) :
26   MenuItem(text, id),
27   list(list_),
28   selected(selected_),
29   m_callback()
30 {
31 }
32 
33 void
draw(DrawingContext & context,const Vector & pos,int menu_width,bool active)34 ItemStringSelect::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active) {
35   float roff = static_cast<float>(Resources::arrow_left->get_width()) * 1.0f;
36   float sel_width = Resources::normal_font->get_text_width(list[*selected]);
37   // Draw left side
38   context.color().draw_text(Resources::normal_font, get_text(),
39                               Vector(pos.x + 16.0f,
40                                      pos.y - Resources::normal_font->get_height() / 2.0f),
41                               ALIGN_LEFT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
42 
43   // Draw right side
44   context.color().draw_surface(Resources::arrow_left,
45                                Vector(pos.x + static_cast<float>(menu_width) - sel_width - 2.0f * roff - 8.0f,
46                                       pos.y - 8.0f),
47                                LAYER_GUI);
48   context.color().draw_surface(Resources::arrow_right,
49                                Vector(pos.x + static_cast<float>(menu_width) - roff - 8.0f,
50                                       pos.y - 8.0f),
51                                LAYER_GUI);
52   context.color().draw_text(Resources::normal_font, list[*selected],
53                             Vector(pos.x + static_cast<float>(menu_width) - roff - 8.0f,
54                                    pos.y - Resources::normal_font->get_height() / 2.0f),
55                             ALIGN_RIGHT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
56 }
57 
58 int
get_width() const59 ItemStringSelect::get_width() const {
60   return static_cast<int>(Resources::normal_font->get_text_width(get_text()) + Resources::normal_font->get_text_width(list[*selected])) + 64;
61 }
62 
63 void
process_action(const MenuAction & action)64 ItemStringSelect::process_action(const MenuAction& action) {
65   switch (action) {
66     case MenuAction::LEFT:
67       if ( (*selected) > 0) {
68         (*selected)--;
69       } else {
70         (*selected) = static_cast<int>(list.size()) - 1;
71       }
72       MenuManager::instance().current_menu()->menu_action(*this);
73       if (m_callback) {
74         m_callback(*selected);
75       }
76       break;
77     case MenuAction::RIGHT:
78     case MenuAction::HIT:
79       if ( (*selected)+1 < int(list.size())) {
80         (*selected)++;
81       } else {
82         (*selected) = 0;
83       }
84       MenuManager::instance().current_menu()->menu_action(*this);
85       if (m_callback) {
86         m_callback(*selected);
87       }
88       break;
89     default:
90       break;
91   }
92 }
93 
94 /* EOF */
95