Path: blob/master/servers/xr/xr_positional_tracker.cpp
10277 views
/**************************************************************************/1/* xr_positional_tracker.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "xr_positional_tracker.h"3132#include "xr_controller_tracker.h"3334void XRPositionalTracker::_bind_methods() {35BIND_ENUM_CONSTANT(TRACKER_HAND_UNKNOWN);36BIND_ENUM_CONSTANT(TRACKER_HAND_LEFT);37BIND_ENUM_CONSTANT(TRACKER_HAND_RIGHT);38BIND_ENUM_CONSTANT(TRACKER_HAND_MAX);3940ClassDB::bind_method(D_METHOD("get_tracker_profile"), &XRPositionalTracker::get_tracker_profile);41ClassDB::bind_method(D_METHOD("set_tracker_profile", "profile"), &XRPositionalTracker::set_tracker_profile);42ADD_PROPERTY(PropertyInfo(Variant::STRING, "profile"), "set_tracker_profile", "get_tracker_profile");4344ClassDB::bind_method(D_METHOD("get_tracker_hand"), &XRPositionalTracker::get_tracker_hand);45ClassDB::bind_method(D_METHOD("set_tracker_hand", "hand"), &XRPositionalTracker::set_tracker_hand);46ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Unknown,Left,Right"), "set_tracker_hand", "get_tracker_hand");4748ClassDB::bind_method(D_METHOD("has_pose", "name"), &XRPositionalTracker::has_pose);49ClassDB::bind_method(D_METHOD("get_pose", "name"), &XRPositionalTracker::get_pose);50ClassDB::bind_method(D_METHOD("invalidate_pose", "name"), &XRPositionalTracker::invalidate_pose);51ClassDB::bind_method(D_METHOD("set_pose", "name", "transform", "linear_velocity", "angular_velocity", "tracking_confidence"), &XRPositionalTracker::set_pose);52ADD_SIGNAL(MethodInfo("pose_changed", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));53ADD_SIGNAL(MethodInfo("pose_lost_tracking", PropertyInfo(Variant::OBJECT, "pose", PROPERTY_HINT_RESOURCE_TYPE, "XRPose")));5455ClassDB::bind_method(D_METHOD("get_input", "name"), &XRPositionalTracker::get_input);56ClassDB::bind_method(D_METHOD("set_input", "name", "value"), &XRPositionalTracker::set_input);57ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::STRING, "name")));58ADD_SIGNAL(MethodInfo("button_released", PropertyInfo(Variant::STRING, "name")));59ADD_SIGNAL(MethodInfo("input_float_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::FLOAT, "value")));60ADD_SIGNAL(MethodInfo("input_vector2_changed", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::VECTOR2, "vector")));61ADD_SIGNAL(MethodInfo("profile_changed", PropertyInfo(Variant::STRING, "role")));62}6364void XRPositionalTracker::set_tracker_profile(const String &p_profile) {65if (profile != p_profile) {66profile = p_profile;6768emit_signal("profile_changed", profile);69}70}7172String XRPositionalTracker::get_tracker_profile() const {73return profile;74}7576XRPositionalTracker::TrackerHand XRPositionalTracker::get_tracker_hand() const {77return tracker_hand;78}7980void XRPositionalTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {81ERR_FAIL_INDEX(p_hand, TRACKER_HAND_MAX);82tracker_hand = p_hand;83}8485bool XRPositionalTracker::has_pose(const StringName &p_action_name) const {86return poses.has(p_action_name);87}8889Ref<XRPose> XRPositionalTracker::get_pose(const StringName &p_action_name) const {90Ref<XRPose> pose;9192if (poses.has(p_action_name)) {93pose = poses[p_action_name];94}9596return pose;97}9899void XRPositionalTracker::invalidate_pose(const StringName &p_action_name) {100// only update this if we were tracking this pose101if (poses.has(p_action_name)) {102// 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.103Ref<XRPose> pose = poses[p_action_name];104pose->set_has_tracking_data(false);105106emit_signal(SNAME("pose_lost_tracking"), pose);107}108}109110void 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) {111Ref<XRPose> new_pose;112113if (poses.has(p_action_name)) {114new_pose = poses[p_action_name];115} else {116new_pose.instantiate();117poses[p_action_name] = new_pose;118}119120new_pose->set_name(p_action_name);121new_pose->set_has_tracking_data(true);122new_pose->set_transform(p_transform);123new_pose->set_linear_velocity(p_linear_velocity);124new_pose->set_angular_velocity(p_angular_velocity);125new_pose->set_tracking_confidence(p_tracking_confidence);126127emit_signal(SNAME("pose_changed"), new_pose);128129// TODO discuss whether we also want to create and emit an InputEventXRPose event130}131132Variant XRPositionalTracker::get_input(const StringName &p_action_name) const {133// Complain if this method is called on a XRPositionalTracker instance.134if (!dynamic_cast<const XRControllerTracker *>(this)) {135WARN_DEPRECATED_MSG(R"*(The "get_input()" method is deprecated, use "XRControllerTracker" instead.)*");136}137138if (inputs.has(p_action_name)) {139return inputs[p_action_name];140} else {141return Variant();142}143}144145void XRPositionalTracker::set_input(const StringName &p_action_name, const Variant &p_value) {146// Complain if this method is called on a XRPositionalTracker instance.147if (!dynamic_cast<XRControllerTracker *>(this)) {148WARN_DEPRECATED_MSG(R"*(The "set_input()" method is deprecated, use "XRControllerTracker" instead.)*");149}150151// XR inputs152bool changed;153if (inputs.has(p_action_name)) {154changed = inputs[p_action_name] != p_value;155} else {156changed = true;157}158159if (changed) {160// store the new value161inputs[p_action_name] = p_value;162163// emit signals to let the rest of the world know164switch (p_value.get_type()) {165case Variant::BOOL: {166bool pressed = p_value;167if (pressed) {168emit_signal(SNAME("button_pressed"), p_action_name);169} else {170emit_signal(SNAME("button_released"), p_action_name);171}172173// TODO discuss whether we also want to create and emit an InputEventXRButton event174} break;175case Variant::FLOAT: {176emit_signal(SNAME("input_float_changed"), p_action_name, p_value);177178// TODO discuss whether we also want to create and emit an InputEventXRValue event179} break;180case Variant::VECTOR2: {181emit_signal(SNAME("input_vector2_changed"), p_action_name, p_value);182183// TODO discuss whether we also want to create and emit an InputEventXRAxis event184} break;185default: {186// ???187} break;188}189}190}191192193