@@ -295,6 +295,50 @@ void TextureRegionEditor::_set_grid_parameters_clamping(bool p_enabled) {
295295 sb_sep_y->set_allow_greater (!p_enabled);
296296}
297297
298+ void TextureRegionEditor::_commit_drag () {
299+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton ();
300+ if (edited_margin >= 0 ) {
301+ undo_redo->create_action (TTR (" Set Margin" ));
302+ static Side side[4 ] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
303+ if (node_ninepatch) {
304+ undo_redo->add_do_method (node_ninepatch, " set_patch_margin" , side[edited_margin], node_ninepatch->get_patch_margin (side[edited_margin]));
305+ undo_redo->add_undo_method (node_ninepatch, " set_patch_margin" , side[edited_margin], prev_margin);
306+ } else if (res_stylebox.is_valid ()) {
307+ undo_redo->add_do_method (res_stylebox.ptr (), " set_texture_margin" , side[edited_margin], res_stylebox->get_texture_margin (side[edited_margin]));
308+ undo_redo->add_undo_method (res_stylebox.ptr (), " set_texture_margin" , side[edited_margin], prev_margin);
309+ res_stylebox->emit_changed ();
310+ }
311+ edited_margin = -1 ;
312+ } else {
313+ undo_redo->create_action (TTR (" Set Region Rect" ));
314+ if (node_sprite_2d) {
315+ undo_redo->add_do_method (node_sprite_2d, " set_region_rect" , node_sprite_2d->get_region_rect ());
316+ undo_redo->add_undo_method (node_sprite_2d, " set_region_rect" , rect_prev);
317+ } else if (node_sprite_3d) {
318+ undo_redo->add_do_method (node_sprite_3d, " set_region_rect" , node_sprite_3d->get_region_rect ());
319+ undo_redo->add_undo_method (node_sprite_3d, " set_region_rect" , rect_prev);
320+ } else if (node_ninepatch) {
321+ undo_redo->add_do_method (node_ninepatch, " set_region_rect" , node_ninepatch->get_region_rect ());
322+ undo_redo->add_undo_method (node_ninepatch, " set_region_rect" , rect_prev);
323+ } else if (res_stylebox.is_valid ()) {
324+ undo_redo->add_do_method (res_stylebox.ptr (), " set_region_rect" , res_stylebox->get_region_rect ());
325+ undo_redo->add_undo_method (res_stylebox.ptr (), " set_region_rect" , rect_prev);
326+ } else if (res_atlas_texture.is_valid ()) {
327+ undo_redo->add_do_method (res_atlas_texture.ptr (), " set_region" , res_atlas_texture->get_region ());
328+ undo_redo->add_undo_method (res_atlas_texture.ptr (), " set_region" , rect_prev);
329+ }
330+ drag_index = -1 ;
331+ }
332+
333+ undo_redo->add_do_method (this , " _update_rect" );
334+ undo_redo->add_undo_method (this , " _update_rect" );
335+ undo_redo->add_do_method (texture_overlay, " queue_redraw" );
336+ undo_redo->add_undo_method (texture_overlay, " queue_redraw" );
337+ undo_redo->commit_action ();
338+ drag = false ;
339+ creating = false ;
340+ }
341+
298342void TextureRegionEditor::_texture_overlay_input (const Ref<InputEvent> &p_input) {
299343 if (panner->gui_input (p_input, texture_overlay->get_global_rect ())) {
300344 return ;
@@ -304,7 +348,8 @@ void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input)
304348 mtx.columns [2 ] = -draw_ofs * draw_zoom;
305349 mtx.scale_basis (Vector2 (draw_zoom, draw_zoom));
306350
307- EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton ();
351+ bool cancel_drag = false ;
352+
308353 Ref<InputEventMouseButton> mb = p_input;
309354 if (mb.is_valid ()) {
310355 if (mb->get_button_index () == MouseButton::LEFT) {
@@ -392,6 +437,7 @@ void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input)
392437 // We didn't hit anything, but we're in the autoslice mode. Handle it.
393438
394439 Vector2 point = mtx.affine_inverse ().xform (mb->get_position ());
440+ EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton ();
395441 for (const Rect2 &E : autoslice_cache) {
396442 if (E.has_point (point)) {
397443 rect = E;
@@ -449,74 +495,41 @@ void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input)
449495 }
450496
451497 } else if (!mb->is_pressed () && drag) {
452- if (edited_margin >= 0 ) {
453- undo_redo->create_action (TTR (" Set Margin" ));
454- static Side side[4 ] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
455- if (node_ninepatch) {
456- undo_redo->add_do_method (node_ninepatch, " set_patch_margin" , side[edited_margin], node_ninepatch->get_patch_margin (side[edited_margin]));
457- undo_redo->add_undo_method (node_ninepatch, " set_patch_margin" , side[edited_margin], prev_margin);
458- } else if (res_stylebox.is_valid ()) {
459- undo_redo->add_do_method (res_stylebox.ptr (), " set_texture_margin" , side[edited_margin], res_stylebox->get_texture_margin (side[edited_margin]));
460- undo_redo->add_undo_method (res_stylebox.ptr (), " set_texture_margin" , side[edited_margin], prev_margin);
461- res_stylebox->emit_changed ();
462- }
463- edited_margin = -1 ;
464- } else {
465- undo_redo->create_action (TTR (" Set Region Rect" ));
466- if (node_sprite_2d) {
467- undo_redo->add_do_method (node_sprite_2d, " set_region_rect" , node_sprite_2d->get_region_rect ());
468- undo_redo->add_undo_method (node_sprite_2d, " set_region_rect" , rect_prev);
469- } else if (node_sprite_3d) {
470- undo_redo->add_do_method (node_sprite_3d, " set_region_rect" , node_sprite_3d->get_region_rect ());
471- undo_redo->add_undo_method (node_sprite_3d, " set_region_rect" , rect_prev);
472- } else if (node_ninepatch) {
473- undo_redo->add_do_method (node_ninepatch, " set_region_rect" , node_ninepatch->get_region_rect ());
474- undo_redo->add_undo_method (node_ninepatch, " set_region_rect" , rect_prev);
475- } else if (res_stylebox.is_valid ()) {
476- undo_redo->add_do_method (res_stylebox.ptr (), " set_region_rect" , res_stylebox->get_region_rect ());
477- undo_redo->add_undo_method (res_stylebox.ptr (), " set_region_rect" , rect_prev);
478- } else if (res_atlas_texture.is_valid ()) {
479- undo_redo->add_do_method (res_atlas_texture.ptr (), " set_region" , res_atlas_texture->get_region ());
480- undo_redo->add_undo_method (res_atlas_texture.ptr (), " set_region" , rect_prev);
481- }
482- drag_index = -1 ;
483- }
484- undo_redo->add_do_method (this , " _update_rect" );
485- undo_redo->add_undo_method (this , " _update_rect" );
486- undo_redo->add_do_method (texture_overlay, " queue_redraw" );
487- undo_redo->add_undo_method (texture_overlay, " queue_redraw" );
488- undo_redo->commit_action ();
489- drag = false ;
490- creating = false ;
498+ _commit_drag ();
491499 }
500+ } else if (drag && mb->get_button_index () == MouseButton::RIGHT && mb->is_pressed ()) {
501+ cancel_drag = true ;
502+ }
503+ }
492504
493- } else if (mb->get_button_index () == MouseButton::RIGHT && mb->is_pressed ()) {
494- if (drag) {
495- drag = false ;
496- if (edited_margin >= 0 ) {
497- static Side side[4 ] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
498- if (node_ninepatch) {
499- node_ninepatch->set_patch_margin (side[edited_margin], prev_margin);
500- }
501- if (res_stylebox.is_valid ()) {
502- res_stylebox->set_texture_margin (side[edited_margin], prev_margin);
503- }
504- edited_margin = -1 ;
505- } else {
506- _apply_rect (rect_prev);
507- rect = rect_prev;
508- texture_preview->queue_redraw ();
509- texture_overlay->queue_redraw ();
510- drag_index = -1 ;
511- }
505+ if (drag && p_input.is_valid () && p_input->is_action_pressed (SNAME (" ui_cancel" ), false , true )) {
506+ cancel_drag = true ;
507+ }
508+
509+ if (cancel_drag) {
510+ drag = false ;
511+ if (edited_margin >= 0 ) {
512+ static Side side[4 ] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
513+ if (node_ninepatch) {
514+ node_ninepatch->set_patch_margin (side[edited_margin], prev_margin);
515+ }
516+ if (res_stylebox.is_valid ()) {
517+ res_stylebox->set_texture_margin (side[edited_margin], prev_margin);
512518 }
519+ edited_margin = -1 ;
520+ } else {
521+ _apply_rect (rect_prev);
522+ rect = rect_prev;
523+ texture_preview->queue_redraw ();
524+ texture_overlay->queue_redraw ();
525+ drag_index = -1 ;
513526 }
514527 }
515528
516529 Ref<InputEventMouseMotion> mm = p_input;
517530
518- if (mm.is_valid ()) {
519- if (drag ) {
531+ if (drag && mm.is_valid ()) {
532+ if (mm-> get_button_mask (). has_flag (MouseButtonMask::LEFT) ) {
520533 if (edited_margin >= 0 ) {
521534 float new_margin = 0 ;
522535
@@ -633,6 +646,8 @@ void TextureRegionEditor::_texture_overlay_input(const Ref<InputEvent> &p_input)
633646 }
634647 texture_preview->queue_redraw ();
635648 texture_overlay->queue_redraw ();
649+ } else {
650+ _commit_drag ();
636651 }
637652 }
638653
@@ -658,6 +673,12 @@ void TextureRegionEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin,
658673 _zoom_on_position (draw_zoom * p_zoom_factor, p_origin);
659674}
660675
676+ void TextureRegionEditor::_input_from_window (const Ref<InputEvent> &p_event) {
677+ if (!drag && p_event.is_valid () && p_event->is_action_pressed (SNAME (" ui_cancel" ), false , true )) {
678+ hide ();
679+ }
680+ }
681+
661682void TextureRegionEditor::_scroll_changed (float ) {
662683 if (updating_scroll) {
663684 return ;
@@ -862,6 +883,12 @@ void TextureRegionEditor::_notification(int p_what) {
862883 }
863884 } break ;
864885
886+ case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
887+ if (drag) {
888+ _commit_drag ();
889+ }
890+ } break ;
891+
865892 case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
866893 // This happens when the user leaves the Editor and returns,
867894 // they could have changed the textures, so the cache is cleared.
@@ -1135,6 +1162,8 @@ TextureRegionEditor::TextureRegionEditor() {
11351162 set_title (TTR (" Region Editor" ));
11361163 set_process_shortcut_input (true );
11371164 set_ok_button_text (TTR (" Close" ));
1165+ // Handled manually, to allow canceling dragging.
1166+ set_close_on_escape (false );
11381167
11391168 // A power-of-two value works better as a default grid size.
11401169 snap_offset = EditorSettings::get_singleton ()->get_project_metadata (" texture_region_editor" , " snap_offset" , Vector2 ());
0 commit comments