File: Qt5InputManager.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (274 lines) | stat: -rw-r--r-- 6,741 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#pragma once

#if defined(WITH_QT5) || defined(DOXYGEN_GENERATING_OUTPUT)

#include <qevent.h>
#include "../Input/IInputManager.h"

#if defined(WITH_QT5GAMEPAD)
class QGamepadManager;
class QGamepad;
#endif

class QKeyEvent;
class QMouseEvent;
class QTouchEvent;
class QWheelEvent;

namespace nCine::Backends
{
	class Qt5Widget;

	/// Utility functions to convert between engine key enumerations and Qt5 ones
	class Qt5Keys
	{
	public:
		static Keys keySymValueToEnum(int keysym);
		static int keyModMaskToEnumMask(Qt::KeyboardModifiers keymod);
	};

	/// Information about Qt5 mouse state
	class Qt5MouseState : public MouseState
	{
	public:
		Qt5MouseState()
			: buttons_(Qt::NoButton) {}

		inline bool isLeftButtonDown() const override {
			return buttons_ & Qt::LeftButton;
		}
		inline bool isMiddleButtonDown() const override {
			return buttons_ & Qt::MiddleButton;
		}
		inline bool isRightButtonDown() const override {
			return buttons_ & Qt::RightButton;
		}
		inline bool isFourthButtonDown() const override {
			return buttons_ & Qt::BackButton;
		}
		inline bool isFifthButtonDown() const override {
			return buttons_ & Qt::ForwardButton;
		}

	private:
		Qt::MouseButtons buttons_;

		friend class Qt5InputManager;
	};

	/// Information about a Qt5 mouse event
	class Qt5MouseEvent : public MouseEvent
	{
	public:
		Qt5MouseEvent()
			: button_(Qt::NoButton) {}

		inline bool isLeftButton() const override {
			return button_ & Qt::LeftButton;
		}
		inline bool isMiddleButton() const override {
			return button_ & Qt::MiddleButton;
		}
		inline bool isRightButton() const override {
			return button_ & Qt::RightButton;
		}
		inline bool isFourthButton() const override {
			return button_ & Qt::BackButton;
		}
		inline bool isFifthButton() const override {
			return button_ & Qt::ForwardButton;
		}

	private:
		Qt::MouseButton button_;

		friend class Qt5InputManager;
	};

	/// Information about a Qt5 scroll event
	class Qt5ScrollEvent : public ScrollEvent
	{
	public:
		Qt5ScrollEvent() {}

		friend class Qt5InputManager;
	};

	//// Simulated information about Qt5 keyboard state
	class Qt5KeyboardState : public KeyboardState
	{
	public:
		Qt5KeyboardState()
		{
			for (unsigned int i = 0; i < NumKeys; i++)
				keys_[i] = 0;
		}

		inline bool isKeyDown(Keys key) const override
		{
			if (key == Keys::Unknown)
				return false;
			else
				return keys_[static_cast<unsigned int>(key)] != 0;
		}

	private:
		static const unsigned int NumKeys = static_cast<unsigned int>(Keys::COUNT);
		unsigned char keys_[NumKeys];

		friend class Qt5InputManager;
	};

	/// Information about Qt5 joystick state
#ifdef WITH_QT5GAMEPAD
	class Qt5JoystickState : public JoystickState
	{
	public:
		Qt5JoystickState();

		bool isButtonPressed(int buttonId) const override;
		unsigned char hatState(int hatId) const override;
		float axisValue(int axisId) const override;

	private:
		static const unsigned int MaxNameLength = 256;
		static const unsigned int NumButtons = 12;
		static const unsigned int NumAxes = 6;
		/// Minimum difference between two axis readings in order to trigger an event
		static const float AxisEventTolerance;
		bool buttonState_[NumButtons];
		unsigned char hatState_;
		float axesValuesState_[NumAxes];

		char name_[MaxNameLength];
		std::unique_ptr<QGamepad> gamepad_;

		friend class Qt5InputManager;
	};
#else
	class Qt5JoystickState : public JoystickState
	{
	public:
		Qt5JoystickState() {}

		inline bool isButtonPressed(int buttonId) const override {
			return false;
		}
		inline unsigned char hatState(int hatId) const override {
			return HatState::CENTERED;
		}
		inline float axisValue(int axisId) const override {
			return 0.0f;
		}

	private:
		friend class Qt5InputManager;
	};
#endif

	/// Class for parsing and dispatching Qt5 input events
	class Qt5InputManager : public IInputManager
	{
	public:
		/// The constructor takes care of opening available joysticks
		Qt5InputManager(Qt5Widget& widget);
		/// The destructor releases every opened joystick
		~Qt5InputManager();

#ifdef WITH_QT5GAMEPAD
		void updateJoystickStates();
#endif

		bool shouldQuitOnRequest();
		bool event(QEvent* event);
		void keyPressEvent(QKeyEvent* event);
		void keyReleaseEvent(QKeyEvent* event);
		void mousePressEvent(QMouseEvent* event);
		void mouseReleaseEvent(QMouseEvent* event);
		void mouseMoveEvent(QMouseEvent* event);
		void touchBeginEvent(QTouchEvent* event);
		void touchUpdateEvent(QTouchEvent* event);
		void touchEndEvent(QTouchEvent* event);
		void wheelEvent(QWheelEvent* event);

		inline const MouseState& mouseState() const override {
			return mouseState_;
		}
		inline const KeyboardState& keyboardState() const override {
			return keyboardState_;
		}

#ifdef WITH_QT5GAMEPAD
		bool isJoyPresent(int joyId) const override;
		const char* joyName(int joyId) const override;
		const JoystickGuid joyGuid(int joyId) const override {
			return { };
		}
		inline int joyNumButtons(int joyId) const override {
			return Qt5JoystickState::NumButtons;
		}
		inline int joyNumHats(int joyId) const override {
			return 1;
		}
		inline int joyNumAxes(int joyId) const override {
			return Qt5JoystickState::NumAxes;
		}
		const JoystickState& joystickState(int joyId) const override;
#else
		inline bool isJoyPresent(int joyId) const override {
			return false;
		}
		inline const char* joyName(int joyId) const override {
			return nullptr;
		}
		inline const JoystickGuid joyGuid(int joyId) const override {
			return { };
		}
		inline int joyNumButtons(int joyId) const override {
			return 0;
		}
		inline int joyNumHats(int joyId) const override {
			return 0;
		}
		inline int joyNumAxes(int joyId) const override {
			return 0;
		}
		inline const JoystickState& joystickState(int joyId) const override {
			return nullJoystickState_;
		}
#endif

		void setCursor(Cursor cursor) override;

	private:
		static const int MaxNumJoysticks = 4;

		static TouchEvent touchEvent_;
		static Qt5MouseState mouseState_;
		static Qt5MouseEvent mouseEvent_;
		static Qt5ScrollEvent scrollEvent_;
		static Qt5KeyboardState keyboardState_;
		static KeyboardEvent keyboardEvent_;
		static TextInputEvent textInputEvent_;
		static Qt5JoystickState nullJoystickState_;
#ifdef WITH_QT5GAMEPAD
		static Qt5JoystickState joystickStates_[MaxNumJoysticks];
		static JoyButtonEvent joyButtonEvent_;
		static JoyHatEvent joyHatEvent_;
		static JoyAxisEvent joyAxisEvent_;
		static JoyConnectionEvent joyConnectionEvent_;
#endif

		Qt5Widget& widget_;

		void updateTouchEvent(const QTouchEvent* event);

		/// Deleted copy constructor
		Qt5InputManager(const Qt5InputManager&) = delete;
		/// Deleted assignment operator
		Qt5InputManager& operator=(const Qt5InputManager&) = delete;
	};

}

#endif