navigation_link_2d.cpp 16 KB

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