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 "inputchord_p.h" |
41 | |
42 | #include <Qt3DInput/qinputchord.h> |
43 | |
44 | #include <Qt3DInput/private/qinputchord_p.h> |
45 | #include <Qt3DInput/private/inputhandler_p.h> |
46 | |
47 | QT_BEGIN_NAMESPACE |
48 | |
49 | namespace Qt3DInput { |
50 | |
51 | namespace Input { |
52 | |
53 | InputChord::InputChord() |
54 | : AbstractActionInput() |
55 | , m_chords() |
56 | , m_inputsToTrigger() |
57 | , m_timeout(0) |
58 | , m_startTime(0) |
59 | { |
60 | } |
61 | |
62 | void InputChord::cleanup() |
63 | { |
64 | setEnabled(false); |
65 | m_timeout = 0; |
66 | m_startTime = 0; |
67 | m_chords.clear(); |
68 | m_inputsToTrigger.clear(); |
69 | } |
70 | |
71 | void InputChord::reset() |
72 | { |
73 | m_startTime = 0; |
74 | m_inputsToTrigger = m_chords; |
75 | } |
76 | |
77 | bool InputChord::actionTriggered(Qt3DCore::QNodeId input) |
78 | { |
79 | m_inputsToTrigger.removeOne(t: input); |
80 | if (m_inputsToTrigger.isEmpty()) { |
81 | //All Triggered |
82 | reset(); |
83 | return true; |
84 | } |
85 | return false; |
86 | } |
87 | |
88 | void InputChord::setStartTime(qint64 time) |
89 | { |
90 | m_startTime = time; |
91 | } |
92 | |
93 | void InputChord::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
94 | { |
95 | AbstractActionInput::syncFromFrontEnd(frontEnd, firstTime); |
96 | const QInputChord *node = qobject_cast<const QInputChord *>(object: frontEnd); |
97 | if (!node) |
98 | return; |
99 | |
100 | m_timeout = milliToNano(milli: node->timeout()); |
101 | m_chords = Qt3DCore::qIdsForNodes(nodes: node->chords()); |
102 | m_inputsToTrigger = m_chords; |
103 | } |
104 | |
105 | bool InputChord::process(InputHandler *inputHandler, qint64 currentTime) |
106 | { |
107 | if (!isEnabled()) |
108 | return false; |
109 | |
110 | const qint64 startTime = m_startTime; |
111 | bool triggered = false; |
112 | int activeInputs = 0; |
113 | for (const Qt3DCore::QNodeId &actionInputId : qAsConst(t&: m_chords)) { |
114 | AbstractActionInput *actionInput = inputHandler->lookupActionInput(id: actionInputId); |
115 | if (actionInput && actionInput->process(inputHandler, currentTime)) { |
116 | triggered |= actionTriggered(input: actionInputId); |
117 | activeInputs++; |
118 | if (startTime == 0) |
119 | m_startTime = currentTime; |
120 | } |
121 | } |
122 | |
123 | if (startTime != 0) { |
124 | // Check if we are still inside the time limit for the chord |
125 | if ((currentTime - startTime) > m_timeout) { |
126 | reset(); |
127 | if (activeInputs > 0) |
128 | m_startTime = startTime; |
129 | return false; |
130 | } |
131 | } |
132 | |
133 | return triggered; |
134 | } |
135 | |
136 | } // namespace Input |
137 | |
138 | } // namespace Qt3DInput |
139 | |
140 | QT_END_NAMESPACE |
141 | |
142 | |