@@ -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+
554594void 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
600640Path2DEditor::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 );
0 commit comments