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 "inputsequence_p.h"
41
42#include <Qt3DInput/qabstractphysicaldevice.h>
43#include <Qt3DInput/qinputsequence.h>
44#include <Qt3DCore/qpropertynodeaddedchange.h>
45#include <Qt3DCore/qpropertynoderemovedchange.h>
46#include <Qt3DCore/qpropertyupdatedchange.h>
47#include <QtCore/QDateTime>
48
49#include <Qt3DInput/private/inputhandler_p.h>
50#include <Qt3DInput/private/qinputsequence_p.h>
51
52QT_BEGIN_NAMESPACE
53
54namespace Qt3DInput {
55
56namespace Input {
57
58InputSequence::InputSequence()
59 : AbstractActionInput()
60 , m_sequences()
61 , m_inputsToTrigger()
62 , m_timeout(0)
63 , m_buttonInterval(0)
64 , m_startTime(0)
65 , m_lastInputTime(0)
66{
67}
68
69void InputSequence::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
70{
71 const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QInputSequenceData>>(change);
72 const QInputSequenceData &data = typedChange->data;
73 m_sequences = data.sequenceIds;
74 m_timeout = milliToNano(data.timeout);
75 m_buttonInterval = milliToNano(data.buttonInterval);
76 m_inputsToTrigger = m_sequences;
77}
78
79void InputSequence::cleanup()
80{
81 setEnabled(false);
82 m_timeout = 0;
83 m_buttonInterval = 0;
84 m_startTime = 0;
85 m_lastInputTime = 0;
86 m_lastInputId = Qt3DCore::QNodeId();
87 m_sequences.clear();
88 m_inputsToTrigger.clear();
89}
90
91void InputSequence::setStartTime(qint64 time)
92{
93 m_startTime = time;
94}
95
96void InputSequence::reset()
97{
98 m_startTime = 0;
99 m_lastInputTime = 0;
100 m_inputsToTrigger = m_sequences;
101 m_lastInputId = Qt3DCore::QNodeId();
102}
103
104bool InputSequence::actionTriggered(Qt3DCore::QNodeId input, const qint64 currentTime)
105{
106 if (input != m_inputsToTrigger.first())
107 return false;
108
109 // Save the last input
110 m_lastInputId = input;
111 // Return false if we've spent too much time in between two sequences
112 if ((m_lastInputTime != 0) && ((currentTime - m_lastInputTime) > m_buttonInterval)) {
113 reset();
114 return false;
115 }
116
117 // Update lastInputTime and remove the inputs to trigger from the sequence
118 m_lastInputTime = currentTime;
119 m_inputsToTrigger.removeOne(input);
120
121 // If we have no more remaining inputs in the sequences of inputs
122 if (m_inputsToTrigger.isEmpty()) {
123 // All Triggered
124 reset();
125 return true;
126 }
127 return false;
128}
129
130void InputSequence::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
131{
132 switch (e->type()) {
133 case Qt3DCore::PropertyUpdated: {
134 const auto change = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
135 if (change->propertyName() == QByteArrayLiteral("timeout")) {
136 m_timeout = milliToNano(change->value().toInt());
137 } else if (change->propertyName() == QByteArrayLiteral("buttonInterval")) {
138 m_buttonInterval = milliToNano(change->value().toInt());
139 }
140 break;
141 }
142
143 case Qt3DCore::PropertyValueAdded: {
144 const auto change = qSharedPointerCast<Qt3DCore::QPropertyNodeAddedChange>(e);
145 if (change->propertyName() == QByteArrayLiteral("sequence")) {
146 m_sequences.push_back(change->addedNodeId());
147 m_inputsToTrigger.push_back(change->addedNodeId());
148 }
149 break;
150 }
151
152 case Qt3DCore::PropertyValueRemoved: {
153 const auto change = qSharedPointerCast<Qt3DCore::QPropertyNodeRemovedChange>(e);
154 if (change->propertyName() == QByteArrayLiteral("sequence")) {
155 m_sequences.removeOne(change->removedNodeId());
156 m_inputsToTrigger.removeOne(change->removedNodeId());
157 }
158 break;
159 }
160
161 default:
162 break;
163 }
164 AbstractActionInput::sceneChangeEvent(e);
165}
166
167bool InputSequence::process(InputHandler *inputHandler, qint64 currentTime)
168{
169 if (!isEnabled())
170 return false;
171
172 if (m_startTime != 0) {
173 // Check if we are still inside the time limit for the sequence
174 if ((currentTime - m_startTime) > m_timeout) {
175 reset();
176 return false;
177 }
178 }
179
180 bool triggered = false;
181 for (const Qt3DCore::QNodeId &actionInputId : qAsConst(m_sequences)) {
182 AbstractActionInput *actionInput = inputHandler->lookupActionInput(actionInputId);
183 if (actionInput && actionInput->process(inputHandler, currentTime)) {
184 triggered |= actionTriggered(actionInputId, currentTime);
185 if (m_startTime == 0)
186 m_startTime = currentTime;
187 }
188 }
189 return triggered;
190}
191
192} // namespace Input
193
194} // namespace Qt3DInput
195
196QT_END_NAMESPACE
197
198