| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). | 
| 4 | ** Contact: http://www.qt-project.org/legal | 
| 5 | ** | 
| 6 | ** This file is part of the Qt3D module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ | 
| 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 http://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free | 
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in | 
| 29 | ** the packaging of this file. Please review the following information to | 
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be | 
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. | 
| 32 | ** | 
| 33 | ** $QT_END_LICENSE$ | 
| 34 | ** | 
| 35 | ****************************************************************************/ | 
| 36 |  | 
| 37 | #include "qgamepadinput_p.h" | 
| 38 |  | 
| 39 | #include <QtCore/QMetaEnum> | 
| 40 | #include <QtGamepad/QGamepadManager> | 
| 41 |  | 
| 42 | #include <Qt3DInput/private/qabstractphysicaldevice_p.h> | 
| 43 |  | 
| 44 | QT_BEGIN_NAMESPACE | 
| 45 | namespace Qt3DInput { | 
| 46 | class QGamepadInputPrivate : public QAbstractPhysicalDevicePrivate { | 
| 47 | public: | 
| 48 |     QGamepadInputPrivate() | 
| 49 |         : QAbstractPhysicalDevicePrivate() | 
| 50 |         , m_deviceId(0) | 
| 51 |     {} | 
| 52 |  | 
| 53 |     int m_deviceId; | 
| 54 | }; | 
| 55 |  | 
| 56 | static void (QHash<QString, int> &hash, const QMetaEnum &metaEnum) | 
| 57 | { | 
| 58 |     hash.reserve(asize: metaEnum.keyCount() - 1); | 
| 59 |     for (int i = 0; i < metaEnum.keyCount(); i++) { | 
| 60 |         if (metaEnum.value(index: i) != -1) | 
| 61 |             hash[QLatin1String(metaEnum.key(index: i))] = metaEnum.value(index: i); | 
| 62 |     } | 
| 63 | } | 
| 64 |  | 
| 65 | QGamepadInput::QGamepadInput(Qt3DCore::QNode *parent) | 
| 66 |     : QAbstractPhysicalDevice(*new QGamepadInputPrivate, parent) | 
| 67 | { | 
| 68 |     Q_D(QGamepadInput); | 
| 69 |     auto metaObject = QGamepadManager::instance()->metaObject(); | 
| 70 |     for (int i = metaObject->enumeratorOffset(); i < metaObject->enumeratorCount(); ++i) { | 
| 71 |         auto metaEnum = metaObject->enumerator(index: i); | 
| 72 |         if (metaEnum.name() == std::string("GamepadButton" )) | 
| 73 |             setValuesFromEnum(hash&: d->m_buttonsHash, metaEnum); | 
| 74 |         else if (metaEnum.name() == std::string("GamepadAxis" )) | 
| 75 |             setValuesFromEnum(hash&: d->m_axesHash, metaEnum); | 
| 76 |     } | 
| 77 |     connect(sender: QGamepadManager::instance(), signal: &QGamepadManager::gamepadAxisEvent, context: this, slot: [this, d](int deviceId, QGamepadManager::GamepadAxis axis, double value) { | 
| 78 |         if (deviceId == d_func()->m_deviceId) | 
| 79 |             d->postAxisEvent(axis, value); | 
| 80 |     }); | 
| 81 |     connect(sender: QGamepadManager::instance(), signal: &QGamepadManager::gamepadButtonPressEvent, context: this, slot: [this, d](int deviceId, QGamepadManager::GamepadButton button, double value) { | 
| 82 |         if (deviceId == d_func()->m_deviceId) | 
| 83 |             d->postButtonEvent(button, value); | 
| 84 |     }); | 
| 85 |     connect(sender: QGamepadManager::instance(), signal: &QGamepadManager::gamepadButtonReleaseEvent, context: this, slot: [this, d](int deviceId, QGamepadManager::GamepadButton button) { | 
| 86 |         if (deviceId == d_func()->m_deviceId) | 
| 87 |             d->postButtonEvent(button, value: 0); | 
| 88 |     }); | 
| 89 | } | 
| 90 |  | 
| 91 | int QGamepadInput::deviceId() const | 
| 92 | { | 
| 93 |     return d_func()->m_deviceId; | 
| 94 | } | 
| 95 |  | 
| 96 | void QGamepadInput::setDeviceId(int deviceId) | 
| 97 | { | 
| 98 |     Q_D(QGamepadInput); | 
| 99 |     if (d->m_deviceId == deviceId) | 
| 100 |         return; | 
| 101 |  | 
| 102 |     d->m_deviceId = deviceId; | 
| 103 |     emit deviceIdChanged(); | 
| 104 | } | 
| 105 |  | 
| 106 | } // Qt3DInput | 
| 107 | QT_END_NAMESPACE | 
| 108 |  |