1/****************************************************************************
2**
3** Copyright (C) 2016 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 "axisaccumulator_p.h"
41
42#include <Qt3DCore/qpropertyupdatedchange.h>
43
44#include <Qt3DInput/private/inputmanagers_p.h>
45#include <Qt3DInput/private/qaxisaccumulator_p.h>
46
47QT_BEGIN_NAMESPACE
48
49namespace Qt3DInput {
50namespace Input {
51
52AxisAccumulator::AxisAccumulator()
53 : Qt3DCore::QBackendNode(ReadWrite)
54 , m_sourceAxisId()
55 , m_sourceAxisType(QAxisAccumulator::Velocity)
56 , m_scale(1.0f)
57 , m_value(0.0f)
58 , m_velocity(0.0f)
59{
60}
61
62void AxisAccumulator::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
63{
64 const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QAxisAccumulatorData>>(change);
65 const auto &data = typedChange->data;
66 m_sourceAxisId = data.sourceAxisId;
67 m_sourceAxisType = data.sourceAxisType;
68 m_scale = data.scale;
69 m_value = 0.0f;
70 m_velocity = 0.0f;
71}
72
73void AxisAccumulator::cleanup()
74{
75 QBackendNode::setEnabled(false);
76 m_sourceAxisId = Qt3DCore::QNodeId();
77 m_sourceAxisType = QAxisAccumulator::Velocity;
78 m_scale = 1.0f;
79 m_value = 0.0f;
80}
81
82void AxisAccumulator::setValue(float value)
83{
84 if (isEnabled() && value != m_value) {
85 m_value = value;
86
87 // Send a change to the frontend
88 auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId());
89 e->setDeliveryFlags(Qt3DCore::QSceneChange::DeliverToAll);
90 e->setPropertyName("value");
91 e->setValue(m_value);
92 notifyObservers(e);
93 }
94}
95
96void AxisAccumulator::setVelocity(float velocity)
97{
98 if (isEnabled() && velocity != m_velocity) {
99 m_velocity = velocity;
100
101 // Send a change to the frontend
102 auto e = Qt3DCore::QPropertyUpdatedChangePtr::create(peerId());
103 e->setDeliveryFlags(Qt3DCore::QSceneChange::DeliverToAll);
104 e->setPropertyName("velocity");
105 e->setValue(m_velocity);
106 notifyObservers(e);
107 }
108}
109
110void AxisAccumulator::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
111{
112 switch (e->type()) {
113 case Qt3DCore::PropertyUpdated: {
114 const auto change = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
115 if (change->propertyName() == QByteArrayLiteral("sourceAxis"))
116 m_sourceAxisId = change->value().value<Qt3DCore::QNodeId>();
117 else if (change->propertyName() == QByteArrayLiteral("sourceAxisType"))
118 m_sourceAxisType = change->value().value<QAxisAccumulator::SourceAxisType>();
119 else if (change->propertyName() == QByteArrayLiteral("scale"))
120 m_scale = change->value().toFloat();
121 break;
122 }
123
124 default:
125 break;
126 }
127 QBackendNode::sceneChangeEvent(e);
128}
129
130void AxisAccumulator::stepIntegration(AxisManager *axisManager, float dt)
131{
132 Axis *sourceAxis = axisManager->lookupResource(m_sourceAxisId);
133 if (!sourceAxis)
134 return;
135
136 const float axisValue = sourceAxis->axisValue();
137 float newVelocity = 0.0f;
138 float newValue = 0.0f;
139 switch (m_sourceAxisType) {
140 case QAxisAccumulator::Velocity:
141 newVelocity = axisValue * m_scale;
142 newValue = m_value + newVelocity * dt;
143 break;
144
145 case QAxisAccumulator::Acceleration:
146 newVelocity = m_velocity + axisValue * m_scale * dt;
147 newValue = m_value + newVelocity * dt;
148 break;
149 }
150 setVelocity(newVelocity);
151 setValue(newValue);
152}
153
154} // namespace Input
155} // namespace Qt3DInput
156
157QT_END_NAMESPACE
158