| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  xr_positional_tracker.cpp                                             */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 00:47:36 +10:00
										 |  |  | #include "xr_positional_tracker.h"
 | 
					
						
							| 
									
										
										
										
											2020-04-28 15:19:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | #include "xr_controller_tracker.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-09 00:47:36 +10:00
										 |  |  | void XRPositionalTracker::_bind_methods() { | 
					
						
							| 
									
										
										
										
											2017-09-10 16:15:11 +10:00
										 |  |  | 	BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN); | 
					
						
							| 
									
										
										
										
											2020-12-10 10:30:37 +00:00
										 |  |  | 	BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT); | 
					
						
							|  |  |  | 	BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT); | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	BIND_ENUM_CONSTANT(TRACKER_HAND_MAX); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-05 21:09:21 +11:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 11:16:14 +00:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand); | 
					
						
							|  |  |  | 	ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose); | 
					
						
							| 
									
										
										
										
											2022-01-19 19:21:06 +11:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose"))); | 
					
						
							| 
									
										
										
										
											2023-09-01 19:13:54 -04:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("pose_lost_tracking", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose"))); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name"))); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name"))); | 
					
						
							| 
									
										
										
										
											2023-01-22 13:17:20 +11:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value"))); | 
					
						
							|  |  |  | 	ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector"))); | 
					
						
							| 
									
										
										
										
											2022-03-05 21:09:21 +11:00
										 |  |  | 	ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role"))); | 
					
						
							| 
									
										
										
										
											2024-10-07 10:57:21 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-05 21:09:21 +11:00
										 |  |  | void XRPositionalTracker::set_tracker_profile(const String &p_profile) { | 
					
						
							|  |  |  | 	if (profile != p_profile) { | 
					
						
							|  |  |  | 		profile = p_profile; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		emit_signal("profile_changed", profile); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String XRPositionalTracker::get_tracker_profile() const { | 
					
						
							|  |  |  | 	return profile; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 11:16:14 +00:00
										 |  |  | XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const { | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	return tracker_hand; | 
					
						
							| 
									
										
										
										
											2024-10-07 10:57:21 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-10 16:15:11 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 11:16:14 +00:00
										 |  |  | void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) { | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	ERR_FAIL_INDEX(p_hand, TRACKER_HAND_MAX); | 
					
						
							|  |  |  | 	tracker_hand = p_hand; | 
					
						
							| 
									
										
										
										
											2024-10-07 10:57:21 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-10 16:15:11 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | bool XRPositionalTracker::has_pose(const StringName &p_action_name) const { | 
					
						
							|  |  |  | 	return poses.has(p_action_name); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const { | 
					
						
							|  |  |  | 	Ref<XRPose> pose; | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	if (poses.has(p_action_name)) { | 
					
						
							|  |  |  | 		pose = poses[p_action_name]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	return pose; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-23 22:10:41 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) { | 
					
						
							|  |  |  | 	// only update this if we were tracking this pose
 | 
					
						
							|  |  |  | 	if (poses.has(p_action_name)) { | 
					
						
							|  |  |  | 		// 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.
 | 
					
						
							| 
									
										
										
										
											2023-09-01 19:13:54 -04:00
										 |  |  | 		Ref<XRPose> pose = poses[p_action_name]; | 
					
						
							|  |  |  | 		pose->set_has_tracking_data(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		emit_signal(SNAME("pose_lost_tracking"), pose); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-19 19:21:06 +11:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	Ref<XRPose> new_pose; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 21:49:56 -04:00
										 |  |  | 	if (poses.has(p_action_name)) { | 
					
						
							|  |  |  | 		new_pose = poses[p_action_name]; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		new_pose.instantiate(); | 
					
						
							|  |  |  | 		poses[p_action_name] = new_pose; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	new_pose->set_name(p_action_name); | 
					
						
							|  |  |  | 	new_pose->set_has_tracking_data(true); | 
					
						
							|  |  |  | 	new_pose->set_transform(p_transform); | 
					
						
							|  |  |  | 	new_pose->set_linear_velocity(p_linear_velocity); | 
					
						
							|  |  |  | 	new_pose->set_angular_velocity(p_angular_velocity); | 
					
						
							| 
									
										
										
										
											2022-01-19 19:21:06 +11:00
										 |  |  | 	new_pose->set_tracking_confidence(p_tracking_confidence); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 15:53:53 +01:00
										 |  |  | 	emit_signal(SNAME("pose_changed"), new_pose); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// TODO discuss whether we also want to create and emit an InputEventXRPose event
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Variant XRPositionalTracker::get_input(const StringName &p_action_name) const { | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	// Complain if this method is called on a XRPositionalTracker instance.
 | 
					
						
							|  |  |  | 	if (!dynamic_cast<const XRControllerTracker *>(this)) { | 
					
						
							|  |  |  | 		WARN_DEPRECATED_MSG(R"*(The "get_input()" method is deprecated, use "XRControllerTracker" instead.)*"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	if (inputs.has(p_action_name)) { | 
					
						
							|  |  |  | 		return inputs[p_action_name]; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return Variant(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) { | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	// Complain if this method is called on a XRPositionalTracker instance.
 | 
					
						
							|  |  |  | 	if (!dynamic_cast<XRControllerTracker *>(this)) { | 
					
						
							|  |  |  | 		WARN_DEPRECATED_MSG(R"*(The "set_input()" method is deprecated, use "XRControllerTracker" instead.)*"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// XR inputs
 | 
					
						
							| 
									
										
										
										
											2024-04-13 17:26:46 -04:00
										 |  |  | 	bool changed; | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 	if (inputs.has(p_action_name)) { | 
					
						
							|  |  |  | 		changed = inputs[p_action_name] != p_value; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		changed = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (changed) { | 
					
						
							|  |  |  | 		// store the new value
 | 
					
						
							|  |  |  | 		inputs[p_action_name] = p_value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// emit signals to let the rest of the world know
 | 
					
						
							|  |  |  | 		switch (p_value.get_type()) { | 
					
						
							|  |  |  | 			case Variant::BOOL: { | 
					
						
							|  |  |  | 				bool pressed = p_value; | 
					
						
							|  |  |  | 				if (pressed) { | 
					
						
							| 
									
										
										
										
											2022-02-06 15:53:53 +01:00
										 |  |  | 					emit_signal(SNAME("button_pressed"), p_action_name); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2022-02-06 15:53:53 +01:00
										 |  |  | 					emit_signal(SNAME("button_released"), p_action_name); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// TODO discuss whether we also want to create and emit an InputEventXRButton event
 | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			case Variant::FLOAT: { | 
					
						
							| 
									
										
										
										
											2023-01-22 13:17:20 +11:00
										 |  |  | 				emit_signal(SNAME("input_float_changed"), p_action_name, p_value); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// TODO discuss whether we also want to create and emit an InputEventXRValue event
 | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			case Variant::VECTOR2: { | 
					
						
							| 
									
										
										
										
											2023-01-22 13:17:20 +11:00
										 |  |  | 				emit_signal(SNAME("input_vector2_changed"), p_action_name, p_value); | 
					
						
							| 
									
										
										
										
											2021-08-29 16:05:11 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				// TODO discuss whether we also want to create and emit an InputEventXRAxis event
 | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 			default: { | 
					
						
							|  |  |  | 				// ???
 | 
					
						
							|  |  |  | 			} break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |