| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "inputchord_p.h" |
| 5 | |
| 6 | #include <Qt3DInput/qinputchord.h> |
| 7 | |
| 8 | #include <Qt3DInput/private/qinputchord_p.h> |
| 9 | #include <Qt3DInput/private/inputhandler_p.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | namespace Qt3DInput { |
| 14 | |
| 15 | namespace Input { |
| 16 | |
| 17 | InputChord::InputChord() |
| 18 | : AbstractActionInput() |
| 19 | , m_chords() |
| 20 | , m_inputsToTrigger() |
| 21 | , m_timeout(0) |
| 22 | , m_startTime(0) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | void InputChord::cleanup() |
| 27 | { |
| 28 | setEnabled(false); |
| 29 | m_timeout = 0; |
| 30 | m_startTime = 0; |
| 31 | m_chords.clear(); |
| 32 | m_inputsToTrigger.clear(); |
| 33 | } |
| 34 | |
| 35 | void InputChord::reset() |
| 36 | { |
| 37 | m_startTime = 0; |
| 38 | m_inputsToTrigger = m_chords; |
| 39 | } |
| 40 | |
| 41 | bool InputChord::actionTriggered(Qt3DCore::QNodeId input) |
| 42 | { |
| 43 | m_inputsToTrigger.removeOne(t: input); |
| 44 | if (m_inputsToTrigger.isEmpty()) { |
| 45 | //All Triggered |
| 46 | reset(); |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | void InputChord::setStartTime(qint64 time) |
| 53 | { |
| 54 | m_startTime = time; |
| 55 | } |
| 56 | |
| 57 | void InputChord::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
| 58 | { |
| 59 | AbstractActionInput::syncFromFrontEnd(frontEnd, firstTime); |
| 60 | const QInputChord *node = qobject_cast<const QInputChord *>(object: frontEnd); |
| 61 | if (!node) |
| 62 | return; |
| 63 | |
| 64 | m_timeout = milliToNano(milli: node->timeout()); |
| 65 | m_chords = Qt3DCore::qIdsForNodes(nodes: node->chords()); |
| 66 | m_inputsToTrigger = m_chords; |
| 67 | } |
| 68 | |
| 69 | bool InputChord::process(InputHandler *inputHandler, qint64 currentTime) |
| 70 | { |
| 71 | if (!isEnabled()) |
| 72 | return false; |
| 73 | |
| 74 | const qint64 startTime = m_startTime; |
| 75 | bool triggered = false; |
| 76 | int activeInputs = 0; |
| 77 | for (const Qt3DCore::QNodeId &actionInputId : std::as_const(t&: m_chords)) { |
| 78 | AbstractActionInput *actionInput = inputHandler->lookupActionInput(id: actionInputId); |
| 79 | if (actionInput && actionInput->process(inputHandler, currentTime)) { |
| 80 | triggered |= actionTriggered(input: actionInputId); |
| 81 | activeInputs++; |
| 82 | if (startTime == 0) |
| 83 | m_startTime = currentTime; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (startTime != 0) { |
| 88 | // Check if we are still inside the time limit for the chord |
| 89 | if ((currentTime - startTime) > m_timeout) { |
| 90 | reset(); |
| 91 | if (activeInputs > 0) |
| 92 | m_startTime = startTime; |
| 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return triggered; |
| 98 | } |
| 99 | |
| 100 | } // namespace Input |
| 101 | |
| 102 | } // namespace Qt3DInput |
| 103 | |
| 104 | QT_END_NAMESPACE |
| 105 | |
| 106 |
