| 1 | /**************************************************************************/ |
| 2 | /* navigation_link_2d.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 "navigation_link_2d.h" |
| 32 | |
| 33 | #include "core/math/geometry_2d.h" |
| 34 | #include "scene/resources/world_2d.h" |
| 35 | #include "servers/navigation_server_2d.h" |
| 36 | #include "servers/navigation_server_3d.h" |
| 37 | |
| 38 | void NavigationLink2D::_bind_methods() { |
| 39 | ClassDB::bind_method(D_METHOD("set_enabled" , "enabled" ), &NavigationLink2D::set_enabled); |
| 40 | ClassDB::bind_method(D_METHOD("is_enabled" ), &NavigationLink2D::is_enabled); |
| 41 | |
| 42 | ClassDB::bind_method(D_METHOD("set_bidirectional" , "bidirectional" ), &NavigationLink2D::set_bidirectional); |
| 43 | ClassDB::bind_method(D_METHOD("is_bidirectional" ), &NavigationLink2D::is_bidirectional); |
| 44 | |
| 45 | ClassDB::bind_method(D_METHOD("set_navigation_layers" , "navigation_layers" ), &NavigationLink2D::set_navigation_layers); |
| 46 | ClassDB::bind_method(D_METHOD("get_navigation_layers" ), &NavigationLink2D::get_navigation_layers); |
| 47 | |
| 48 | ClassDB::bind_method(D_METHOD("set_navigation_layer_value" , "layer_number" , "value" ), &NavigationLink2D::set_navigation_layer_value); |
| 49 | ClassDB::bind_method(D_METHOD("get_navigation_layer_value" , "layer_number" ), &NavigationLink2D::get_navigation_layer_value); |
| 50 | |
| 51 | ClassDB::bind_method(D_METHOD("set_start_position" , "position" ), &NavigationLink2D::set_start_position); |
| 52 | ClassDB::bind_method(D_METHOD("get_start_position" ), &NavigationLink2D::get_start_position); |
| 53 | |
| 54 | ClassDB::bind_method(D_METHOD("set_end_position" , "position" ), &NavigationLink2D::set_end_position); |
| 55 | ClassDB::bind_method(D_METHOD("get_end_position" ), &NavigationLink2D::get_end_position); |
| 56 | |
| 57 | ClassDB::bind_method(D_METHOD("set_global_start_position" , "position" ), &NavigationLink2D::set_global_start_position); |
| 58 | ClassDB::bind_method(D_METHOD("get_global_start_position" ), &NavigationLink2D::get_global_start_position); |
| 59 | |
| 60 | ClassDB::bind_method(D_METHOD("set_global_end_position" , "position" ), &NavigationLink2D::set_global_end_position); |
| 61 | ClassDB::bind_method(D_METHOD("get_global_end_position" ), &NavigationLink2D::get_global_end_position); |
| 62 | |
| 63 | ClassDB::bind_method(D_METHOD("set_enter_cost" , "enter_cost" ), &NavigationLink2D::set_enter_cost); |
| 64 | ClassDB::bind_method(D_METHOD("get_enter_cost" ), &NavigationLink2D::get_enter_cost); |
| 65 | |
| 66 | ClassDB::bind_method(D_METHOD("set_travel_cost" , "travel_cost" ), &NavigationLink2D::set_travel_cost); |
| 67 | ClassDB::bind_method(D_METHOD("get_travel_cost" ), &NavigationLink2D::get_travel_cost); |
| 68 | |
| 69 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled" ), "set_enabled" , "is_enabled" ); |
| 70 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bidirectional" ), "set_bidirectional" , "is_bidirectional" ); |
| 71 | ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers" , PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers" , "get_navigation_layers" ); |
| 72 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "start_position" ), "set_start_position" , "get_start_position" ); |
| 73 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "end_position" ), "set_end_position" , "get_end_position" ); |
| 74 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost" ), "set_enter_cost" , "get_enter_cost" ); |
| 75 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost" ), "set_travel_cost" , "get_travel_cost" ); |
| 76 | } |
| 77 | |
| 78 | #ifndef DISABLE_DEPRECATED |
| 79 | bool NavigationLink2D::_set(const StringName &p_name, const Variant &p_value) { |
| 80 | if (p_name == "start_location" ) { |
| 81 | set_start_position(p_value); |
| 82 | return true; |
| 83 | } |
| 84 | if (p_name == "end_location" ) { |
| 85 | set_end_position(p_value); |
| 86 | return true; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | bool NavigationLink2D::_get(const StringName &p_name, Variant &r_ret) const { |
| 92 | if (p_name == "start_location" ) { |
| 93 | r_ret = get_start_position(); |
| 94 | return true; |
| 95 | } |
| 96 | if (p_name == "end_location" ) { |
| 97 | r_ret = get_end_position(); |
| 98 | return true; |
| 99 | } |
| 100 | return false; |
| 101 | } |
| 102 | #endif // DISABLE_DEPRECATED |
| 103 | |
| 104 | void NavigationLink2D::_notification(int p_what) { |
| 105 | switch (p_what) { |
| 106 | case NOTIFICATION_ENTER_TREE: { |
| 107 | if (enabled) { |
| 108 | NavigationServer2D::get_singleton()->link_set_map(link, get_world_2d()->get_navigation_map()); |
| 109 | } |
| 110 | current_global_transform = get_global_transform(); |
| 111 | NavigationServer2D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position)); |
| 112 | NavigationServer2D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position)); |
| 113 | } break; |
| 114 | |
| 115 | case NOTIFICATION_TRANSFORM_CHANGED: { |
| 116 | set_physics_process_internal(true); |
| 117 | } break; |
| 118 | |
| 119 | case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { |
| 120 | set_physics_process_internal(false); |
| 121 | if (is_inside_tree()) { |
| 122 | Transform2D new_global_transform = get_global_transform(); |
| 123 | if (current_global_transform != new_global_transform) { |
| 124 | current_global_transform = new_global_transform; |
| 125 | NavigationServer2D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position)); |
| 126 | NavigationServer2D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position)); |
| 127 | queue_redraw(); |
| 128 | } |
| 129 | } |
| 130 | } break; |
| 131 | |
| 132 | case NOTIFICATION_EXIT_TREE: { |
| 133 | NavigationServer2D::get_singleton()->link_set_map(link, RID()); |
| 134 | } break; |
| 135 | case NOTIFICATION_DRAW: { |
| 136 | #ifdef DEBUG_ENABLED |
| 137 | if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled())) { |
| 138 | Color color; |
| 139 | if (enabled) { |
| 140 | color = NavigationServer2D::get_singleton()->get_debug_navigation_link_connection_color(); |
| 141 | } else { |
| 142 | color = NavigationServer2D::get_singleton()->get_debug_navigation_link_connection_disabled_color(); |
| 143 | } |
| 144 | |
| 145 | real_t radius = NavigationServer2D::get_singleton()->map_get_link_connection_radius(get_world_2d()->get_navigation_map()); |
| 146 | |
| 147 | draw_line(get_start_position(), get_end_position(), color); |
| 148 | draw_arc(get_start_position(), radius, 0, Math_TAU, 10, color); |
| 149 | draw_arc(get_end_position(), radius, 0, Math_TAU, 10, color); |
| 150 | } |
| 151 | #endif // DEBUG_ENABLED |
| 152 | } break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | #ifdef TOOLS_ENABLED |
| 157 | Rect2 NavigationLink2D::_edit_get_rect() const { |
| 158 | if (!is_inside_tree()) { |
| 159 | return Rect2(); |
| 160 | } |
| 161 | |
| 162 | real_t radius = NavigationServer2D::get_singleton()->map_get_link_connection_radius(get_world_2d()->get_navigation_map()); |
| 163 | |
| 164 | Rect2 rect(get_start_position(), Size2()); |
| 165 | rect.expand_to(get_end_position()); |
| 166 | rect.grow_by(radius); |
| 167 | return rect; |
| 168 | } |
| 169 | |
| 170 | bool NavigationLink2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { |
| 171 | Point2 segment[2] = { get_start_position(), get_end_position() }; |
| 172 | |
| 173 | Vector2 closest_point = Geometry2D::get_closest_point_to_segment(p_point, segment); |
| 174 | return p_point.distance_to(closest_point) < p_tolerance; |
| 175 | } |
| 176 | #endif // TOOLS_ENABLED |
| 177 | |
| 178 | void NavigationLink2D::set_enabled(bool p_enabled) { |
| 179 | if (enabled == p_enabled) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | enabled = p_enabled; |
| 184 | |
| 185 | NavigationServer3D::get_singleton()->link_set_enabled(link, enabled); |
| 186 | |
| 187 | #ifdef DEBUG_ENABLED |
| 188 | if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) { |
| 189 | queue_redraw(); |
| 190 | } |
| 191 | #endif // DEBUG_ENABLED |
| 192 | } |
| 193 | |
| 194 | void NavigationLink2D::set_bidirectional(bool p_bidirectional) { |
| 195 | if (bidirectional == p_bidirectional) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | bidirectional = p_bidirectional; |
| 200 | |
| 201 | NavigationServer2D::get_singleton()->link_set_bidirectional(link, bidirectional); |
| 202 | } |
| 203 | |
| 204 | void NavigationLink2D::set_navigation_layers(uint32_t p_navigation_layers) { |
| 205 | if (navigation_layers == p_navigation_layers) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | navigation_layers = p_navigation_layers; |
| 210 | |
| 211 | NavigationServer2D::get_singleton()->link_set_navigation_layers(link, navigation_layers); |
| 212 | } |
| 213 | |
| 214 | void NavigationLink2D::set_navigation_layer_value(int p_layer_number, bool p_value) { |
| 215 | ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive." ); |
| 216 | ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive." ); |
| 217 | |
| 218 | uint32_t _navigation_layers = get_navigation_layers(); |
| 219 | |
| 220 | if (p_value) { |
| 221 | _navigation_layers |= 1 << (p_layer_number - 1); |
| 222 | } else { |
| 223 | _navigation_layers &= ~(1 << (p_layer_number - 1)); |
| 224 | } |
| 225 | |
| 226 | set_navigation_layers(_navigation_layers); |
| 227 | } |
| 228 | |
| 229 | bool NavigationLink2D::get_navigation_layer_value(int p_layer_number) const { |
| 230 | ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive." ); |
| 231 | ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive." ); |
| 232 | |
| 233 | return get_navigation_layers() & (1 << (p_layer_number - 1)); |
| 234 | } |
| 235 | |
| 236 | void NavigationLink2D::set_start_position(Vector2 p_position) { |
| 237 | if (start_position.is_equal_approx(p_position)) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | start_position = p_position; |
| 242 | |
| 243 | if (!is_inside_tree()) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | NavigationServer2D::get_singleton()->link_set_start_position(link, current_global_transform.xform(start_position)); |
| 248 | |
| 249 | update_configuration_warnings(); |
| 250 | |
| 251 | #ifdef DEBUG_ENABLED |
| 252 | if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) { |
| 253 | queue_redraw(); |
| 254 | } |
| 255 | #endif // DEBUG_ENABLED |
| 256 | } |
| 257 | |
| 258 | void NavigationLink2D::set_end_position(Vector2 p_position) { |
| 259 | if (end_position.is_equal_approx(p_position)) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | end_position = p_position; |
| 264 | |
| 265 | if (!is_inside_tree()) { |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | NavigationServer2D::get_singleton()->link_set_end_position(link, current_global_transform.xform(end_position)); |
| 270 | |
| 271 | update_configuration_warnings(); |
| 272 | |
| 273 | #ifdef DEBUG_ENABLED |
| 274 | if (Engine::get_singleton()->is_editor_hint() || NavigationServer2D::get_singleton()->get_debug_enabled()) { |
| 275 | queue_redraw(); |
| 276 | } |
| 277 | #endif // DEBUG_ENABLED |
| 278 | } |
| 279 | |
| 280 | void NavigationLink2D::set_global_start_position(Vector2 p_position) { |
| 281 | if (is_inside_tree()) { |
| 282 | set_start_position(to_local(p_position)); |
| 283 | } else { |
| 284 | set_start_position(p_position); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | Vector2 NavigationLink2D::get_global_start_position() const { |
| 289 | if (is_inside_tree()) { |
| 290 | return to_global(start_position); |
| 291 | } else { |
| 292 | return start_position; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | void NavigationLink2D::set_global_end_position(Vector2 p_position) { |
| 297 | if (is_inside_tree()) { |
| 298 | set_end_position(to_local(p_position)); |
| 299 | } else { |
| 300 | set_end_position(p_position); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | Vector2 NavigationLink2D::get_global_end_position() const { |
| 305 | if (is_inside_tree()) { |
| 306 | return to_global(end_position); |
| 307 | } else { |
| 308 | return end_position; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void NavigationLink2D::set_enter_cost(real_t p_enter_cost) { |
| 313 | ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive." ); |
| 314 | if (Math::is_equal_approx(enter_cost, p_enter_cost)) { |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | enter_cost = p_enter_cost; |
| 319 | |
| 320 | NavigationServer2D::get_singleton()->link_set_enter_cost(link, enter_cost); |
| 321 | } |
| 322 | |
| 323 | void NavigationLink2D::set_travel_cost(real_t p_travel_cost) { |
| 324 | ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive." ); |
| 325 | if (Math::is_equal_approx(travel_cost, p_travel_cost)) { |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | travel_cost = p_travel_cost; |
| 330 | |
| 331 | NavigationServer2D::get_singleton()->link_set_travel_cost(link, travel_cost); |
| 332 | } |
| 333 | |
| 334 | PackedStringArray NavigationLink2D::get_configuration_warnings() const { |
| 335 | PackedStringArray warnings = Node::get_configuration_warnings(); |
| 336 | |
| 337 | if (start_position.is_equal_approx(end_position)) { |
| 338 | warnings.push_back(RTR("NavigationLink2D start position should be different than the end position to be useful." )); |
| 339 | } |
| 340 | |
| 341 | return warnings; |
| 342 | } |
| 343 | |
| 344 | NavigationLink2D::NavigationLink2D() { |
| 345 | link = NavigationServer2D::get_singleton()->link_create(); |
| 346 | NavigationServer2D::get_singleton()->link_set_owner_id(link, get_instance_id()); |
| 347 | |
| 348 | set_notify_transform(true); |
| 349 | set_hide_clip_children(true); |
| 350 | } |
| 351 | |
| 352 | NavigationLink2D::~NavigationLink2D() { |
| 353 | ERR_FAIL_NULL(NavigationServer2D::get_singleton()); |
| 354 | NavigationServer2D::get_singleton()->free(link); |
| 355 | link = RID(); |
| 356 | } |
| 357 | |