Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dxrcontroller.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
9
10#if !defined(Q_OS_VISIONOS)
11# include "openxr/qopenxrinputmanager_p.h"
12#endif
13
16
17#include <QtGui/qquaternion.h>
18
20
21/*!
22 \qmltype XrController
23 \inherits Node
24 \inqmlmodule QtQuick3D.Xr
25 \brief A tracked spatial node that tracks the position and orientation of an input controller.
26
27 The XrController is a tracked spatial node that tracks the position and orientation of an input controller.
28
29 Since this is a tracked node, its spatial properties should be considered read-only.
30 They are relative to the \l XrOrigin, which the XrController must be a child of.
31
32 \sa XrInputAction, {Coordinate Systems and Units in \qxr}
33*/
34
35QQuick3DXrController::QQuick3DXrController()
36{
37 m_inputManager = QQuick3DXrInputManager::instance();
38}
39
40/*!
41 \qmlproperty enumeration QtQuick3D.Xr::XrController::controller
42
43 Specifies the controller to track.
44
45 It can be one of:
46 \value XrController.LeftController
47 \value XrController.RightController
48 \value XrController.UnknownController
49 \value XrController.LeftHand (alias for \c LeftController)
50 \value XrController.RightHand (alias for \c RightController)
51*/
52
53QQuick3DXrController::Controller QQuick3DXrController::controller() const
54{
55 return static_cast<QQuick3DXrController::Controller>(m_controller);
56}
57
58void QQuick3DXrController::setController(QQuick3DXrController::Controller newController)
59{
60 if (m_controller == newController)
61 return;
62 m_controller = newController;
63 emit controllerChanged();
64
65 disconnect(m_isActiveConnection);
66
67 QQuick3DXrInputManager::instance()->registerController(this);
68 auto *input = handInput();
69 if (input) {
70 setIsActive(input->isActive());
71 setVisible(m_isActive); // ### position not set yet, so might show up briefly in wrong location
72
73 m_isActiveConnection = connect(input, &QQuick3DXrHandInput::isActiveChanged, this, [this, input]{
74 setIsActive(input->isActive());
75 setVisible(m_isActive);
76 });
77
78 connect(input, &QQuick3DXrHandInput::pokePositionChanged, this, &QQuick3DXrController::pokePositionChanged);
79 connect(input, &QQuick3DXrHandInput::jointPositionsChanged, this, &QQuick3DXrController::jointPositionsChanged);
80 connect(input, &QQuick3DXrHandInput::jointRotationsChanged, this, &QQuick3DXrController::jointRotationsChanged);
81 connect(input, &QQuick3DXrHandInput::jointDataUpdated, this, &QQuick3DXrController::jointDataUpdated);
82 } else {
83 setIsActive(false);
84 setVisible(false);
85 }
86}
87
88QQuick3DXrHandInput *QQuick3DXrController::handInput() const
89{
90 if (m_inputManager && m_inputManager->isValid()) {
91 if (m_controller == Controller::ControllerRight)
92 return m_inputManager->rightHandInput();
93 else if (m_controller == Controller::ControllerLeft)
94 return m_inputManager->leftHandInput();
95 }
96
97 return nullptr;
98}
99
100/*!
101 \qmlproperty enumeration XrController::poseSpace
102
103 Specifies the pose of the controller to track, that is, the orientation and
104 position relative to the physical controller.
105
106 It can be one of:
107 \value XrController.AimPose Used when aiming at something, such as with \l XrVirtualMouse.
108 \value XrController.GripPose Used when grabbing something, such as when holding an object in the hand.
109 \default XrController.AimPose
110*/
111
112QQuick3DXrController::HandPoseSpace QQuick3DXrController::poseSpace() const
113{
114 return m_poseSpace;
115}
116
117void QQuick3DXrController::setPoseSpace(HandPoseSpace newPoseSpace)
118{
119 if (m_poseSpace == newPoseSpace)
120 return;
121 m_poseSpace = newPoseSpace;
122 QQuick3DXrInputManager::instance()->registerController(this);
123 emit poseSpaceChanged();
124}
125
126/*!
127 \qmlproperty vector3d XrController::pokePosition
128
129 \readonly
130 This property holds the position to be used for touch interactions.
131 Typically, it will be the tip of the index finger when tracking a hand.
132
133 The position is in the \l XrOrigin's coordinate system. Convert it with
134 \l{Node::mapPositionToScene}{mapPositionToScene} before using it against scene content.
135
136 \sa XrView::processTouch, XrView::setTouchpoint, {Coordinate Systems and Units in \qxr}
137*/
138
139QVector3D QQuick3DXrController::pokePosition() const
140{
141 auto *input = handInput();
142 if (input)
143 return input->pokePosition();
144 return {};
145}
146
147/*!
148 \qmlproperty list<vector3d> XrController::jointPositions
149
150 \readonly
151 When using hand tracking, this property holds the positions of all the bones in the hand,
152 in the \l XrOrigin's coordinate system.
153
154 \sa jointRotations, XrHandModel, {Coordinate Systems and Units in \qxr}
155*/
156QList<QVector3D> QQuick3DXrController::jointPositions() const
157{
158 auto *input = handInput();
159 if (input)
160 return input->jointPositions();
161 return {};
162}
163
164/*!
165 \qmlproperty list<quaternion> XrController::jointRotations
166
167 \readonly
168 When using hand tracking, this property holds the orientation of all the bones in the hand.
169
170 \sa jointPositions, XrHandModel
171*/
172QList<QQuaternion> QQuick3DXrController::jointRotations() const
173{
174 auto *input = handInput();
175 if (input)
176 return input->jointRotations();
177 return {};
178}
179
180/*!
181 \qmlproperty bool XrController::isActive
182 \brief Indicates whether the controller is providing input.
183
184 \readonly
185 This property is true if the corresponding physical controller is present
186 and tracking.
187*/
188
189bool QQuick3DXrController::isActive() const
190{
191 return m_isActive;
192}
193
194void QQuick3DXrController::setIsActive(bool isActive)
195{
196 if (m_isActive == isActive)
197 return;
198 m_isActive = isActive;
199 emit isActiveChanged();
200}
201
203{
204 QSSG_ASSERT(controller != QtQuick3DXr::Controller::UnknownController, return QtQuick3DXr::Handedness::Right);
205 switch (controller) {
210 default:
211 Q_UNREACHABLE();
212 }
213}
214
215QT_END_NAMESPACE
Combined button and popup list for selecting options.
QtQuick3DXr::Handedness handednessForController(QtQuick3DXr::Controller controller)