| 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 "updateaxisactionjob_p.h" |
| 41 | #include <Qt3DCore/private/qaspectmanager_p.h> |
| 42 | #include <Qt3DInput/qaction.h> |
| 43 | #include <Qt3DInput/qaxis.h> |
| 44 | #include <Qt3DInput/private/qaction_p.h> |
| 45 | #include <Qt3DInput/private/qaxis_p.h> |
| 46 | #include <Qt3DInput/private/inputhandler_p.h> |
| 47 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 48 | #include <Qt3DInput/private/job_common_p.h> |
| 49 | |
| 50 | QT_BEGIN_NAMESPACE |
| 51 | |
| 52 | namespace Qt3DInput { |
| 53 | |
| 54 | namespace Input { |
| 55 | |
| 56 | class UpdateAxisActionJobPrivate : public Qt3DCore::QAspectJobPrivate |
| 57 | { |
| 58 | public: |
| 59 | UpdateAxisActionJobPrivate() { } |
| 60 | ~UpdateAxisActionJobPrivate() override { } |
| 61 | |
| 62 | void postFrame(Qt3DCore::QAspectManager *manager) override; |
| 63 | |
| 64 | QVector<QPair<Qt3DCore::QNodeId, bool>> m_triggeredActions; |
| 65 | QVector<QPair<Qt3DCore::QNodeId, float>> m_triggeredAxis; |
| 66 | }; |
| 67 | |
| 68 | UpdateAxisActionJob::UpdateAxisActionJob(qint64 currentTime, InputHandler *handler, HLogicalDevice handle) |
| 69 | : Qt3DCore::QAspectJob(*new UpdateAxisActionJobPrivate()) |
| 70 | , m_currentTime(currentTime) |
| 71 | , m_handler(handler) |
| 72 | , m_handle(handle) |
| 73 | { |
| 74 | SET_JOB_RUN_STAT_TYPE(this, JobTypes::UpdateAxisAction, 0) |
| 75 | } |
| 76 | |
| 77 | void UpdateAxisActionJob::run() |
| 78 | { |
| 79 | // Note: we assume axis/action are not really shared: |
| 80 | // there's no benefit in sharing those when it comes to computing |
| 81 | LogicalDevice *device = m_handler->logicalDeviceManager()->data(handle: m_handle); |
| 82 | |
| 83 | if (!device->isEnabled()) |
| 84 | return; |
| 85 | |
| 86 | updateAction(device); |
| 87 | updateAxis(device); |
| 88 | } |
| 89 | |
| 90 | void UpdateAxisActionJob::updateAction(LogicalDevice *device) |
| 91 | { |
| 92 | Q_D(UpdateAxisActionJob); |
| 93 | const auto actionIds = device->actions(); |
| 94 | d->m_triggeredActions.reserve(asize: actionIds.size()); |
| 95 | |
| 96 | for (const Qt3DCore::QNodeId actionId : actionIds) { |
| 97 | bool actionTriggered = false; |
| 98 | Action *action = m_handler->actionManager()->lookupResource(id: actionId); |
| 99 | |
| 100 | const auto actionInputIds = action->inputs(); |
| 101 | for (const Qt3DCore::QNodeId actionInputId : actionInputIds) |
| 102 | actionTriggered |= processActionInput(actionInputId); |
| 103 | |
| 104 | if (action->isEnabled() && (action->actionTriggered() != actionTriggered)) { |
| 105 | action->setActionTriggered(actionTriggered); |
| 106 | d->m_triggeredActions.push_back(t: {actionId, actionTriggered}); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | bool UpdateAxisActionJob::processActionInput(const Qt3DCore::QNodeId actionInputId) |
| 112 | { |
| 113 | AbstractActionInput *actionInput = m_handler->lookupActionInput(id: actionInputId); |
| 114 | Q_ASSERT(actionInput); |
| 115 | return actionInput->process(inputHandler: m_handler, currentTime: m_currentTime); |
| 116 | } |
| 117 | |
| 118 | void UpdateAxisActionJob::updateAxis(LogicalDevice *device) |
| 119 | { |
| 120 | Q_D(UpdateAxisActionJob); |
| 121 | const auto axisIds = device->axes(); |
| 122 | d->m_triggeredAxis.reserve(asize: axisIds.size()); |
| 123 | |
| 124 | for (const Qt3DCore::QNodeId axisId : axisIds) { |
| 125 | Axis *axis = m_handler->axisManager()->lookupResource(id: axisId); |
| 126 | float axisValue = 0.0f; |
| 127 | |
| 128 | const auto axisInputIds = axis->inputs(); |
| 129 | for (const Qt3DCore::QNodeId axisInputId : axisInputIds) |
| 130 | axisValue += processAxisInput(axisInputId); |
| 131 | |
| 132 | // Clamp the axisValue -1/1 |
| 133 | axisValue = qMin(a: 1.0f, b: qMax(a: axisValue, b: -1.0f)); |
| 134 | |
| 135 | if (axis->isEnabled() && !qFuzzyCompare(p1: axisValue, p2: axis->axisValue())) { |
| 136 | axis->setAxisValue(axisValue); |
| 137 | d->m_triggeredAxis.push_back(t: {axisId, axisValue}); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | float UpdateAxisActionJob::processAxisInput(const Qt3DCore::QNodeId axisInputId) |
| 143 | { |
| 144 | AnalogAxisInput *analogInput = m_handler->analogAxisInputManager()->lookupResource(id: axisInputId); |
| 145 | if (analogInput) |
| 146 | return analogInput->process(inputHandler: m_handler, currentTime: m_currentTime); |
| 147 | |
| 148 | ButtonAxisInput *buttonInput = m_handler->buttonAxisInputManager()->lookupResource(id: axisInputId); |
| 149 | if (buttonInput) |
| 150 | return buttonInput->process(inputHandler: m_handler, currentTime: m_currentTime); |
| 151 | |
| 152 | Q_UNREACHABLE(); |
| 153 | return 0.0f; |
| 154 | } |
| 155 | |
| 156 | void UpdateAxisActionJobPrivate::postFrame(Qt3DCore::QAspectManager *manager) |
| 157 | { |
| 158 | for (const auto &data: qAsConst(t&: m_triggeredActions)) { |
| 159 | Qt3DInput::QAction *action = qobject_cast<Qt3DInput::QAction *>(object: manager->lookupNode(id: data.first)); |
| 160 | if (!action) |
| 161 | continue; |
| 162 | |
| 163 | Qt3DInput::QActionPrivate *daction = static_cast<Qt3DInput::QActionPrivate *>(Qt3DCore::QNodePrivate::get(q: action)); |
| 164 | daction->setActive(data.second); |
| 165 | } |
| 166 | |
| 167 | for (const auto &data: qAsConst(t&: m_triggeredAxis)) { |
| 168 | Qt3DInput::QAxis *axis = qobject_cast<Qt3DInput::QAxis *>(object: manager->lookupNode(id: data.first)); |
| 169 | if (!axis) |
| 170 | continue; |
| 171 | |
| 172 | Qt3DInput::QAxisPrivate *daxis = static_cast<Qt3DInput::QAxisPrivate *>(Qt3DCore::QNodePrivate::get(q: axis)); |
| 173 | daxis->setValue(data.second); |
| 174 | } |
| 175 | |
| 176 | m_triggeredActions.clear(); |
| 177 | m_triggeredAxis.clear(); |
| 178 | } |
| 179 | |
| 180 | } // Input |
| 181 | |
| 182 | } // Qt3DInput |
| 183 | |
| 184 | QT_END_NAMESPACE |
| 185 | |