1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "genericdevicebackendnode_p.h" |
5 | |
6 | #include <Qt3DInput/qabstractphysicaldevice.h> |
7 | |
8 | #include <Qt3DInput/private/inputhandler_p.h> |
9 | #include <Qt3DInput/private/inputmanagers_p.h> |
10 | #include <Qt3DInput/private/qgenericinputdevice_p.h> |
11 | #include <Qt3DInput/private/qabstractphysicaldevice_p.h> |
12 | |
13 | QT_BEGIN_NAMESPACE |
14 | |
15 | namespace Qt3DInput { |
16 | namespace Input { |
17 | |
18 | GenericDeviceBackendNode::GenericDeviceBackendNode(QBackendNode::Mode mode) |
19 | : QAbstractPhysicalDeviceBackendNode(mode) |
20 | , m_mutex() |
21 | { |
22 | } |
23 | |
24 | GenericDeviceBackendNode::~GenericDeviceBackendNode() |
25 | { |
26 | } |
27 | |
28 | void GenericDeviceBackendNode::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
29 | { |
30 | QAbstractPhysicalDeviceBackendNode::syncFromFrontEnd(frontEnd, firstTime); |
31 | const Qt3DInput::QGenericInputDevice *node = qobject_cast<const Qt3DInput::QGenericInputDevice *>(object: frontEnd); |
32 | if (!node) |
33 | return; |
34 | |
35 | auto *d = static_cast<Qt3DInput::QAbstractPhysicalDevicePrivate *>( Qt3DCore::QNodePrivate::get(q: const_cast<Qt3DCore::QNode *>(frontEnd)) ); |
36 | |
37 | { |
38 | const QMutexLocker lock(&m_mutex); |
39 | for (const auto &val: std::as_const(t&: d->m_pendingAxisEvents)) |
40 | m_axesValues[val.first] = val.second; |
41 | for (const auto &val: std::as_const(t&: d->m_pendingButtonsEvents)) |
42 | m_buttonsValues[val.first] = val.second; |
43 | |
44 | d->m_pendingAxisEvents.clear(); |
45 | d->m_pendingButtonsEvents.clear(); |
46 | } |
47 | } |
48 | |
49 | void GenericDeviceBackendNode::cleanup() |
50 | { |
51 | const QMutexLocker lock(&m_mutex); |
52 | m_axesValues.clear(); |
53 | m_buttonsValues.clear(); |
54 | QAbstractPhysicalDeviceBackendNode::cleanup(); |
55 | } |
56 | |
57 | float GenericDeviceBackendNode::axisValue(int axisIdentifier) const |
58 | { |
59 | const QMutexLocker lock(&m_mutex); |
60 | return m_axesValues[axisIdentifier]; |
61 | } |
62 | |
63 | bool GenericDeviceBackendNode::isButtonPressed(int buttonIdentifier) const |
64 | { |
65 | const QMutexLocker lock(&m_mutex); |
66 | return m_buttonsValues[buttonIdentifier]; |
67 | } |
68 | |
69 | GenericDeviceBackendFunctor::GenericDeviceBackendFunctor(QInputAspect *inputaspect, InputHandler *handler) |
70 | : m_inputAspect(inputaspect) |
71 | , m_handler(handler) |
72 | { |
73 | } |
74 | |
75 | Qt3DCore::QBackendNode *GenericDeviceBackendFunctor::create(Qt3DCore::QNodeId id) const |
76 | { |
77 | GenericDeviceBackendNode *backendNode = m_handler->genericDeviceBackendNodeManager()->getOrCreateResource(id); |
78 | backendNode->setInputAspect(m_inputAspect); |
79 | m_handler->appendGenericDevice(device: m_handler->genericDeviceBackendNodeManager()->lookupHandle(id)); |
80 | return backendNode; |
81 | } |
82 | |
83 | Qt3DCore::QBackendNode *GenericDeviceBackendFunctor::get(Qt3DCore::QNodeId id) const |
84 | { |
85 | return m_handler->genericDeviceBackendNodeManager()->lookupResource(id); |
86 | } |
87 | |
88 | void GenericDeviceBackendFunctor::destroy(Qt3DCore::QNodeId id) const |
89 | { |
90 | m_handler->removeGenericDevice(device: m_handler->genericDeviceBackendNodeManager()->lookupHandle(id)); |
91 | m_handler->genericDeviceBackendNodeManager()->releaseResource(id); |
92 | } |
93 | |
94 | } // namespace Input |
95 | } // namespace Qt3DInput |
96 | |
97 | QT_END_NAMESPACE |
98 |