1 |
|
|
/* |
2 |
|
|
* The ManaPlus Client |
3 |
|
|
* Copyright (C) 2004-2009 The Mana World Development Team |
4 |
|
|
* Copyright (C) 2009-2010 The Mana Developers |
5 |
|
|
* Copyright (C) 2011-2019 The ManaPlus Developers |
6 |
|
|
* Copyright (C) 2019-2021 Andrei Karas |
7 |
|
|
* |
8 |
|
|
* This file is part of The ManaPlus Client. |
9 |
|
|
* |
10 |
|
|
* This program is free software; you can redistribute it and/or modify |
11 |
|
|
* it under the terms of the GNU General Public License as published by |
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
13 |
|
|
* any later version. |
14 |
|
|
* |
15 |
|
|
* This program is distributed in the hope that it will be useful, |
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
|
|
* GNU General Public License for more details. |
19 |
|
|
* |
20 |
|
|
* You should have received a copy of the GNU General Public License |
21 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
#ifndef INPUT_JOYSTICK_H |
25 |
|
|
#define INPUT_JOYSTICK_H |
26 |
|
|
|
27 |
|
|
#include "events/inputevent.h" |
28 |
|
|
|
29 |
|
|
PRAGMA48(GCC diagnostic push) |
30 |
|
|
PRAGMA48(GCC diagnostic ignored "-Wshadow") |
31 |
|
|
#include <SDL_events.h> |
32 |
|
|
PRAGMA48(GCC diagnostic pop) |
33 |
|
|
|
34 |
|
|
class Joystick final |
35 |
|
|
{ |
36 |
|
|
public: |
37 |
|
|
/** |
38 |
|
|
* Number of buttons we can handle. |
39 |
|
|
*/ |
40 |
|
|
enum |
41 |
|
|
{ |
42 |
|
|
MAX_BUTTONS = 64 |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* Directions, to be used as bitmask values. |
47 |
|
|
*/ |
48 |
|
|
enum |
49 |
|
|
{ |
50 |
|
|
UP = 1, |
51 |
|
|
DOWN = 2, |
52 |
|
|
LEFT = 4, |
53 |
|
|
RIGHT = 8 |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
/** |
57 |
|
|
* Initializes the joystick subsystem. |
58 |
|
|
*/ |
59 |
|
|
static void init(); |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* Returns the number of available joysticks. |
63 |
|
|
*/ |
64 |
|
|
static int getNumberOfJoysticks() A_WARN_UNUSED |
65 |
|
|
{ return joystickCount; } |
66 |
|
|
|
67 |
|
|
/** |
68 |
|
|
* Constructor, pass the number of the joystick the new object |
69 |
|
|
* should access. |
70 |
|
|
*/ |
71 |
|
|
explicit Joystick(const int no); |
72 |
|
|
|
73 |
|
|
A_DELETE_COPY(Joystick) |
74 |
|
|
|
75 |
|
|
~Joystick(); |
76 |
|
|
|
77 |
|
|
bool open(); |
78 |
|
|
|
79 |
|
|
void close(); |
80 |
|
|
|
81 |
|
|
static bool isEnabled() A_WARN_UNUSED |
82 |
|
|
{ return mEnabled; } |
83 |
|
|
|
84 |
|
|
void setNumber(const int n); |
85 |
|
|
|
86 |
|
|
constexpr2 static void setEnabled(const bool enabled) noexcept2 |
87 |
|
|
{ mEnabled = enabled; } |
88 |
|
|
|
89 |
|
|
static void getNames(STD_VECTOR <std::string> &names); |
90 |
|
|
|
91 |
|
|
/** |
92 |
|
|
* Updates the direction and button information. |
93 |
|
|
*/ |
94 |
|
|
void logic(); |
95 |
|
|
|
96 |
|
|
void startCalibration(); |
97 |
|
|
|
98 |
|
|
void finishCalibration(); |
99 |
|
|
|
100 |
|
|
bool isCalibrating() const noexcept2 A_WARN_UNUSED |
101 |
|
|
{ return mCalibrating; } |
102 |
|
|
|
103 |
|
|
bool buttonPressed(const unsigned char no) const A_WARN_UNUSED; |
104 |
|
|
|
105 |
|
|
bool isUp() const noexcept2 A_WARN_UNUSED |
106 |
|
|
{ return mEnabled && ((mDirection & UP) != 0); } |
107 |
|
|
|
108 |
|
|
bool isDown() const noexcept2 A_WARN_UNUSED |
109 |
|
|
{ return mEnabled && ((mDirection & DOWN) != 0); } |
110 |
|
|
|
111 |
|
|
bool isLeft() const noexcept2 A_WARN_UNUSED |
112 |
|
|
{ return mEnabled && ((mDirection & LEFT) != 0); } |
113 |
|
|
|
114 |
|
|
bool isRight() const noexcept2 A_WARN_UNUSED |
115 |
|
|
{ return mEnabled && ((mDirection & RIGHT) != 0); } |
116 |
|
|
|
117 |
|
|
int getNumber() const noexcept2 A_WARN_UNUSED |
118 |
|
|
{ return mNumber; } |
119 |
|
|
|
120 |
|
|
void setUseInactive(const bool b) |
121 |
|
|
{ mUseInactive = b; } |
122 |
|
|
|
123 |
|
|
void update(); |
124 |
|
|
|
125 |
|
|
KeysVector *getActionVector(const SDL_Event &event) A_WARN_UNUSED; |
126 |
|
|
|
127 |
|
|
KeysVector *getActionVectorByKey(const int i) A_WARN_UNUSED; |
128 |
|
|
|
129 |
|
|
int getButtonFromEvent(const SDL_Event &event) const A_WARN_UNUSED; |
130 |
|
|
|
131 |
|
|
bool isActionActive(const InputActionT index) const A_WARN_UNUSED; |
132 |
|
|
|
133 |
|
|
bool validate() const A_WARN_UNUSED; |
134 |
|
|
|
135 |
|
|
void handleRepeat(const int time); |
136 |
|
|
|
137 |
|
|
void resetRepeat(const int key); |
138 |
|
|
|
139 |
|
|
void reload(); |
140 |
|
|
|
141 |
|
|
protected: |
142 |
|
|
unsigned char mDirection; |
143 |
|
|
|
144 |
|
|
bool mActiveButtons[MAX_BUTTONS]; |
145 |
|
|
|
146 |
|
|
SDL_Joystick *mJoystick; |
147 |
|
|
|
148 |
|
|
int mUpTolerance; |
149 |
|
|
int mDownTolerance; |
150 |
|
|
int mLeftTolerance; |
151 |
|
|
int mRightTolerance; |
152 |
|
|
bool mCalibrating; |
153 |
|
|
int mNumber; |
154 |
|
|
bool mCalibrated; |
155 |
|
|
int mButtonsNumber; |
156 |
|
|
bool mUseInactive; |
157 |
|
|
bool mHaveHats; |
158 |
|
|
|
159 |
|
|
KeyToActionMap mKeyToAction; |
160 |
|
|
|
161 |
|
|
KeyToIdMap mKeyToId; |
162 |
|
|
|
163 |
|
|
KeyTimeMap mKeyTimeMap; |
164 |
|
|
|
165 |
|
|
/** |
166 |
|
|
* Is joystick support enabled. |
167 |
|
|
*/ |
168 |
|
|
static bool mEnabled; |
169 |
|
|
static int joystickCount; |
170 |
|
|
|
171 |
|
|
void doCalibration(); |
172 |
|
|
}; |
173 |
|
|
|
174 |
|
|
extern Joystick *joystick; |
175 |
|
|
|
176 |
|
|
#endif // INPUT_JOYSTICK_H |