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 <QtCore/QDateTime> |
45 | |
46 | #include <Qt3DInput/private/inputhandler_p.h> |
47 | #include <Qt3DInput/private/qinputsequence_p.h> |
48 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | namespace Qt3DInput { |
52 | |
53 | namespace Input { |
54 | |
55 | InputSequence::InputSequence() |
56 | : AbstractActionInput() |
57 | , m_sequences() |
58 | , m_inputsToTrigger() |
59 | , m_timeout(0) |
60 | , m_buttonInterval(0) |
61 | , m_startTime(0) |
62 | , m_lastInputTime(0) |
63 | { |
64 | } |
65 | |
66 | void InputSequence::cleanup() |
67 | { |
68 | setEnabled(false); |
69 | m_timeout = 0; |
70 | m_buttonInterval = 0; |
71 | m_startTime = 0; |
72 | m_lastInputTime = 0; |
73 | m_lastInputId = Qt3DCore::QNodeId(); |
74 | m_sequences.clear(); |
75 | m_inputsToTrigger.clear(); |
76 | } |
77 | |
78 | void InputSequence::setStartTime(qint64 time) |
79 | { |
80 | m_startTime = time; |
81 | } |
82 | |
83 | void InputSequence::reset() |
84 | { |
85 | m_startTime = 0; |
86 | m_lastInputTime = 0; |
87 | m_inputsToTrigger = m_sequences; |
88 | m_lastInputId = Qt3DCore::QNodeId(); |
89 | } |
90 | |
91 | bool InputSequence::actionTriggered(Qt3DCore::QNodeId input, const qint64 currentTime) |
92 | { |
93 | if (input != m_inputsToTrigger.first()) |
94 | return false; |
95 | |
96 | // Save the last input |
97 | m_lastInputId = input; |
98 | // Return false if we've spent too much time in between two sequences |
99 | if ((m_lastInputTime != 0) && ((currentTime - m_lastInputTime) > m_buttonInterval)) { |
100 | reset(); |
101 | return false; |
102 | } |
103 | |
104 | // Update lastInputTime and remove the inputs to trigger from the sequence |
105 | m_lastInputTime = currentTime; |
106 | m_inputsToTrigger.removeOne(t: input); |
107 | |
108 | // If we have no more remaining inputs in the sequences of inputs |
109 | if (m_inputsToTrigger.isEmpty()) { |
110 | // All Triggered |
111 | reset(); |
112 | return true; |
113 | } |
114 | return false; |
115 | } |
116 | |
117 | void InputSequence::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
118 | { |
119 | AbstractActionInput::syncFromFrontEnd(frontEnd, firstTime); |
120 | const QInputSequence *node = qobject_cast<const QInputSequence *>(object: frontEnd); |
121 | if (!node) |
122 | return; |
123 | |
124 | m_timeout = milliToNano(milli: node->timeout()); |
125 | m_buttonInterval = milliToNano(milli: node->buttonInterval()); |
126 | m_sequences = Qt3DCore::qIdsForNodes(nodes: node->sequences()); |
127 | m_inputsToTrigger = m_sequences; |
128 | } |
129 | |
130 | bool InputSequence::process(InputHandler *inputHandler, qint64 currentTime) |
131 | { |
132 | if (!isEnabled()) |
133 | return false; |
134 | |
135 | if (m_startTime != 0) { |
136 | // Check if we are still inside the time limit for the sequence |
137 | if ((currentTime - m_startTime) > m_timeout) { |
138 | reset(); |
139 | return false; |
140 | } |
141 | } |
142 | |
143 | bool triggered = false; |
144 | for (const Qt3DCore::QNodeId &actionInputId : qAsConst(t&: m_sequences)) { |
145 | AbstractActionInput *actionInput = inputHandler->lookupActionInput(id: actionInputId); |
146 | if (actionInput && actionInput->process(inputHandler, currentTime)) { |
147 | triggered |= actionTriggered(input: actionInputId, currentTime); |
148 | if (m_startTime == 0) |
149 | m_startTime = currentTime; |
150 | } |
151 | } |
152 | return triggered; |
153 | } |
154 | |
155 | } // namespace Input |
156 | |
157 | } // namespace Qt3DInput |
158 | |
159 | QT_END_NAMESPACE |
160 | |
161 | |