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 "control/controller.hpp"
18
19#include <ostream>
20
21namespace {
22
23const char* g_control_names[] = {
24 "left",
25 "right",
26 "up",
27 "down",
28 "jump",
29 "action",
30 "start",
31 "escape",
32 "menu-select",
33 "menu-select-space",
34 "menu-back",
35 "remove",
36 "cheat-menu",
37 "debug-menu",
38 "console",
39 "peek-left",
40 "peek-right",
41 "peek-up",
42 "peek-down",
43 nullptr
44};
45
46} // namespace
47
48std::ostream& operator<<(std::ostream& os, Control control)
49{
50 return os << g_control_names[static_cast<int>(control)];
51}
52
53std::string Control_to_string(Control control)
54{
55 return g_control_names[static_cast<int>(control)];
56}
57
58boost::optional<Control> Control_from_string(const std::string& text)
59{
60 for(int i = 0; g_control_names[i] != nullptr; ++i) {
61 if (text == g_control_names[i]) {
62 return static_cast<Control>(i);
63 }
64 }
65
66 return boost::none;
67}
68
69Controller::Controller()
70{
71 reset();
72}
73
74Controller::~Controller()
75{}
76
77void
78Controller::reset()
79{
80 for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
81 m_controls[i] = false;
82 m_old_controls[i] = false;
83 }
84}
85
86void
87Controller::set_control(Control control, bool value)
88{
89 m_controls[static_cast<int>(control)] = value;
90}
91
92bool
93Controller::hold(Control control) const
94{
95 return m_controls[static_cast<int>(control)];
96}
97
98bool
99Controller::pressed(Control control) const
100{
101 return !m_old_controls[static_cast<int>(control)] && m_controls[static_cast<int>(control)];
102}
103
104bool
105Controller::released(Control control) const
106{
107 return m_old_controls[static_cast<int>(control)] && !m_controls[static_cast<int>(control)];
108}
109
110void
111Controller::update()
112{
113 for (int i = 0; i < static_cast<int>(Control::CONTROLCOUNT); ++i) {
114 m_old_controls[i] = m_controls[i];
115 }
116}
117
118/* EOF */
119