summaryrefslogtreecommitdiffstats
path: root/src/input/backend/keyboardcontroller_p.h
blob: 3f4f8da535c79dde9454fa52390dbe7f70e21e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QT3DINPUT_INPUT_KEYBOARDCONTROLLER_P_H
#define QT3DINPUT_INPUT_KEYBOARDCONTROLLER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <Qt3DInput/qabstractphysicaldevicebackendnode.h>
#include <Qt3DCore/qnodeid.h>
#include <Qt3DInput/private/handle_types_p.h>
#include <QKeyEvent>

QT_BEGIN_NAMESPACE

namespace Qt3DInput {

class QInputAspect;

namespace Input {

class InputHandler;

class KeyboardController : public Qt3DInput::QAbstractPhysicalDeviceBackendNode
{
public:
    KeyboardController();
    void updateFromPeer(Qt3DCore::QNode *peer) Q_DECL_OVERRIDE;

    void cleanup();

    void requestFocusForInput(const Qt3DCore::QNodeId &inputId);
    void setInputHandler(InputHandler *handler);

    void addKeyboardInput(const Qt3DCore::QNodeId &input);
    void removeKeyboardInput(const Qt3DCore::QNodeId &input);

    void setCurrentFocusItem(const Qt3DCore::QNodeId &input);

    float axisValue(int axisIdentifier) const Q_DECL_OVERRIDE;
    bool isButtonPressed(int buttonIdentifier) const Q_DECL_OVERRIDE;

    void updateKeyEvents(const QList<QT_PREPEND_NAMESPACE(QKeyEvent)> &events);

    inline Qt3DCore::QNodeId currentFocusItem() const { return m_currentFocusItem; }
    inline Qt3DCore::QNodeId lastKeyboardInputRequester() const { return m_lastRequester; }
    inline QVector<Qt3DCore::QNodeId> keyboardInputs() const { return m_keyboardInputs; }
    inline QVector<HKeyboardInput> keyboardInputsHandles() const { return m_keyboardInputHandles; }

protected:
    void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &) Q_DECL_OVERRIDE;

private:
    void setButtonValue(int key, bool value);

    InputHandler *m_inputHandler;
    QVector<Qt3DCore::QNodeId> m_keyboardInputs;
    QVector<HKeyboardInput> m_keyboardInputHandles;
    Qt3DCore::QNodeId m_lastRequester;
    Qt3DCore::QNodeId m_currentFocusItem;

    union KeyStates {

        struct Buttons
        {
            // first 4 bytes
            bool keyEscape:1;           // 0
            bool keyTab:1;              // 1
            bool keyBacktab:1;          // 2
            bool keyBackspace:1;        // 3
            bool keyReturn:1;           // 4
            bool keyEnter:1;            // 5
            bool keyInsert:1;           // 6
            bool keyDelete:1;           // 7
            bool keyPause:1;            // 8
            bool keyPrint:1;            // 9
            bool keySysReq:1;           // 10
            bool keyClear:1;            // 11
            bool keyHome:1;             // 12
            bool keyEnd:1;              // 13
            bool keyLeft:1;             // 14
            bool keyUp:1;               // 15
            bool keyRight:1;            // 16
            bool keyDown:1;             // 17
            bool keyPageUp:1;           // 18
            bool keyPageDown:1;         // 19
            bool keyShift:1;            // 20
            bool keyControl:1;          // 21
            bool keyMeta:1;             // 22
            bool keyAlt:1;              // 23
            bool keyCapsLock:1;         // 24
            bool keyNumLock:1;          // 25
            bool keyScrollLock:1;       // 26
            bool keyF1:1;               // 27
            bool keyF2:1;               // 28
            bool keyF3:1;               // 29
            bool keyF4:1;               // 30
            bool keyF5:1;               // 31

            // second 4 bytes
            bool keyF6:1;               // 0
            bool keyF7:1;               // 1
            bool keyF8:1;               // 2
            bool keyF9:1;               // 3
            bool keyF10:1;              // 4
            bool keyF11:1;              // 5
            bool keyF12:1;              // 6
            bool keyF13:1;              // 7
            bool keyF14:1;              // 8
            bool keyF15:1;              // 9
            bool keyF16:1;              // 10
            bool keyF17:1;              // 11
            bool keyF18:1;              // 12
            bool keyF19:1;              // 13
            bool keyF20:1;              // 14
            bool keyF21:1;              // 15
            bool keyF22:1;              // 16
            bool keyF23:1;              // 17
            bool keyF24:1;              // 18
            bool keyF25:1;              // 19
            bool keyF26:1;              // 20
            bool keyF27:1;              // 21
            bool keyF28:1;              // 22
            bool keyF29:1;              // 23
            bool keyF30:1;              // 24
            bool keyF31:1;              // 25
            bool keyF32:1;              // 26
            bool keyF33:1;              // 27
            bool keyF34:1;              // 28
            bool keyF35:1;              // 29
            bool keySuper_L:1;          // 30
            bool keySuper_R:1;          // 31

            // third 4 bytes
            // unused                   // 0
            bool keyMenu:1;             // 1
            bool keyHyper_L:1;          // 2
            bool keyHyper_R:1;          // 3
            bool keyHelp:1;             // 4
            bool keyDirection_L:1;      // 5
            bool keyDirection_R:1;      // 6
            bool keySpace:1;            // 7
            bool keyAny:1;              // 8
            bool keyExclam:1;           // 9
            bool keyQuoteDbl:1;         // 10
            bool keyNumberSign:1;       // 11
            bool keyDollar:1;           // 12
            bool keyPercent:1;          // 13
            bool keyAmpersand:1;        // 14
            bool keyApostrophe:1;       // 15
            bool keyParenLeft:1;        // 16
            bool keyParenRight:1;       // 17
            bool keyAsterisk:1;         // 18
            bool keyPlus:1;             // 19
            bool keyComma:1;            // 20
            bool keyMinus:1;            // 21
            bool keyPeriod:1;           // 22
            bool keySlash:1;            // 23
            bool key0:1;                // 24
            bool key1:1;                // 25
            bool key2:1;                // 26
            bool key3:1;                // 27
            bool key4:1;                // 28
            bool key5:1;                // 29
            bool key6:1;                // 30
            bool key7:1;                // 31

            // fourth 4 bytes
            bool key8:1;                // 0
            bool key9:1;                // 1
            bool keyColon:1;            // 2
            bool keySemicolon:1;        // 3
            bool keyLess:1;             // 4
            bool keyEqual:1;            // 5
            bool keyGreater:1;          // 6
            bool keyQuestion:1;         // 7
            bool keyAt:1;               // 8
            bool keyA:1;                // 9
            bool keyB:1;                // 10
            bool keyC:1;                // 11
            bool keyD:1;                // 12
            bool keyE:1;                // 13
            bool keyF:1;                // 14
            bool keyG:1;                // 15
            bool keyH:1;                // 16
            bool keyI:1;                // 17
            bool keyJ:1;                // 18
            bool keyK:1;                // 19
            bool keyL:1;                // 20
            bool keyM:1;                // 21
            bool keyN:1;                // 22
            bool keyO:1;                // 23
            bool keyP:1;                // 24
            bool keyQ:1;                // 25
            bool keyR:1;                // 26
            bool keyS:1;                // 27
            bool keyT:1;                // 28
            bool keyU:1;                // 29
            bool keyV:1;                // 30
            bool keyW:1;                // 31

            // fifth 4 bytes
            bool keyX:1;                // 0
            bool keyY:1;                // 1
            bool keyZ:1;                // 2
            bool keyBracketLeft:1;      // 3
            bool keyBackslash:1;        // 4
            bool keyBracketRight:1;     // 5
            bool keyAsciiCircum:1;      // 6
            bool keyUnderscore:1;       // 7
            bool keyQuoteLeft:1;        // 8
            bool keyBraceLeft:1;        // 9
            bool keyBar:1;              // 10
            bool keyBraceRight:1;       // 11
            bool keyAsciiTilde:1;       // 12
            bool keyplusminus:1;        // 13
            bool keyonesuperior:1;      // 14
            bool keymultiply:1;         // 15
            bool keydivision:1;         // 16
            bool keyydiaeresis:1;       // 17
        };
        qint32 keys[5];
    };

    KeyStates m_keyStates;
};

class KeyboardControllerFunctor : public Qt3DCore::QBackendNodeFunctor
{
public:
    explicit KeyboardControllerFunctor(QInputAspect *inputaspect, InputHandler *handler);

    Qt3DCore::QBackendNode *create(Qt3DCore::QNode *frontend, const Qt3DCore::QBackendNodeFactory *factory) const Q_DECL_OVERRIDE;
    Qt3DCore::QBackendNode *get(const Qt3DCore::QNodeId &id) const Q_DECL_OVERRIDE;
    void destroy(const Qt3DCore::QNodeId &id) const Q_DECL_OVERRIDE;

private:
    QInputAspect *m_inputAspect;
    InputHandler *m_handler;
};

} // namespace Input
} // namespace Qt3DInput

QT_END_NAMESPACE

#endif // QT3DINPUT_INPUT_KEYBOARDCONTROLLER_P_H