RhythmGame 1.4.3
Loading...
Searching...
No Matches
InputTranslator.h
Go to the documentation of this file.
1//
2// Created by bobini on 09.12.23.
3//
4
5#ifndef GAMEPADINPUTTRANSLATOR_H
6#define GAMEPADINPUTTRANSLATOR_H
7#include "BmsKeys.h"
8#include "GamepadManager.h"
9#include "MidiManager.h"
10
11#include <QKeyEvent>
12#include <QObject>
13#include <QVariant>
14#include <QTimer>
15#include <magic_enum/magic_enum.hpp>
16#include <array>
17#include <chrono>
18#include <cstdint>
19#include <functional>
20#include <limits>
21#include <optional>
22
23namespace db {
24class SqliteCppDb;
25}
26namespace input {
27class Key
28{
29 Q_GADGET
35 Q_PROPERTY(QVariant gamepad MEMBER gamepad)
36 Q_PROPERTY(QVariant midi READ getMidi)
37 Q_PROPERTY(Device device MEMBER device)
38 Q_PROPERTY(quint32 code MEMBER code)
39 Q_PROPERTY(Direction direction MEMBER direction)
40
41 public:
42 enum class Device
43 {
45 Button,
46 Axis,
50 };
51 Q_ENUM(Device)
52 enum class Direction
53 {
54 None,
55 Up,
56 Down
57 };
58 Q_ENUM(Direction)
59
60 QVariant gamepad = QVariant::fromValue(nullptr);
62 quint32 code;
64
65 auto getMidi() const -> QVariant;
66
67 friend auto operator>>(QDataStream& stream, Key& key) -> QDataStream&;
68 friend auto operator<<(QDataStream& stream, const Key& key) -> QDataStream&;
69
70 auto operator<=>(const Key& key) const -> std::weak_ordering;
71 auto operator==(const Key& key) const -> bool;
72 auto operator!=(const Key& key) const -> bool;
73};
74} // namespace input
75
76template<>
77struct std::hash<input::Key>
78{
79 std::size_t operator()(const input::Key& s) const noexcept
80 {
81 auto gp = s.gamepad.canConvert<input::Gamepad>()
82 ? std::optional{ s.gamepad.value<input::Gamepad>() }
83 : std::nullopt;
84 auto midi = s.gamepad.canConvert<input::MidiDevice>()
85 ? std::optional{ s.gamepad.value<input::MidiDevice>() }
86 : std::nullopt;
87 return std::hash<std::optional<input::Gamepad>>{}(gp) ^
88 std::hash<std::optional<input::MidiDevice>>{}(midi) ^
89 std::hash<int>{}(s.code) ^
90 std::hash<int>{}(static_cast<int>(s.device)) ^
91 std::hash<int>{}(static_cast<int>(s.direction));
92 }
93};
94namespace input {
96{
97 Q_GADGET
98 Q_PROPERTY(Key key MEMBER key)
99 Q_PROPERTY(BmsKey button MEMBER button)
100
101 public:
102 Key key;
103 BmsKey button;
104
105 auto operator<=>(const Mapping&) const = default;
106
107 friend auto operator<<(QDataStream& stream, const Mapping& mapping)
108 -> QDataStream&;
109 friend auto operator>>(QDataStream& stream, Mapping& mapping)
110 -> QDataStream&;
111};
112
113class AnalogAxisConfig : public QObject
114{
115 Q_OBJECT
121 Q_PROPERTY(double triggerThreshold READ getTriggerThreshold WRITE
122 setTriggerThreshold NOTIFY triggerThresholdChanged)
128 Q_PROPERTY(double releaseThreshold READ getReleaseThreshold WRITE
129 setReleaseThreshold NOTIFY releaseThresholdChanged)
135 Q_PROPERTY(
136 uint timeout READ getTimeout WRITE setTimeout NOTIFY timeoutChanged)
140 Q_PROPERTY(ScratchAlgorithm scratchAlgorithm READ getScratchAlgorithm WRITE
141 setScratchAlgorithm NOTIFY scratchAlgorithmChanged)
142 public:
147 {
151 ScratchAlgorithmClassic
152 };
153 Q_ENUM(ScratchAlgorithm)
154 private:
155 double triggerThreshold = 0.008;
156 double releaseThreshold = 0.004;
157 uint timeout = 100;
158 ScratchAlgorithm algorithm = ScratchAlgorithmAnalog;
159
160 public:
161 auto getTriggerThreshold() const -> double;
162 void setTriggerThreshold(double value);
163 auto getReleaseThreshold() const -> double;
164 void setReleaseThreshold(double value);
165 auto getTimeout() const -> uint;
166 void setTimeout(uint value);
167 auto getScratchAlgorithm() const -> ScratchAlgorithm;
168 void setScratchAlgorithm(ScratchAlgorithm value);
169 explicit AnalogAxisConfig(QObject* parent = nullptr);
170 signals:
171 void triggerThresholdChanged();
172 void releaseThresholdChanged();
173 void timeoutChanged();
174 void scratchAlgorithmChanged();
175};
176
177class InputTranslator final : public QObject
178{
179 Q_OBJECT
183 Q_PROPERTY(bool configuring READ isConfiguring NOTIFY configuringChanged)
187 Q_PROPERTY(QVariant configuredButton READ getConfiguredButton WRITE
188 setConfiguredButton NOTIFY configuredButtonChanged)
189 Q_PROPERTY(QList<Mapping> keyConfig READ getKeyConfig WRITE setKeyConfig
190 NOTIFY keyConfigModified)
191 Q_PROPERTY(bool col11 READ col11 NOTIFY col11Changed)
192 Q_PROPERTY(bool col12 READ col12 NOTIFY col12Changed)
193 Q_PROPERTY(bool col13 READ col13 NOTIFY col13Changed)
194 Q_PROPERTY(bool col14 READ col14 NOTIFY col14Changed)
195 Q_PROPERTY(bool col15 READ col15 NOTIFY col15Changed)
196 Q_PROPERTY(bool col16 READ col16 NOTIFY col16Changed)
197 Q_PROPERTY(bool col17 READ col17 NOTIFY col17Changed)
198 Q_PROPERTY(bool col1sUp READ col1sUp NOTIFY col1sUpChanged)
199 Q_PROPERTY(bool col1sDown READ col1sDown NOTIFY col1sDownChanged)
200 Q_PROPERTY(bool col21 READ col21 NOTIFY col21Changed)
201 Q_PROPERTY(bool col22 READ col22 NOTIFY col22Changed)
202 Q_PROPERTY(bool col23 READ col23 NOTIFY col23Changed)
203 Q_PROPERTY(bool col24 READ col24 NOTIFY col24Changed)
204 Q_PROPERTY(bool col25 READ col25 NOTIFY col25Changed)
205 Q_PROPERTY(bool col26 READ col26 NOTIFY col26Changed)
206 Q_PROPERTY(bool col27 READ col27 NOTIFY col27Changed)
207 Q_PROPERTY(bool col2sUp READ col2sUp NOTIFY col2sUpChanged)
208 Q_PROPERTY(bool col2sDown READ col2sDown NOTIFY col2sDownChanged)
209 Q_PROPERTY(bool start1 READ start1 NOTIFY start1Changed)
210 Q_PROPERTY(bool select1 READ select1 NOTIFY select1Changed)
211 Q_PROPERTY(bool start2 READ start2 NOTIFY start2Changed)
212 Q_PROPERTY(bool select2 READ select2 NOTIFY select2Changed)
213 Q_PROPERTY(input::AnalogAxisConfig* analogAxisConfig1 READ
214 getAnalogAxisConfig1 NOTIFY analogAxisConfig1Changed)
215 Q_PROPERTY(input::AnalogAxisConfig* analogAxisConfig2 READ
216 getAnalogAxisConfig2 NOTIFY analogAxisConfig2Changed)
217 Q_PROPERTY(double debounceMs READ getDebounceMs WRITE setDebounceMs NOTIFY
218 debounceMsChanged RESET resetDebounceMs)
219
220 public:
221 struct Scratch
222 {
223 std::unique_ptr<QTimer> timer = std::make_unique<QTimer>();
224 QMetaObject::Connection timerConnection;
225 double delta = 0;
226 Key::Direction direction = Key::Direction::None;
227 double value = std::numeric_limits<double>::quiet_NaN();
228 };
230 {
233 ClassicScratchTick
234 };
235 Q_ENUM(TickType)
236
237 private:
238 enum class ReleaseMode
239 {
240 Debounced,
241 Immediate
242 };
243
244 struct PendingRelease
245 {
246 int64_t timestamp;
247 std::optional<int64_t> inputDeadline;
248 };
249
250 struct ButtonState
251 {
252 bool physicallyPressed{};
253 bool logicallyPressed{};
254 std::optional<PendingRelease> pendingRelease;
255 QTimer debounceTimer;
256 };
257
258 struct PairHash
259 {
260 template<typename T, typename U>
261 auto operator()(const std::pair<T, U>& x) const -> std::size_t
262 {
263 return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
264 }
265 };
266 std::unordered_map<std::pair<Gamepad, uint8_t>, Scratch, PairHash>
267 scratches;
268 std::optional<BmsKey> configuredButton;
269 std::unordered_map<std::pair<Gamepad, uint8_t>, AnalogAxisConfig*, PairHash>
270 axisConfig;
271 QHash<Key, BmsKey> config;
273 std::array<ButtonState, magic_enum::enum_count<BmsKey>()> buttons;
274 std::array<QTimer, magic_enum::enum_count<BmsKey>()> tickTimers;
275 std::array<int, magic_enum::enum_count<BmsKey>()> tickNumbers{ {} };
276 std::array<TickType, magic_enum::enum_count<BmsKey>()> tickTypes{ {} };
277 std::optional<std::pair<Gamepad, uint8_t>> scratchAxis1;
278 std::optional<std::pair<Gamepad, uint8_t>> scratchAxis2;
279 double debounceMs = 5.0;
280
281 void pressButton(BmsKey button, int64_t time);
282 void releaseButton(BmsKey button,
283 int64_t time,
284 ReleaseMode mode = ReleaseMode::Debounced);
285 void commitPendingRelease(BmsKey button);
286 void finishRelease(BmsKey button, int64_t time);
287 auto debounceInterval() const -> std::chrono::milliseconds;
288 void unpressAndUnbind(const Key& key, int64_t time);
289 void bindKeyToButton(const Key& key, BmsKey button, int64_t time);
290 void saveKeyConfig() const;
291 void saveAnalogAxisConfig() const;
292 void handleAxisChange(Gamepad gamepad,
293 Uint8 axis,
294 int64_t time,
295 bool analog,
296 int analogTickCount = 1);
297 void emitAnalogScratchTicks(Gamepad gamepad,
298 Uint8 axis,
299 Key::Direction direction,
300 int tickCount);
301 void checkAnalogAxisStatus();
302 void autoReleaseScratch(const std::pair<Gamepad, uint8_t>& scratchKey,
303 int64_t time);
304 void emitTick(BmsKey button);
305
306 public:
307 void handleAxis(Gamepad gamepad, Uint8 axis, double value, int64_t time);
308 void handlePress(Gamepad gamepad, Uint8 button, int64_t time);
309 void handleRelease(Gamepad gamepad, Uint8 button, int64_t time);
310 void handleMidiNote(MidiDevice device,
311 int channel,
312 int note,
313 int velocity,
314 int64_t time);
315 void handleMidiControl(MidiDevice device,
316 int channel,
317 int control,
318 int value,
319 int64_t time);
320 void handleMidiPitchBend(MidiDevice device,
321 int channel,
322 int value,
323 int64_t time);
324 void handleMidiDeviceRemoved(MidiDevice device, int64_t time);
325 // Handles a single physical keyboard key event identified by its native
326 // scan code. Called by the platform-specific input path (LL hook on
327 // Windows, CustomNotifyApp::notify on other platforms).
328 void handleKeyEvent(quint32 nativeScanCode, bool isPress, int64_t time);
329
330 void loadKeyConfig();
331 void connectAnalogAxisConfig(const AnalogAxisConfig& config);
332 void loadAnalogAxisConfig();
333 void loadDebounce();
334 void saveDebounce();
335 explicit InputTranslator(db::SqliteCppDb* db, QObject* parent = nullptr);
336 void setConfiguredButton(const QVariant& button);
337 auto getConfiguredButton() const -> QVariant;
338 auto isConfiguring() const -> bool;
339 void setKeyConfig(const QList<Mapping>& config);
340 auto getKeyConfig() -> QList<Mapping>;
341 void setKeyConfig(const QHash<Key, BmsKey>& config);
342 auto getKeyConfigHash() -> QHash<Key, BmsKey>;
343 Q_INVOKABLE void resetButton(BmsKey key);
344 auto col11() const -> bool;
345 auto col12() const -> bool;
346 auto col13() const -> bool;
347 auto col14() const -> bool;
348 auto col15() const -> bool;
349 auto col16() const -> bool;
350 auto col17() const -> bool;
351 auto col1sUp() const -> bool;
352 auto col1sDown() const -> bool;
353 auto col21() const -> bool;
354 auto col22() const -> bool;
355 auto col23() const -> bool;
356 auto col24() const -> bool;
357 auto col25() const -> bool;
358 auto col26() const -> bool;
359 auto col27() const -> bool;
360 auto col2sUp() const -> bool;
361 auto col2sDown() const -> bool;
362 auto start1() const -> bool;
363 auto select1() const -> bool;
364 auto start2() const -> bool;
365 auto select2() const -> bool;
366 auto getAnalogAxisConfig1() -> AnalogAxisConfig*;
367 auto getAnalogAxisConfig2() -> AnalogAxisConfig*;
368 void eventFilter(std::chrono::milliseconds timePoint, QEvent* event);
369 Q_INVOKABLE static QString scancodeToString(int virtualKey);
370 static auto computeAnalogScratchTicks(double oldValue, double newValue)
371 -> int;
372 auto getDebounceMs() const -> double;
373 void setDebounceMs(double value);
374 void resetDebounceMs();
375
376 signals:
377 void buttonPressed(BmsKey button, int64_t time);
378 void buttonReleased(BmsKey button, int64_t time);
379 void keyConfigModified();
380 void configuringChanged();
381 void configuredButtonChanged();
382 void col11Changed();
383 void col12Changed();
384 void col13Changed();
385 void col14Changed();
386 void col15Changed();
387 void col16Changed();
388 void col17Changed();
389 void col1sUpChanged();
390 void col1sDownChanged();
391 void col21Changed();
392 void col22Changed();
393 void col23Changed();
394 void col24Changed();
395 void col25Changed();
396 void col26Changed();
397 void col27Changed();
398 void col2sUpChanged();
399 void col2sDownChanged();
400 void start1Changed();
401 void select1Changed();
402 void start2Changed();
403 void select2Changed();
404 void analogAxisConfig1Changed();
405 void analogAxisConfig2Changed();
406 void debounceMsChanged();
407
408 void col11Ticked(int tickNumber, TickType type);
409 void col12Ticked(int tickNumber, TickType type);
410 void col13Ticked(int tickNumber, TickType type);
411 void col14Ticked(int tickNumber, TickType type);
412 void col15Ticked(int tickNumber, TickType type);
413 void col16Ticked(int tickNumber, TickType type);
414 void col17Ticked(int tickNumber, TickType type);
415 void col1sUpTicked(int tickNumber, TickType type);
416 void col1sDownTicked(int tickNumber, TickType type);
417 void col21Ticked(int tickNumber, TickType type);
418 void col22Ticked(int tickNumber, TickType type);
419 void col23Ticked(int tickNumber, TickType type);
420 void col24Ticked(int tickNumber, TickType type);
421 void col25Ticked(int tickNumber, TickType type);
422 void col26Ticked(int tickNumber, TickType type);
423 void col27Ticked(int tickNumber, TickType type);
424 void col2sUpTicked(int tickNumber, TickType type);
425 void col2sDownTicked(int tickNumber, TickType type);
426 void start1Ticked(int tickNumber, TickType type);
427 void select1Ticked(int tickNumber, TickType type);
428 void start2Ticked(int tickNumber, TickType type);
429 void select2Ticked(int tickNumber, TickType type);
430};
431
432} // namespace input
433
434#endif // GAMEPADINPUTTRANSLATOR_H
Type type
Definition BmsNotesData.cpp:33
double value
Definition BmsNotesData.cpp:35
int64_t timestamp
Definition ImportedScoreImporter.cpp:58
size_t hash
Definition SongAssetStore.cpp:435
Database wrapper for SQLiteCpp.
Definition SqliteCppDb.h:30
Definition InputTranslator.h:114
ScratchAlgorithm
The algorithm used to interpret axis movement as scratch movement.
Definition InputTranslator.h:147
@ ScratchAlgorithmAnalog
Beatoraja style.
Definition InputTranslator.h:149
Definition GamepadManager.h:15
Definition InputTranslator.h:178
TickType
Definition InputTranslator.h:230
@ ButtonTick
Definition InputTranslator.h:231
@ AnalogScratchTick
Definition InputTranslator.h:232
Definition InputTranslator.h:28
Device
Definition InputTranslator.h:43
Direction direction
Definition InputTranslator.h:39
Device device
Definition InputTranslator.h:37
quint32 code
Definition InputTranslator.h:38
QVariant gamepad
The gamepad this key belongs to. Null if the key is from the keyboard.
Definition InputTranslator.h:35
QVariant midi
Definition InputTranslator.h:36
Direction
Definition InputTranslator.h:53
auto getMidi() const -> QVariant
Definition InputTranslator.cpp:78
Definition InputTranslator.h:96
Definition MidiManager.h:18
Namespace for database related classes and functions.
Definition QtArenaRoundLoader.h:15
Various input-related classes and functions.
Definition BmsKeys.h:12
BmsKey
The keys used for BMS.
Definition BmsKeys.h:20
Definition InputTranslator.h:222
QMetaObject::Connection timerConnection
Definition InputTranslator.h:224
std::size_t operator()(const input::Key &s) const noexcept
Definition InputTranslator.h:79