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_toggle.hpp"
18 
19 #include "supertux/colorscheme.hpp"
20 #include "supertux/resources.hpp"
21 #include "video/drawing_context.hpp"
22 #include "video/surface.hpp"
23 
ItemToggle(const std::string & text_,bool * toggled,int id_)24 ItemToggle::ItemToggle(const std::string& text_, bool* toggled, int id_) :
25   MenuItem(text_, id_),
26   m_get_func([toggled]{ return *toggled; }),
__anon81386b580202(bool value)27   m_set_func([toggled](bool value){ *toggled = value; })
28 {
29 }
30 
ItemToggle(const std::string & text_,std::function<bool ()> get_func,std::function<void (bool)> set_func,int id_)31 ItemToggle::ItemToggle(const std::string& text_,
32                        std::function<bool()> get_func,
33                        std::function<void(bool)> set_func,
34                        int id_) :
35   MenuItem(text_, id_),
36   m_get_func(std::move(get_func)),
37   m_set_func(std::move(set_func))
38 {
39 }
40 
41 void
draw(DrawingContext & context,const Vector & pos,int menu_width,bool active)42 ItemToggle::draw(DrawingContext& context, const Vector& pos, int menu_width, bool active)
43 {
44   context.color().draw_text(Resources::normal_font, get_text(),
45                             Vector(pos.x + 16, pos.y - (Resources::normal_font->get_height()/2)),
46                             ALIGN_LEFT, LAYER_GUI, active ? ColorScheme::Menu::active_color : get_color());
47 
48   if (m_get_func()) {
49     context.color().draw_surface(Resources::checkbox_checked,
50                                  Vector(pos.x + static_cast<float>(menu_width) - 16.0f - static_cast<float>(Resources::checkbox->get_width()),
51                                         pos.y - 8.0f),
52                                  LAYER_GUI);
53   } else {
54     context.color().draw_surface(Resources::checkbox,
55                                  Vector(pos.x + static_cast<float>(menu_width) - 16.0f - static_cast<float>(Resources::checkbox->get_width()),
56                                         pos.y - 8.0f),
57                                  LAYER_GUI);
58   }
59 }
60 
61 int
get_width() const62 ItemToggle::get_width() const
63 {
64   return static_cast<int>(Resources::normal_font->get_text_width(get_text())) + 16 + Resources::checkbox->get_width();
65 }
66 
67 void
process_action(const MenuAction & action)68 ItemToggle::process_action(const MenuAction& action)
69 {
70   if (action == MenuAction::HIT) {
71     m_set_func(!m_get_func());
72   }
73 }
74 
75 /* EOF */
76