navigation_polygon.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*************************************************************************/
  2. /* navigation_polygon.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "navigation_2d.h"
  34. #include "thirdparty/misc/triangulator.h"
  35. #ifdef TOOLS_ENABLED
  36. Rect2 NavigationPolygon::_edit_get_rect() const {
  37. if (rect_cache_dirty) {
  38. item_rect = Rect2();
  39. bool first = true;
  40. for (int i = 0; i < outlines.size(); i++) {
  41. const PoolVector<Vector2> &outline = outlines[i];
  42. const int outline_size = outline.size();
  43. if (outline_size < 3)
  44. continue;
  45. PoolVector<Vector2>::Read p = outline.read();
  46. for (int j = 0; j < outline_size; j++) {
  47. if (first) {
  48. item_rect = Rect2(p[j], Vector2(0, 0));
  49. first = false;
  50. } else {
  51. item_rect.expand_to(p[j]);
  52. }
  53. }
  54. }
  55. rect_cache_dirty = false;
  56. }
  57. return item_rect;
  58. }
  59. bool NavigationPolygon::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  60. for (int i = 0; i < outlines.size(); i++) {
  61. const PoolVector<Vector2> &outline = outlines[i];
  62. const int outline_size = outline.size();
  63. if (outline_size < 3)
  64. continue;
  65. if (Geometry::is_point_in_polygon(p_point, Variant(outline)))
  66. return true;
  67. }
  68. return false;
  69. }
  70. #endif
  71. void NavigationPolygon::set_vertices(const PoolVector<Vector2> &p_vertices) {
  72. vertices = p_vertices;
  73. rect_cache_dirty = true;
  74. }
  75. PoolVector<Vector2> NavigationPolygon::get_vertices() const {
  76. return vertices;
  77. }
  78. void NavigationPolygon::_set_polygons(const Array &p_array) {
  79. polygons.resize(p_array.size());
  80. for (int i = 0; i < p_array.size(); i++) {
  81. polygons.write[i].indices = p_array[i];
  82. }
  83. }
  84. Array NavigationPolygon::_get_polygons() const {
  85. Array ret;
  86. ret.resize(polygons.size());
  87. for (int i = 0; i < ret.size(); i++) {
  88. ret[i] = polygons[i].indices;
  89. }
  90. return ret;
  91. }
  92. void NavigationPolygon::_set_outlines(const Array &p_array) {
  93. outlines.resize(p_array.size());
  94. for (int i = 0; i < p_array.size(); i++) {
  95. outlines.write[i] = p_array[i];
  96. }
  97. rect_cache_dirty = true;
  98. }
  99. Array NavigationPolygon::_get_outlines() const {
  100. Array ret;
  101. ret.resize(outlines.size());
  102. for (int i = 0; i < ret.size(); i++) {
  103. ret[i] = outlines[i];
  104. }
  105. return ret;
  106. }
  107. void NavigationPolygon::add_polygon(const Vector<int> &p_polygon) {
  108. Polygon polygon;
  109. polygon.indices = p_polygon;
  110. polygons.push_back(polygon);
  111. }
  112. void NavigationPolygon::add_outline_at_index(const PoolVector<Vector2> &p_outline, int p_index) {
  113. outlines.insert(p_index, p_outline);
  114. rect_cache_dirty = true;
  115. }
  116. int NavigationPolygon::get_polygon_count() const {
  117. return polygons.size();
  118. }
  119. Vector<int> NavigationPolygon::get_polygon(int p_idx) {
  120. ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
  121. return polygons[p_idx].indices;
  122. }
  123. void NavigationPolygon::clear_polygons() {
  124. polygons.clear();
  125. }
  126. void NavigationPolygon::add_outline(const PoolVector<Vector2> &p_outline) {
  127. outlines.push_back(p_outline);
  128. rect_cache_dirty = true;
  129. }
  130. int NavigationPolygon::get_outline_count() const {
  131. return outlines.size();
  132. }
  133. void NavigationPolygon::set_outline(int p_idx, const PoolVector<Vector2> &p_outline) {
  134. ERR_FAIL_INDEX(p_idx, outlines.size());
  135. outlines.write[p_idx] = p_outline;
  136. rect_cache_dirty = true;
  137. }
  138. void NavigationPolygon::remove_outline(int p_idx) {
  139. ERR_FAIL_INDEX(p_idx, outlines.size());
  140. outlines.remove(p_idx);
  141. rect_cache_dirty = true;
  142. }
  143. PoolVector<Vector2> NavigationPolygon::get_outline(int p_idx) const {
  144. ERR_FAIL_INDEX_V(p_idx, outlines.size(), PoolVector<Vector2>());
  145. return outlines[p_idx];
  146. }
  147. void NavigationPolygon::clear_outlines() {
  148. outlines.clear();
  149. rect_cache_dirty = true;
  150. }
  151. void NavigationPolygon::make_polygons_from_outlines() {
  152. List<TriangulatorPoly> in_poly, out_poly;
  153. Vector2 outside_point(-1e10, -1e10);
  154. for (int i = 0; i < outlines.size(); i++) {
  155. PoolVector<Vector2> ol = outlines[i];
  156. int olsize = ol.size();
  157. if (olsize < 3)
  158. continue;
  159. PoolVector<Vector2>::Read r = ol.read();
  160. for (int j = 0; j < olsize; j++) {
  161. outside_point.x = MAX(r[j].x, outside_point.x);
  162. outside_point.y = MAX(r[j].y, outside_point.y);
  163. }
  164. }
  165. outside_point += Vector2(0.7239784, 0.819238); //avoid precision issues
  166. for (int i = 0; i < outlines.size(); i++) {
  167. PoolVector<Vector2> ol = outlines[i];
  168. int olsize = ol.size();
  169. if (olsize < 3)
  170. continue;
  171. PoolVector<Vector2>::Read r = ol.read();
  172. int interscount = 0;
  173. //test if this is an outer outline
  174. for (int k = 0; k < outlines.size(); k++) {
  175. if (i == k)
  176. continue; //no self intersect
  177. PoolVector<Vector2> ol2 = outlines[k];
  178. int olsize2 = ol2.size();
  179. if (olsize2 < 3)
  180. continue;
  181. PoolVector<Vector2>::Read r2 = ol2.read();
  182. for (int l = 0; l < olsize2; l++) {
  183. if (Geometry::segment_intersects_segment_2d(r[0], outside_point, r2[l], r2[(l + 1) % olsize2], NULL)) {
  184. interscount++;
  185. }
  186. }
  187. }
  188. bool outer = (interscount % 2) == 0;
  189. TriangulatorPoly tp;
  190. tp.Init(olsize);
  191. for (int j = 0; j < olsize; j++) {
  192. tp[j] = r[j];
  193. }
  194. if (outer)
  195. tp.SetOrientation(TRIANGULATOR_CCW);
  196. else {
  197. tp.SetOrientation(TRIANGULATOR_CW);
  198. tp.SetHole(true);
  199. }
  200. in_poly.push_back(tp);
  201. }
  202. TriangulatorPartition tpart;
  203. if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed!
  204. ERR_PRINTS("NavigationPolygon: Convex partition failed!");
  205. return;
  206. }
  207. polygons.clear();
  208. vertices.resize(0);
  209. Map<Vector2, int> points;
  210. for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
  211. TriangulatorPoly &tp = I->get();
  212. struct Polygon p;
  213. for (int64_t i = 0; i < tp.GetNumPoints(); i++) {
  214. Map<Vector2, int>::Element *E = points.find(tp[i]);
  215. if (!E) {
  216. E = points.insert(tp[i], vertices.size());
  217. vertices.push_back(tp[i]);
  218. }
  219. p.indices.push_back(E->get());
  220. }
  221. polygons.push_back(p);
  222. }
  223. emit_signal(CoreStringNames::get_singleton()->changed);
  224. }
  225. void NavigationPolygon::_bind_methods() {
  226. ClassDB::bind_method(D_METHOD("set_vertices", "vertices"), &NavigationPolygon::set_vertices);
  227. ClassDB::bind_method(D_METHOD("get_vertices"), &NavigationPolygon::get_vertices);
  228. ClassDB::bind_method(D_METHOD("add_polygon", "polygon"), &NavigationPolygon::add_polygon);
  229. ClassDB::bind_method(D_METHOD("get_polygon_count"), &NavigationPolygon::get_polygon_count);
  230. ClassDB::bind_method(D_METHOD("get_polygon", "idx"), &NavigationPolygon::get_polygon);
  231. ClassDB::bind_method(D_METHOD("clear_polygons"), &NavigationPolygon::clear_polygons);
  232. ClassDB::bind_method(D_METHOD("add_outline", "outline"), &NavigationPolygon::add_outline);
  233. ClassDB::bind_method(D_METHOD("add_outline_at_index", "outline", "index"), &NavigationPolygon::add_outline_at_index);
  234. ClassDB::bind_method(D_METHOD("get_outline_count"), &NavigationPolygon::get_outline_count);
  235. ClassDB::bind_method(D_METHOD("set_outline", "idx", "outline"), &NavigationPolygon::set_outline);
  236. ClassDB::bind_method(D_METHOD("get_outline", "idx"), &NavigationPolygon::get_outline);
  237. ClassDB::bind_method(D_METHOD("remove_outline", "idx"), &NavigationPolygon::remove_outline);
  238. ClassDB::bind_method(D_METHOD("clear_outlines"), &NavigationPolygon::clear_outlines);
  239. ClassDB::bind_method(D_METHOD("make_polygons_from_outlines"), &NavigationPolygon::make_polygons_from_outlines);
  240. ClassDB::bind_method(D_METHOD("_set_polygons", "polygons"), &NavigationPolygon::_set_polygons);
  241. ClassDB::bind_method(D_METHOD("_get_polygons"), &NavigationPolygon::_get_polygons);
  242. ClassDB::bind_method(D_METHOD("_set_outlines", "outlines"), &NavigationPolygon::_set_outlines);
  243. ClassDB::bind_method(D_METHOD("_get_outlines"), &NavigationPolygon::_get_outlines);
  244. ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_vertices", "get_vertices");
  245. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_polygons", "_get_polygons");
  246. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_outlines", "_get_outlines");
  247. }
  248. NavigationPolygon::NavigationPolygon() :
  249. rect_cache_dirty(true) {
  250. }
  251. void NavigationPolygonInstance::set_enabled(bool p_enabled) {
  252. if (enabled == p_enabled)
  253. return;
  254. enabled = p_enabled;
  255. if (!is_inside_tree())
  256. return;
  257. if (!enabled) {
  258. if (nav_id != -1) {
  259. navigation->navpoly_remove(nav_id);
  260. nav_id = -1;
  261. }
  262. } else {
  263. if (navigation) {
  264. if (navpoly.is_valid()) {
  265. nav_id = navigation->navpoly_add(navpoly, get_relative_transform_to_parent(navigation), this);
  266. }
  267. }
  268. }
  269. if (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())
  270. update();
  271. }
  272. bool NavigationPolygonInstance::is_enabled() const {
  273. return enabled;
  274. }
  275. /////////////////////////////
  276. #ifdef TOOLS_ENABLED
  277. Rect2 NavigationPolygonInstance::_edit_get_rect() const {
  278. return navpoly.is_valid() ? navpoly->_edit_get_rect() : Rect2();
  279. }
  280. bool NavigationPolygonInstance::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
  281. return navpoly.is_valid() ? navpoly->_edit_is_selected_on_click(p_point, p_tolerance) : false;
  282. }
  283. #endif
  284. void NavigationPolygonInstance::_notification(int p_what) {
  285. switch (p_what) {
  286. case NOTIFICATION_ENTER_TREE: {
  287. Node2D *c = this;
  288. while (c) {
  289. navigation = Object::cast_to<Navigation2D>(c);
  290. if (navigation) {
  291. if (enabled && navpoly.is_valid()) {
  292. nav_id = navigation->navpoly_add(navpoly, get_relative_transform_to_parent(navigation), this);
  293. }
  294. break;
  295. }
  296. c = Object::cast_to<Node2D>(c->get_parent());
  297. }
  298. } break;
  299. case NOTIFICATION_TRANSFORM_CHANGED: {
  300. if (navigation && nav_id != -1) {
  301. navigation->navpoly_set_transform(nav_id, get_relative_transform_to_parent(navigation));
  302. }
  303. } break;
  304. case NOTIFICATION_EXIT_TREE: {
  305. if (navigation) {
  306. if (nav_id != -1) {
  307. navigation->navpoly_remove(nav_id);
  308. nav_id = -1;
  309. }
  310. }
  311. navigation = NULL;
  312. } break;
  313. case NOTIFICATION_DRAW: {
  314. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) && navpoly.is_valid()) {
  315. PoolVector<Vector2> verts = navpoly->get_vertices();
  316. int vsize = verts.size();
  317. if (vsize < 3)
  318. return;
  319. Color color;
  320. if (enabled) {
  321. color = get_tree()->get_debug_navigation_color();
  322. } else {
  323. color = get_tree()->get_debug_navigation_disabled_color();
  324. }
  325. Vector<Color> colors;
  326. Vector<Vector2> vertices;
  327. vertices.resize(vsize);
  328. colors.resize(vsize);
  329. {
  330. PoolVector<Vector2>::Read vr = verts.read();
  331. for (int i = 0; i < vsize; i++) {
  332. vertices.write[i] = vr[i];
  333. colors.write[i] = color;
  334. }
  335. }
  336. Vector<int> indices;
  337. for (int i = 0; i < navpoly->get_polygon_count(); i++) {
  338. Vector<int> polygon = navpoly->get_polygon(i);
  339. for (int j = 2; j < polygon.size(); j++) {
  340. int kofs[3] = { 0, j - 1, j };
  341. for (int k = 0; k < 3; k++) {
  342. int idx = polygon[kofs[k]];
  343. ERR_FAIL_INDEX(idx, vsize);
  344. indices.push_back(idx);
  345. }
  346. }
  347. }
  348. VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, vertices, colors);
  349. }
  350. } break;
  351. }
  352. }
  353. void NavigationPolygonInstance::set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly) {
  354. if (p_navpoly == navpoly) {
  355. return;
  356. }
  357. if (navigation && nav_id != -1) {
  358. navigation->navpoly_remove(nav_id);
  359. nav_id = -1;
  360. }
  361. if (navpoly.is_valid()) {
  362. navpoly->disconnect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
  363. }
  364. navpoly = p_navpoly;
  365. if (navpoly.is_valid()) {
  366. navpoly->connect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
  367. }
  368. _navpoly_changed();
  369. if (navigation && navpoly.is_valid() && enabled) {
  370. nav_id = navigation->navpoly_add(navpoly, get_relative_transform_to_parent(navigation), this);
  371. }
  372. _change_notify("navpoly");
  373. update_configuration_warning();
  374. }
  375. Ref<NavigationPolygon> NavigationPolygonInstance::get_navigation_polygon() const {
  376. return navpoly;
  377. }
  378. void NavigationPolygonInstance::_navpoly_changed() {
  379. if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()))
  380. update();
  381. }
  382. String NavigationPolygonInstance::get_configuration_warning() const {
  383. if (!is_visible_in_tree() || !is_inside_tree())
  384. return String();
  385. if (!navpoly.is_valid()) {
  386. return TTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon.");
  387. }
  388. const Node2D *c = this;
  389. while (c) {
  390. if (Object::cast_to<Navigation2D>(c)) {
  391. return String();
  392. }
  393. c = Object::cast_to<Node2D>(c->get_parent());
  394. }
  395. return TTR("NavigationPolygonInstance must be a child or grandchild to a Navigation2D node. It only provides navigation data.");
  396. }
  397. void NavigationPolygonInstance::_bind_methods() {
  398. ClassDB::bind_method(D_METHOD("set_navigation_polygon", "navpoly"), &NavigationPolygonInstance::set_navigation_polygon);
  399. ClassDB::bind_method(D_METHOD("get_navigation_polygon"), &NavigationPolygonInstance::get_navigation_polygon);
  400. ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &NavigationPolygonInstance::set_enabled);
  401. ClassDB::bind_method(D_METHOD("is_enabled"), &NavigationPolygonInstance::is_enabled);
  402. ClassDB::bind_method(D_METHOD("_navpoly_changed"), &NavigationPolygonInstance::_navpoly_changed);
  403. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navpoly", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), "set_navigation_polygon", "get_navigation_polygon");
  404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
  405. }
  406. NavigationPolygonInstance::NavigationPolygonInstance() {
  407. navigation = NULL;
  408. nav_id = -1;
  409. enabled = true;
  410. set_notify_transform(true);
  411. }