From aea7e555221e071e65fe76eb9571198622bbc547 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 27 Oct 2021 19:01:46 +1100 Subject: WM: de-duplicate cursor motion checks for selection picking Replace local static mouse coordinate storage with a single function. also resolve inconsistencies. - Edit-mesh selection used equality check (ignoring `U.move_threshold`). - Motion to clear tooltips checked the value without scaling by the DPI. Also prevent the unlikely case of the previous motion check matching a different area by resetting the value when the active region changes. --- source/blender/editors/armature/armature_select.c | 6 +----- source/blender/editors/mesh/editmesh_select.c | 8 ++------ source/blender/editors/screen/screen_edit.c | 4 ++++ .../blender/editors/space_view3d/view3d_select.c | 9 +++------ source/blender/windowmanager/WM_api.h | 1 + .../blender/windowmanager/intern/wm_event_query.c | 22 ++++++++++++++++++++++ .../blender/windowmanager/intern/wm_event_system.c | 3 ++- 7 files changed, 35 insertions(+), 18 deletions(-) (limited to 'source') diff --git a/source/blender/editors/armature/armature_select.c b/source/blender/editors/armature/armature_select.c index bd799c00373..937385f9ffa 100644 --- a/source/blender/editors/armature/armature_select.c +++ b/source/blender/editors/armature/armature_select.c @@ -673,11 +673,7 @@ static EditBone *get_nearest_editbonepoint( } if (use_cycle) { - static int last_mval[2] = {-100, -100}; - if ((len_manhattan_v2v2_int(vc->mval, last_mval) <= WM_EVENT_CURSOR_MOTION_THRESHOLD) == 0) { - use_cycle = false; - } - copy_v2_v2_int(last_mval, vc->mval); + use_cycle = !WM_cursor_test_motion_and_update(vc->mval); } const bool do_nearest = !(XRAY_ACTIVE(vc->v3d) || use_cycle); diff --git a/source/blender/editors/mesh/editmesh_select.c b/source/blender/editors/mesh/editmesh_select.c index 2fcf8fa6f8f..e0768bcff24 100644 --- a/source/blender/editors/mesh/editmesh_select.c +++ b/source/blender/editors/mesh/editmesh_select.c @@ -884,9 +884,8 @@ static bool unified_findnearest(ViewContext *vc, BMFace **r_efa) { BMEditMesh *em = vc->em; - static short mval_prev[2] = {-1, -1}; - /* only cycle while the mouse remains still */ - const bool use_cycle = ((mval_prev[0] == vc->mval[0]) && (mval_prev[1] == vc->mval[1])); + + const bool use_cycle = !WM_cursor_test_motion_and_update(vc->mval); const float dist_init = ED_view3d_select_dist_px(); /* since edges select lines, we give dots advantage of ~20 pix */ const float dist_margin = (dist_init / 2); @@ -988,9 +987,6 @@ static bool unified_findnearest(ViewContext *vc, } } - mval_prev[0] = vc->mval[0]; - mval_prev[1] = vc->mval[1]; - /* Only one element type will be non-null. */ BLI_assert(((hit.v.ele != NULL) + (hit.e.ele != NULL) + (hit.f.ele != NULL)) <= 1); diff --git a/source/blender/editors/screen/screen_edit.c b/source/blender/editors/screen/screen_edit.c index 841792d5f2d..fa0cfd16817 100644 --- a/source/blender/editors/screen/screen_edit.c +++ b/source/blender/editors/screen/screen_edit.c @@ -935,6 +935,10 @@ void ED_screen_set_active_region(bContext *C, wmWindow *win, const int xy[2]) } } } + + /* Ensure test-motion values are never shared between regions. */ + const bool use_cycle = !WM_cursor_test_motion_and_update((const int[2]){-1, -1}); + UNUSED_VARS(use_cycle); } /* Cursors, for time being set always on edges, diff --git a/source/blender/editors/space_view3d/view3d_select.c b/source/blender/editors/space_view3d/view3d_select.c index 07f1f8a753c..18820039c7f 100644 --- a/source/blender/editors/space_view3d/view3d_select.c +++ b/source/blender/editors/space_view3d/view3d_select.c @@ -2047,19 +2047,16 @@ static int mixed_bones_object_selectbuffer_extended(ViewContext *vc, bool enumerate, bool *r_do_nearest) { - static int last_mval[2] = {-100, -100}; bool do_nearest = false; View3D *v3d = vc->v3d; /* define if we use solid nearest select or not */ if (use_cycle) { + /* Update the coordinates (even if the return value isn't used). */ + const bool has_motion = WM_cursor_test_motion_and_update(mval); if (!XRAY_ACTIVE(v3d)) { - do_nearest = true; - if (len_manhattan_v2v2_int(mval, last_mval) <= WM_EVENT_CURSOR_MOTION_THRESHOLD) { - do_nearest = false; - } + do_nearest = has_motion; } - copy_v2_v2_int(last_mval, mval); } else { if (!XRAY_ACTIVE(v3d)) { diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index b5acecc4762..59e03f472f0 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -937,6 +937,7 @@ bool WM_event_is_modal_tweak_exit(const struct wmEvent *event, int tweak_event); bool WM_event_is_last_mousemove(const struct wmEvent *event); bool WM_event_is_mouse_drag(const struct wmEvent *event); bool WM_event_is_mouse_drag_or_press(const wmEvent *event); +bool WM_cursor_test_motion_and_update(const int mval[2]) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT; int WM_event_drag_threshold(const struct wmEvent *event); bool WM_event_drag_test(const struct wmEvent *event, const int prev_xy[2]); diff --git a/source/blender/windowmanager/intern/wm_event_query.c b/source/blender/windowmanager/intern/wm_event_query.c index 9ee114674ed..ef733837bf7 100644 --- a/source/blender/windowmanager/intern/wm_event_query.c +++ b/source/blender/windowmanager/intern/wm_event_query.c @@ -276,6 +276,28 @@ bool WM_event_is_mouse_drag_or_press(const wmEvent *event) (ISMOUSE_BUTTON(event->type) && (event->val == KM_PRESS)); } +/** + * Detect motion between selection (callers should only use this for selection picking), + * typically mouse press/click events. + * + * \param mval: Region relative coordinates, call with (-1, -1) resets the last cursor location. + * \returns True when there was motion since last called. + * + * NOTE(@campbellbarton): The logic used here isn't foolproof. + * It's possible that users move the cursor past #WM_EVENT_CURSOR_MOTION_THRESHOLD then back to + * a position within the threshold (between mouse clicks). + * In practice users never reported this since the threshold is very small (a few pixels). + * To prevent the unlikely case of values matching from another region, + * changing regions resets this value to (-1, -1). + */ +bool WM_cursor_test_motion_and_update(const int mval[2]) +{ + static int mval_prev[2] = {-1, -1}; + bool use_cycle = (len_manhattan_v2v2_int(mval, mval_prev) <= WM_EVENT_CURSOR_MOTION_THRESHOLD); + copy_v2_v2_int(mval_prev, mval); + return !use_cycle; +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c index 2aa9a4d8676..a426841b49c 100644 --- a/source/blender/windowmanager/intern/wm_event_system.c +++ b/source/blender/windowmanager/intern/wm_event_system.c @@ -3628,7 +3628,8 @@ void wm_event_do_handlers(bContext *C) /* Clear tool-tip on mouse move. */ if (screen->tool_tip && screen->tool_tip->exit_on_event) { if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) { - if (len_manhattan_v2v2_int(screen->tool_tip->event_xy, event->xy) > U.move_threshold) { + if (len_manhattan_v2v2_int(screen->tool_tip->event_xy, event->xy) > + WM_EVENT_CURSOR_MOTION_THRESHOLD) { WM_tooltip_clear(C, win); } } -- cgit v1.2.3