ManaPlus
joystick.h
Go to the documentation of this file.
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 
35 {
36  public:
40  enum
41  {
42  MAX_BUTTONS = 64
43  };
44 
48  enum
49  {
50  UP = 1,
51  DOWN = 2,
52  LEFT = 4,
53  RIGHT = 8
54  };
55 
59  static void init();
60 
65  { return joystickCount; }
66 
71  explicit Joystick(const int no);
72 
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 
94  void logic();
95 
96  void startCalibration();
97 
98  void finishCalibration();
99 
101  { return mCalibrating; }
102 
103  bool buttonPressed(const unsigned char no) const A_WARN_UNUSED;
104 
106  { return mEnabled && ((mDirection & UP) != 0); }
107 
109  { return mEnabled && ((mDirection & DOWN) != 0); }
110 
112  { return mEnabled && ((mDirection & LEFT) != 0); }
113 
115  { return mEnabled && ((mDirection & RIGHT) != 0); }
116 
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 
168  static bool mEnabled;
169  static int joystickCount;
170 
171  void doCalibration();
172 };
173 
174 extern Joystick *joystick;
175 
176 #endif // INPUT_JOYSTICK_H
bool isCalibrating() const
Definition: joystick.h:100
bool isRight() const
Definition: joystick.h:114
static void setEnabled(const bool enabled)
Definition: joystick.h:86
bool isDown() const
Definition: joystick.h:108
static int getNumberOfJoysticks()
Definition: joystick.h:64
void setUseInactive(const bool b)
Definition: joystick.h:120
bool isLeft() const
Definition: joystick.h:111
bool isUp() const
Definition: joystick.h:105
static bool isEnabled()
Definition: joystick.h:81
int getNumber() const
Definition: joystick.h:117
InputAction ::T InputActionT
Definition: inputaction.h:717
std::map< int, KeysVector > KeyToActionMap
Definition: inputevent.h:40
std::map< int, InputActionT > KeyToIdMap
Definition: inputevent.h:43
std::vector< InputActionT > KeysVector
Definition: inputevent.h:34
std::map< int, int > KeyTimeMap
Definition: inputevent.h:46
Joystick * joystick
Definition: joystick.cpp:43
#define constexpr2
Definition: localconsts.h:49
#define A_WARN_UNUSED
Definition: localconsts.h:161
#define noexcept2
Definition: localconsts.h:50
#define final
Definition: localconsts.h:46
#define PRAGMA48(str)
Definition: localconsts.h:199
#define A_DELETE_COPY(func)
Definition: localconsts.h:53
void init()
Definition: playerinfo.cpp:434
TcpNet::Socket open(IPaddress *const ip)
Definition: sdltcpnet.cpp:110
void update()
Definition: useragent.cpp:32
int close(File *const file)
Definition: fs.cpp:808