From e46f28e02dde08bb515fdd796ffcf114361e4877 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Tue, 8 Jan 2019 19:10:24 -0300 Subject: [PATCH] Removed splits in Polygon editor, replace by internal vertices and polygon support. --- core/math/geometry.cpp | 2 + core/math/geometry.h | 32 +- editor/icons/icon_edit_internal.svg | 67 +++ editor/icons/icon_editor_internal_handle.svg | 70 ++++ editor/icons/icon_remove_internal.svg | 67 +++ editor/plugins/polygon_2d_editor_plugin.cpp | 410 ++++++++++++------- editor/plugins/polygon_2d_editor_plugin.h | 14 +- scene/2d/polygon_2d.cpp | 318 +++++++++++--- scene/2d/polygon_2d.h | 10 +- 9 files changed, 766 insertions(+), 224 deletions(-) create mode 100644 editor/icons/icon_edit_internal.svg create mode 100644 editor/icons/icon_editor_internal_handle.svg create mode 100644 editor/icons/icon_remove_internal.svg diff --git a/core/math/geometry.cpp b/core/math/geometry.cpp index c51ffd7b0ad..12c88f43b31 100644 --- a/core/math/geometry.cpp +++ b/core/math/geometry.cpp @@ -32,6 +32,7 @@ #include "core/print_string.h" +/* this implementation is very inefficient, commenting unless bugs happen. See the other one. bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon) { Vector indices = Geometry::triangulate_polygon(p_polygon); @@ -42,6 +43,7 @@ bool Geometry::is_point_in_polygon(const Vector2 &p_point, const Vector } return false; } +*/ void Geometry::MeshData::optimize_vertices() { diff --git a/core/math/geometry.h b/core/math/geometry.h index 67805dd6c95..29493516b85 100644 --- a/core/math/geometry.h +++ b/core/math/geometry.h @@ -514,7 +514,7 @@ public: return (cn.cross(an) > 0) == orientation; } - static bool is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon); + //static bool is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon); static Vector2 get_closest_point_to_segment_uncapped_2d(const Vector2 &p_point, const Vector2 *p_segment) { @@ -815,6 +815,36 @@ public: return sum > 0.0f; } + /* alternate implementation that should be faster */ + static bool is_point_in_polygon(const Vector2 &p_point, const Vector &p_polygon) { + int c = p_polygon.size(); + if (c < 3) + return false; + const Vector2 *p = p_polygon.ptr(); + Vector2 further_away(-1e20, -1e20); + Vector2 further_away_opposite(1e20, 1e20); + + for (int i = 0; i < c; i++) { + further_away.x = MAX(p[i].x, further_away.x); + further_away.y = MAX(p[i].y, further_away.y); + further_away_opposite.x = MIN(p[i].x, further_away_opposite.x); + further_away_opposite.y = MIN(p[i].y, further_away_opposite.y); + } + + further_away += (further_away - further_away_opposite) * Vector2(1.221313, 1.512312); // make point outside that wont intersect with points in segment from p_point + + int intersections = 0; + for (int i = 0; i < c; i++) { + const Vector2 &v1 = p[i]; + const Vector2 &v2 = p[(i + 1) % c]; + if (segment_intersects_segment_2d(v1, v2, p_point, further_away, NULL)) { + intersections++; + } + } + + return (intersections & 1); + } + static PoolVector > separate_objects(PoolVector p_array); static PoolVector wrap_geometry(PoolVector p_array, real_t *p_error = NULL); ///< create a "wrap" that encloses the given geometry diff --git a/editor/icons/icon_edit_internal.svg b/editor/icons/icon_edit_internal.svg new file mode 100644 index 00000000000..4acb54ed82e --- /dev/null +++ b/editor/icons/icon_edit_internal.svg @@ -0,0 +1,67 @@ + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/editor/icons/icon_editor_internal_handle.svg b/editor/icons/icon_editor_internal_handle.svg new file mode 100644 index 00000000000..8f6698f2b23 --- /dev/null +++ b/editor/icons/icon_editor_internal_handle.svg @@ -0,0 +1,70 @@ + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/editor/icons/icon_remove_internal.svg b/editor/icons/icon_remove_internal.svg new file mode 100644 index 00000000000..392003e83b2 --- /dev/null +++ b/editor/icons/icon_remove_internal.svg @@ -0,0 +1,67 @@ + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/editor/plugins/polygon_2d_editor_plugin.cpp b/editor/plugins/polygon_2d_editor_plugin.cpp index 7879d2ac7f2..a2873fe7d72 100644 --- a/editor/plugins/polygon_2d_editor_plugin.cpp +++ b/editor/plugins/polygon_2d_editor_plugin.cpp @@ -52,6 +52,14 @@ Vector2 Polygon2DEditor::_get_offset(int p_idx) const { return node->get_offset(); } +int Polygon2DEditor::_get_polygon_count() const { + if (node->get_internal_vertex_count() > 0) { + return 0; //do not edit if internal vertices exist + } else { + return 1; + } +} + void Polygon2DEditor::_notification(int p_what) { switch (p_what) { @@ -66,13 +74,15 @@ void Polygon2DEditor::_notification(int p_what) { button_uv->set_icon(get_icon("Uv", "EditorIcons")); - uv_button[UV_MODE_CREATE]->set_icon(get_icon("Add", "EditorIcons")); + uv_button[UV_MODE_CREATE]->set_icon(get_icon("Edit", "EditorIcons")); + uv_button[UV_MODE_CREATE_INTERNAL]->set_icon(get_icon("EditInternal", "EditorIcons")); + uv_button[UV_MODE_REMOVE_INTERNAL]->set_icon(get_icon("RemoveInternal", "EditorIcons")); uv_button[UV_MODE_EDIT_POINT]->set_icon(get_icon("ToolSelect", "EditorIcons")); uv_button[UV_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons")); uv_button[UV_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons")); uv_button[UV_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons")); - uv_button[UV_MODE_ADD_SPLIT]->set_icon(get_icon("AddSplit", "EditorIcons")); - uv_button[UV_MODE_REMOVE_SPLIT]->set_icon(get_icon("DeleteSplit", "EditorIcons")); + uv_button[UV_MODE_ADD_POLYGON]->set_icon(get_icon("Edit", "EditorIcons")); + uv_button[UV_MODE_REMOVE_POLYGON]->set_icon(get_icon("Close", "EditorIcons")); uv_button[UV_MODE_PAINT_WEIGHT]->set_icon(get_icon("PaintVertex", "EditorIcons")); uv_button[UV_MODE_CLEAR_WEIGHT]->set_icon(get_icon("UnpaintVertex", "EditorIcons")); @@ -191,11 +201,13 @@ void Polygon2DEditor::_uv_edit_mode_select(int p_mode) { if (p_mode == 0) { //uv uv_button[UV_MODE_CREATE]->hide(); + uv_button[UV_MODE_CREATE_INTERNAL]->hide(); + uv_button[UV_MODE_REMOVE_INTERNAL]->hide(); for (int i = UV_MODE_MOVE; i <= UV_MODE_SCALE; i++) { uv_button[i]->show(); } - uv_button[UV_MODE_ADD_SPLIT]->hide(); - uv_button[UV_MODE_REMOVE_SPLIT]->hide(); + uv_button[UV_MODE_ADD_POLYGON]->hide(); + uv_button[UV_MODE_REMOVE_POLYGON]->hide(); uv_button[UV_MODE_PAINT_WEIGHT]->hide(); uv_button[UV_MODE_CLEAR_WEIGHT]->hide(); _uv_mode(UV_MODE_EDIT_POINT); @@ -209,8 +221,8 @@ void Polygon2DEditor::_uv_edit_mode_select(int p_mode) { for (int i = 0; i <= UV_MODE_SCALE; i++) { uv_button[i]->show(); } - uv_button[UV_MODE_ADD_SPLIT]->hide(); - uv_button[UV_MODE_REMOVE_SPLIT]->hide(); + uv_button[UV_MODE_ADD_POLYGON]->hide(); + uv_button[UV_MODE_REMOVE_POLYGON]->hide(); uv_button[UV_MODE_PAINT_WEIGHT]->hide(); uv_button[UV_MODE_CLEAR_WEIGHT]->hide(); _uv_mode(UV_MODE_EDIT_POINT); @@ -224,11 +236,11 @@ void Polygon2DEditor::_uv_edit_mode_select(int p_mode) { for (int i = 0; i <= UV_MODE_SCALE; i++) { uv_button[i]->hide(); } - uv_button[UV_MODE_ADD_SPLIT]->show(); - uv_button[UV_MODE_REMOVE_SPLIT]->show(); + uv_button[UV_MODE_ADD_POLYGON]->show(); + uv_button[UV_MODE_REMOVE_POLYGON]->show(); uv_button[UV_MODE_PAINT_WEIGHT]->hide(); uv_button[UV_MODE_CLEAR_WEIGHT]->hide(); - _uv_mode(UV_MODE_ADD_SPLIT); + _uv_mode(UV_MODE_ADD_POLYGON); bone_scroll_main_vb->hide(); bone_paint_strength->hide(); @@ -236,7 +248,7 @@ void Polygon2DEditor::_uv_edit_mode_select(int p_mode) { bone_paint_radius_label->hide(); } else if (p_mode == 3) { //bonesĀ“ - for (int i = 0; i <= UV_MODE_REMOVE_SPLIT; i++) { + for (int i = 0; i <= UV_MODE_REMOVE_POLYGON; i++) { uv_button[i]->hide(); } uv_button[UV_MODE_PAINT_WEIGHT]->show(); @@ -290,6 +302,7 @@ void Polygon2DEditor::_menu_option(int p_option) { uv_edit->popup(EditorSettings::get_singleton()->get("interface/dialogs/uv_editor_bounds")); else uv_edit->popup_centered_ratio(0.85); + _update_bone_list(); } break; case UVEDIT_POLYGON_TO_UV: { @@ -348,8 +361,10 @@ void Polygon2DEditor::_cancel_editing() { uv_create = false; node->set_uv(uv_create_uv_prev); node->set_polygon(uv_create_poly_prev); + node->set_internal_vertex_count(uv_create_prev_internal_vertices); + node->set_vertex_colors(uv_create_colors_prev); node->call("_set_bones", uv_create_bones_prev); - node->set_splits(splits_prev); + node->set_polygons(polygons_prev); } else if (uv_drag) { uv_drag = false; if (uv_edit_mode[0]->is_pressed()) { // Edit UV. @@ -357,9 +372,9 @@ void Polygon2DEditor::_cancel_editing() { } else if (uv_edit_mode[1]->is_pressed()) { // Edit polygon. node->set_polygon(points_prev); } - } else if (split_create) { - split_create = false; } + + polygon_create.clear(); } void Polygon2DEditor::_commit_action() { @@ -409,7 +424,7 @@ void Polygon2DEditor::_set_snap_step_y(float p_val) { void Polygon2DEditor::_uv_mode(int p_mode) { - split_create = false; + polygon_create.clear(); uv_drag = false; uv_create = false; @@ -461,8 +476,10 @@ void Polygon2DEditor::_uv_input(const Ref &p_input) { uv_create = true; uv_create_uv_prev = node->get_uv(); uv_create_poly_prev = node->get_polygon(); + uv_create_prev_internal_vertices = node->get_internal_vertex_count(); + uv_create_colors_prev = node->get_vertex_colors(); uv_create_bones_prev = node->call("_get_bones"); - splits_prev = node->get_splits(); + polygons_prev = node->get_polygons(); node->set_polygon(points_prev); node->set_uv(points_prev); @@ -477,6 +494,10 @@ void Polygon2DEditor::_uv_input(const Ref &p_input) { undo_redo->add_undo_method(node, "set_uv", uv_create_uv_prev); undo_redo->add_do_method(node, "set_polygon", node->get_polygon()); undo_redo->add_undo_method(node, "set_polygon", uv_create_poly_prev); + undo_redo->add_do_method(node, "set_internal_vertex_count", 0); + undo_redo->add_undo_method(node, "set_internal_vertex_count", uv_create_prev_internal_vertices); + undo_redo->add_do_method(node, "set_vertex_colors", Vector()); + undo_redo->add_undo_method(node, "set_vertex_colors", uv_create_colors_prev); undo_redo->add_do_method(node, "clear_bones"); undo_redo->add_undo_method(node, "_set_bones", uv_create_bones_prev); undo_redo->add_do_method(uv_edit_draw, "update"); @@ -499,6 +520,97 @@ void Polygon2DEditor::_uv_input(const Ref &p_input) { CanvasItemEditor::get_singleton()->update_viewport(); } + if (uv_move_current == UV_MODE_CREATE_INTERNAL) { + + uv_create_uv_prev = node->get_uv(); + uv_create_poly_prev = node->get_polygon(); + uv_create_colors_prev = node->get_vertex_colors(); + uv_create_bones_prev = node->call("_get_bones"); + int internal_vertices = node->get_internal_vertex_count(); + + Vector2 pos = mtx.affine_inverse().xform(snap_point(Vector2(mb->get_position().x, mb->get_position().y))); + + uv_create_poly_prev.push_back(pos); + uv_create_uv_prev.push_back(pos); + if (uv_create_colors_prev.size()) { + uv_create_colors_prev.push_back(Color(1, 1, 1)); + } + + undo_redo->create_action(TTR("Create Internal Vertex")); + undo_redo->add_do_method(node, "set_uv", uv_create_uv_prev); + undo_redo->add_undo_method(node, "set_uv", node->get_uv()); + undo_redo->add_do_method(node, "set_polygon", uv_create_poly_prev); + undo_redo->add_undo_method(node, "set_polygon", node->get_polygon()); + undo_redo->add_do_method(node, "set_vertex_colors", uv_create_colors_prev); + undo_redo->add_undo_method(node, "set_vertex_colors", node->get_vertex_colors()); + for (int i = 0; i < node->get_bone_count(); i++) { + PoolVector bonew = node->get_bone_weights(i); + bonew.push_back(0); + undo_redo->add_do_method(node, "set_bone_weights", i, bonew); + undo_redo->add_undo_method(node, "set_bone_weights", i, node->get_bone_weights(i)); + } + undo_redo->add_do_method(node, "set_internal_vertex_count", internal_vertices + 1); + undo_redo->add_undo_method(node, "set_internal_vertex_count", internal_vertices); + + undo_redo->add_do_method(uv_edit_draw, "update"); + undo_redo->add_undo_method(uv_edit_draw, "update"); + undo_redo->commit_action(); + } + + if (uv_move_current == UV_MODE_REMOVE_INTERNAL) { + + uv_create_uv_prev = node->get_uv(); + uv_create_poly_prev = node->get_polygon(); + uv_create_colors_prev = node->get_vertex_colors(); + uv_create_bones_prev = node->call("_get_bones"); + int internal_vertices = node->get_internal_vertex_count(); + + if (internal_vertices <= 0) + return; + + int closest = -1; + float closest_dist = 1e20; + + for (int i = points_prev.size() - internal_vertices; i < points_prev.size(); i++) { + + Vector2 tuv = mtx.xform(uv_create_poly_prev[i]); + float dist = tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)); + if (dist < 8 && dist < closest_dist) { + closest = i; + closest_dist = dist; + } + } + + if (closest == -1) + return; + + uv_create_poly_prev.remove(closest); + uv_create_uv_prev.remove(closest); + if (uv_create_colors_prev.size()) { + uv_create_colors_prev.remove(closest); + } + + undo_redo->create_action(TTR("Remove Internal Vertex")); + undo_redo->add_do_method(node, "set_uv", uv_create_uv_prev); + undo_redo->add_undo_method(node, "set_uv", node->get_uv()); + undo_redo->add_do_method(node, "set_polygon", uv_create_poly_prev); + undo_redo->add_undo_method(node, "set_polygon", node->get_polygon()); + undo_redo->add_do_method(node, "set_vertex_colors", uv_create_colors_prev); + undo_redo->add_undo_method(node, "set_vertex_colors", node->get_vertex_colors()); + for (int i = 0; i < node->get_bone_count(); i++) { + PoolVector bonew = node->get_bone_weights(i); + bonew.remove(closest); + undo_redo->add_do_method(node, "set_bone_weights", i, bonew); + undo_redo->add_undo_method(node, "set_bone_weights", i, node->get_bone_weights(i)); + } + undo_redo->add_do_method(node, "set_internal_vertex_count", internal_vertices - 1); + undo_redo->add_undo_method(node, "set_internal_vertex_count", internal_vertices); + + undo_redo->add_do_method(uv_edit_draw, "update"); + undo_redo->add_undo_method(uv_edit_draw, "update"); + undo_redo->commit_action(); + } + if (uv_move_current == UV_MODE_EDIT_POINT) { if (mb->get_shift() && mb->get_command()) @@ -526,129 +638,80 @@ void Polygon2DEditor::_uv_input(const Ref &p_input) { } } - if (uv_move_current == UV_MODE_ADD_SPLIT) { + if (uv_move_current == UV_MODE_ADD_POLYGON) { + + int closest = -1; + float closest_dist = 1e20; - int split_to_index = -1; - split_to_index = -1; for (int i = 0; i < points_prev.size(); i++) { Vector2 tuv = mtx.xform(points_prev[i]); - if (tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)) < 8) { - split_to_index = i; + float dist = tuv.distance_to(Vector2(mb->get_position().x, mb->get_position().y)); + if (dist < 8 && dist < closest_dist) { + closest = i; + closest_dist = dist; } } - if (split_to_index == -1) { - split_create = false; - return; - } - - if (split_create) { - - split_create = false; - if (split_to_index < point_drag_index) { - SWAP(split_to_index, point_drag_index); - } - bool valid = true; - String split_error; - if (split_to_index == point_drag_index) { - split_error = TTR("Split point with itself."); - valid = false; - } - if (split_to_index + 1 == point_drag_index) { - //not a split,goes along the edge - split_error = TTR("Split can't form an existing edge."); - valid = false; - } - if (split_to_index == points_prev.size() - 1 && point_drag_index == 0) { - //not a split,goes along the edge - split_error = TTR("Split can't form an existing edge."); - valid = false; - } - - for (int i = 0; i < splits_prev.size(); i += 2) { - - if (splits_prev[i] == point_drag_index && splits_prev[i + 1] == split_to_index) { - //already exists - split_error = TTR("Split already exists."); - valid = false; - break; - } - - int a_state; //-1, outside split, 0 split point, +1, inside split - if (point_drag_index == splits_prev[i] || point_drag_index == splits_prev[i + 1]) { - a_state = 0; - } else if (point_drag_index < splits_prev[i] || point_drag_index > splits_prev[i + 1]) { - a_state = -1; + if (closest != -1) { + if (polygon_create.size() && closest == polygon_create[0]) { + //close + if (polygon_create.size() < 3) { + error->set_text(TTR("Invalid Polygon (need 3 different vertices)")); + error->popup_centered_minsize(); } else { - a_state = 1; + Array polygons = node->get_polygons(); + polygons = polygons.duplicate(); //copy because its a reference + + //todo, could check whether it already exists? + polygons.push_back(polygon_create); + undo_redo->create_action(TTR("Add Polygon")); + undo_redo->add_do_method(node, "set_polygons", polygons); + undo_redo->add_undo_method(node, "set_polygons", polygons_prev); + undo_redo->add_do_method(uv_edit_draw, "update"); + undo_redo->add_undo_method(uv_edit_draw, "update"); + undo_redo->commit_action(); } - int b_state; //-1, outside split, 0 split point, +1, inside split - if (split_to_index == splits_prev[i] || split_to_index == splits_prev[i + 1]) { - b_state = 0; - } else if (split_to_index < splits_prev[i] || split_to_index > splits_prev[i + 1]) { - b_state = -1; - } else { - b_state = 1; - } - - if (b_state * a_state < 0) { - //crossing - split_error = "Split crosses another split."; - valid = false; - break; - } + polygon_create.clear(); + } else if (polygon_create.find(closest) == -1) { + //add temporarily if not exists + polygon_create.push_back(closest); } - - if (valid) { - - splits_prev.push_back(point_drag_index); - splits_prev.push_back(split_to_index); - - undo_redo->create_action(TTR("Add Split")); - undo_redo->add_do_method(node, "set_splits", splits_prev); - undo_redo->add_undo_method(node, "set_splits", node->get_splits()); - undo_redo->add_do_method(uv_edit_draw, "update"); - undo_redo->add_undo_method(uv_edit_draw, "update"); - undo_redo->commit_action(); - } else { - error->set_text(TTR("Invalid Split: ") + split_error); - error->popup_centered_minsize(); - } - - } else { - point_drag_index = split_to_index; - split_create = true; - splits_prev = node->get_splits(); - uv_create_to = mtx.affine_inverse().xform(Vector2(mb->get_position().x, mb->get_position().y)); } } - if (uv_move_current == UV_MODE_REMOVE_SPLIT) { + if (uv_move_current == UV_MODE_REMOVE_POLYGON) { + Array polygons = node->get_polygons(); + polygons = polygons.duplicate(); //copy because its a reference - splits_prev = node->get_splits(); - for (int i = 0; i < splits_prev.size(); i += 2) { - if (splits_prev[i] < 0 || splits_prev[i] >= points_prev.size()) - continue; - if (splits_prev[i + 1] < 0 || splits_prev[i] >= points_prev.size()) - continue; - Vector2 e[2] = { mtx.xform(points_prev[splits_prev[i]]), mtx.xform(points_prev[splits_prev[i + 1]]) }; - Vector2 mp = Vector2(mb->get_position().x, mb->get_position().y); - Vector2 cp = Geometry::get_closest_point_to_segment_2d(mp, e); - if (cp.distance_to(mp) < 8) { - splits_prev.remove(i); - splits_prev.remove(i); + int erase_index = -1; + for (int i = polygons.size() - 1; i >= 0; i--) { + PoolVector points = polygons[i]; + Vector polys; + polys.resize(points.size()); + for (int j = 0; j < polys.size(); j++) { + int idx = points[j]; + if (idx < 0 || idx >= points_prev.size()) + continue; + polys.write[j] = mtx.xform(points_prev[idx]); + } - undo_redo->create_action(TTR("Remove Split")); - undo_redo->add_do_method(node, "set_splits", splits_prev); - undo_redo->add_undo_method(node, "set_splits", node->get_splits()); - undo_redo->add_do_method(uv_edit_draw, "update"); - undo_redo->add_undo_method(uv_edit_draw, "update"); - undo_redo->commit_action(); + if (Geometry::is_point_in_polygon(Vector2(mb->get_position().x, mb->get_position().y), polys)) { + erase_index = i; break; } } + + if (erase_index != -1) { + polygons.remove(erase_index); + undo_redo->create_action(TTR("Remove Polygon")); + undo_redo->add_do_method(node, "set_polygons", polygons); + undo_redo->add_undo_method(node, "set_polygons", polygons_prev); + undo_redo->add_do_method(uv_edit_draw, "update"); + undo_redo->add_undo_method(uv_edit_draw, "update"); + undo_redo->commit_action(); + } } if (uv_move_current == UV_MODE_PAINT_WEIGHT || uv_move_current == UV_MODE_CLEAR_WEIGHT) { @@ -849,7 +912,7 @@ void Polygon2DEditor::_uv_input(const Ref &p_input) { uv_edit_draw->update(); CanvasItemEditor::get_singleton()->update_viewport(); - } else if (split_create) { + } else if (polygon_create.size()) { uv_create_to = mtx.affine_inverse().xform(Vector2(mm->get_position().x, mm->get_position().y)); uv_edit_draw->update(); } else if (uv_mode == UV_MODE_PAINT_WEIGHT || uv_mode == UV_MODE_CLEAR_WEIGHT) { @@ -930,6 +993,8 @@ void Polygon2DEditor::_uv_draw() { } } + Array polygons = node->get_polygons(); + PoolVector uvs; if (uv_edit_mode[0]->is_pressed()) { //edit uv uvs = node->get_uv(); @@ -956,17 +1021,54 @@ void Polygon2DEditor::_uv_draw() { } Ref handle = get_icon("EditorHandle", "EditorIcons"); + Ref internal_handle = get_icon("EditorInternalHandle", "EditorIcons"); Color poly_line_color = Color(0.9, 0.5, 0.5); + Color polygon_line_color = Color(0.5, 0.5, 0.9); + Vector polygon_fill_color; + { + Color pf = polygon_line_color; + pf.a *= 0.5; + polygon_fill_color.push_back(pf); + } Color prev_color = Color(0.5, 0.5, 0.5); Rect2 rect(Point2(), mtx.basis_xform(base_tex->get_size())); rect.expand_to(mtx.basis_xform(uv_edit_draw->get_size())); + int uv_draw_max = uvs.size(); + + for (int i = 0; i < polygons.size(); i++) { + + PoolVector points = polygons[i]; + Vector polypoints; + for (int i = 0; i < points.size(); i++) { + int next = (i + 1) % points.size(); + + int idx = points[i]; + int idx_next = points[next]; + if (idx < 0 || idx >= uvs.size()) + continue; + polypoints.push_back(mtx.xform(uvs[idx])); + + if (idx_next < 0 || idx_next >= uvs.size()) + continue; + uv_edit_draw->draw_line(mtx.xform(uvs[idx]), mtx.xform(uvs[idx_next]), polygon_line_color, 2 * EDSCALE); + } + if (points.size() >= 3) { + uv_edit_draw->draw_polygon(polypoints, polygon_fill_color); + } + } + + uv_draw_max -= node->get_internal_vertex_count(); + if (uv_draw_max < 0) { + uv_draw_max = 0; + } + for (int i = 0; i < uvs.size(); i++) { - int next = (i + 1) % uvs.size(); + int next = uv_draw_max > 0 ? (i + 1) % uv_draw_max : 0; - if (uv_drag && uv_move_current == UV_MODE_EDIT_POINT && EDITOR_DEF("editors/poly_editor/show_previous_outline", true)) { + if (i < uv_draw_max && uv_drag && uv_move_current == UV_MODE_EDIT_POINT && EDITOR_DEF("editors/poly_editor/show_previous_outline", true)) { uv_edit_draw->draw_line(mtx.xform(points_prev[i]), mtx.xform(points_prev[next]), prev_color, 2 * EDSCALE); } @@ -974,24 +1076,33 @@ void Polygon2DEditor::_uv_draw() { if (uv_create && i == uvs.size() - 1) { next_point = uv_create_to; } - uv_edit_draw->draw_line(mtx.xform(uvs[i]), mtx.xform(next_point), poly_line_color, 2 * EDSCALE); + if (i < uv_draw_max && polygons.size() == 0 && polygon_create.size() == 0) { //if using or creating polygons, do not show outline (will show polygons instead) + uv_edit_draw->draw_line(mtx.xform(uvs[i]), mtx.xform(next_point), poly_line_color, 2 * EDSCALE); + } if (weight_r.ptr()) { Vector2 draw_pos = mtx.xform(uvs[i]); float weight = weight_r[i]; uv_edit_draw->draw_rect(Rect2(draw_pos - Vector2(2, 2) * EDSCALE, Vector2(5, 5) * EDSCALE), Color(weight, weight, weight, 1.0)); } else { - uv_edit_draw->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5); + if (i < uv_draw_max) { + uv_edit_draw->draw_texture(handle, mtx.xform(uvs[i]) - handle->get_size() * 0.5); + } else { + uv_edit_draw->draw_texture(internal_handle, mtx.xform(uvs[i]) - internal_handle->get_size() * 0.5); + } } rect.expand_to(mtx.basis_xform(uvs[i])); } - if (split_create) { - Vector2 from = uvs[point_drag_index]; - Vector2 to = uv_create_to; - uv_edit_draw->draw_line(mtx.xform(from), mtx.xform(to), poly_line_color, 2); + if (polygon_create.size()) { + for (int i = 0; i < polygon_create.size(); i++) { + Vector2 from = uvs[polygon_create[i]]; + Vector2 to = (i + 1) < polygon_create.size() ? uvs[polygon_create[i + 1]] : uv_create_to; + uv_edit_draw->draw_line(mtx.xform(from), mtx.xform(to), poly_line_color, 2); + } } +#if 0 PoolVector splits = node->get_splits(); for (int i = 0; i < splits.size(); i += 2) { @@ -1001,7 +1112,7 @@ void Polygon2DEditor::_uv_draw() { continue; uv_edit_draw->draw_line(mtx.xform(uvs[idx_from]), mtx.xform(uvs[idx_to]), poly_line_color, 2); } - +#endif if (uv_mode == UV_MODE_PAINT_WEIGHT || uv_mode == UV_MODE_CLEAR_WEIGHT) { NodePath bone_path; @@ -1155,8 +1266,8 @@ Polygon2DEditor::Polygon2DEditor(EditorNode *p_editor) : uv_edit_mode[0]->set_text(TTR("UV")); uv_edit_mode[0]->set_pressed(true); - uv_edit_mode[1]->set_text(TTR("Poly")); - uv_edit_mode[2]->set_text(TTR("Splits")); + uv_edit_mode[1]->set_text(TTR("Points")); + uv_edit_mode[2]->set_text(TTR("Polygons")); uv_edit_mode[3]->set_text(TTR("Bones")); uv_edit_mode[0]->set_button_group(uv_edit_group); @@ -1181,22 +1292,26 @@ Polygon2DEditor::Polygon2DEditor(EditorNode *p_editor) : uv_button[i]->set_focus_mode(FOCUS_NONE); } - uv_button[0]->set_tooltip(TTR("Create Polygon")); - uv_button[1]->set_tooltip(TTR("Move Points") + "\n" + TTR("Ctrl: Rotate") + "\n" + TTR("Shift: Move All") + "\n" + TTR("Shift+Ctrl: Scale")); - uv_button[2]->set_tooltip(TTR("Move Polygon")); - uv_button[3]->set_tooltip(TTR("Rotate Polygon")); - uv_button[4]->set_tooltip(TTR("Scale Polygon")); - uv_button[5]->set_tooltip(TTR("Connect two points to make a split.")); - uv_button[6]->set_tooltip(TTR("Select a split to erase it.")); - uv_button[7]->set_tooltip(TTR("Paint weights with specified intensity.")); - uv_button[8]->set_tooltip(TTR("Unpaint weights with specified intensity.")); + uv_button[UV_MODE_CREATE]->set_tooltip(TTR("Create Polygon")); + uv_button[UV_MODE_CREATE_INTERNAL]->set_tooltip(TTR("Create Internal Vertex")); + uv_button[UV_MODE_REMOVE_INTERNAL]->set_tooltip(TTR("Erase Internal Vertex")); + uv_button[UV_MODE_EDIT_POINT]->set_tooltip(TTR("Move Points") + "\n" + TTR("Ctrl: Rotate") + "\n" + TTR("Shift: Move All") + "\n" + TTR("Shift+Ctrl: Scale")); + uv_button[UV_MODE_MOVE]->set_tooltip(TTR("Move Polygon")); + uv_button[UV_MODE_ROTATE]->set_tooltip(TTR("Rotate Polygon")); + uv_button[UV_MODE_SCALE]->set_tooltip(TTR("Scale Polygon")); + uv_button[UV_MODE_ADD_POLYGON]->set_tooltip(TTR("Create a custom polygon. Enables custom polygon rendering.")); + uv_button[UV_MODE_REMOVE_POLYGON]->set_tooltip(TTR("Remove a custom polygon. If none remain, custom polygon rendering is disabled.")); + uv_button[UV_MODE_PAINT_WEIGHT]->set_tooltip(TTR("Paint weights with specified intensity.")); + uv_button[UV_MODE_CLEAR_WEIGHT]->set_tooltip(TTR("Unpaint weights with specified intensity.")); - uv_button[0]->hide(); - uv_button[5]->hide(); - uv_button[6]->hide(); - uv_button[7]->hide(); - uv_button[8]->hide(); - uv_button[1]->set_pressed(true); + uv_button[UV_MODE_CREATE]->hide(); + uv_button[UV_MODE_CREATE_INTERNAL]->hide(); + uv_button[UV_MODE_REMOVE_INTERNAL]->hide(); + uv_button[UV_MODE_ADD_POLYGON]->hide(); + uv_button[UV_MODE_REMOVE_POLYGON]->hide(); + uv_button[UV_MODE_PAINT_WEIGHT]->hide(); + uv_button[UV_MODE_CLEAR_WEIGHT]->hide(); + uv_button[UV_MODE_EDIT_POINT]->set_pressed(true); bone_paint_strength = memnew(HSlider); uv_mode_hb->add_child(bone_paint_strength); @@ -1354,7 +1469,6 @@ Polygon2DEditor::Polygon2DEditor(EditorNode *p_editor) : uv_drag = false; uv_create = false; updating_uv_scroll = false; - split_create = false; bone_painting = false; error = memnew(AcceptDialog); diff --git a/editor/plugins/polygon_2d_editor_plugin.h b/editor/plugins/polygon_2d_editor_plugin.h index cc596da7359..d1849dd09b7 100644 --- a/editor/plugins/polygon_2d_editor_plugin.h +++ b/editor/plugins/polygon_2d_editor_plugin.h @@ -50,12 +50,14 @@ class Polygon2DEditor : public AbstractPolygon2DEditor { enum UVMode { UV_MODE_CREATE, + UV_MODE_CREATE_INTERNAL, + UV_MODE_REMOVE_INTERNAL, UV_MODE_EDIT_POINT, UV_MODE_MOVE, UV_MODE_ROTATE, UV_MODE_SCALE, - UV_MODE_ADD_SPLIT, - UV_MODE_REMOVE_SPLIT, + UV_MODE_ADD_POLYGON, + UV_MODE_REMOVE_POLYGON, UV_MODE_PAINT_WEIGHT, UV_MODE_CLEAR_WEIGHT, UV_MODE_MAX @@ -100,14 +102,16 @@ class Polygon2DEditor : public AbstractPolygon2DEditor { PoolVector points_prev; PoolVector uv_create_uv_prev; PoolVector uv_create_poly_prev; + PoolVector uv_create_colors_prev; + int uv_create_prev_internal_vertices; Array uv_create_bones_prev; - PoolVector splits_prev; + Array polygons_prev; Vector2 uv_create_to; int point_drag_index; bool uv_drag; bool uv_create; - bool split_create; + Vector polygon_create; UVMode uv_move_current; Vector2 uv_drag_from; bool updating_uv_scroll; @@ -141,6 +145,8 @@ class Polygon2DEditor : public AbstractPolygon2DEditor { void _uv_edit_popup_hide(); void _bone_paint_selected(int p_index); + int _get_polygon_count() const; + protected: virtual Node2D *_get_node() const; virtual void _set_node(Node *p_polygon); diff --git a/scene/2d/polygon_2d.cpp b/scene/2d/polygon_2d.cpp index b9f7c4972aa..bfa82fa12e7 100644 --- a/scene/2d/polygon_2d.cpp +++ b/scene/2d/polygon_2d.cpp @@ -81,7 +81,11 @@ bool Polygon2D::_edit_use_rect() const { bool Polygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const { - return Geometry::is_point_in_polygon(p_point - get_offset(), Variant(polygon)); + Vector polygon2d = Variant(polygon); + if (internal_vertices > 0) { + polygon2d.resize(polygon2d.size() - internal_vertices); + } + return Geometry::is_point_in_polygon(p_point - get_offset(), polygon2d); } void Polygon2D::_notification(int p_what) { @@ -108,9 +112,17 @@ void Polygon2D::_notification(int p_what) { Vector bones; Vector weights; - points.resize(polygon.size()); + int len = polygon.size(); + if ((invert || polygons.size() == 0) && internal_vertices > 0) { + //if no polygons are around, internal vertices must not be drawn, else let them be + len -= internal_vertices; + } + + if (len <= 0) { + return; + } + points.resize(len); - int len = points.size(); { PoolVector::Read polyr = polygon.read(); @@ -177,7 +189,8 @@ void Polygon2D::_notification(int p_what) { Transform2D texmat(tex_rot, tex_ofs); texmat.scale(tex_scale); Size2 tex_size = texture->get_size(); - uvs.resize(points.size()); + + uvs.resize(len); if (points.size() == uv.size()) { @@ -196,7 +209,7 @@ void Polygon2D::_notification(int p_what) { if (skeleton_node && !invert && bone_weights.size()) { //a skeleton is set! fill indices and weights - int vc = points.size(); + int vc = len; bones.resize(vc * 4); weights.resize(vc * 4); @@ -211,12 +224,15 @@ void Polygon2D::_notification(int p_what) { for (int i = 0; i < bone_weights.size(); i++) { if (bone_weights[i].weights.size() != points.size()) { continue; //different number of vertices, sorry not using. + print_line("wrong weight size"); } if (!skeleton_node->has_node(bone_weights[i].path)) { + print_line("no node"); continue; //node does not exist } Bone2D *bone = Object::cast_to(skeleton_node->get_node(bone_weights[i].path)); if (!bone) { + print_line("no bone"); continue; } @@ -258,93 +274,245 @@ void Polygon2D::_notification(int p_what) { } Vector colors; - int color_len = vertex_colors.size(); - colors.resize(len); - { + if (vertex_colors.size() == points.size()) { + colors.resize(len); PoolVector::Read color_r = vertex_colors.read(); - for (int i = 0; i < color_len && i < len; i++) { + for (int i = 0; i < len; i++) { colors.write[i] = color_r[i]; } - for (int i = color_len; i < len; i++) { - colors.write[i] = color; - } + } else { + colors.push_back(color); } // Vector indices = Geometry::triangulate_polygon(points); // VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, texture.is_valid() ? texture->get_rid() : RID()); - if (invert || splits.size() == 0) { + if (invert || polygons.size() == 0) { Vector indices = Geometry::triangulate_polygon(points); VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); } else { + //draw individual polygons + Vector total_indices; + for (int i = 0; i < polygons.size(); i++) { + PoolVector src_indices = polygons[i]; + int ic = src_indices.size(); + if (ic < 3) + continue; + PoolVector::Read r = src_indices.read(); + + Vector tmp_points; + tmp_points.resize(ic); + + for (int j = 0; j < ic; j++) { + int idx = r[j]; + ERR_CONTINUE(idx < 0 || idx >= points.size()); + tmp_points.write[j] = points[r[j]]; + } + Vector indices = Geometry::triangulate_polygon(tmp_points); + int ic2 = indices.size(); + const int *r2 = indices.ptr(); + + int bic = total_indices.size(); + total_indices.resize(bic + ic2); + int *w2 = total_indices.ptrw(); + + for (int j = 0; j < ic2; j++) { + w2[j + bic] = r[r2[j]]; + } + } + + if (total_indices.size()) { + VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), total_indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); + } + +#if 0 //use splits Vector loop; int sc = splits.size(); PoolVector::Read r = splits.read(); - int last = points.size(); + + print_line("has splits, amount " + itos(splits.size())); Vector > loops; - for (int i = 0; i < last; i++) { + // find a point that can be used to begin, must not be in a split, and have to the left and right the same one + // like this one -> x---o + // \ / \ . + // o---o + int base_point = -1; + { + int current_point = -1; + int base_point_prev_split = -1; - int split; - int min_end = -1; - do { + for (int i = 0; i < points.size(); i++) { - loop.push_back(i); - - split = -1; - int end = -1; + //find if this point is in a split + int split_index = -1; + bool has_prev_split = false; + int min_dist_to_end = 0x7FFFFFFF; for (int j = 0; j < sc; j += 2) { - if (r[j + 1] >= last) - continue; //no longer valid - if (min_end != -1 && r[j + 1] >= min_end) - continue; - if (r[j] == i) { - if (split == -1 || r[j + 1] > end) { - split = r[j]; - end = r[j + 1]; - } + + int split_pos = -1; + int split_end = -1; + + if (r[j + 0] == i) { //found split in first point + split_pos = r[j + 0]; + split_end = r[j + 1]; + } else if (r[j + 1] == i) { //found split in second point + split_pos = r[j + 1]; + split_end = r[j + 0]; + } + + if (split_pos == split_end) { + continue; //either nothing found or begin == end, this not a split in either case + } + + if (j == base_point_prev_split) { + has_prev_split = true; + } + + //compute distance from split pos to split end in current traversal direction + int dist_to_end = split_end > split_pos ? split_end - split_pos : (last - split_pos + split_end); + + if (dist_to_end < min_dist_to_end) { + //always keep the valid split with the least distance to the loop + min_dist_to_end = dist_to_end; + split_index = j; } } - if (split != -1) { - for (int j = end; j < last; j++) { - loop.push_back(j); - } - loops.push_back(loop); - last = end + 1; - loop.clear(); - min_end = end; //avoid this split from repeating + if (split_index == -1) { + current_point = i; //no split here, we are testing this point + } else if (has_prev_split) { + base_point = current_point; // there is a split and it contains the previous visited split, success + break; + } else { + //invalidate current point and keep split + current_point = -1; + base_point_prev_split = split_index; } - - } while (split != -1); + } } - if (loop.size()) { + print_line("found base point: " + itos(base_point)); + + if (base_point != -1) { + + int point = base_point; + int last = base_point; + //go through all the points, find splits + do { + + int split; + int last_dist_to_end = -1; //maximum valid distance to end + + do { + + loop.push_back(point); //push current point + + split = -1; + int end = -1; + + int max_dist_to_end = 0; + + //find if this point is in a split + for (int j = 0; j < sc; j += 2) { + + int split_pos = -1; + int split_end = -1; + + if (r[j + 0] == point) { //match first split index + split_pos = r[j + 0]; + split_end = r[j + 1]; + } else if (r[j + 1] == point) { //match second split index + split_pos = r[j + 1]; + split_end = r[j + 0]; + } + + if (split_pos == split_end) { + continue; //either nothing found or begin == end, this not a split in either case + } + + //compute distance from split pos to split end + int dist_to_end = split_end > split_pos ? split_end - split_pos : (points.size() - split_pos + split_end); + + if (last_dist_to_end != -1 && dist_to_end >= last_dist_to_end) { + //distance must be shorter than in last iteration, means we've tested this before so ignore + continue; + } else if (dist_to_end > max_dist_to_end) { + //always keep the valid point with the most distance (as long as it's valid) + max_dist_to_end = dist_to_end; + split = split_pos; + end = split_end; + } + } + + if (split != -1) { + //found a split! + int from = end; + + //add points until last is reached + while (true) { + //find if point is in a split + loop.push_back(from); + + if (from == last) { + break; + } + + from++; + if (from >= points.size()) { //wrap if reached end + from = 0; + } + + if (from == loop[0]) { + break; //end because we reached split source + } + } + + loops.push_back(loop); //done with this loop + loop.clear(); + + last_dist_to_end = max_dist_to_end; + last = end; //algorithm can safely finish in this split point + } + + } while (split != -1); + + } while (point != last); + } + + if (loop.size() >=2 ) { //points remained + //points remain + loop.push_back(last); //no splits found, use last loops.push_back(loop); } - Vector indices; + print_line("total loops: " + itos(loops.size())); - for (int i = 0; i < loops.size(); i++) { - Vector loop = loops[i]; - Vector vertices; - vertices.resize(loop.size()); - for (int j = 0; j < vertices.size(); j++) { - vertices.write[j] = points[loop[j]]; - } - Vector sub_indices = Geometry::triangulate_polygon(vertices); - int from = indices.size(); - indices.resize(from + sub_indices.size()); - for (int j = 0; j < sub_indices.size(); j++) { - indices.write[from + j] = loop[sub_indices[j]]; + if (loops.size()) { //loops found + Vector indices; + + for (int i = 0; i < loops.size(); i++) { + Vector loop = loops[i]; + Vector vertices; + vertices.resize(loop.size()); + for (int j = 0; j < vertices.size(); j++) { + vertices.write[j] = points[loop[j]]; + } + Vector sub_indices = Geometry::triangulate_polygon(vertices); + int from = indices.size(); + indices.resize(from + sub_indices.size()); + for (int j = 0; j < sub_indices.size(); j++) { + indices.write[from + j] = loop[sub_indices[j]]; + } } + + VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); } - - VS::get_singleton()->canvas_item_add_triangle_array(get_canvas_item(), indices, points, colors, uvs, bones, weights, texture.is_valid() ? texture->get_rid() : RID()); +#endif } } break; @@ -362,6 +530,15 @@ PoolVector Polygon2D::get_polygon() const { return polygon; } +void Polygon2D::set_internal_vertex_count(int p_count) { + + internal_vertices = p_count; +} + +int Polygon2D::get_internal_vertex_count() const { + return internal_vertices; +} + void Polygon2D::set_uv(const PoolVector &p_uv) { uv = p_uv; @@ -373,16 +550,15 @@ PoolVector Polygon2D::get_uv() const { return uv; } -void Polygon2D::set_splits(const PoolVector &p_splits) { +void Polygon2D::set_polygons(const Array &p_polygons) { - ERR_FAIL_COND(p_splits.size() & 1); //splits should be multiple of 2 - splits = p_splits; + polygons = p_polygons; update(); } -PoolVector Polygon2D::get_splits() const { +Array Polygon2D::get_polygons() const { - return splits; + return polygons; } void Polygon2D::set_color(const Color &p_color) { @@ -585,8 +761,8 @@ void Polygon2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_color", "color"), &Polygon2D::set_color); ClassDB::bind_method(D_METHOD("get_color"), &Polygon2D::get_color); - ClassDB::bind_method(D_METHOD("set_splits", "splits"), &Polygon2D::set_splits); - ClassDB::bind_method(D_METHOD("get_splits"), &Polygon2D::get_splits); + ClassDB::bind_method(D_METHOD("set_polygons", "polygons"), &Polygon2D::set_polygons); + ClassDB::bind_method(D_METHOD("get_polygons"), &Polygon2D::get_polygons); ClassDB::bind_method(D_METHOD("set_vertex_colors", "vertex_colors"), &Polygon2D::set_vertex_colors); ClassDB::bind_method(D_METHOD("get_vertex_colors"), &Polygon2D::get_vertex_colors); @@ -630,14 +806,13 @@ void Polygon2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_skeleton", "skeleton"), &Polygon2D::set_skeleton); ClassDB::bind_method(D_METHOD("get_skeleton"), &Polygon2D::get_skeleton); + ClassDB::bind_method(D_METHOD("set_internal_vertex_count", "internal_vertex_count"), &Polygon2D::set_internal_vertex_count); + ClassDB::bind_method(D_METHOD("get_internal_vertex_count"), &Polygon2D::get_internal_vertex_count); + ClassDB::bind_method(D_METHOD("_set_bones", "bones"), &Polygon2D::_set_bones); ClassDB::bind_method(D_METHOD("_get_bones"), &Polygon2D::_get_bones); - ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon"); - ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv"); - ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "splits"), "set_splits", "get_splits"); ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color"); - ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "antialiased"), "set_antialiased", "get_antialiased"); ADD_GROUP("Texture", ""); @@ -654,7 +829,13 @@ void Polygon2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "invert_enable"), "set_invert", "get_invert"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "invert_border", PROPERTY_HINT_RANGE, "0.1,16384,0.1"), "set_invert_border", "get_invert_border"); + ADD_GROUP("Data", ""); + ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "uv"), "set_uv", "get_uv"); + ADD_PROPERTY(PropertyInfo(Variant::POOL_COLOR_ARRAY, "vertex_colors"), "set_vertex_colors", "get_vertex_colors"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "polygons"), "set_polygons", "get_polygons"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bones", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_bones", "_get_bones"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "internal_vertex_count", PROPERTY_HINT_RANGE, "0,1000"), "set_internal_vertex_count", "get_internal_vertex_count"); } Polygon2D::Polygon2D() { @@ -667,4 +848,5 @@ Polygon2D::Polygon2D() { tex_scale = Vector2(1, 1); color = Color(1, 1, 1); rect_cache_dirty = true; + internal_vertices = 0; } diff --git a/scene/2d/polygon_2d.h b/scene/2d/polygon_2d.h index b4b87358a08..c1d6ebe46e5 100644 --- a/scene/2d/polygon_2d.h +++ b/scene/2d/polygon_2d.h @@ -40,7 +40,8 @@ class Polygon2D : public Node2D { PoolVector polygon; PoolVector uv; PoolVector vertex_colors; - PoolVector splits; + Array polygons; + int internal_vertices; struct Bone { NodePath path; @@ -87,11 +88,14 @@ public: void set_polygon(const PoolVector &p_polygon); PoolVector get_polygon() const; + void set_internal_vertex_count(int p_count); + int get_internal_vertex_count() const; + void set_uv(const PoolVector &p_uv); PoolVector get_uv() const; - void set_splits(const PoolVector &p_uv); - PoolVector get_splits() const; + void set_polygons(const Array &p_polygons); + Array get_polygons() const; void set_color(const Color &p_color); Color get_color() const;