navigation_polygon.cpp 14 KB

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