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 "buttonaxisinput_p.h" |
41 | |
42 | #include <Qt3DInput/qbuttonaxisinput.h> |
43 | #include <Qt3DInput/qabstractphysicaldevice.h> |
44 | #include <Qt3DCore/qpropertyupdatedchange.h> |
45 | |
46 | #include <Qt3DInput/private/qabstractphysicaldevicebackendnode_p.h> |
47 | #include <Qt3DInput/private/qbuttonaxisinput_p.h> |
48 | #include <Qt3DInput/private/utils_p.h> |
49 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | namespace Qt3DInput { |
53 | |
54 | namespace Input { |
55 | |
56 | ButtonAxisInput::ButtonAxisInput() |
57 | : AbstractAxisInput() |
58 | , m_scale(0.0f) |
59 | , m_acceleration(-1.0f) |
60 | , m_deceleration(-1.0f) |
61 | , m_speedRatio(0.0f) |
62 | , m_lastUpdateTime(0) |
63 | { |
64 | } |
65 | |
66 | void ButtonAxisInput::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change) |
67 | { |
68 | const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QButtonAxisInputData>>(change); |
69 | const auto &data = typedChange->data; |
70 | m_buttons = data.buttons; |
71 | m_scale = data.scale; |
72 | m_acceleration = data.acceleration; |
73 | m_deceleration = data.deceleration; |
74 | AbstractAxisInput::initializeFromPeer(change); |
75 | } |
76 | |
77 | void ButtonAxisInput::cleanup() |
78 | { |
79 | m_scale = 0.0f; |
80 | m_buttons.clear(); |
81 | m_acceleration = -1.0f; |
82 | m_deceleration = -1.0f; |
83 | AbstractAxisInput::cleanup(); |
84 | } |
85 | |
86 | void ButtonAxisInput::updateSpeedRatio(qint64 currentTime, ButtonAxisInput::UpdateType type) |
87 | { |
88 | const float accel = (type == Accelerate) ? acceleration() : -deceleration(); |
89 | |
90 | // Was in nanoseconds, while acceleration will be in units per square seconds |
91 | const float delta = m_lastUpdateTime ? (currentTime - m_lastUpdateTime) / 1000000000.0f : 0.0f; |
92 | const float speedRatio = m_speedRatio + delta * accel; |
93 | |
94 | // Clamp it |
95 | m_speedRatio = qMax(0.0f, qMin(speedRatio, 1.0f)); |
96 | |
97 | // If we stopped, time to start over |
98 | if (type == Decelerate && m_speedRatio == 0.0f) |
99 | m_lastUpdateTime = 0; |
100 | else |
101 | m_lastUpdateTime = currentTime; |
102 | } |
103 | |
104 | void ButtonAxisInput::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) |
105 | { |
106 | if (e->type() == Qt3DCore::PropertyUpdated) { |
107 | Qt3DCore::QPropertyUpdatedChangePtr propertyChange = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e); |
108 | if (propertyChange->propertyName() == QByteArrayLiteral("scale" )) { |
109 | m_scale = propertyChange->value().toFloat(); |
110 | } else if (propertyChange->propertyName() == QByteArrayLiteral("buttons" )) { |
111 | m_buttons = propertyChange->value().value<QVector<int>>(); |
112 | } else if (propertyChange->propertyName() == QByteArrayLiteral("acceleration" )) { |
113 | m_acceleration = propertyChange->value().toFloat(); |
114 | } else if (propertyChange->propertyName() == QByteArrayLiteral("deceleration" )) { |
115 | m_deceleration = propertyChange->value().toFloat(); |
116 | } |
117 | } |
118 | AbstractAxisInput::sceneChangeEvent(e); |
119 | } |
120 | |
121 | namespace { |
122 | |
123 | bool anyOfRequiredButtonsPressed(const QVector<int> &buttons, QAbstractPhysicalDeviceBackendNode *physicalDeviceBackend) |
124 | { |
125 | bool validButtonWasPressed = false; |
126 | for (int button : buttons) { |
127 | if (physicalDeviceBackend->isButtonPressed(button)) { |
128 | validButtonWasPressed = true; |
129 | break; |
130 | } |
131 | } |
132 | return validButtonWasPressed; |
133 | } |
134 | |
135 | } // anonymous |
136 | |
137 | float ButtonAxisInput::process(InputHandler *inputHandler, qint64 currentTime) |
138 | { |
139 | if (!isEnabled()) |
140 | return 0.0f; |
141 | |
142 | if (m_buttons.isEmpty()) |
143 | return 0.0f; |
144 | |
145 | QAbstractPhysicalDeviceBackendNode *physicalDeviceBackend = Utils::physicalDeviceForInput(this, inputHandler); |
146 | if (!physicalDeviceBackend) |
147 | return 0.0f; |
148 | |
149 | // TO DO: Linear Curver for the progression of the scale value |
150 | if (anyOfRequiredButtonsPressed(m_buttons, physicalDeviceBackend)) |
151 | updateSpeedRatio(currentTime, ButtonAxisInput::Accelerate); |
152 | else if (m_speedRatio != 0.0f) |
153 | updateSpeedRatio(currentTime, ButtonAxisInput::Decelerate); |
154 | |
155 | return m_speedRatio * m_scale; |
156 | } |
157 | |
158 | } // Input |
159 | |
160 | } // Qt3DInput |
161 | |
162 | QT_END_NAMESPACE |
163 | |