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