xr_positional_tracker.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. void XRPositionalTracker::_bind_methods() {
  33. BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);
  34. BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT);
  35. BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT);
  36. ClassDB::bind_method(D_METHOD("get_tracker_type"), &XRPositionalTracker::get_tracker_type);
  37. ClassDB::bind_method(D_METHOD("set_tracker_type", "type"), &XRPositionalTracker::set_tracker_type);
  38. ADD_PROPERTY(PropertyInfo(Variant::INT, "type"), "set_tracker_type", "get_tracker_type");
  39. ClassDB::bind_method(D_METHOD("get_tracker_name"), &XRPositionalTracker::get_tracker_name);
  40. ClassDB::bind_method(D_METHOD("set_tracker_name", "name"), &XRPositionalTracker::set_tracker_name);
  41. ADD_PROPERTY(PropertyInfo(Variant::STRING, "name"), "set_tracker_name", "get_tracker_name");
  42. ClassDB::bind_method(D_METHOD("get_tracker_desc"), &XRPositionalTracker::get_tracker_desc);
  43. ClassDB::bind_method(D_METHOD("set_tracker_desc", "description"), &XRPositionalTracker::set_tracker_desc);
  44. ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_tracker_desc", "get_tracker_desc");
  45. ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile);
  46. ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile);
  47. ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile");
  48. ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);
  49. ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand);
  50. ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand");
  51. ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose);
  52. ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose);
  53. ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose);
  54. ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose);
  55. ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
  56. ADD_SIGNAL(MethodInfo("pose_lost_tracking", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));
  57. ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input);
  58. ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input);
  59. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));
  60. ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));
  61. ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));
  62. ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector")));
  63. ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role")));
  64. };
  65. void XRPositionalTracker::set_tracker_type(XRServer::TrackerType p_type) {
  66. if (type != p_type) {
  67. type = p_type;
  68. hand = XRPositionalTracker::TRACKER_HAND_UNKNOWN;
  69. };
  70. };
  71. XRServer::TrackerType XRPositionalTracker::get_tracker_type() const {
  72. return type;
  73. };
  74. void XRPositionalTracker::set_tracker_name(const StringName &p_name) {
  75. // Note: this should not be changed after the tracker is registered with the XRServer!
  76. name = p_name;
  77. };
  78. StringName XRPositionalTracker::get_tracker_name() const {
  79. return name;
  80. };
  81. void XRPositionalTracker::set_tracker_desc(const String &p_desc) {
  82. description = p_desc;
  83. }
  84. String XRPositionalTracker::get_tracker_desc() const {
  85. return description;
  86. }
  87. void XRPositionalTracker::set_tracker_profile(const String &p_profile) {
  88. if (profile != p_profile) {
  89. profile = p_profile;
  90. emit_signal("profile_changed", profile);
  91. }
  92. }
  93. String XRPositionalTracker::get_tracker_profile() const {
  94. return profile;
  95. }
  96. XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const {
  97. return hand;
  98. };
  99. void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
  100. XRServer *xr_server = XRServer::get_singleton();
  101. ERR_FAIL_NULL(xr_server);
  102. if (hand != p_hand) {
  103. // we can only set this if we've previously set this to be a controller!!
  104. ERR_FAIL_COND((type != XRServer::TRACKER_CONTROLLER) && (p_hand != XRPositionalTracker::TRACKER_HAND_UNKNOWN));
  105. hand = p_hand;
  106. };
  107. };
  108. bool XRPositionalTracker::has_pose(const StringName &p_action_name) const {
  109. return poses.has(p_action_name);
  110. }
  111. Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const {
  112. Ref<XRPose> pose;
  113. if (poses.has(p_action_name)) {
  114. pose = poses[p_action_name];
  115. }
  116. return pose;
  117. }
  118. void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) {
  119. // only update this if we were tracking this pose
  120. if (poses.has(p_action_name)) {
  121. // 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.
  122. Ref<XRPose> pose = poses[p_action_name];
  123. pose->set_has_tracking_data(false);
  124. emit_signal(SNAME("pose_lost_tracking"), pose);
  125. }
  126. }
  127. 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) {
  128. Ref<XRPose> new_pose;
  129. if (poses.has(p_action_name)) {
  130. new_pose = poses[p_action_name];
  131. } else {
  132. new_pose.instantiate();
  133. poses[p_action_name] = new_pose;
  134. }
  135. new_pose->set_name(p_action_name);
  136. new_pose->set_has_tracking_data(true);
  137. new_pose->set_transform(p_transform);
  138. new_pose->set_linear_velocity(p_linear_velocity);
  139. new_pose->set_angular_velocity(p_angular_velocity);
  140. new_pose->set_tracking_confidence(p_tracking_confidence);
  141. emit_signal(SNAME("pose_changed"), new_pose);
  142. // TODO discuss whether we also want to create and emit an InputEventXRPose event
  143. }
  144. Variant XRPositionalTracker::get_input(const StringName &p_action_name) const {
  145. if (inputs.has(p_action_name)) {
  146. return inputs[p_action_name];
  147. } else {
  148. return Variant();
  149. }
  150. }
  151. void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) {
  152. bool changed = false;
  153. // XR inputs
  154. if (inputs.has(p_action_name)) {
  155. changed = inputs[p_action_name] != p_value;
  156. } else {
  157. changed = true;
  158. }
  159. if (changed) {
  160. // store the new value
  161. inputs[p_action_name] = p_value;
  162. // emit signals to let the rest of the world know
  163. switch (p_value.get_type()) {
  164. case Variant::BOOL: {
  165. bool pressed = p_value;
  166. if (pressed) {
  167. emit_signal(SNAME("button_pressed"), p_action_name);
  168. } else {
  169. emit_signal(SNAME("button_released"), p_action_name);
  170. }
  171. // TODO discuss whether we also want to create and emit an InputEventXRButton event
  172. } break;
  173. case Variant::FLOAT: {
  174. emit_signal(SNAME("input_float_changed"), p_action_name, p_value);
  175. // TODO discuss whether we also want to create and emit an InputEventXRValue event
  176. } break;
  177. case Variant::VECTOR2: {
  178. emit_signal(SNAME("input_vector2_changed"), p_action_name, p_value);
  179. // TODO discuss whether we also want to create and emit an InputEventXRAxis event
  180. } break;
  181. default: {
  182. // ???
  183. } break;
  184. }
  185. }
  186. }
  187. XRPositionalTracker::XRPositionalTracker() {
  188. type = XRServer::TRACKER_UNKNOWN;
  189. name = "Unknown";
  190. hand = TRACKER_HAND_UNKNOWN;
  191. };