Skip to content

Commit 85a8c3e

Browse files
committed
Allow cancelling actions in Path2D editor
1 parent 2fb296a commit 85a8c3e

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

editor/plugins/path_2d_editor_plugin.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
155155
}
156156
}
157157

158+
if (action != ACTION_NONE && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
159+
_cancel_current_action();
160+
return true;
161+
}
162+
158163
// Check for point creation.
159164
if (mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && ((mb->is_command_or_control_pressed() && mode == MODE_EDIT) || mode == MODE_CREATE)) {
160165
Ref<Curve2D> curve = node->get_curve();
161166
curve->add_point(cpoint);
162-
163-
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
164-
undo_redo->create_action(TTR("Add Point to Curve"));
165-
undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
166-
undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
167+
moving_from = cpoint;
167168

168169
action = ACTION_MOVING_NEW_POINT;
169170
action_point = curve->get_point_count() - 1;
@@ -235,9 +236,12 @@ bool Path2DEditor::forward_gui_input(const Ref<InputEvent> &p_event) {
235236
}
236237
break;
237238
case ACTION_MOVING_NEW_POINT: {
239+
undo_redo->create_action(TTR("Add Point to Curve"));
240+
undo_redo->add_do_method(curve.ptr(), "add_point", cpoint);
238241
undo_redo->add_do_method(curve.ptr(), "set_point_position", action_point, cpoint);
239-
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
240242
undo_redo->add_do_method(canvas_item_editor, "update_viewport");
243+
undo_redo->add_undo_method(curve.ptr(), "remove_point", curve->get_point_count() - 1);
244+
undo_redo->add_undo_method(canvas_item_editor, "update_viewport");
241245
undo_redo->commit_action(false);
242246
} break;
243247

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

454+
if (action != ACTION_NONE) {
455+
_cancel_current_action();
456+
}
457+
450458
if (p_path2d) {
451459
node = Object::cast_to<Path2D>(p_path2d);
452460

@@ -551,6 +559,38 @@ void Path2DEditor::_handle_option_pressed(int p_option) {
551559
}
552560
}
553561

562+
void Path2DEditor::_cancel_current_action() {
563+
ERR_FAIL_NULL(node);
564+
Ref<Curve2D> curve = node->get_curve();
565+
ERR_FAIL_COND(curve.is_null());
566+
567+
switch (action) {
568+
case ACTION_MOVING_POINT: {
569+
curve->set_point_position(action_point, moving_from);
570+
} break;
571+
572+
case ACTION_MOVING_NEW_POINT: {
573+
curve->remove_point(curve->get_point_count() - 1);
574+
} break;
575+
576+
case ACTION_MOVING_IN: {
577+
curve->set_point_in(action_point, moving_from);
578+
curve->set_point_out(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_out_length));
579+
} break;
580+
581+
case ACTION_MOVING_OUT: {
582+
curve->set_point_out(action_point, moving_from);
583+
curve->set_point_in(action_point, mirror_handle_length ? -moving_from : (-moving_from.normalized() * orig_in_length));
584+
} break;
585+
586+
default: {
587+
}
588+
}
589+
590+
canvas_item_editor->update_viewport();
591+
action = ACTION_NONE;
592+
}
593+
554594
void Path2DEditor::_confirm_clear_points() {
555595
if (!node || node->get_curve().is_null()) {
556596
return;
@@ -598,14 +638,6 @@ void Path2DEditor::_restore_curve_points(Path2D *p_path2d, const PackedVector2Ar
598638
}
599639

600640
Path2DEditor::Path2DEditor() {
601-
canvas_item_editor = nullptr;
602-
mirror_handle_angle = true;
603-
mirror_handle_length = true;
604-
on_edge = false;
605-
606-
mode = MODE_EDIT;
607-
action = ACTION_NONE;
608-
609641
curve_edit = memnew(Button);
610642
curve_edit->set_theme_type_variation("FlatButton");
611643
curve_edit->set_toggle_mode(true);

editor/plugins/path_2d_editor_plugin.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Path2DEditor : public HBoxContainer {
5757
MODE_CLEAR_POINTS,
5858
};
5959

60-
Mode mode;
60+
Mode mode = MODE_EDIT;
6161
Button *curve_clear_points = nullptr;
6262
Button *curve_close = nullptr;
6363
Button *curve_create = nullptr;
@@ -68,9 +68,9 @@ class Path2DEditor : public HBoxContainer {
6868

6969
ConfirmationDialog *clear_points_dialog = nullptr;
7070

71-
bool mirror_handle_angle;
72-
bool mirror_handle_length;
73-
bool on_edge;
71+
bool mirror_handle_angle = true;
72+
bool mirror_handle_length = true;
73+
bool on_edge = false;
7474

7575
enum HandleOption {
7676
HANDLE_OPTION_ANGLE,
@@ -85,7 +85,7 @@ class Path2DEditor : public HBoxContainer {
8585
ACTION_MOVING_OUT,
8686
};
8787

88-
Action action;
88+
Action action = ACTION_NONE;
8989
int action_point = 0;
9090
Point2 moving_from;
9191
Point2 moving_screen_from;
@@ -96,6 +96,7 @@ class Path2DEditor : public HBoxContainer {
9696

9797
void _mode_selected(int p_mode);
9898
void _handle_option_pressed(int p_option);
99+
void _cancel_current_action();
99100

100101
void _node_visibility_changed();
101102

0 commit comments

Comments
 (0)