navigation_polygon.cpp 16 KB

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