1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#ifndef TESTDEVICE_H
30#define TESTDEVICE_H
31
32#include <Qt3DCore/private/qnode_p.h>
33#include <Qt3DInput/QAbstractPhysicalDevice>
34#include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p.h>
35#include <Qt3DInput/private/qinputdeviceintegration_p.h>
36#include <qbackendnodetester.h>
37
38class TestDevice : public Qt3DInput::QAbstractPhysicalDevice
39{
40 Q_OBJECT
41public:
42 explicit TestDevice(Qt3DCore::QNode *parent = nullptr)
43 : Qt3DInput::QAbstractPhysicalDevice(parent)
44 {}
45
46 int axisCount() const final { return 0; }
47 int buttonCount() const final { return 0; }
48 QStringList axisNames() const final { return QStringList(); }
49 QStringList buttonNames() const final { return QStringList(); }
50 int axisIdentifier(const QString &name) const final { Q_UNUSED(name) return 0; }
51 int buttonIdentifier(const QString &name) const final { Q_UNUSED(name) return 0; }
52
53private:
54 friend class TestDeviceBackendNode;
55};
56
57class TestDeviceBackendNode : public Qt3DInput::QAbstractPhysicalDeviceBackendNode
58{
59public:
60 explicit TestDeviceBackendNode(TestDevice *device)
61 : Qt3DInput::QAbstractPhysicalDeviceBackendNode(ReadOnly)
62 {
63 Qt3DCore::QBackendNodeTester().simulateInitialization(frontend: device, backend: this);
64 }
65
66 float axisValue(int axisIdentifier) const final
67 {
68 return m_axisValues.value(key: axisIdentifier);
69 }
70
71 void setAxisValue(int axisIdentifier, float value)
72 {
73 m_axisValues.insert(key: axisIdentifier, value);
74 }
75
76 bool isButtonPressed(int buttonIdentifier) const final
77 {
78 return m_buttonStates.value(key: buttonIdentifier);
79 }
80
81 void setButtonPressed(int buttonIdentifier, bool pressed)
82 {
83 m_buttonStates.insert(key: buttonIdentifier, value: pressed);
84 }
85
86private:
87 QHash<int, float> m_axisValues;
88 QHash<int, bool> m_buttonStates;
89};
90
91class TestDeviceIntegration : public Qt3DInput::QInputDeviceIntegration
92{
93 Q_OBJECT
94public:
95 explicit TestDeviceIntegration(QObject *parent = nullptr)
96 : Qt3DInput::QInputDeviceIntegration(parent),
97 m_devicesParent(new Qt3DCore::QNode)
98 {
99 }
100
101 ~TestDeviceIntegration()
102 {
103 qDeleteAll(c: m_deviceBackendNodes);
104 }
105
106 QVector<Qt3DCore::QAspectJobPtr> jobsToExecute(qint64 time) final
107 {
108 Q_UNUSED(time);
109 return QVector<Qt3DCore::QAspectJobPtr>();
110 }
111
112 TestDevice *createPhysicalDevice(const QString &name) final
113 {
114 Q_ASSERT(!deviceNames().contains(name));
115 auto device = new TestDevice(m_devicesParent.data()); // Avoids unwanted reparenting
116 device->setObjectName(name);
117 m_devices.append(t: device);
118 m_deviceBackendNodes.append(t: new TestDeviceBackendNode(device));
119 return device;
120 }
121
122 QVector<Qt3DCore::QNodeId> physicalDevices() const final
123 {
124 QVector<Qt3DCore::QNodeId> ids;
125 std::transform(first: m_devices.constBegin(), last: m_devices.constEnd(),
126 result: std::back_inserter(x&: ids),
127 unary_op: [] (TestDevice *device) { return device->id(); });
128 return ids;
129 }
130
131 TestDeviceBackendNode *physicalDevice(Qt3DCore::QNodeId id) const final
132 {
133 auto it = std::find_if(first: m_deviceBackendNodes.constBegin(), last: m_deviceBackendNodes.constEnd(),
134 pred: [id] (TestDeviceBackendNode *node) { return node->peerId() == id; });
135 if (it == m_deviceBackendNodes.constEnd())
136 return nullptr;
137 else
138 return *it;
139 }
140
141 QStringList deviceNames() const final
142 {
143 QStringList names;
144 std::transform(first: m_devices.constBegin(), last: m_devices.constEnd(),
145 result: std::back_inserter(x&: names),
146 unary_op: [] (TestDevice *device) { return device->objectName(); });
147 return names;
148 }
149
150private:
151 void onInitialize() final {}
152
153 QScopedPointer<Qt3DCore::QNode> m_devicesParent;
154 QVector<TestDevice*> m_devices;
155 QVector<TestDeviceBackendNode*> m_deviceBackendNodes;
156};
157
158#endif // TESTDEVICE_H
159

source code of qt3d/tests/auto/input/commons/testdevice.h