xr_positional_tracker.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "xr_positional_tracker.h"
  31. #include "core/input/input.h"
  32. #include "xr_controller_tracker.h"
  33. void XRPositionalTracker::_bind_methods() {
  34. BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
  35. BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT);
  36. BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT);
  37. BIND_ENUM_CONSTANT(TRACKER_HAND_MAX);
  38. ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile);
  39. ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile);
  40. ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile");
  41. ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);
  42. ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand);
  43. ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand");
  44. ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose);
  45. ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose);
  46. ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose);
  47. ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose);
  48. ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
  49. ADD_SIGNAL(MethodInfo("pose_lost_tracking", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
  50. ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input);
  51. ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input);
  52. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));
  53. ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));
  54. ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));
  55. ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector")));
  56. ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role")));
  57. };
  58. void XRPositionalTracker::set_tracker_profile(const String &p_profile) {
  59. if (profile != p_profile) {
  60. profile = p_profile;
  61. emit_signal("profile_changed", profile);
  62. }
  63. }
  64. String XRPositionalTracker::get_tracker_profile() const {
  65. return profile;
  66. }
  67. XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const {
  68. return tracker_hand;
  69. };
  70. void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
  71. ERR_FAIL_INDEX(p_hand, TRACKER_HAND_MAX);
  72. tracker_hand = p_hand;
  73. };
  74. bool XRPositionalTracker::has_pose(const StringName &p_action_name) const {
  75. return poses.has(p_action_name);
  76. }
  77. Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const {
  78. Ref<XRPose> pose;
  79. if (poses.has(p_action_name)) {
  80. pose = poses[p_action_name];
  81. }
  82. return pose;
  83. }
  84. void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) {
  85. // only update this if we were tracking this pose
  86. if (poses.has(p_action_name)) {
  87. // 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.
  88. Ref<XRPose> pose = poses[p_action_name];
  89. pose->set_has_tracking_data(false);
  90. emit_signal(SNAME("pose_lost_tracking"), pose);
  91. }
  92. }
  93. 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) {
  94. Ref<XRPose> new_pose;
  95. if (poses.has(p_action_name)) {
  96. new_pose = poses[p_action_name];
  97. } else {
  98. new_pose.instantiate();
  99. poses[p_action_name] = new_pose;
  100. }
  101. new_pose->set_name(p_action_name);
  102. new_pose->set_has_tracking_data(true);
  103. new_pose->set_transform(p_transform);
  104. new_pose->set_linear_velocity(p_linear_velocity);
  105. new_pose->set_angular_velocity(p_angular_velocity);
  106. new_pose->set_tracking_confidence(p_tracking_confidence);
  107. emit_signal(SNAME("pose_changed"), new_pose);
  108. // TODO discuss whether we also want to create and emit an InputEventXRPose event
  109. }
  110. Variant XRPositionalTracker::get_input(const StringName &p_action_name) const {
  111. // Complain if this method is called on a XRPositionalTracker instance.
  112. if (!dynamic_cast<const XRControllerTracker *>(this)) {
  113. WARN_DEPRECATED_MSG(R"*(The "get_input()" method is deprecated, use "XRControllerTracker" instead.)*");
  114. }
  115. if (inputs.has(p_action_name)) {
  116. return inputs[p_action_name];
  117. } else {
  118. return Variant();
  119. }
  120. }
  121. void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) {
  122. // Complain if this method is called on a XRPositionalTracker instance.
  123. if (!dynamic_cast<XRControllerTracker *>(this)) {
  124. WARN_DEPRECATED_MSG(R"*(The "set_input()" method is deprecated, use "XRControllerTracker" instead.)*");
  125. }
  126. // XR inputs
  127. bool changed;
  128. if (inputs.has(p_action_name)) {
  129. changed = inputs[p_action_name] != p_value;
  130. } else {
  131. changed = true;
  132. }
  133. if (changed) {
  134. // store the new value
  135. inputs[p_action_name] = p_value;
  136. // emit signals to let the rest of the world know
  137. switch (p_value.get_type()) {
  138. case Variant::BOOL: {
  139. bool pressed = p_value;
  140. if (pressed) {
  141. emit_signal(SNAME("button_pressed"), p_action_name);
  142. } else {
  143. emit_signal(SNAME("button_released"), p_action_name);
  144. }
  145. // TODO discuss whether we also want to create and emit an InputEventXRButton event
  146. } break;
  147. case Variant::FLOAT: {
  148. emit_signal(SNAME("input_float_changed"), p_action_name, p_value);
  149. // TODO discuss whether we also want to create and emit an InputEventXRValue event
  150. } break;
  151. case Variant::VECTOR2: {
  152. emit_signal(SNAME("input_vector2_changed"), p_action_name, p_value);
  153. // TODO discuss whether we also want to create and emit an InputEventXRAxis event
  154. } break;
  155. default: {
  156. // ???
  157. } break;
  158. }
  159. }
  160. }