forked from powersst/Maple-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad.cpp
More file actions
112 lines (101 loc) · 2.87 KB
/
Copy pathgamepad.cpp
File metadata and controls
112 lines (101 loc) · 2.87 KB
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
#include "gamepad.h"
#include "configuration.h"
Gamepad *Gamepad::instance;
bool Gamepad::isEnabled = false;
Gamepad::Gamepad(QObject *parent) : QObject(parent), m_gamepad(nullptr)
{
QLoggingCategory::setFilterRules(QStringLiteral("qt.gamepad.debug=true"));
manager = QGamepadManager::instance();
qInfo() << "Gamepad handler initialized";
}
Gamepad::~Gamepad()
{
delete m_gamepad;
qInfo() << "Gamepad handler terminated";
}
void Gamepad::init()
{
connect(manager, &QGamepadManager::gamepadConnected, this, [=](int deviceId)
{
qInfo("Gamepad connected: (%i)", deviceId);
m_gamepads.insert(deviceId, m_gamepad = new QGamepad(deviceId, this));
});
connect(manager, &QGamepadManager::gamepadDisconnected, this, [=](int deviceId)
{
qInfo("Gamepad disconnected: (%i)", deviceId);
m_gamepads.remove(deviceId);
});
connect(manager, &QGamepadManager::gamepadButtonPressEvent, this, Gamepad::button);
connect(manager, &QGamepadManager::gamepadButtonReleaseEvent, this, &Gamepad::release);
}
void Gamepad::closeGame()
{
if (l1 && l2 && r1 && r2 && select)
{
qDebug() << "Terminating cemu process";
emit instance->gameClose(true);
}
}
void Gamepad::button(int deviceId, QGamepadManager::GamepadButton button, double value)
{
auto buttons = instance->manager;
qDebug() << "Gamepad(" << deviceId << ") button:" << button << "=" << value;
if (button == buttons->ButtonUp)
{
emit instance->gameUp(static_cast<bool>(value) && isEnabled);
}
if (button == buttons->ButtonDown)
{
emit instance->gameDown(static_cast<bool>(value) && isEnabled);
}
if (button == buttons->ButtonA)
{
emit instance->gameStart(static_cast<bool>(value) && isEnabled);
}
if (button == buttons->ButtonL1)
{
if ((instance->l1 = static_cast<bool>(value)))
{
emit instance->prevTab(static_cast<bool>(value) && isEnabled);
}
}
if (button == buttons->ButtonL2)
{
instance->l2 = static_cast<bool>(value);
}
if (button == buttons->ButtonR1)
{if ((instance->r1 = static_cast<bool>(value)))
{
emit instance->nextTab(static_cast<bool>(value) && isEnabled);
}
}
if (button == buttons->ButtonR2)
{
instance->r2 = static_cast<bool>(value);
}
if (button == buttons->ButtonSelect)
{
instance->select = static_cast<bool>(value);
instance->closeGame();
}
}
void Gamepad::release(int deviceId, QGamepadManager::GamepadButton button)
{
return Gamepad::button(deviceId, button, 0);
}
void Gamepad::enable()
{
qInfo() << "Gamepad mode" << (isEnabled = true);
}
void Gamepad::disable()
{
qInfo() << "Gamepad mode" << (isEnabled = false);
}
void Gamepad::terminate()
{
isEnabled = false;
if (instance)
{
delete instance;
}
}