aboutsummaryrefslogtreecommitdiffstats
path: root/src/xr/quick3dxr/qquick3dxractionmapper_p.h
blob: 015c1927e6bf32cb1c1998f0ba87dd16726a5097 (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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef QQUICK3DXRACTIONMAPPER_H
#define QQUICK3DXRACTIONMAPPER_H

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

#include <QObject>
#include <QQmlEngine>
#include <QQuick3DObject>
#include <QMultiHash>
#include <QPointer>
#include "qquick3dxrabstracthapticeffect_p.h"

QT_BEGIN_NAMESPACE

class QQuick3DXrController;
class QQuick3DXrInputAction;
class QQuick3DXrActionMapper;

class QQuick3DXrInputAction : public QObject, public QQmlParserStatus
{
    Q_OBJECT
    Q_INTERFACES(QQmlParserStatus)
    QML_NAMED_ELEMENT(XrInputAction)
    QML_ADDED_IN_VERSION(6, 8)

    Q_PROPERTY(float value READ value NOTIFY valueChanged FINAL)
    Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged FINAL)
    Q_PROPERTY(QString actionName READ actionName WRITE setActionName NOTIFY actionNameChanged FINAL)
    Q_PROPERTY(QList<Action> actionId READ actionId WRITE setActionId NOTIFY actionIdChanged FINAL)
    Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged FINAL REVISION(6, 9))

    Q_PROPERTY(Controller hand READ hand WRITE setHand NOTIFY handChanged FINAL)
    Q_PROPERTY(Controller controller READ controller WRITE setController NOTIFY controllerChanged FINAL REVISION(6, 10))

public:

    // Same values as XrController and XrHandModel enums
    enum Controller : quint8 {
        LeftHand = 0,
        RightHand,
        Unknown,
        LeftController = LeftHand,
        RightController = RightHand,
        UnknownController = Unknown
    };
    Q_ENUM(Controller)

    enum Action : qint16 {
        CustomAction = -1,
        Button1Pressed,
        Button1Touched,
        Button2Pressed,
        Button2Touched,
        ButtonMenuPressed,
        ButtonMenuTouched,
        ButtonSystemPressed,
        ButtonSystemTouched,
        SqueezeValue,
        SqueezeForce,
        SqueezePressed,
        TriggerValue,
        TriggerPressed,
        TriggerTouched,
        ThumbstickX,
        ThumbstickY,
        ThumbstickPressed,
        ThumbstickTouched,
        ThumbrestTouched,
        TrackpadX,
        TrackpadY,
        TrackpadForce,
        TrackpadTouched,
        TrackpadPressed,
        IndexFingerPinch,
        MiddleFingerPinch,
        RingFingerPinch,
        LittleFingerPinch,
        HandTrackingMenuPress,
        NumHandActions,
        NumActions
    };
    Q_ENUM(Action)

    explicit QQuick3DXrInputAction(QObject *parent = nullptr);
    ~QQuick3DXrInputAction() override;
    float value() const;
    void setValue(float newValue);
    bool pressed() const;
    void setPressed(bool newPressed);

    QString actionName() const;
    void setActionName(const QString &newActionName);

    QList<Action> actionId() const;
    void setActionId(const QList<Action> &newActionId);

    void classBegin() override;
    void componentComplete() override;

    Controller hand() const;
    void setHand(Controller newHand);

    bool enabled() const;
    void setEnabled(bool newEnabled);

    Controller controller() const;
    void setController(Controller newController);

Q_SIGNALS:
    void valueChanged();
    void pressedChanged();
    void triggered();

    void actionNameChanged();
    void actionIdChanged();

    void handChanged();

    void enabledChanged();

    void controllerChanged();

private:
    QString m_actionName;
    float m_value = 0;
    bool m_pressed = false;
    bool m_componentComplete = false;
    bool m_enabled = true;
    Controller m_controller;

    QList<Action> m_actionIds;
};

class QQuick3DXrHapticFeedback : public QObject, public QQmlParserStatus
{
    Q_OBJECT
    Q_INTERFACES(QQmlParserStatus)
    QML_NAMED_ELEMENT(XrHapticFeedback)
    QML_ADDED_IN_VERSION(6, 9)

    Q_PROPERTY(Controller controller READ controller WRITE setController NOTIFY controllerChanged FINAL)
    Q_PROPERTY(QQuick3DXrAbstractHapticEffect *hapticEffect READ hapticEffect WRITE setHapticEffect NOTIFY hapticEffectChanged FINAL)
    Q_PROPERTY(bool trigger READ trigger WRITE setTrigger NOTIFY triggerChanged FINAL)
    Q_PROPERTY(Condition condition READ condition WRITE setCondition NOTIFY conditionChanged FINAL)

public:

    // Same values as XrController and XrHandModel enums
    enum class Controller : quint8 {
        LeftController = 0,
        RightController,
        UnknownController,
    };
    Q_ENUM(Controller)

    enum class Condition : quint8 {
        RisingEdge = 0,
        TrailingEdge,
    };
    Q_ENUM(Condition)

    explicit QQuick3DXrHapticFeedback(QObject *parent = nullptr);
    ~QQuick3DXrHapticFeedback() override;

    void classBegin() override;
    void componentComplete() override;

    QQuick3DXrAbstractHapticEffect *hapticEffect() const;
    void setHapticEffect(QQuick3DXrAbstractHapticEffect *newHapticEffect);

    Controller controller() const;
    void setController(Controller newController);

    bool trigger();
    void setTrigger(bool newTrigger);

    enum Condition condition()  const;
    void setCondition(enum Condition newCondition);

    bool testAndClear();

Q_SIGNALS:
    void controllerChanged();
    void hapticEffectChanged();
    void triggerChanged();
    void conditionChanged();

public Q_SLOTS:
    void start();
    void stop();

private:
    QMetaObject::Connection m_triggerConnection;
    Controller m_controller = Controller::UnknownController;
    Condition m_condition = Condition::RisingEdge;
    QPointer<QQuick3DXrAbstractHapticEffect> m_hapticEffect;
    bool m_trigger = false;
    bool m_componentComplete = false;
    bool m_pending = false;
};

class QQuick3DXrActionMapper : public QObject
{
    Q_OBJECT
public:
    static QQuick3DXrActionMapper *instance();

    static QList<QPointer<QQuick3DXrHapticFeedback>> getHapticEffects(QQuick3DXrInputAction::Controller hand);

    static void handleInput(QQuick3DXrInputAction::Action id, QQuick3DXrInputAction::Controller hand, const char *shortName, float value);
    static void registerAction(QQuick3DXrInputAction *action);
    static void registerHapticEffect(QPointer<QQuick3DXrHapticFeedback>);
    static void removeAction(QQuick3DXrInputAction *action);
    static void removeHapticEffect(QQuick3DXrHapticFeedback *action);

private:
    explicit QQuick3DXrActionMapper(QObject *parent = nullptr);

    struct HapticData
    {
        QList<QPointer<QQuick3DXrHapticFeedback>> m_hapticEffects;
    } m_hapticData[2];

    QMultiHash<quint32, QQuick3DXrInputAction *> m_actions;
    QMultiHash<QString, QQuick3DXrInputAction *> m_customActions;
};

QT_END_NAMESPACE

#endif // QQUICK3DXRACTIONMAPPER_H