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#include <Qt3DCore/qpropertyupdatedchange.h>
44#include <Qt3DCore/qpropertynodeaddedchange.h>
45#include <Qt3DCore/qpropertynoderemovedchange.h>
46
47#include <Qt3DInput/private/qinputchord_p.h>
48#include <Qt3DInput/private/inputhandler_p.h>
49
50QT_BEGIN_NAMESPACE
51
52namespace Qt3DInput {
53
54namespace Input {
55
56InputChord::InputChord()
57 : AbstractActionInput()
58 , m_chords()
59 , m_inputsToTrigger()
60 , m_timeout(0)
61 , m_startTime(0)
62{
63}
64
65void InputChord::initializeFromPeer(const Qt3DCore::QNodeCreatedChangeBasePtr &change)
66{
67 const auto typedChange = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<QInputChordData>>(change);
68 const QInputChordData &data = typedChange->data;
69 m_chords = data.chordIds;
70 m_timeout = milliToNano(data.timeout);
71 m_inputsToTrigger = m_chords;
72}
73
74void InputChord::cleanup()
75{
76 setEnabled(false);
77 m_timeout = 0;
78 m_startTime = 0;
79 m_chords.clear();
80 m_inputsToTrigger.clear();
81}
82
83void InputChord::reset()
84{
85 m_startTime = 0;
86 m_inputsToTrigger = m_chords;
87}
88
89bool InputChord::actionTriggered(Qt3DCore::QNodeId input)
90{
91 m_inputsToTrigger.removeOne(input);
92 if (m_inputsToTrigger.isEmpty()) {
93 //All Triggered
94 reset();
95 return true;
96 }
97 return false;
98}
99
100void InputChord::setStartTime(qint64 time)
101{
102 m_startTime = time;
103}
104
105void InputChord::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e)
106{
107 switch (e->type()) {
108 case Qt3DCore::PropertyUpdated: {
109 const auto change = qSharedPointerCast<Qt3DCore::QPropertyUpdatedChange>(e);
110 if (change->propertyName() == QByteArrayLiteral("timeout"))
111 m_timeout = milliToNano(change->value().toInt());
112 break;
113 }
114
115 case Qt3DCore::PropertyValueAdded: {
116 const auto change = qSharedPointerCast<Qt3DCore::QPropertyNodeAddedChange>(e);
117 if (change->propertyName() == QByteArrayLiteral("chord")) {
118 m_chords.push_back(change->addedNodeId());
119 m_inputsToTrigger.push_back(change->addedNodeId());
120 }
121 break;
122 }
123
124 case Qt3DCore::PropertyValueRemoved: {
125 const auto change = qSharedPointerCast<Qt3DCore::QPropertyNodeRemovedChange>(e);
126 if (change->propertyName() == QByteArrayLiteral("chord")) {
127 m_chords.removeOne(change->removedNodeId());
128 m_inputsToTrigger.removeOne(change->removedNodeId());
129 }
130 break;
131 }
132
133 default:
134 break;
135 }
136 AbstractActionInput::sceneChangeEvent(e);
137}
138
139bool InputChord::process(InputHandler *inputHandler, qint64 currentTime)
140{
141 if (!isEnabled())
142 return false;
143
144 const qint64 startTime = m_startTime;
145 bool triggered = false;
146 int activeInputs = 0;
147 for (const Qt3DCore::QNodeId &actionInputId : qAsConst(m_chords)) {
148 AbstractActionInput *actionInput = inputHandler->lookupActionInput(actionInputId);
149 if (actionInput && actionInput->process(inputHandler, currentTime)) {
150 triggered |= actionTriggered(actionInputId);
151 activeInputs++;
152 if (startTime == 0)
153 m_startTime = currentTime;
154 }
155 }
156
157 if (startTime != 0) {
158 // Check if we are still inside the time limit for the chord
159 if ((currentTime - startTime) > m_timeout) {
160 reset();
161 if (activeInputs > 0)
162 m_startTime = startTime;
163 return false;
164 }
165 }
166
167 return triggered;
168}
169
170} // namespace Input
171
172} // namespace Qt3DInput
173
174QT_END_NAMESPACE
175
176