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.
20 #ifndef JOYSTICK_HEADER
21 #define JOYSTICK_HEADER
23 #include "irrlichttypes_extrabloated.h"
32 JA_FRUSTUM_HORIZONTAL,
35 // To know the count of enum values
39 struct JoystickAxisLayout {
41 // -1 if to invert, 1 if to keep it.
46 struct JoystickCombination {
48 virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const=0;
53 struct JoystickButtonCmb : public JoystickCombination {
55 JoystickButtonCmb() {}
56 JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
57 filter_mask(filter_mask),
58 compare_mask(compare_mask)
63 virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
69 struct JoystickAxisCmb : public JoystickCombination {
72 JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
73 axis_to_compare(axis_to_compare),
80 virtual bool isTriggered(const irr::SEvent::SJoystickEvent &ev) const;
84 // if -1, thresh must be smaller than the axis value in order to trigger
85 // if 1, thresh must be bigger than the axis value in order to trigger
90 struct JoystickLayout {
91 std::vector<JoystickButtonCmb> button_keys;
92 std::vector<JoystickAxisCmb> axis_keys;
93 JoystickAxisLayout axes[JA_COUNT];
97 class JoystickController {
100 JoystickController();
101 bool handleEvent(const irr::SEvent::SJoystickEvent &ev);
104 bool wasKeyDown(GameKeyType b)
106 bool r = m_past_pressed_keys[b];
107 m_past_pressed_keys[b] = false;
110 bool getWasKeyDown(GameKeyType b)
112 return m_past_pressed_keys[b];
114 void clearWasKeyDown(GameKeyType b)
116 m_past_pressed_keys[b] = false;
119 bool wasKeyReleased(GameKeyType b)
121 bool r = m_past_released_keys[b];
122 m_past_released_keys[b] = false;
125 bool getWasKeyReleased(GameKeyType b)
127 return m_past_pressed_keys[b];
129 void clearWasKeyReleased(GameKeyType b)
131 m_past_pressed_keys[b] = false;
134 bool isKeyDown(GameKeyType b)
136 return m_pressed_keys[b];
139 s16 getAxis(JoystickAxis axis)
141 return m_axes_vals[axis];
144 s16 getAxisWithoutDead(JoystickAxis axis);
149 const JoystickLayout *m_layout;
151 s16 m_axes_vals[JA_COUNT];
153 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_pressed_keys;
157 f32 m_past_pressed_time[KeyType::INTERNAL_ENUM_COUNT];
159 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_pressed_keys;
160 std::bitset<KeyType::INTERNAL_ENUM_COUNT> m_past_released_keys;