navigation_polygon.cpp 21 KB

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