| 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:LGPL$ |
| 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 Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qabstractphysicaldevice.h" |
| 41 | #include "qabstractphysicaldevice_p.h" |
| 42 | |
| 43 | #include <Qt3DInput/qaxissetting.h> |
| 44 | #include <Qt3DInput/qphysicaldevicecreatedchange.h> |
| 45 | |
| 46 | #include <Qt3DCore/private/qnode_p.h> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | namespace Qt3DInput { |
| 51 | |
| 52 | /*! \internal */ |
| 53 | QAbstractPhysicalDevicePrivate::QAbstractPhysicalDevicePrivate() |
| 54 | : m_axisSettings() |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | /*! \internal */ |
| 59 | QAbstractPhysicalDevicePrivate::~QAbstractPhysicalDevicePrivate() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | /*! |
| 64 | \class Qt3DInput::QAbstractPhysicalDevice |
| 65 | \inmodule Qt3DInput |
| 66 | \inherits Qt3DCore::QNode |
| 67 | \brief QAbstractPhysicalDevice is the base class used by Qt3d to interact with arbitrary input devices. |
| 68 | |
| 69 | \since 5.6 |
| 70 | */ |
| 71 | |
| 72 | /*! |
| 73 | \qmltype AbstractPhysicalDevice |
| 74 | \inqmlmodule Qt3D.Input |
| 75 | \instantiates Qt3DInput::QAbstractPhysicalDevice |
| 76 | \brief QML frontend for the abstract Qt3DInput::QAbstractPhysicalDevice C++ class. |
| 77 | |
| 78 | The base class used by Qt3d to interact with arbitrary input devices. |
| 79 | |
| 80 | \since 5.6 |
| 81 | */ |
| 82 | |
| 83 | /*! |
| 84 | Constructs a new QAbstractPhysicalDevice instance with \a parent. |
| 85 | */ |
| 86 | QAbstractPhysicalDevice::QAbstractPhysicalDevice(Qt3DCore::QNode *parent) |
| 87 | : Qt3DCore::QNode(*new QAbstractPhysicalDevicePrivate, parent) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | /*! \internal */ |
| 92 | QAbstractPhysicalDevice::~QAbstractPhysicalDevice() |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | QAbstractPhysicalDevice::QAbstractPhysicalDevice(QAbstractPhysicalDevicePrivate &dd, Qt3DCore::QNode *parent) |
| 97 | : Qt3DCore::QNode(dd, parent) |
| 98 | { |
| 99 | } |
| 100 | |
| 101 | /*! |
| 102 | \return the number of axis this device has. |
| 103 | */ |
| 104 | int QAbstractPhysicalDevice::axisCount() const |
| 105 | { |
| 106 | Q_D(const QAbstractPhysicalDevice); |
| 107 | return d->m_axesHash.size(); |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | \return the number of buttons this device has. |
| 112 | */ |
| 113 | int QAbstractPhysicalDevice::buttonCount() const |
| 114 | { |
| 115 | Q_D(const QAbstractPhysicalDevice); |
| 116 | return d->m_buttonsHash.size(); |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | \return a list of the names of device's axis. |
| 121 | */ |
| 122 | QStringList QAbstractPhysicalDevice::axisNames() const |
| 123 | { |
| 124 | Q_D(const QAbstractPhysicalDevice); |
| 125 | return d->m_axesHash.keys(); |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | \return a list of the names of device's buttons. |
| 130 | */ |
| 131 | QStringList QAbstractPhysicalDevice::buttonNames() const |
| 132 | { |
| 133 | Q_D(const QAbstractPhysicalDevice); |
| 134 | return d->m_buttonsHash.keys(); |
| 135 | } |
| 136 | |
| 137 | /*! |
| 138 | \return the integer identifer of the axis \a name or -1 if it does not exist on this device. |
| 139 | */ |
| 140 | int QAbstractPhysicalDevice::axisIdentifier(const QString &name) const |
| 141 | { |
| 142 | Q_D(const QAbstractPhysicalDevice); |
| 143 | auto it = d->m_axesHash.find(akey: name); |
| 144 | if (it != d->m_axesHash.end()) |
| 145 | return *it; |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | /*! |
| 150 | \return the integer identifer of the button \a name or -1 if it does not exist on this device. |
| 151 | */ |
| 152 | int QAbstractPhysicalDevice::buttonIdentifier(const QString &name) const |
| 153 | { |
| 154 | Q_D(const QAbstractPhysicalDevice); |
| 155 | auto it = d->m_buttonsHash.find(akey: name); |
| 156 | if (it != d->m_buttonsHash.end()) |
| 157 | return *it; |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | /*! |
| 162 | Add the axisSetting \a axisSetting to this device. |
| 163 | */ |
| 164 | void QAbstractPhysicalDevice::addAxisSetting(QAxisSetting *axisSetting) |
| 165 | { |
| 166 | Q_D(QAbstractPhysicalDevice); |
| 167 | if (axisSetting && !d->m_axisSettings.contains(t: axisSetting)) { |
| 168 | d->updateNode(node: axisSetting, property: "axisSettings" , change: Qt3DCore::PropertyValueAdded); |
| 169 | d->m_axisSettings.push_back(t: axisSetting); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /*! |
| 174 | Remove the axisSetting \a axisSetting to this device. |
| 175 | */ |
| 176 | void QAbstractPhysicalDevice::removeAxisSetting(QAxisSetting *axisSetting) |
| 177 | { |
| 178 | Q_D(QAbstractPhysicalDevice); |
| 179 | if (axisSetting && d->m_axisSettings.contains(t: axisSetting)) { |
| 180 | d->updateNode(node: axisSetting, property: "axisSettings" , change: Qt3DCore::PropertyValueRemoved); |
| 181 | d->m_axisSettings.removeOne(t: axisSetting); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /*! |
| 186 | Returns the axisSettings associated with this device. |
| 187 | */ |
| 188 | QVector<QAxisSetting *> QAbstractPhysicalDevice::axisSettings() const |
| 189 | { |
| 190 | Q_D(const QAbstractPhysicalDevice); |
| 191 | return d->m_axisSettings; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | Used to notify observers that an axis value has been changed. |
| 196 | */ |
| 197 | void QAbstractPhysicalDevicePrivate::postAxisEvent(int axis, qreal value) |
| 198 | { |
| 199 | m_pendingAxisEvents.push_back(t: {axis, value}); |
| 200 | update(); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | Used to notify observers that an button value has been changed. |
| 205 | */ |
| 206 | void QAbstractPhysicalDevicePrivate::postButtonEvent(int button, qreal value) |
| 207 | { |
| 208 | m_pendingButtonsEvents.push_back(t: {button, value}); |
| 209 | update(); |
| 210 | } |
| 211 | |
| 212 | /*! |
| 213 | \internal |
| 214 | */ |
| 215 | Qt3DCore::QNodeCreatedChangeBasePtr QAbstractPhysicalDevice::createNodeCreationChange() const |
| 216 | { |
| 217 | auto creationChange = QPhysicalDeviceCreatedChangeBasePtr::create(arguments: this); |
| 218 | return creationChange; |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | QT_END_NAMESPACE |
| 224 | |