| 1 | // Copyright (C) 2014 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 "keyboardhandler_p.h" |
| 5 | |
| 6 | #include <Qt3DInput/qkeyboarddevice.h> |
| 7 | #include <Qt3DInput/qkeyboardhandler.h> |
| 8 | #include <QtCore/QVariant> |
| 9 | |
| 10 | #include <Qt3DInput/private/inputhandler_p.h> |
| 11 | #include <Qt3DInput/private/inputmanagers_p.h> |
| 12 | #include <Qt3DInput/private/qkeyboardhandler_p.h> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | |
| 17 | namespace Qt3DInput { |
| 18 | namespace Input { |
| 19 | |
| 20 | using namespace Qt3DCore; |
| 21 | |
| 22 | KeyboardHandler::KeyboardHandler() |
| 23 | : BackendNode(QBackendNode::ReadWrite) |
| 24 | , m_inputHandler(nullptr) |
| 25 | , m_focus(false) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | Qt3DCore::QNodeId KeyboardHandler::keyboardDevice() const |
| 30 | { |
| 31 | return m_keyboardDevice; |
| 32 | } |
| 33 | |
| 34 | void KeyboardHandler::setInputHandler(InputHandler *handler) |
| 35 | { |
| 36 | m_inputHandler = handler; |
| 37 | } |
| 38 | |
| 39 | // Called by the KeyboadDevice when the focus for the KeyboardHandler has changed |
| 40 | // Sends a change notification so that the frontend can update itself |
| 41 | void KeyboardHandler::setFocus(bool focus) |
| 42 | { |
| 43 | if (focus != m_focus) |
| 44 | m_focus = focus; |
| 45 | } |
| 46 | |
| 47 | void KeyboardHandler::syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) |
| 48 | { |
| 49 | BackendNode::syncFromFrontEnd(frontEnd, firstTime); |
| 50 | const Qt3DInput::QKeyboardHandler *node = qobject_cast<const Qt3DInput::QKeyboardHandler *>(object: frontEnd); |
| 51 | if (!node) |
| 52 | return; |
| 53 | |
| 54 | if (firstTime) |
| 55 | m_focus = false; |
| 56 | |
| 57 | bool focusRequest = false; |
| 58 | auto id = Qt3DCore::qIdForNode(node: node->sourceDevice()); |
| 59 | if (m_keyboardDevice != id) { |
| 60 | setSourcerDevice(id); |
| 61 | focusRequest = m_focus; |
| 62 | } |
| 63 | |
| 64 | if (m_focus != node->focus()) |
| 65 | focusRequest = node->focus(); |
| 66 | |
| 67 | if (focusRequest) |
| 68 | requestFocus(); |
| 69 | } |
| 70 | |
| 71 | void KeyboardHandler::requestFocus() |
| 72 | { |
| 73 | KeyboardDevice *keyboardDevice = m_inputHandler->keyboardDeviceManager()->lookupResource(id: m_keyboardDevice); |
| 74 | if (keyboardDevice && isEnabled()) |
| 75 | keyboardDevice->requestFocusForInput(inputId: peerId()); |
| 76 | } |
| 77 | |
| 78 | void KeyboardHandler::setSourcerDevice(QNodeId device) |
| 79 | { |
| 80 | m_keyboardDevice = device; |
| 81 | } |
| 82 | |
| 83 | KeyboardHandlerFunctor::KeyboardHandlerFunctor(InputHandler *handler) |
| 84 | : m_handler(handler) |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | Qt3DCore::QBackendNode *KeyboardHandlerFunctor::create(Qt3DCore::QNodeId id) const |
| 89 | { |
| 90 | KeyboardHandler *input = m_handler->keyboardInputManager()->getOrCreateResource(id); |
| 91 | input->setInputHandler(m_handler); |
| 92 | return input; |
| 93 | } |
| 94 | |
| 95 | QBackendNode *KeyboardHandlerFunctor::get(QNodeId id) const |
| 96 | { |
| 97 | return m_handler->keyboardInputManager()->lookupResource(id); |
| 98 | } |
| 99 | |
| 100 | void KeyboardHandlerFunctor::destroy(QNodeId id) const |
| 101 | { |
| 102 | m_handler->keyboardInputManager()->releaseResource(id); |
| 103 | } |
| 104 | |
| 105 | } // namespace Input |
| 106 | } // namespace Qt3DInput |
| 107 | |
| 108 | QT_END_NAMESPACE |
| 109 |
