navigation_link_2d.cpp 15 KB

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