Merge pull request #15111 from poke1024/fix15097

Fixes various Line2D and Polygon2D editor bugs
This commit is contained in:
Noshyaar 2018-01-01 18:46:33 +07:00 committed by GitHub
commit 9186047767
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -235,7 +235,7 @@ void AbstractPolygon2DEditor::_wip_close() {
if (_is_line()) { if (_is_line()) {
_set_polygon(0, wip); _set_polygon(0, wip);
} else if (wip.size() >= 3) { } else if (wip.size() >= (_is_line() ? 2 : 3)) {
undo_redo->create_action(TTR("Create Poly")); undo_redo->create_action(TTR("Create Poly"));
_action_add_polygon(wip); _action_add_polygon(wip);
@ -281,46 +281,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
Vector2 gpoint = mb->get_position(); Vector2 gpoint = mb->get_position();
Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position()))); Vector2 cpoint = _get_node()->get_global_transform().affine_inverse().xform(canvas_item_editor->snap_point(canvas_item_editor->get_canvas_transform().affine_inverse().xform(mb->get_position())));
if (mode == MODE_CREATE) { if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
if (!wip_active) {
wip.clear();
wip.push_back(cpoint);
wip_active = true;
_wip_changed();
edited_point = PosVertex(-1, 1, cpoint);
canvas_item_editor->get_viewport_control()->update();
hover_point = Vertex();
selected_point = Vertex(0);
edge_point = PosVertex();
return true;
} else {
const real_t grab_threshold = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8);
if (!_is_line() && wip.size() > 1 && xform.xform(wip[0]).distance_to(gpoint) < grab_threshold) {
//wip closed
_wip_close();
return true;
} else {
//add wip point
wip.push_back(cpoint);
_wip_changed();
edited_point = PosVertex(-1, wip.size(), cpoint);
selected_point = Vertex(wip.size() - 1);
canvas_item_editor->get_viewport_control()->update();
return true;
}
}
} else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed() && wip_active) {
_wip_close();
}
} else if (mode == MODE_EDIT) {
if (mb->get_button_index() == BUTTON_LEFT) { if (mb->get_button_index() == BUTTON_LEFT) {
@ -332,7 +293,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
Vector<Vector2> vertices = _get_polygon(insert.polygon); Vector<Vector2> vertices = _get_polygon(insert.polygon);
if (vertices.size() < 3) { if (vertices.size() < (_is_line() ? 2 : 3)) {
vertices.push_back(cpoint); vertices.push_back(cpoint);
undo_redo->create_action(TTR("Edit Poly")); undo_redo->create_action(TTR("Edit Poly"));
@ -344,6 +305,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
Vector<Vector2> vertices = _get_polygon(insert.polygon); Vector<Vector2> vertices = _get_polygon(insert.polygon);
pre_move_edit = vertices; pre_move_edit = vertices;
printf("setting pre_move_edit\n");
edited_point = PosVertex(insert.polygon, insert.vertex + 1, xform.affine_inverse().xform(insert.pos)); edited_point = PosVertex(insert.polygon, insert.vertex + 1, xform.affine_inverse().xform(insert.pos));
vertices.insert(edited_point.vertex, edited_point.pos); vertices.insert(edited_point.vertex, edited_point.pos);
selected_point = edited_point; selected_point = edited_point;
@ -362,6 +324,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
if (closest.valid()) { if (closest.valid()) {
printf("setting pre_move_edit\n");
pre_move_edit = _get_polygon(closest.polygon); pre_move_edit = _get_polygon(closest.polygon);
edited_point = PosVertex(closest, xform.affine_inverse().xform(closest.pos)); edited_point = PosVertex(closest, xform.affine_inverse().xform(closest.pos));
selected_point = closest; selected_point = closest;
@ -414,6 +377,56 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
} }
} }
} }
if (mode == MODE_CREATE) {
if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
if (_is_line()) {
// for lines, we don't have a wip mode, and we can undo each single add point.
Vector<Vector2> vertices = _get_polygon(0);
vertices.push_back(cpoint);
undo_redo->create_action(TTR("Insert Point"));
_action_set_polygon(0, vertices);
_commit_action();
return true;
} else if (!wip_active) {
wip.clear();
wip.push_back(cpoint);
wip_active = true;
_wip_changed();
edited_point = PosVertex(-1, 1, cpoint);
canvas_item_editor->get_viewport_control()->update();
hover_point = Vertex();
selected_point = Vertex(0);
edge_point = PosVertex();
return true;
} else {
const real_t grab_threshold = EDITOR_DEF("editors/poly_editor/point_grab_radius", 8);
if (!_is_line() && wip.size() > 1 && xform.xform(wip[0]).distance_to(gpoint) < grab_threshold) {
//wip closed
_wip_close();
return true;
} else {
//add wip point
wip.push_back(cpoint);
_wip_changed();
edited_point = PosVertex(-1, wip.size(), cpoint);
selected_point = Vertex(wip.size() - 1);
canvas_item_editor->get_viewport_control()->update();
return true;
}
}
} else if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed() && wip_active) {
_wip_close();
}
}
} }
Ref<InputEventMouseMotion> mm = p_event; Ref<InputEventMouseMotion> mm = p_event;
@ -436,7 +449,7 @@ bool AbstractPolygon2DEditor::forward_gui_input(const Ref<InputEvent> &p_event)
} }
canvas_item_editor->get_viewport_control()->update(); canvas_item_editor->get_viewport_control()->update();
} else if (mode == MODE_EDIT) { } else if (mode == MODE_EDIT || (_is_line() && mode == MODE_CREATE)) {
const PosVertex onEdgeVertex = closest_edge_point(gpoint); const PosVertex onEdgeVertex = closest_edge_point(gpoint);
@ -535,7 +548,7 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
const Color col = Color(0.5, 0.5, 0.5); // FIXME polygon->get_outline_color(); const Color col = Color(0.5, 0.5, 0.5); // FIXME polygon->get_outline_color();
const int n = pre_move_edit.size(); const int n = pre_move_edit.size();
for (int i = 0; i < n - is_closed ? 0 : 1; i++) { for (int i = 0; i < n - (is_closed ? 0 : 1); i++) {
Vector2 p, p2; Vector2 p, p2;
p = pre_move_edit[i] + offset; p = pre_move_edit[i] + offset;
@ -544,7 +557,7 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
Vector2 point = xform.xform(p); Vector2 point = xform.xform(p);
Vector2 next_point = xform.xform(p2); Vector2 next_point = xform.xform(p2);
vpc->draw_line(point, next_point, col, 2); vpc->draw_line(point, next_point, col, 2 * EDSCALE);
} }
} }
@ -568,7 +581,7 @@ void AbstractPolygon2DEditor::forward_draw_over_viewport(Control *p_overlay) {
p2 = points[(i + 1) % n_points] + offset; p2 = points[(i + 1) % n_points] + offset;
const Vector2 next_point = xform.xform(p2); const Vector2 next_point = xform.xform(p2);
vpc->draw_line(point, next_point, col, 2); vpc->draw_line(point, next_point, col, 2 * EDSCALE);
} }
} }
@ -630,7 +643,7 @@ void AbstractPolygon2DEditor::remove_point(const Vertex &p_vertex) {
PoolVector<Vector2> vertices = _get_polygon(p_vertex.polygon); PoolVector<Vector2> vertices = _get_polygon(p_vertex.polygon);
if (vertices.size() > 3) { if (vertices.size() > (_is_line() ? 2 : 3)) {
vertices.remove(p_vertex.vertex); vertices.remove(p_vertex.vertex);
@ -705,8 +718,9 @@ AbstractPolygon2DEditor::PosVertex AbstractPolygon2DEditor::closest_edge_point(c
PoolVector<Vector2> points = _get_polygon(j); PoolVector<Vector2> points = _get_polygon(j);
const Vector2 offset = _get_offset(j); const Vector2 offset = _get_offset(j);
const int n_points = points.size(); const int n_points = points.size();
const int n_segments = n_points - (_is_line() ? 1 : 0);
for (int i = 0; i < n_points; i++) { for (int i = 0; i < n_segments; i++) {
Vector2 segment[2] = { xform.xform(points[i] + offset), Vector2 segment[2] = { xform.xform(points[i] + offset),
xform.xform(points[(i + 1) % n_points] + offset) }; xform.xform(points[(i + 1) % n_points] + offset) };