3 Copyright (C) 2016 est31, <MTest31@outlook.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes_extrabloated.h"
31 JA_FRUSTUM_HORIZONTAL,
34 // To know the count of enum values
38 struct JoystickAxisLayout {
40 // -1 if to invert, 1 if to keep it.
45 struct JoystickCombination {
47 virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
52 struct JoystickButtonCmb : public JoystickCombination {
54 JoystickButtonCmb() = default;
56 JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
57 filter_mask(filter_mask),
58 compare_mask(compare_mask)
63 virtual ~JoystickButtonCmb() = default;
65 virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
71 struct JoystickAxisCmb : public JoystickCombination {
73 JoystickAxisCmb() = default;
75 JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
76 axis_to_compare(axis_to_compare),
83 virtual ~JoystickAxisCmb() = default;
85 bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const override;
89 // if -1, thresh must be smaller than the axis value in order to trigger
90 // if 1, thresh must be bigger than the axis value in order to trigger
95 struct JoystickLayout {
96 std::vector<JoystickButtonCmb> button_keys;
97 std::vector<JoystickAxisCmb> axis_keys;
98 JoystickAxisLayout axes[JA_COUNT];
102 class JoystickController {
105 JoystickController();
107 void onJoystickConnect(const std::vector<irr::SJoystickInfo> &joystick_infos);
109 bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
112 bool wasKeyDown(GameKeyType b)
114 bool r = m_past_keys_pressed[b];
115 m_past_keys_pressed[b] = false;
119 bool wasKeyReleased(GameKeyType b)
121 return m_keys_released[b];
123 void clearWasKeyReleased(GameKeyType b)
125 m_keys_released[b] = false;
128 bool wasKeyPressed(GameKeyType b)
130 return m_keys_pressed[b];
132 void clearWasKeyPressed(GameKeyType b)
134 m_keys_pressed[b] = false;
137 bool isKeyDown(GameKeyType b)
139 return m_keys_down[b];
142 s16 getAxis(JoystickAxis axis)
144 return m_axes_vals[axis];
147 s16 getAxisWithoutDead(JoystickAxis axis);
152 void setLayoutFromControllerName(const std::string &name);
154 JoystickLayout m_layout;
156 s16 m_axes_vals[JA_COUNT];
158 u8 m_joystick_id = 0;
160 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_down;
161 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_pressed;
165 f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
167 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_keys_pressed;
168 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_keys_released;