xr_positional_tracker.cpp 9.3 KB

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