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