navigation_link_2d.cpp 13 KB

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