Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.5] Fix broken TileSet Navigation due to wrong NavigationPolygon format #61266

Merged
merged 1 commit into from
May 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions editor/plugins/tile_set_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1665,16 +1665,20 @@ void TileSetEditor::_on_workspace_input(const Ref<InputEvent> &p_ie) {

w.release();

undo_redo->create_action(TTR("Edit Navigation Polygon"));
undo_redo->add_do_method(edited_navigation_shape.ptr(), "set_vertices", polygon);
undo_redo->add_undo_method(edited_navigation_shape.ptr(), "set_vertices", edited_navigation_shape->get_vertices());
undo_redo->add_do_method(edited_navigation_shape.ptr(), "clear_polygons");
undo_redo->add_undo_method(edited_navigation_shape.ptr(), "clear_polygons");
undo_redo->add_do_method(edited_navigation_shape.ptr(), "add_polygon", indices);
undo_redo->add_undo_method(edited_navigation_shape.ptr(), "add_polygon", edited_navigation_shape->get_polygon(0));
undo_redo->add_do_method(this, "_select_edited_shape_coord");
undo_redo->add_undo_method(this, "_select_edited_shape_coord");
undo_redo->commit_action();
edited_navigation_shape->clear_outlines();
edited_navigation_shape->add_outline(polygon);
edited_navigation_shape->make_polygons_from_outlines();
// FIXME Couldn't figure out the undo_redo quagmire
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @KoBeWi can help?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the exact methods, but probably something like

undo_redo->create_action(TTR("Edit Navigation Polygon"));
undo_redo->add_do_method(edited_navigation_shape.ptr(), "clear_outlines");
undo_redo->add_do_method(edited_navigation_shape.ptr(), "add_outline", polygon);
undo_redo->add_do_method(edited_navigation_shape.ptr(), "make_polygons_from_outlines");
undo_redo->add_undo_method(edited_navigation_shape.ptr(), "clear_outlines");
for (int i = 0; i < edited_navigation_shape->get_polygon_count(); i++) {
    undo_redo->add_undo_method(edited_navigation_shape.ptr(), "add_polygon", edited_navigation_shape->get_polygon(i));
}
undo_redo->commit_action();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I tested this but it seems something is still missing. It does not go back to previous state on undo only visually deletes it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code looks similar to that one, maybe it can do something similar?

w.release();
shape->clear_outlines();
shape->add_outline(polygon);
shape->make_polygons_from_outlines();
undo_redo->create_action(TTR("Create Navigation Polygon"));
if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
undo_redo->add_do_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), shape, edited_shape_coord);
undo_redo->add_undo_method(tileset.ptr(), "autotile_set_navigation_polygon", get_current_tile(), tileset->autotile_get_navigation_polygon(get_current_tile(), edited_shape_coord), edited_shape_coord);
} else {
undo_redo->add_do_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), shape);
undo_redo->add_undo_method(tileset.ptr(), "tile_set_navigation_polygon", get_current_tile(), tileset->tile_get_navigation_polygon(get_current_tile()));
}
tools[TOOL_SELECT]->set_pressed(true);
undo_redo->add_do_method(this, "_select_edited_shape_coord");
undo_redo->add_undo_method(this, "_select_edited_shape_coord");
undo_redo->commit_action();

Notably, it looks like the methods called on edited_navigation_shape don't need to be in the undo redo, it's edited_navigation_shape itself which is used in the do methods.

//undo_redo->create_action(TTR("Edit Navigation Polygon"));
//undo_redo->add_do_method(edited_navigation_shape.ptr(), "set_vertices", polygon);
//undo_redo->add_undo_method(edited_navigation_shape.ptr(), "set_vertices", edited_navigation_shape->get_vertices());
//undo_redo->add_do_method(edited_navigation_shape.ptr(), "clear_polygons");
//undo_redo->add_undo_method(edited_navigation_shape.ptr(), "clear_polygons");
//undo_redo->add_do_method(edited_navigation_shape.ptr(), "add_polygon", indices);
//undo_redo->add_undo_method(edited_navigation_shape.ptr(), "add_polygon", edited_navigation_shape->get_polygon(0));
//undo_redo->add_do_method(this, "_select_edited_shape_coord");
//undo_redo->add_undo_method(this, "_select_edited_shape_coord");
//undo_redo->commit_action();
}
}
}
Expand Down Expand Up @@ -2808,11 +2812,14 @@ void TileSetEditor::draw_polygon_shapes() {
colors.push_back(c_bg);
}
} else {
PoolVector<Vector2> vertices = shape->get_vertices();
for (int j = 0; j < shape->get_polygon(0).size(); j++) {
polygon.push_back(vertices[shape->get_polygon(0)[j]] + anchor);
colors.push_back(c_bg);
for (int outline_idx = 0; outline_idx < shape->get_outline_count(); outline_idx++) {
PoolVector<Vector2> outline_vertices = shape->get_outline(outline_idx);
for (int vertex_idx = 0; vertex_idx < outline_vertices.size(); vertex_idx++) {
polygon.push_back(outline_vertices[vertex_idx] + anchor);
}
}
colors.resize(polygon.size());
colors.fill(c_bg);
}
workspace->draw_polygon(polygon, colors);

Expand Down Expand Up @@ -2856,11 +2863,14 @@ void TileSetEditor::draw_polygon_shapes() {
colors.push_back(c_bg);
}
} else {
PoolVector<Vector2> vertices = shape->get_vertices();
for (int j = 0; j < shape->get_polygon(0).size(); j++) {
polygon.push_back(vertices[shape->get_polygon(0)[j]] + anchor);
colors.push_back(c_bg);
for (int outline_idx = 0; outline_idx < shape->get_outline_count(); outline_idx++) {
PoolVector<Vector2> outline_vertices = shape->get_outline(outline_idx);
for (int vertex_idx = 0; vertex_idx < outline_vertices.size(); vertex_idx++) {
polygon.push_back(outline_vertices[vertex_idx] + anchor);
}
}
colors.resize(polygon.size());
colors.fill(c_bg);
}
workspace->draw_polygon(polygon, colors);

Expand Down Expand Up @@ -2982,8 +2992,9 @@ void TileSetEditor::close_shape(const Vector2 &shape_anchor) {
}

w.release();
shape->set_vertices(polygon);
shape->add_polygon(indices);
shape->clear_outlines();
shape->add_outline(polygon);
shape->make_polygons_from_outlines();

undo_redo->create_action(TTR("Create Navigation Polygon"));
if (tileset->tile_get_tile_mode(get_current_tile()) == TileSet::AUTO_TILE || tileset->tile_get_tile_mode(get_current_tile()) == TileSet::ATLAS_TILE) {
Expand Down Expand Up @@ -3037,10 +3048,10 @@ void TileSetEditor::select_coord(const Vector2 &coord) {
} else if (edit_mode == EDITMODE_NAVIGATION) {
current_shape.resize(0);
if (edited_navigation_shape.is_valid()) {
if (edited_navigation_shape->get_polygon_count() > 0) {
PoolVector<Vector2> vertices = edited_navigation_shape->get_vertices();
for (int i = 0; i < edited_navigation_shape->get_polygon(0).size(); i++) {
current_shape.push_back(vertices[edited_navigation_shape->get_polygon(0)[i]] + current_tile_region.position);
for (int outline_idx = 0; outline_idx < edited_navigation_shape->get_outline_count(); outline_idx++) {
PoolVector<Vector2> outline_vertices = edited_navigation_shape->get_outline(outline_idx);
for (int vertex_inx = 0; vertex_inx < outline_vertices.size(); vertex_inx++) {
current_shape.push_back(outline_vertices[vertex_inx] + current_tile_region.position);
}
}
}
Expand Down Expand Up @@ -3090,10 +3101,10 @@ void TileSetEditor::select_coord(const Vector2 &coord) {
} else if (edit_mode == EDITMODE_NAVIGATION) {
current_shape.resize(0);
if (edited_navigation_shape.is_valid()) {
if (edited_navigation_shape->get_polygon_count() > 0) {
PoolVector<Vector2> vertices = edited_navigation_shape->get_vertices();
for (int i = 0; i < edited_navigation_shape->get_polygon(0).size(); i++) {
current_shape.push_back(vertices[edited_navigation_shape->get_polygon(0)[i]] + shape_anchor);
for (int outline_idx = 0; outline_idx < edited_navigation_shape->get_outline_count(); outline_idx++) {
PoolVector<Vector2> outline_vertices = edited_navigation_shape->get_outline(outline_idx);
for (int vertex_inx = 0; vertex_inx < outline_vertices.size(); vertex_inx++) {
current_shape.push_back(outline_vertices[vertex_inx] + shape_anchor);
}
}
}
Expand Down