Debian Codegraph @

servers/xr/xr_positional_tracker.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* xr_positional_tracker.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "xr_positional_tracker.h"
32
33 #include "xr_controller_tracker.h"
34
35 void XRPositionalTracker::_bind_methods() {
36 BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
37 BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT);
38 BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT);
39 BIND_ENUM_CONSTANT(TRACKER_HAND_MAX);
40
41 ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile);
42 ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile);
43 ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile");
44
45 ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);
46 ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand);
47 ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand");
48
49 ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose);
50 ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose);
51 ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose);
52 ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose);
53 ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
54 ADD_SIGNAL(MethodInfo("pose_lost_tracking", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
55
56 ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input);
57 ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input);
58 ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));
59 ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));
60 ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));
61 ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector")));
62 ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role")));
63 }
64
65 void XRPositionalTracker::set_tracker_profile(const String &p_profile) {
66 if (profile != p_profile) {
67 profile = p_profile;
68
69 emit_signal("profile_changed", profile);
70 }
71 }
72
73 String XRPositionalTracker::get_tracker_profile() const {
74 return profile;
75 }
76
77 XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const {
78 return tracker_hand;
79 }
80
81 void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
82 ERR_FAIL_INDEX(p_hand, TRACKER_HAND_MAX);
83 tracker_hand = p_hand;
84 }
85
86 bool XRPositionalTracker::has_pose(const StringName &p_action_name) const {
87 return poses.has(p_action_name);
88 }
89
90 Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const {
91 Ref<XRPose> pose;
92
93 if (poses.has(p_action_name)) {
94 pose = poses[p_action_name];
95 }
96
97 return pose;
98 }
99
100 void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) {
101 // only update this if we were tracking this pose
102 if (poses.has(p_action_name)) {
103 // We just set tracking data as invalid, we leave our current transform and velocity data as is so controllers don't suddenly jump to origin.
104 Ref<XRPose> pose = poses[p_action_name];
105 pose->set_has_tracking_data(false);
106
107 emit_signal(SNAME("pose_lost_tracking"), pose);
108 }
109 }
110
111 void XRPositionalTracker::set_pose(const StringName &p_action_name, const Transform3D &p_transform, const Vector3 &p_linear_velocity, const Vector3 &p_angular_velocity, const XRPose::TrackingConfidence p_tracking_confidence) {
112 Ref<XRPose> new_pose;
113
114 if (poses.has(p_action_name)) {
115 new_pose = poses[p_action_name];
116 } else {
117 new_pose.instantiate();
118 new_pose->set_name(p_action_name);
119 poses[p_action_name] = new_pose;
120 }
121
122 new_pose->set_has_tracking_data(true);
123 new_pose->set_transform(p_transform);
124 new_pose->set_linear_velocity(p_linear_velocity);
125 new_pose->set_angular_velocity(p_angular_velocity);
126 new_pose->set_tracking_confidence(p_tracking_confidence);
127
128 emit_signal(SNAME("pose_changed"), new_pose);
129
130 // TODO discuss whether we also want to create and emit an InputEventXRPose event
131 }
132
133 Variant XRPositionalTracker::get_input(const StringName &p_action_name) const {
134 // Complain if this method is called on a XRPositionalTracker instance.
135 if (!dynamic_cast<const XRControllerTracker *>(this)) {
136 WARN_DEPRECATED_MSG(R"*(The "get_input()" method is deprecated, use "XRControllerTracker" instead.)*");
137 }
138
139 if (inputs.has(p_action_name)) {
140 return inputs[p_action_name];
141 } else {
142 return Variant();
143 }
144 }
145
146 void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) {
147 // Complain if this method is called on a XRPositionalTracker instance.
148 if (!dynamic_cast<XRControllerTracker *>(this)) {
149 WARN_DEPRECATED_MSG(R"*(The "set_input()" method is deprecated, use "XRControllerTracker" instead.)*");
150 }
151
152 // XR inputs
153 bool changed;
154 if (inputs.has(p_action_name)) {
155 changed = inputs[p_action_name] != p_value;
156 } else {
157 changed = true;
158 }
159
160 if (changed) {
161 // store the new value
162 inputs[p_action_name] = p_value;
163
164 // emit signals to let the rest of the world know
165 switch (p_value.get_type()) {
166 case Variant::BOOL: {
167 bool pressed = p_value;
168 if (pressed) {
169 emit_signal(SNAME("button_pressed"), p_action_name);
170 } else {
171 emit_signal(SNAME("button_released"), p_action_name);
172 }
173
174 // TODO discuss whether we also want to create and emit an InputEventXRButton event
175 } break;
176 case Variant::FLOAT: {
177 emit_signal(SNAME("input_float_changed"), p_action_name, p_value);
178
179 // TODO discuss whether we also want to create and emit an InputEventXRValue event
180 } break;
181 case Variant::VECTOR2: {
182 emit_signal(SNAME("input_vector2_changed"), p_action_name, p_value);
183
184 // TODO discuss whether we also want to create and emit an InputEventXRAxis event
185 } break;
186 default: {
187 // ???
188 } break;
189 }
190 }
191 }