2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* navigation_polygon.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2018-01-02 10:27:24 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2015-02-14 15:09:52 +00:00
|
|
|
#include "navigation_polygon.h"
|
2017-04-28 16:29:15 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "core_string_names.h"
|
2015-02-14 15:09:52 +00:00
|
|
|
#include "navigation2d.h"
|
2017-04-28 16:29:15 +00:00
|
|
|
|
|
|
|
#include "thirdparty/misc/triangulator.h"
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::set_vertices(const DVector<Vector2> &p_vertices) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
vertices = p_vertices;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector2> NavigationPolygon::get_vertices() const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
return vertices;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::_set_polygons(const Array &p_array) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
polygons.resize(p_array.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_array.size(); i++) {
|
|
|
|
polygons[i].indices = p_array[i];
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array NavigationPolygon::_get_polygons() const {
|
|
|
|
|
|
|
|
Array ret;
|
|
|
|
ret.resize(polygons.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ret.size(); i++) {
|
|
|
|
ret[i] = polygons[i].indices;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::_set_outlines(const Array &p_array) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
outlines.resize(p_array.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < p_array.size(); i++) {
|
|
|
|
outlines[i] = p_array[i];
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array NavigationPolygon::_get_outlines() const {
|
|
|
|
|
|
|
|
Array ret;
|
|
|
|
ret.resize(outlines.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < ret.size(); i++) {
|
|
|
|
ret[i] = outlines[i];
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::add_polygon(const Vector<int> &p_polygon) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
Polygon polygon;
|
2017-03-18 23:36:26 +00:00
|
|
|
polygon.indices = p_polygon;
|
2015-02-14 15:09:52 +00:00
|
|
|
polygons.push_back(polygon);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::add_outline_at_index(const DVector<Vector2> &p_outline, int p_index) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
outlines.insert(p_index, p_outline);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int NavigationPolygon::get_polygon_count() const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
return polygons.size();
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<int> NavigationPolygon::get_polygon(int p_idx) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, polygons.size(), Vector<int>());
|
2015-02-14 15:09:52 +00:00
|
|
|
return polygons[p_idx].indices;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::clear_polygons() {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
polygons.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::add_outline(const DVector<Vector2> &p_outline) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
outlines.push_back(p_outline);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int NavigationPolygon::get_outline_count() const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
return outlines.size();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::set_outline(int p_idx, const DVector<Vector2> &p_outline) {
|
|
|
|
ERR_FAIL_INDEX(p_idx, outlines.size());
|
|
|
|
outlines[p_idx] = p_outline;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationPolygon::remove_outline(int p_idx) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, outlines.size());
|
2015-02-14 15:09:52 +00:00
|
|
|
outlines.remove(p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
DVector<Vector2> NavigationPolygon::get_outline(int p_idx) const {
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX_V(p_idx, outlines.size(), DVector<Vector2>());
|
2015-02-14 15:09:52 +00:00
|
|
|
return outlines[p_idx];
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::clear_outlines() {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-01-14 17:03:38 +00:00
|
|
|
outlines.clear();
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygon::make_polygons_from_outlines() {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
List<TriangulatorPoly> in_poly, out_poly;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 outside_point(-1e10, -1e10);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < outlines.size(); i++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
DVector<Vector2> ol = outlines[i];
|
|
|
|
int olsize = ol.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (olsize < 3)
|
2015-02-14 15:09:52 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector2>::Read r = ol.read();
|
|
|
|
for (int j = 0; j < olsize; j++) {
|
|
|
|
outside_point.x = MAX(r[j].x, outside_point.x);
|
|
|
|
outside_point.y = MAX(r[j].y, outside_point.y);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
outside_point += Vector2(0.7239784, 0.819238); //avoid precision issues
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < outlines.size(); i++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
DVector<Vector2> ol = outlines[i];
|
|
|
|
int olsize = ol.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (olsize < 3)
|
2015-02-14 15:09:52 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector2>::Read r = ol.read();
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int interscount = 0;
|
2015-02-14 15:09:52 +00:00
|
|
|
//test if this is an outer outline
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int k = 0; k < outlines.size(); k++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (i == k)
|
2015-02-14 15:09:52 +00:00
|
|
|
continue; //no self intersect
|
|
|
|
|
|
|
|
DVector<Vector2> ol2 = outlines[k];
|
|
|
|
int olsize2 = ol2.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (olsize2 < 3)
|
2015-02-14 15:09:52 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector2>::Read r2 = ol2.read();
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int l = 0; l < olsize2; l++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (Geometry::segment_intersects_segment_2d(r[0], outside_point, r2[l], r2[(l + 1) % olsize2], NULL)) {
|
2015-02-14 15:09:52 +00:00
|
|
|
interscount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool outer = (interscount % 2) == 0;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
TriangulatorPoly tp;
|
|
|
|
tp.Init(olsize);
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 0; j < olsize; j++) {
|
|
|
|
tp[j] = r[j];
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outer)
|
|
|
|
tp.SetOrientation(TRIANGULATOR_CCW);
|
|
|
|
else {
|
|
|
|
tp.SetOrientation(TRIANGULATOR_CW);
|
|
|
|
tp.SetHole(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
in_poly.push_back(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
TriangulatorPartition tpart;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (tpart.ConvexPartition_HM(&in_poly, &out_poly) == 0) { //failed!
|
2015-02-14 15:09:52 +00:00
|
|
|
print_line("convex partition failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
polygons.clear();
|
|
|
|
vertices.resize(0);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<Vector2, int> points;
|
|
|
|
for (List<TriangulatorPoly>::Element *I = out_poly.front(); I; I = I->next()) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
TriangulatorPoly &tp = I->get();
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
struct Polygon p;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < tp.GetNumPoints(); i++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<Vector2, int>::Element *E = points.find(tp[i]);
|
2015-02-14 15:09:52 +00:00
|
|
|
if (!E) {
|
2017-03-18 23:36:26 +00:00
|
|
|
E = points.insert(tp[i], vertices.size());
|
2015-02-14 15:09:52 +00:00
|
|
|
vertices.push_back(tp[i]);
|
|
|
|
}
|
|
|
|
p.indices.push_back(E->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
polygons.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationPolygon::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_vertices", "vertices"), &NavigationPolygon::set_vertices);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_vertices"), &NavigationPolygon::get_vertices);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_polygon", "polygon"), &NavigationPolygon::add_polygon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_polygon_count"), &NavigationPolygon::get_polygon_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_polygon", "idx"), &NavigationPolygon::get_polygon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_polygons"), &NavigationPolygon::clear_polygons);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("add_outline", "outline"), &NavigationPolygon::add_outline);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_outline_at_index", "outline", "index"), &NavigationPolygon::add_outline_at_index);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_outline_count"), &NavigationPolygon::get_outline_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_outline", "idx", "outline"), &NavigationPolygon::set_outline);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_outline", "idx"), &NavigationPolygon::get_outline);
|
|
|
|
ObjectTypeDB::bind_method(_MD("remove_outline", "idx"), &NavigationPolygon::remove_outline);
|
|
|
|
ObjectTypeDB::bind_method(_MD("clear_outlines"), &NavigationPolygon::clear_outlines);
|
|
|
|
ObjectTypeDB::bind_method(_MD("make_polygons_from_outlines"), &NavigationPolygon::make_polygons_from_outlines);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_polygons", "polygons"), &NavigationPolygon::_set_polygons);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_polygons"), &NavigationPolygon::_get_polygons);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_set_outlines", "outlines"), &NavigationPolygon::_set_outlines);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_get_outlines"), &NavigationPolygon::_get_outlines);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3_ARRAY, "vertices", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("set_vertices"), _SCS("get_vertices"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_polygons"), _SCS("_get_polygons"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_outlines"), _SCS("_get_outlines"));
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NavigationPolygon::NavigationPolygon() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationPolygonInstance::set_enabled(bool p_enabled) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (enabled == p_enabled)
|
2015-02-14 15:09:52 +00:00
|
|
|
return;
|
2017-03-18 23:36:26 +00:00
|
|
|
enabled = p_enabled;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (nav_id != -1) {
|
2015-02-14 15:09:52 +00:00
|
|
|
navigation->navpoly_remove(nav_id);
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = -1;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (navigation) {
|
|
|
|
|
|
|
|
if (navpoly.is_valid()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = navigation->navpoly_create(navpoly, get_relative_transform_to_parent(navigation), this);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 16:03:46 +00:00
|
|
|
if (get_tree()->is_editor_hint() || get_tree()->is_debugging_navigation_hint())
|
2015-02-14 15:09:52 +00:00
|
|
|
update();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// update_gizmo();
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool NavigationPolygonInstance::is_enabled() const {
|
|
|
|
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////
|
|
|
|
|
|
|
|
void NavigationPolygonInstance::_notification(int p_what) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
switch (p_what) {
|
2015-02-14 15:09:52 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Node2D *c = this;
|
|
|
|
while (c) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
navigation = c->cast_to<Navigation2D>();
|
2015-02-14 15:09:52 +00:00
|
|
|
if (navigation) {
|
|
|
|
|
|
|
|
if (enabled && navpoly.is_valid()) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = navigation->navpoly_create(navpoly, get_relative_transform_to_parent(navigation), this);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
c = c->get_parent()->cast_to<Node2D>();
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (navigation && nav_id != -1) {
|
|
|
|
navigation->navpoly_set_transform(nav_id, get_relative_transform_to_parent(navigation));
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
|
|
|
|
if (navigation) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (nav_id != -1) {
|
2015-02-14 15:09:52 +00:00
|
|
|
navigation->navpoly_remove(nav_id);
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = -1;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
navigation = NULL;
|
2015-02-14 15:09:52 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
|
2015-09-20 16:03:46 +00:00
|
|
|
if (is_inside_tree() && (get_tree()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()) && navpoly.is_valid()) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
DVector<Vector2> verts = navpoly->get_vertices();
|
2015-02-14 15:09:52 +00:00
|
|
|
int vsize = verts.size();
|
2017-03-18 23:36:26 +00:00
|
|
|
if (vsize < 3)
|
2015-02-14 15:09:52 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
Color color;
|
|
|
|
if (enabled) {
|
2017-03-18 23:36:26 +00:00
|
|
|
color = get_tree()->get_debug_navigation_color();
|
2015-02-14 15:09:52 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
color = get_tree()->get_debug_navigation_disabled_color();
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
Vector<Color> colors;
|
|
|
|
Vector<Vector2> vertices;
|
|
|
|
vertices.resize(vsize);
|
|
|
|
colors.resize(vsize);
|
|
|
|
{
|
|
|
|
DVector<Vector2>::Read vr = verts.read();
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < vsize; i++) {
|
|
|
|
vertices[i] = vr[i];
|
|
|
|
colors[i] = color;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<int> indices;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < navpoly->get_polygon_count(); i++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
Vector<int> polygon = navpoly->get_polygon(i);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int j = 2; j < polygon.size(); j++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int kofs[3] = { 0, j - 1, j };
|
|
|
|
for (int k = 0; k < 3; k++) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int idx = polygon[kofs[k]];
|
|
|
|
ERR_FAIL_INDEX(idx, vsize);
|
2015-02-14 15:09:52 +00:00
|
|
|
indices.push_back(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, vertices, colors);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void NavigationPolygonInstance::set_navigation_polygon(const Ref<NavigationPolygon> &p_navpoly) {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_navpoly == navpoly)
|
2015-02-14 15:09:52 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (navigation && nav_id != -1) {
|
2015-02-14 15:09:52 +00:00
|
|
|
navigation->navpoly_remove(nav_id);
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = -1;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
if (navpoly.is_valid()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
navpoly->disconnect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
navpoly = p_navpoly;
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
if (navpoly.is_valid()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
navpoly->connect(CoreStringNames::get_singleton()->changed, this, "_navpoly_changed");
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (navigation && navpoly.is_valid() && enabled) {
|
2017-03-18 23:36:26 +00:00
|
|
|
nav_id = navigation->navpoly_create(navpoly, get_relative_transform_to_parent(navigation), this);
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
//update_gizmo();
|
|
|
|
_change_notify("navpoly");
|
2016-05-17 21:27:15 +00:00
|
|
|
update_configuration_warning();
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<NavigationPolygon> NavigationPolygonInstance::get_navigation_polygon() const {
|
2015-02-14 15:09:52 +00:00
|
|
|
|
|
|
|
return navpoly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavigationPolygonInstance::_navpoly_changed() {
|
|
|
|
|
2015-09-20 16:03:46 +00:00
|
|
|
if (is_inside_tree() && (get_tree()->is_editor_hint() || get_tree()->is_debugging_navigation_hint()))
|
2015-02-14 15:09:52 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-05-17 21:27:15 +00:00
|
|
|
String NavigationPolygonInstance::get_configuration_warning() const {
|
|
|
|
|
|
|
|
if (!is_visible() || !is_inside_tree())
|
|
|
|
return String();
|
|
|
|
|
|
|
|
if (!navpoly.is_valid()) {
|
|
|
|
return TTR("A NavigationPolygon resource must be set or created for this node to work. Please set a property or draw a polygon.");
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
const Node2D *c = this;
|
|
|
|
while (c) {
|
2016-05-17 21:27:15 +00:00
|
|
|
|
|
|
|
if (c->cast_to<Navigation2D>()) {
|
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
c = c->get_parent()->cast_to<Node2D>();
|
2016-05-17 21:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TTR("NavigationPolygonInstance must be a child or grandchild to a Navigation2D node. It only provides navigation data.");
|
|
|
|
}
|
|
|
|
|
2015-02-14 15:09:52 +00:00
|
|
|
void NavigationPolygonInstance::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_navigation_polygon", "navpoly:NavigationPolygon"), &NavigationPolygonInstance::set_navigation_polygon);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_navigation_polygon:NavigationPolygon"), &NavigationPolygonInstance::get_navigation_polygon);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_enabled", "enabled"), &NavigationPolygonInstance::set_enabled);
|
|
|
|
ObjectTypeDB::bind_method(_MD("is_enabled"), &NavigationPolygonInstance::is_enabled);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("_navpoly_changed"), &NavigationPolygonInstance::_navpoly_changed);
|
2015-02-14 15:09:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "navpoly", PROPERTY_HINT_RESOURCE_TYPE, "NavigationPolygon"), _SCS("set_navigation_polygon"), _SCS("get_navigation_polygon"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), _SCS("set_enabled"), _SCS("is_enabled"));
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NavigationPolygonInstance::NavigationPolygonInstance() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
navigation = NULL;
|
|
|
|
nav_id = -1;
|
|
|
|
enabled = true;
|
2015-02-14 15:09:52 +00:00
|
|
|
}
|