navigation_link_2d.cpp 14 KB

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