supertux
controller.hpp
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 #ifndef HEADER_SUPERTUX_CONTROL_CONTROLLER_HPP
18 #define HEADER_SUPERTUX_CONTROL_CONTROLLER_HPP
19 
20 #include <iosfwd>
21 #include <optional>
22 #include <string>
23 
24 enum class Control {
25  LEFT = 0,
26  RIGHT,
27  UP,
28  DOWN,
29 
30  JUMP,
31  ACTION,
32 
33  START,
34  ESCAPE,
35  MENU_SELECT,
36  MENU_SELECT_SPACE,
37  MENU_BACK,
38  REMOVE,
39 
40  CHEAT_MENU,
41  DEBUG_MENU,
42  CONSOLE,
43 
44  PEEK_LEFT,
45  PEEK_RIGHT,
46  PEEK_UP,
47  PEEK_DOWN,
48 
49  CONTROLCOUNT
50 };
51 
52 std::ostream& operator<<(std::ostream& os, Control control);
53 
54 std::string Control_to_string(Control control);
55 std::optional<Control> Control_from_string(const std::string& text);
56 
58 {
59 public:
60  Controller();
61  virtual ~Controller();
62 
63  virtual void update();
64 
65  void set_control(Control control, bool value);
66 
67  void set_jump_key_with_up(bool value);
68 
70  void set_touchscreen(bool value);
71 
73  bool hold(Control control) const;
74 
76  bool pressed(Control control) const;
77 
78  template<typename... Control>
79  bool pressed_any(Control&&... controls) const
80  {
81  for(const auto& control : {controls... })
82  {
83  if(pressed(control))
84  return true;
85  }
86  return false;
87  }
88 
90  bool released(Control control) const;
91 
92  void reset();
93 
95  bool is_touchscreen() const;
96 
97 protected:
99  bool m_controls[static_cast<int>(Control::CONTROLCOUNT)];
100 
102  bool m_old_controls[static_cast<int>(Control::CONTROLCOUNT)];
103 
106 
109 
110 private:
111  Controller(const Controller&) = delete;
112  Controller& operator=(const Controller&) = delete;
113 };
114 
115 #endif
116 
117 /* EOF */
Definition: controller.hpp:57
bool m_touchscreen
the event has been generated by touchscreen
Definition: controller.hpp:105
bool m_jump_key_pressed
the jump key is pressed for this controller
Definition: controller.hpp:108