Skip to content

Commit

Permalink
Merge pull request #50508 from Calinou/add-drag-threshold-select-mode…
Browse files Browse the repository at this point in the history
…-3.x

Prevent accidental drags by adding drag distance threshold
  • Loading branch information
akien-mga authored Jul 17, 2021
2 parents d7f49c7 + 44b0175 commit 9636dea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
41 changes: 29 additions & 12 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2491,22 +2491,39 @@ bool CanvasItemEditor::_gui_input_select(const Ref<InputEvent> &p_event) {
// Start dragging
if (still_selected) {
// Drag the node(s) if requested
List<CanvasItem *> selection2 = _get_edited_canvas_items();
drag_start_origin = click;
drag_type = DRAG_QUEUED;
}
// Select the item
return true;
}
}
}

drag_selection.clear();
for (int i = 0; i < selection2.size(); i++) {
if (_is_node_movable(selection2[i], true)) {
drag_selection.push_back(selection2[i]);
}
}
if (drag_type == DRAG_QUEUED) {
if (b.is_valid() && !b->is_pressed()) {
drag_type = DRAG_NONE;
return true;
}
if (m.is_valid()) {
Point2 click = transform.affine_inverse().xform(m->get_position());
// Scale movement threshold with zoom (which itself is set relative to the editor scale).
bool movement_threshold_passed = drag_start_origin.distance_to(click) > (8 * MAX(1, EDSCALE)) / zoom;
if (m.is_valid() && movement_threshold_passed) {
List<CanvasItem *> selection2 = _get_edited_canvas_items();

if (selection2.size() > 0) {
drag_type = DRAG_MOVE;
drag_from = click;
_save_canvas_item_state(drag_selection);
drag_selection.clear();
for (int i = 0; i < selection2.size(); i++) {
if (_is_node_movable(selection2[i], true)) {
drag_selection.push_back(selection2[i]);
}
}
// Select the item

if (selection2.size() > 0) {
drag_type = DRAG_MOVE;
drag_from = click;
_save_canvas_item_state(drag_selection);
}
return true;
}
}
Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/canvas_item_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ class CanvasItemEditor : public VBoxContainer {
DRAG_ANCHOR_BOTTOM_RIGHT,
DRAG_ANCHOR_BOTTOM_LEFT,
DRAG_ANCHOR_ALL,
DRAG_QUEUED,
DRAG_MOVE,
DRAG_SCALE_X,
DRAG_SCALE_Y,
Expand Down Expand Up @@ -390,6 +391,7 @@ class CanvasItemEditor : public VBoxContainer {
Control *top_ruler;
Control *left_ruler;

Point2 drag_start_origin;
DragType drag_type;
Point2 drag_from;
Point2 drag_to;
Expand Down
4 changes: 3 additions & 1 deletion editor/plugins/spatial_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
}

_edit.mouse_pos = b->get_position();
_edit.original_mouse_pos = b->get_position();
_edit.snap = spatial_editor->is_snap_enabled();
_edit.mode = TRANSFORM_NONE;

Expand Down Expand Up @@ -1412,7 +1413,8 @@ void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
} else if (nav_scheme == NAVIGATION_MODO && m->get_alt()) {
nav_mode = NAVIGATION_ORBIT;
} else {
if (clicked) {
const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE;
if (clicked && movement_threshold_passed) {
if (!clicked_includes_current) {
_select_clicked(clicked_wants_append, true);
// Processing was deferred.
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/spatial_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class SpatialEditorViewport : public Control {
Vector3 orig_gizmo_pos;
int edited_gizmo;
Point2 mouse_pos;
Point2 original_mouse_pos;
bool snap;
Ref<EditorSpatialGizmo> gizmo;
int gizmo_handle;
Expand Down

0 comments on commit 9636dea

Please sign in to comment.