navigation_region_2d.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*************************************************************************/
  2. /* navigation_region_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_region_2d.h"
  31. #include "core/core_string_names.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "core/os/mutex.h"
  34. #include "scene/resources/world_2d.h"
  35. #include "servers/navigation_server_2d.h"
  36. #include "servers/navigation_server_3d.h"
  37. #include "thirdparty/misc/polypartition.h"
  38. #ifdef TOOLS_ENABLED
  39. Rect2 NavigationPolygon::_edit_get_rect() const {
  40. if (rect_cache_dirty) {
  41. item_rect = Rect2();
  42. bool first = true;
  43. for (int i = 0; i < outlines.size(); i++) {
  44. const Vector<Vector2> &outline = outlines[i];
  45. const int outline_size = outline.size();
  46. if (outline_size < 3) {
  47. continue;
  48. }
  49. const Vector2 *p = outline.ptr();
  50. for (int j = 0; j < outline_size; j++) {
  51. if (first) {
  52. item_rect = Rect2(p[j], Vector2(0, 0));
  53. first = false;
  54. } else {
  55. item_rect.expand_to(p[j]);
  56. }
  57. }
  58. }
  59. rect_cache_dirty = false;
  60. }
  61. return item_rect;
  62. }
  63. bool NavigationPolygon::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  64. for (int i = 0; i < outlines.size(); i++) {
  65. const Vector<Vector2> &outline = outlines[i];
  66. const int outline_size = outline.size();
  67. if (outline_size < 3) {
  68. continue;
  69. }
  70. if (Geometry2D::is_point_in_polygon(p_point, Variant(outline))) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. #endif
  77. void NavigationPolygon::set_vertices(const Vector<Vector2> &p_vertices) {
  78. {
  79. MutexLock lock(navmesh_generation);
  80. navmesh.unref();
  81. }
  82. vertices = p_vertices;
  83. rect_cache_dirty = true;
  84. }
  85. Vector<Vector2> NavigationPolygon::get_vertices() const {
  86. return vertices;
  87. }
  88. void NavigationPolygon::_set_polygons(const TypedArray<Vector<int32_t>> &p_array) {
  89. {
  90. MutexLock lock(navmesh_generation);
  91. navmesh.unref();
  92. }
  93. polygons.resize(p_array.size());
  94. for (int i = 0; i < p_array.size(); i++) {
  95. polygons.write[i].indices = p_array[i];
  96. }
  97. }
  98. TypedArray<Vector<int32_t>> NavigationPolygon::_get_polygons() const {
  99. TypedArray<Vector<int32_t>> ret;
  100. ret.resize(polygons.size());
  101. for (int i = 0; i < ret.size(); i++) {
  102. ret[i] = polygons[i].indices;
  103. }
  104. return ret;
  105. }
  106. void NavigationPolygon::_set_outlines(const TypedArray<Vector<Vector2>> &p_array) {
  107. outlines.resize(p_array.size());
  108. for (int i = 0; i < p_array.size(); i++) {
  109. outlines.write[i] = p_array[i];
  110. }
  111. rect_cache_dirty = true;
  112. }
  113. TypedArray<Vector<Vector2>> NavigationPolygon::_get_outlines() const {
  114. TypedArray<Vector<Vector2>> ret;
  115. ret.resize(outlines.size());
  116. for (int i = 0; i < ret.size(); i++) {
  117. ret[i] = outlines[i];
  118. }
  119. return ret;
  120. }
  121. void NavigationPolygon::add_polygon(const Vector<int> &p_polygon) {
  122. Polygon polygon;
  123. polygon.indices = p_polygon;
  124. polygons.push_back(polygon);
  125. {
  126. MutexLock lock(navmesh_generation);
  127. navmesh.unref();
  128. }
  129. }
  130. void NavigationPolygon::add_outline_at_index(const Vector<Vector2> &p_outline, int p_index) {
  131. outlines.insert(p_index, p_outline);
  132. rect_cache_dirty = true;
  133. }
  134. int NavigationPolygon::get_polygon_count() const {
  135. return polygons.size();
  136. }
  137. Vector<int> NavigationPolygon::get_polygon(int p_idx) {
  138. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  139. return polygons[p_idx].indices;
  140. }
  141. void NavigationPolygon::clear_polygons() {
  142. polygons.clear();
  143. {
  144. MutexLock lock(navmesh_generation);
  145. navmesh.unref();
  146. }
  147. }
  148. Ref<NavigationMesh> NavigationPolygon::get_mesh() {
  149. MutexLock lock(navmesh_generation);
  150. if (navmesh.is_null()) {
  151. navmesh.instantiate();
  152. Vector<Vector3> verts;
  153. {
  154. verts.resize(get_vertices().size());
  155. Vector3 *w = verts.ptrw();
  156. const Vector2 *r = get_vertices().ptr();
  157. for (int i(0); i < get_vertices().size(); i++) {
  158. w[i] = Vector3(r[i].x, 0.0, r[i].y);
  159. }
  160. }
  161. navmesh->set_vertices(verts);
  162. for (int i(0); i < get_polygon_count(); i++) {
  163. navmesh->add_polygon(get_polygon(i));
  164. }
  165. }
  166. return navmesh;
  167. }
  168. void NavigationPolygon::add_outline(const Vector<Vector2> &p_outline) {
  169. outlines.push_back(p_outline);
  170. rect_cache_dirty = true;
  171. }
  172. int NavigationPolygon::get_outline_count() const {
  173. return outlines.size();
  174. }
  175. void NavigationPolygon::set_outline(int p_idx, const Vector<Vector2> &p_outline) {
  176. ERR_FAIL_INDEX(p_idx, outlines.size());
  177. outlines.write[p_idx] = p_outline;
  178. rect_cache_dirty = true;
  179. }
  180. void NavigationPolygon::remove_outline(int p_idx) {
  181. ERR_FAIL_INDEX(p_idx, outlines.size());
  182. outlines.remove_at(p_idx);
  183. rect_cache_dirty = true;
  184. }
  185. Vector<Vector2> NavigationPolygon::get_outline(int p_idx) const {
  186. ERR_FAIL_INDEX_V(p_idx, outlines.size(), Vector<Vector2>());
  187. return outlines[p_idx];
  188. }
  189. void NavigationPolygon::clear_outlines() {
  190. outlines.clear();
  191. rect_cache_dirty = true;
  192. }
  193. void NavigationPolygon::make_polygons_from_outlines() {
  194. {
  195. MutexLock lock(navmesh_generation);
  196. navmesh.unref();
  197. }
  198. List<TPPLPoly> in_poly, out_poly;
  199. Vector2 outside_point(-1e10, -1e10);
  200. for (int i = 0; i < outlines.size(); i++) {
  201. Vector<Vector2> ol = outlines[i];
  202. int olsize = ol.size();
  203. if (olsize < 3) {
  204. continue;
  205. }
  206. const Vector2 *r = ol.ptr();
  207. for (int j = 0; j < olsize; j++) {
  208. outside_point.x = MAX(r[j].x, outside_point.x);
  209. outside_point.y = MAX(r[j].y, outside_point.y);
  210. }
  211. }
  212. outside_point += Vector2(0.7239784, 0.819238); //avoid precision issues
  213. for (int i = 0; i < outlines.size(); i++) {
  214. Vector<Vector2> ol = outlines[i];
  215. int olsize = ol.size();
  216. if (olsize < 3) {
  217. continue;
  218. }
  219. const Vector2 *r = ol.ptr();
  220. int interscount = 0;
  221. //test if this is an outer outline
  222. for (int k = 0; k < outlines.size(); k++) {
  223. if (i == k) {
  224. continue; //no self intersect
  225. }
  226. Vector<Vector2> ol2 = outlines[k];
  227. int olsize2 = ol2.size();
  228. if (olsize2 < 3) {
  229. continue;
  230. }
  231. const Vector2 *r2 = ol2.ptr();
  232. for (int l = 0; l < olsize2; l++) {
  233. if (Geometry2D::segment_intersects_segment(r[0], outside_point, r2[l], r2[(l + 1) % olsize2], nullptr)) {
  234. interscount++;
  235. }
  236. }
  237. }
  238. bool outer = (interscount % 2) == 0;
  239. TPPLPoly tp;
  240. tp.Init(olsize);
  241. for (int j = 0; j < olsize; j++) {
  242. tp[j] = r[j];
  243. }
  244. if (outer) {
  245. tp.SetOrientation(TPPL_ORIENTATION_CCW);
  246. } else {
  247. tp.SetOrientation(TPPL_ORIENTATION_CW);
  248. tp.SetHole(true);
  249. }
  250. in_poly.push_back(tp);
  251. }
  252. TPPLPartition tpart;
  253. if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed!
  254. ERR_PRINT("NavigationPolygon: Convex partition failed!");
  255. return;
  256. }
  257. polygons.clear();
  258. vertices.clear();
  259. HashMap<Vector2, int> points;
  260. for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
  261. TPPLPoly &tp = I->get();
  262. struct Polygon p;
  263. for (int64_t i = 0; i < tp.GetNumPoints(); i++) {
  264. HashMap<Vector2, int>::Iterator E = points.find(tp[i]);
  265. if (!E) {
  266. E = points.insert(tp[i], vertices.size());
  267. vertices.push_back(tp[i]);
  268. }
  269. p.indices.push_back(E->value);
  270. }
  271. polygons.push_back(p);
  272. }
  273. emit_signal(CoreStringNames::get_singleton()->changed);
  274. }
  275. void NavigationPolygon::_bind_methods() {
  276. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationPolygon::set_vertices);
  277. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationPolygon::get_vertices);
  278. ClassDB::bind_method(D_METHOD("add_polygon", "polygon"), &NavigationPolygon::add_polygon);
  279. ClassDB::bind_method(D_METHOD("get_polygon_count"), &NavigationPolygon::get_polygon_count);
  280. ClassDB::bind_method(D_METHOD("get_polygon", "idx"), &NavigationPolygon::get_polygon);
  281. ClassDB::bind_method(D_METHOD("clear_polygons"), &NavigationPolygon::clear_polygons);
  282. ClassDB::bind_method(D_METHOD("get_mesh"), &NavigationPolygon::get_mesh);
  283. ClassDB::bind_method(D_METHOD("add_outline", "outline"), &NavigationPolygon::add_outline);
  284. ClassDB::bind_method(D_METHOD("add_outline_at_index", "outline", "index"), &NavigationPolygon::add_outline_at_index);
  285. ClassDB::bind_method(D_METHOD("get_outline_count"), &NavigationPolygon::get_outline_count);
  286. ClassDB::bind_method(D_METHOD("set_outline", "idx", "outline"), &NavigationPolygon::set_outline);
  287. ClassDB::bind_method(D_METHOD("get_outline", "idx"), &NavigationPolygon::get_outline);
  288. ClassDB::bind_method(D_METHOD("remove_outline", "idx"), &NavigationPolygon::remove_outline);
  289. ClassDB::bind_method(D_METHOD("clear_outlines"), &NavigationPolygon::clear_outlines);
  290. ClassDB::bind_method(D_METHOD("make_polygons_from_outlines"), &NavigationPolygon::make_polygons_from_outlines);
  291. ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationPolygon::_set_polygons);
  292. ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationPolygon::_get_polygons);
  293. ClassDB::bind_method(D_METHOD("_set_outlines", "outlines"), &NavigationPolygon::_set_outlines);
  294. ClassDB::bind_method(D_METHOD("_get_outlines"), &NavigationPolygon::_get_outlines);
  295. ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
  296. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons");
  297. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_outlines", "_get_outlines");
  298. }
  299. void NavigationRegion2D::set_enabled(bool p_enabled) {
  300. if (enabled == p_enabled) {
  301. return;
  302. }
  303. enabled = p_enabled;
  304. if (!is_inside_tree()) {
  305. return;
  306. }
  307. if (!enabled) {
  308. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  309. NavigationServer2D::get_singleton_mut()->disconnect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  310. } else {
  311. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  312. NavigationServer2D::get_singleton_mut()->connect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  313. }
  314. #ifdef DEBUG_ENABLED
  315. if (Engine::get_singleton()->is_editor_hint() || NavigationServer3D::get_singleton()->get_debug_enabled()) {
  316. update();
  317. }
  318. #endif // DEBUG_ENABLED
  319. }
  320. bool NavigationRegion2D::is_enabled() const {
  321. return enabled;
  322. }
  323. void NavigationRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
  324. NavigationServer2D::get_singleton()->region_set_navigation_layers(region, p_navigation_layers);
  325. }
  326. uint32_t NavigationRegion2D::get_navigation_layers() const {
  327. return NavigationServer2D::get_singleton()->region_get_navigation_layers(region);
  328. }
  329. void NavigationRegion2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  330. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  331. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  332. uint32_t _navigation_layers = get_navigation_layers();
  333. if (p_value) {
  334. _navigation_layers |= 1 << (p_layer_number - 1);
  335. } else {
  336. _navigation_layers &= ~(1 << (p_layer_number - 1));
  337. }
  338. set_navigation_layers(_navigation_layers);
  339. }
  340. bool NavigationRegion2D::get_navigation_layer_value(int p_layer_number) const {
  341. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  342. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  343. return get_navigation_layers() & (1 << (p_layer_number - 1));
  344. }
  345. void NavigationRegion2D::set_enter_cost(real_t p_enter_cost) {
  346. ERR_FAIL_COND_MSG(p_enter_cost < 0.0, "The enter_cost must be positive.");
  347. enter_cost = MAX(p_enter_cost, 0.0);
  348. NavigationServer2D::get_singleton()->region_set_enter_cost(region, p_enter_cost);
  349. }
  350. real_t NavigationRegion2D::get_enter_cost() const {
  351. return enter_cost;
  352. }
  353. void NavigationRegion2D::set_travel_cost(real_t p_travel_cost) {
  354. ERR_FAIL_COND_MSG(p_travel_cost < 0.0, "The travel_cost must be positive.");
  355. travel_cost = MAX(p_travel_cost, 0.0);
  356. NavigationServer2D::get_singleton()->region_set_travel_cost(region, travel_cost);
  357. }
  358. real_t NavigationRegion2D::get_travel_cost() const {
  359. return travel_cost;
  360. }
  361. RID NavigationRegion2D::get_region_rid() const {
  362. return region;
  363. }
  364. /////////////////////////////
  365. #ifdef TOOLS_ENABLED
  366. Rect2 NavigationRegion2D::_edit_get_rect() const {
  367. return navpoly.is_valid() ? navpoly->_edit_get_rect() : Rect2();
  368. }
  369. bool NavigationRegion2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  370. return navpoly.is_valid() ? navpoly->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  371. }
  372. #endif
  373. void NavigationRegion2D::_notification(int p_what) {
  374. switch (p_what) {
  375. case NOTIFICATION_ENTER_TREE: {
  376. if (enabled) {
  377. NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map());
  378. NavigationServer2D::get_singleton_mut()->connect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  379. }
  380. } break;
  381. case NOTIFICATION_TRANSFORM_CHANGED: {
  382. NavigationServer2D::get_singleton()->region_set_transform(region, get_global_transform());
  383. } break;
  384. case NOTIFICATION_EXIT_TREE: {
  385. NavigationServer2D::get_singleton()->region_set_map(region, RID());
  386. if (enabled) {
  387. NavigationServer2D::get_singleton_mut()->disconnect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  388. }
  389. } break;
  390. case NOTIFICATION_DRAW: {
  391. #ifdef DEBUG_ENABLED
  392. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || NavigationServer3D::get_singleton()->get_debug_enabled()) && navpoly.is_valid()) {
  393. Vector<Vector2> verts = navpoly->get_vertices();
  394. if (verts.size() < 3) {
  395. return;
  396. }
  397. Color color;
  398. if (enabled) {
  399. color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  400. } else {
  401. color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_color();
  402. }
  403. Color doors_color = NavigationServer3D::get_singleton()->get_debug_navigation_edge_connection_color();
  404. RandomPCG rand;
  405. for (int i = 0; i < navpoly->get_polygon_count(); i++) {
  406. // An array of vertices for this polygon.
  407. Vector<int> polygon = navpoly->get_polygon(i);
  408. Vector<Vector2> vertices;
  409. vertices.resize(polygon.size());
  410. for (int j = 0; j < polygon.size(); j++) {
  411. ERR_FAIL_INDEX(polygon[j], verts.size());
  412. vertices.write[j] = verts[polygon[j]];
  413. }
  414. // Generate the polygon color, slightly randomly modified from the settings one.
  415. Color random_variation_color;
  416. random_variation_color.set_hsv(color.get_h() + rand.random(-1.0, 1.0) * 0.05, color.get_s(), color.get_v() + rand.random(-1.0, 1.0) * 0.1);
  417. random_variation_color.a = color.a;
  418. Vector<Color> colors;
  419. colors.push_back(random_variation_color);
  420. RS::get_singleton()->canvas_item_add_polygon(get_canvas_item(), vertices, colors);
  421. }
  422. // Draw the region
  423. Transform2D xform = get_global_transform();
  424. const NavigationServer2D *ns = NavigationServer2D::get_singleton();
  425. real_t radius = ns->map_get_edge_connection_margin(get_world_2d()->get_navigation_map()) / 2.0;
  426. for (int i = 0; i < ns->region_get_connections_count(region); i++) {
  427. // Two main points
  428. Vector2 a = ns->region_get_connection_pathway_start(region, i);
  429. a = xform.affine_inverse().xform(a);
  430. Vector2 b = ns->region_get_connection_pathway_end(region, i);
  431. b = xform.affine_inverse().xform(b);
  432. draw_line(a, b, doors_color);
  433. // Draw a circle to illustrate the margins.
  434. real_t angle = a.angle_to_point(b);
  435. draw_arc(a, radius, angle + Math_PI / 2.0, angle - Math_PI / 2.0 + Math_TAU, 10, doors_color);
  436. draw_arc(b, radius, angle - Math_PI / 2.0, angle + Math_PI / 2.0, 10, doors_color);
  437. }
  438. }
  439. #endif // DEBUG_ENABLED
  440. } break;
  441. }
  442. }
  443. void NavigationRegion2D::set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly) {
  444. if (p_navpoly == navpoly) {
  445. return;
  446. }
  447. if (navpoly.is_valid()) {
  448. navpoly->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NavigationRegion2D::_navpoly_changed));
  449. }
  450. navpoly = p_navpoly;
  451. NavigationServer2D::get_singleton()->region_set_navpoly(region, p_navpoly);
  452. if (navpoly.is_valid()) {
  453. navpoly->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &NavigationRegion2D::_navpoly_changed));
  454. }
  455. _navpoly_changed();
  456. update_configuration_warnings();
  457. }
  458. Ref<NavigationPolygon> NavigationRegion2D::get_navigation_polygon() const {
  459. return navpoly;
  460. }
  461. void NavigationRegion2D::_navpoly_changed() {
  462. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())) {
  463. update();
  464. }
  465. if (navpoly.is_valid()) {
  466. NavigationServer2D::get_singleton()->region_set_navpoly(region, navpoly);
  467. }
  468. }
  469. void NavigationRegion2D::_map_changed(RID p_map) {
  470. #ifdef DEBUG_ENABLED
  471. if (is_inside_tree() && get_world_2d()->get_navigation_map() == p_map) {
  472. update();
  473. }
  474. #endif // DEBUG_ENABLED
  475. }
  476. TypedArray<String> NavigationRegion2D::get_configuration_warnings() const {
  477. TypedArray<String> warnings = Node2D::get_configuration_warnings();
  478. if (is_visible_in_tree() && is_inside_tree()) {
  479. if (!navpoly.is_valid()) {
  480. warnings.push_back(RTR("A NavigationMesh resource must be set or created for this node to work. Please set a property or draw a polygon."));
  481. }
  482. }
  483. return warnings;
  484. }
  485. void NavigationRegion2D::_bind_methods() {
  486. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navpoly"), &NavigationRegion2D::set_navigation_polygon);
  487. ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationRegion2D::get_navigation_polygon);
  488. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationRegion2D::set_enabled);
  489. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationRegion2D::is_enabled);
  490. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationRegion2D::set_navigation_layers);
  491. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationRegion2D::get_navigation_layers);
  492. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationRegion2D::set_navigation_layer_value);
  493. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationRegion2D::get_navigation_layer_value);
  494. ClassDB::bind_method(D_METHOD("get_region_rid"), &NavigationRegion2D::get_region_rid);
  495. ClassDB::bind_method(D_METHOD("set_enter_cost", "enter_cost"), &NavigationRegion2D::set_enter_cost);
  496. ClassDB::bind_method(D_METHOD("get_enter_cost"), &NavigationRegion2D::get_enter_cost);
  497. ClassDB::bind_method(D_METHOD("set_travel_cost", "travel_cost"), &NavigationRegion2D::set_travel_cost);
  498. ClassDB::bind_method(D_METHOD("get_travel_cost"), &NavigationRegion2D::get_travel_cost);
  499. ClassDB::bind_method(D_METHOD("_navpoly_changed"), &NavigationRegion2D::_navpoly_changed);
  500. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navpoly", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
  501. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  502. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  503. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "enter_cost"), "set_enter_cost", "get_enter_cost");
  504. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "travel_cost"), "set_travel_cost", "get_travel_cost");
  505. }
  506. NavigationRegion2D::NavigationRegion2D() {
  507. set_notify_transform(true);
  508. region = NavigationServer2D::get_singleton()->region_create();
  509. NavigationServer2D::get_singleton()->region_set_enter_cost(region, get_enter_cost());
  510. NavigationServer2D::get_singleton()->region_set_travel_cost(region, get_travel_cost());
  511. #ifdef DEBUG_ENABLED
  512. NavigationServer3D::get_singleton_mut()->connect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  513. NavigationServer3D::get_singleton_mut()->connect("navigation_debug_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  514. #endif // DEBUG_ENABLED
  515. }
  516. NavigationRegion2D::~NavigationRegion2D() {
  517. NavigationServer2D::get_singleton()->free(region);
  518. #ifdef DEBUG_ENABLED
  519. NavigationServer3D::get_singleton_mut()->disconnect("map_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  520. NavigationServer3D::get_singleton_mut()->disconnect("navigation_debug_changed", callable_mp(this, &NavigationRegion2D::_map_changed));
  521. #endif // DEBUG_ENABLED
  522. }