Skip to content

Commit

Permalink
Merge pull request godotengine#93087 from KoBeWi/path_to_less_bugs
Browse files Browse the repository at this point in the history
Allow cancelling actions in Path2D editor
  • Loading branch information
akien-mga committed Jun 13, 2024
2 parents de8a05f + 85a8c3e commit 680e489
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
60 changes: 46 additions & 14 deletions editor/plugins/path_2d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,16 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
}
}

if (action != ACTION_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
_cancel_current_action();
return true;
}

// Check for point creation.
if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
Ref<Curve2D> curve = node->get_curve();
curve->add_point(cpoint);

EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
undo_redo->create_action(TTR("Add Point to Curve"));
undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
moving_from = cpoint;

action = ACTION_MOVING_NEW_POINT;
action_point = curve->get_point_count() - 1;
Expand Down Expand Up @@ -235,9 +236,12 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
}
break;
case ACTION_MOVING_NEW_POINT: {
undo_redo->create_action(TTR("Add Point to Curve"));
undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
undo_redo->commit_action(false);
} break;

Expand Down Expand Up @@ -447,6 +451,10 @@ void Path2DEditor::edit(Node *p_path2d) {
canvas_item_editor = CanvasItemEditor::get_singleton();
}

if (action != ACTION_NONE) {
_cancel_current_action();
}

if (p_path2d) {
node = Object::cast_to<Path2D>(p_path2d);

Expand Down Expand Up @@ -551,6 +559,38 @@ void Path2DEditor::_handle_option_pressed(int p_option) {
}
}

void Path2DEditor::_cancel_current_action() {
ERR_FAIL_NULL(node);
Ref<Curve2D> curve = node->get_curve();
ERR_FAIL_COND(curve.is_null());

switch (action) {
case ACTION_MOVING_POINT: {
curve->set_point_position(action_point, moving_from);
} break;

case ACTION_MOVING_NEW_POINT: {
curve->remove_point(curve->get_point_count() - 1);
} break;

case ACTION_MOVING_IN: {
curve->set_point_in(action_point, moving_from);
curve->set_point_out(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));
} break;

case ACTION_MOVING_OUT: {
curve->set_point_out(action_point, moving_from);
curve->set_point_in(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));
} break;

default: {
}
}

canvas_item_editor->update_viewport();
action = ACTION_NONE;
}

void Path2DEditor::_confirm_clear_points() {
if (!node || node->get_curve().is_null()) {
return;
Expand Down Expand Up @@ -598,14 +638,6 @@ void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Ar
}

Path2DEditor::Path2DEditor() {
canvas_item_editor = nullptr;
mirror_handle_angle = true;
mirror_handle_length = true;
on_edge = false;

mode = MODE_EDIT;
action = ACTION_NONE;

curve_edit = memnew(Button);
curve_edit->set_theme_type_variation("FlatButton");
curve_edit->set_toggle_mode(true);
Expand Down
11 changes: 6 additions & 5 deletions editor/plugins/path_2d_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Path2DEditor : public HBoxContainer {
MODE_CLEAR_POINTS,
};

Mode mode;
Mode mode = MODE_EDIT;
Button *curve_clear_points = nullptr;
Button *curve_close = nullptr;
Button *curve_create = nullptr;
Expand All @@ -68,9 +68,9 @@ class Path2DEditor : public HBoxContainer {

ConfirmationDialog *clear_points_dialog = nullptr;

bool mirror_handle_angle;
bool mirror_handle_length;
bool on_edge;
bool mirror_handle_angle = true;
bool mirror_handle_length = true;
bool on_edge = false;

enum HandleOption {
HANDLE_OPTION_ANGLE,
Expand All @@ -85,7 +85,7 @@ class Path2DEditor : public HBoxContainer {
ACTION_MOVING_OUT,
};

Action action;
Action action = ACTION_NONE;
int action_point = 0;
Point2 moving_from;
Point2 moving_screen_from;
Expand All @@ -96,6 +96,7 @@ class Path2DEditor : public HBoxContainer {

void _mode_selected(int p_mode);
void _handle_option_pressed(int p_option);
void _cancel_current_action();

void _node_visibility_changed();

Expand Down

0 comments on commit 680e489

Please sign in to comment.