| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#ifndef TESTDEVICE_H |
| 5 |
#define TESTDEVICE_H |
| 6 |
|
| 7 |
#include <Qt3DCore/private/qnode_p.h> |
| 8 |
#include <Qt3DInput/QAbstractPhysicalDevice> |
| 9 |
#include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p.h> |
| 10 |
#include <Qt3DInput/private/qinputdeviceintegration_p.h> |
| 11 |
#include <qbackendnodetester.h> |
| 12 |
|
| 13 |
class TestDevice : public Qt3DInput::QAbstractPhysicalDevice |
| 14 |
{ |
| 15 |
Q_OBJECT |
| 16 |
public: |
| 17 |
explicit TestDevice(Qt3DCore::QNode *parent = nullptr) |
| 18 |
: Qt3DInput::QAbstractPhysicalDevice(parent) |
| 19 |
{} |
| 20 |
|
| 21 |
int axisCount() const final { return 0; } |
| 22 |
int buttonCount() const final { return 0; } |
| 23 |
QStringList axisNames() const final { return QStringList(); } |
| 24 |
QStringList buttonNames() const final { return QStringList(); } |
| 25 |
int axisIdentifier(const QString &name) const final { Q_UNUSED(name); return 0; } |
| 26 |
int buttonIdentifier(const QString &name) const final { Q_UNUSED(name); return 0; } |
| 27 |
|
| 28 |
private: |
| 29 |
friend class TestDeviceBackendNode; |
| 30 |
}; |
| 31 |
|
| 32 |
class TestDeviceBackendNode : public Qt3DInput::QAbstractPhysicalDeviceBackendNode |
| 33 |
{ |
| 34 |
public: |
| 35 |
explicit TestDeviceBackendNode(TestDevice *device) |
| 36 |
: Qt3DInput::QAbstractPhysicalDeviceBackendNode(ReadOnly) |
| 37 |
{ |
| 38 |
Qt3DCore::QBackendNodeTester().simulateInitializationSync(device, this); |
| 39 |
} |
| 40 |
|
| 41 |
float axisValue(int axisIdentifier) const final |
| 42 |
{ |
| 43 |
return m_axisValues.value(axisIdentifier); |
| 44 |
} |
| 45 |
|
| 46 |
void setAxisValue(int axisIdentifier, float value) |
| 47 |
{ |
| 48 |
m_axisValues.insert(axisIdentifier, value); |
| 49 |
} |
| 50 |
|
| 51 |
bool isButtonPressed(int buttonIdentifier) const final |
| 52 |
{ |
| 53 |
return m_buttonStates.value(buttonIdentifier); |
| 54 |
} |
| 55 |
|
| 56 |
void setButtonPressed(int buttonIdentifier, bool pressed) |
| 57 |
{ |
| 58 |
m_buttonStates.insert(buttonIdentifier, pressed); |
| 59 |
} |
| 60 |
|
| 61 |
private: |
| 62 |
QHash<int, float> m_axisValues; |
| 63 |
QHash<int, bool> m_buttonStates; |
| 64 |
}; |
| 65 |
|
| 66 |
class TestDeviceIntegration : public Qt3DInput::QInputDeviceIntegration |
| 67 |
{ |
| 68 |
Q_OBJECT |
| 69 |
public: |
| 70 |
explicit TestDeviceIntegration(QObject *parent = nullptr) |
| 71 |
: Qt3DInput::QInputDeviceIntegration(parent), |
| 72 |
m_devicesParent(new Qt3DCore::QNode) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
~TestDeviceIntegration() |
| 77 |
{ |
| 78 |
qDeleteAll(m_deviceBackendNodes); |
| 79 |
} |
| 80 |
|
| 81 |
std::vector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64 time) final |
| 82 |
{ |
| 83 |
Q_UNUSED(time); |
| 84 |
return std::vector<Qt3DCore::QAspectJobPtr>(); |
| 85 |
} |
| 86 |
|
| 87 |
TestDevice *createPhysicalDevice(const QString &name) final |
| 88 |
{ |
| 89 |
Q_ASSERT(!deviceNames().contains(name)); |
| 90 |
auto device = new TestDevice(m_devicesParent.data()); |
| 91 |
device->setObjectName(name); |
| 92 |
m_devices.append(device); |
| 93 |
m_deviceBackendNodes.append(new TestDeviceBackendNode(device)); |
| 94 |
return device; |
| 95 |
} |
| 96 |
|
| 97 |
QList<Qt3DCore::QNodeId> physicalDevices() const final |
| 98 |
{ |
| 99 |
QList<Qt3DCore::QNodeId> ids; |
| 100 |
std::transform(m_devices.constBegin(), m_devices.constEnd(), |
| 101 |
std::back_inserter(ids), |
| 102 |
[] (TestDevice *device) { return device->id(); }); |
| 103 |
return ids; |
| 104 |
} |
| 105 |
|
| 106 |
TestDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId id) const final |
| 107 |
{ |
| 108 |
auto it = std::find_if(m_deviceBackendNodes.constBegin(), m_deviceBackendNodes.constEnd(), |
| 109 |
[id] (TestDeviceBackendNode *node) { return node->peerId() == id; }); |
| 110 |
if (it == m_deviceBackendNodes.constEnd()) |
| 111 |
return nullptr; |
| 112 |
else |
| 113 |
return *it; |
| 114 |
} |
| 115 |
|
| 116 |
QStringList deviceNames() const final |
| 117 |
{ |
| 118 |
QStringList names; |
| 119 |
std::transform(m_devices.constBegin(), m_devices.constEnd(), |
| 120 |
std::back_inserter(names), |
| 121 |
[] (TestDevice *device) { return device->objectName(); }); |
| 122 |
return names; |
| 123 |
} |
| 124 |
|
| 125 |
private: |
| 126 |
void onInitialize() final {} |
| 127 |
|
| 128 |
QScopedPointer<Qt3DCore::QNode> m_devicesParent; |
| 129 |
QList<TestDevice *> m_devices; |
| 130 |
QList<TestDeviceBackendNode *> m_deviceBackendNodes; |
| 131 |
}; |
| 132 |
|
| 133 |
#endif |