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
5
7
8#if !defined(Q_OS_VISIONOS)
9# include "openxr/qopenxrinputmanager_p.h"
10#endif
11
14
15#include <QtGui/qquaternion.h>
16
18
19/*!
20 \qmltype XrController
21 \inherits Node
22 \inqmlmodule QtQuick3D.Xr
23 \brief A tracked spatial node that tracks the position and orientation of an input controller.
24
25 The XrController is a tracked spatial node that tracks the position and orientation of an input controller.
26
27 Since this is a tracked node, its spatial properties should be considered read-only.
28
29 \sa XrInputAction
30*/
31
32QQuick3DXrController::QQuick3DXrController()
33{
34 m_inputManager = QQuick3DXrInputManager::instance();
35}
36
37/*!
38 \qmlproperty enumeration QtQuick3D.Xr::XrController::controller
39
40 Specifies the controller to track.
41
42 It can be one of:
43 \value XrController.LeftController
44 \value XrController.RightController
45 \value XrController.UnknownController
46 \value XrController.LeftHand (alias for \c LeftController)
47 \value XrController.RightHand (alias for \c RightController)
48*/
49
50QQuick3DXrController::Controller QQuick3DXrController::controller() const
51{
52 return m_controller;
53}
54
55void QQuick3DXrController::setController(QQuick3DXrController::Controller newController)
56{
57 if (m_controller == newController)
58 return;
59 m_controller = newController;
60 emit controllerChanged();
61
62 disconnect(m_isActiveConnection);
63
64 QQuick3DXrInputManager::instance()->registerController(this);
65 auto *input = handInput();
66 if (input) {
67 setVisible(input->isActive()); // ### position not set yet, so might show up briefly in wrong location
68
69 m_isActiveConnection = connect(input, &QQuick3DXrHandInput::isActiveChanged, this, [this, input]{
70 setVisible(input->isActive());
71 });
72
73 connect(input, &QQuick3DXrHandInput::pokePositionChanged, this, &QQuick3DXrController::pokePositionChanged);
74 connect(input, &QQuick3DXrHandInput::jointPositionsChanged, this, &QQuick3DXrController::jointPositionsChanged);
75 connect(input, &QQuick3DXrHandInput::jointRotationsChanged, this, &QQuick3DXrController::jointRotationsChanged);
76 connect(input, &QQuick3DXrHandInput::jointDataUpdated, this, &QQuick3DXrController::jointDataUpdated);
77 } else {
78 setVisible(false);
79 }
80}
81
82QQuick3DXrHandInput *QQuick3DXrController::handInput() const
83{
84 if (m_inputManager && m_inputManager->isValid()) {
85 if (m_controller == ControllerRight)
86 return m_inputManager->rightHandInput();
87 else if (m_controller == ControllerLeft)
88 return m_inputManager->leftHandInput();
89 }
90
91 return nullptr;
92}
93
94/*!
95 \qmlproperty enumeration XrController::poseSpace
96
97 Specifies the pose of the controller to track, that is, the orientation and
98 position relative to the physical controller.
99
100 It can be one of:
101 \value XrController.AimPose Used when aiming at something, such as with \l XrVirtualMouse.
102 \value XrController.GripPose Used when grabbing something, such as when holding an object in the hand.
103 \default XrController.AimPose
104*/
105
106QQuick3DXrController::HandPoseSpace QQuick3DXrController::poseSpace() const
107{
108 return m_poseSpace;
109}
110
111void QQuick3DXrController::setPoseSpace(HandPoseSpace newPoseSpace)
112{
113 if (m_poseSpace == newPoseSpace)
114 return;
115 m_poseSpace = newPoseSpace;
116 QQuick3DXrInputManager::instance()->registerController(this);
117 emit poseSpaceChanged();
118}
119
120/*!
121 \qmlproperty vector3d XrController::pokePosition
122
123 \readonly
124 This property holds the position to be used for touch interactions.
125 Typically, it will be the tip of the index finger when tracking a hand.
126
127 \sa XrView::processTouch, XrView::setTouchpoint
128*/
129
130QVector3D QQuick3DXrController::pokePosition() const
131{
132 auto *input = handInput();
133 if (input)
134 return input->pokePosition();
135 return {};
136}
137
138/*!
139 \qmlproperty list<vector3d> XrController::jointPositions
140
141 \readonly
142 When using hand tracking, this property holds the positions of all the bones in the hand.
143
144 \sa jointRotations, XrHandModel
145*/
146QList<QVector3D> QQuick3DXrController::jointPositions() const
147{
148 auto *input = handInput();
149 if (input)
150 return input->jointPositions();
151 return {};
152}
153
154/*!
155 \qmlproperty list<quaternion> XrController::jointRotations
156
157 \readonly
158 When using hand tracking, this property holds the orientation of all the bones in the hand.
159
160 \sa jointPositions, XrHandModel
161*/
162QList<QQuaternion> QQuick3DXrController::jointRotations() const
163{
164 auto *input = handInput();
165 if (input)
166 return input->jointRotations();
167 return {};
168}
169
170/*!
171 \qmlproperty bool XrController::isActive
172 \brief Indicates whether the controller is providing input.
173
174 \readonly
175 This property is true if the corresponding physical controller is present
176 and tracking.
177*/
178
179bool QQuick3DXrController::isActive() const
180{
181 return m_isActive;
182}
183
184QtQuick3DXr::Hand QtQuick3DXr::handForController(QQuick3DXrController::Controller controller)
185{
186 QSSG_ASSERT(controller != QQuick3DXrController::ControllerNone, return QQuick3DXrInputManager::Hand::RightHand);
187 switch (controller) {
188 case QQuick3DXrController::ControllerLeft:
189 return QQuick3DXrInputManager::Hand::LeftHand;
190 case QQuick3DXrController::ControllerRight:
191 return QQuick3DXrInputManager::Hand::RightHand;
192 default:
193 Q_UNREACHABLE();
194 }
195}
196
197QtQuick3DXr::HandPoseSpace QtQuick3DXr::pose_cast(QQuick3DXrController::HandPoseSpace poseSpace)
198{
199 return static_cast<QtQuick3DXr::HandPoseSpace>(poseSpace);
200}
201
202QT_END_NAMESPACE
QtQuick3DXr::Hand handForController(QQuick3DXrController::Controller controller)
QtQuick3DXr::HandPoseSpace pose_cast(QQuick3DXrController::HandPoseSpace poseSpace)