| 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 "axisaccumulator_p.h" |
| 5 | |
| 6 | #include <Qt3DInput/qaxis.h> |
| 7 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 8 | #include <Qt3DInput/private/qaxisaccumulator_p.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | namespace Qt3DInput { |
| 13 | namespace Input { |
| 14 | |
| 15 | AxisAccumulator::AxisAccumulator() |
| 16 | : BackendNode(ReadWrite) |
| 17 | , m_sourceAxisId() |
| 18 | , m_sourceAxisType(QAxisAccumulator::Velocity) |
| 19 | , m_scale(1.0f) |
| 20 | , m_value(0.0f) |
| 21 | , m_velocity(0.0f) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void AxisAccumulator::cleanup() |
| 26 | { |
| 27 | QBackendNode::setEnabled(false); |
| 28 | m_sourceAxisId = Qt3DCore::QNodeId(); |
| 29 | m_sourceAxisType = QAxisAccumulator::Velocity; |
| 30 | m_scale = 1.0f; |
| 31 | m_value = 0.0f; |
| 32 | } |
| 33 | |
| 34 | void AxisAccumulator::setValue(float value) |
| 35 | { |
| 36 | if (isEnabled() && value != m_value) |
| 37 | m_value = value; |
| 38 | } |
| 39 | |
| 40 | void AxisAccumulator::setVelocity(float velocity) |
| 41 | { |
| 42 | if (isEnabled() && velocity != m_velocity) |
| 43 | m_velocity = velocity; |
| 44 | } |
| 45 | |
| 46 | void AxisAccumulator::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
| 47 | { |
| 48 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
| 49 | const QAxisAccumulator *node = qobject_cast<const QAxisAccumulator *>(object: frontEnd); |
| 50 | if (!node) |
| 51 | return; |
| 52 | |
| 53 | m_sourceAxisId = Qt3DCore::qIdForNode(node: node->sourceAxis()); |
| 54 | m_sourceAxisType = node->sourceAxisType(); |
| 55 | m_scale = node->scale(); |
| 56 | |
| 57 | if (firstTime) { |
| 58 | m_value = 0.f; |
| 59 | m_velocity = 0.f; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void AxisAccumulator::stepIntegration(AxisManager *axisManager, float dt) |
| 64 | { |
| 65 | Axis *sourceAxis = axisManager->lookupResource(id: m_sourceAxisId); |
| 66 | if (!sourceAxis) |
| 67 | return; |
| 68 | |
| 69 | const float axisValue = sourceAxis->axisValue(); |
| 70 | float newVelocity = 0.0f; |
| 71 | float newValue = 0.0f; |
| 72 | switch (m_sourceAxisType) { |
| 73 | case QAxisAccumulator::Velocity: |
| 74 | newVelocity = axisValue * m_scale; |
| 75 | newValue = m_value + newVelocity * dt; |
| 76 | break; |
| 77 | |
| 78 | case QAxisAccumulator::Acceleration: |
| 79 | newVelocity = m_velocity + axisValue * m_scale * dt; |
| 80 | newValue = m_value + newVelocity * dt; |
| 81 | break; |
| 82 | } |
| 83 | setVelocity(newVelocity); |
| 84 | setValue(newValue); |
| 85 | } |
| 86 | |
| 87 | } // namespace Input |
| 88 | } // namespace Qt3DInput |
| 89 | |
| 90 | QT_END_NAMESPACE |
| 91 |
