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