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