1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#ifndef QQUICK3DXRACTIONMAPPER_H
5#define QQUICK3DXRACTIONMAPPER_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QObject>
19#include <QQmlEngine>
20#include <QQuick3DObject>
21#include <QMultiHash>
22#include <QPointer>
23#include "qquick3dxrabstracthapticeffect_p.h"
24
25QT_BEGIN_NAMESPACE
26
27class QQuick3DXrController;
28class QQuick3DXrInputAction;
29class QQuick3DXrActionMapper;
30
31class QQuick3DXrInputAction : public QObject, public QQmlParserStatus
32{
33 Q_OBJECT
34 Q_INTERFACES(QQmlParserStatus)
35 QML_NAMED_ELEMENT(XrInputAction)
36 QML_ADDED_IN_VERSION(6, 8)
37
38 Q_PROPERTY(float value READ value NOTIFY valueChanged FINAL)
39 Q_PROPERTY(bool pressed READ pressed NOTIFY pressedChanged FINAL)
40 Q_PROPERTY(QString actionName READ actionName WRITE setActionName NOTIFY actionNameChanged FINAL)
41 Q_PROPERTY(QList<Action> actionId READ actionId WRITE setActionId NOTIFY actionIdChanged FINAL)
42 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged FINAL REVISION(6, 9))
43
44 Q_PROPERTY(Controller hand READ hand WRITE setHand NOTIFY handChanged FINAL)
45 Q_PROPERTY(Controller controller READ controller WRITE setController NOTIFY controllerChanged FINAL REVISION(6, 10))
46
47public:
48
49 // Same values as XrController and XrHandModel enums
50 enum Controller : quint8 {
51 LeftHand = 0,
52 RightHand,
53 Unknown,
54 LeftController = LeftHand,
55 RightController = RightHand,
56 UnknownController = Unknown
57 };
58 Q_ENUM(Controller)
59
60 enum Action : qint16 {
61 CustomAction = -1,
62 Button1Pressed,
63 Button1Touched,
64 Button2Pressed,
65 Button2Touched,
66 ButtonMenuPressed,
67 ButtonMenuTouched,
68 ButtonSystemPressed,
69 ButtonSystemTouched,
70 SqueezeValue,
71 SqueezeForce,
72 SqueezePressed,
73 TriggerValue,
74 TriggerPressed,
75 TriggerTouched,
76 ThumbstickX,
77 ThumbstickY,
78 ThumbstickPressed,
79 ThumbstickTouched,
80 ThumbrestTouched,
81 TrackpadX,
82 TrackpadY,
83 TrackpadForce,
84 TrackpadTouched,
85 TrackpadPressed,
86 IndexFingerPinch,
87 MiddleFingerPinch,
88 RingFingerPinch,
89 LittleFingerPinch,
90 HandTrackingMenuPress,
91 NumHandActions,
92 NumActions
93 };
94 Q_ENUM(Action)
95
96 explicit QQuick3DXrInputAction(QObject *parent = nullptr);
97 ~QQuick3DXrInputAction() override;
98 float value() const;
99 void setValue(float newValue);
100 bool pressed() const;
101 void setPressed(bool newPressed);
102
103 QString actionName() const;
104 void setActionName(const QString &newActionName);
105
106 QList<Action> actionId() const;
107 void setActionId(const QList<Action> &newActionId);
108
109 void classBegin() override;
110 void componentComplete() override;
111
112 Controller hand() const;
113 void setHand(Controller newHand);
114
115 bool enabled() const;
116 void setEnabled(bool newEnabled);
117
118 Controller controller() const;
119 void setController(Controller newController);
120
121Q_SIGNALS:
122 void valueChanged();
123 void pressedChanged();
124 void triggered();
125
126 void actionNameChanged();
127 void actionIdChanged();
128
129 void handChanged();
130
131 void enabledChanged();
132
133 void controllerChanged();
134
135private:
136 QString m_actionName;
137 float m_value = 0;
138 bool m_pressed = false;
139 bool m_componentComplete = false;
140 bool m_enabled = true;
141 Controller m_controller;
142
143 QList<Action> m_actionIds;
144};
145
146class QQuick3DXrHapticFeedback : public QObject, public QQmlParserStatus
147{
148 Q_OBJECT
149 Q_INTERFACES(QQmlParserStatus)
150 QML_NAMED_ELEMENT(XrHapticFeedback)
151 QML_ADDED_IN_VERSION(6, 9)
152
153 Q_PROPERTY(Controller controller READ controller WRITE setController NOTIFY controllerChanged FINAL)
154 Q_PROPERTY(QQuick3DXrAbstractHapticEffect *hapticEffect READ hapticEffect WRITE setHapticEffect NOTIFY hapticEffectChanged FINAL)
155 Q_PROPERTY(bool trigger READ trigger WRITE setTrigger NOTIFY triggerChanged FINAL)
156 Q_PROPERTY(Condition condition READ condition WRITE setCondition NOTIFY conditionChanged FINAL)
157
158public:
159
160 // Same values as XrController and XrHandModel enums
161 enum class Controller : quint8 {
162 LeftController = 0,
163 RightController,
164 UnknownController,
165 };
166 Q_ENUM(Controller)
167
168 enum class Condition : quint8 {
169 RisingEdge = 0,
170 TrailingEdge,
171 };
172 Q_ENUM(Condition)
173
174 explicit QQuick3DXrHapticFeedback(QObject *parent = nullptr);
175 ~QQuick3DXrHapticFeedback() override;
176
177 void classBegin() override;
178 void componentComplete() override;
179
180 QQuick3DXrAbstractHapticEffect *hapticEffect() const;
181 void setHapticEffect(QQuick3DXrAbstractHapticEffect *newHapticEffect);
182
183 Controller controller() const;
184 void setController(Controller newController);
185
186 bool trigger();
187 void setTrigger(bool newTrigger);
188
189 enum Condition condition() const;
190 void setCondition(enum Condition newCondition);
191
192 bool testAndClear();
193
194Q_SIGNALS:
195 void controllerChanged();
196 void hapticEffectChanged();
197 void triggerChanged();
198 void conditionChanged();
199
200public Q_SLOTS:
201 void start();
202 void stop();
203
204private:
205 QMetaObject::Connection m_triggerConnection;
206 Controller m_controller = Controller::UnknownController;
207 Condition m_condition = Condition::RisingEdge;
208 QPointer<QQuick3DXrAbstractHapticEffect> m_hapticEffect;
209 bool m_trigger = false;
210 bool m_componentComplete = false;
211 bool m_pending = false;
212};
213
214class QQuick3DXrActionMapper : public QObject
215{
216 Q_OBJECT
217public:
218 static QQuick3DXrActionMapper *instance();
219
220 static QList<QPointer<QQuick3DXrHapticFeedback>> getHapticEffects(QQuick3DXrInputAction::Controller hand);
221
222 static void handleInput(QQuick3DXrInputAction::Action id, QQuick3DXrInputAction::Controller hand, const char *shortName, float value);
223 static void registerAction(QQuick3DXrInputAction *action);
224 static void registerHapticEffect(QPointer<QQuick3DXrHapticFeedback>);
225 static void removeAction(QQuick3DXrInputAction *action);
226 static void removeHapticEffect(QQuick3DXrHapticFeedback *action);
227
228private:
229 explicit QQuick3DXrActionMapper(QObject *parent = nullptr);
230
231 struct HapticData
232 {
233 QList<QPointer<QQuick3DXrHapticFeedback>> m_hapticEffects;
234 } m_hapticData[2];
235
236 QMultiHash<quint32, QQuick3DXrInputAction *> m_actions;
237 QMultiHash<QString, QQuick3DXrInputAction *> m_customActions;
238};
239
240QT_END_NAMESPACE
241
242#endif // QQUICK3DXRACTIONMAPPER_H
243

source code of qtquick3d/src/xr/quick3dxr/qquick3dxractionmapper_p.h