diff options
author | Campbell Barton <ideasman42@gmail.com> | 2019-05-30 07:17:39 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2019-05-30 08:02:53 +0300 |
commit | 82e8e5c871f86e65f332cdde3bd484a55e7e8572 (patch) | |
tree | ccbd4e45e339bd4c62231d12b3499bdfdeaed6e4 /source/blender/windowmanager | |
parent | 2437a8b6f02144d796829a32461878ff2160c791 (diff) |
Cleanup: move click/drag events to functions
Simplifies future changes to dragging checks and avoids
each check for drag using slightly different logic.
Diffstat (limited to 'source/blender/windowmanager')
-rw-r--r-- | source/blender/windowmanager/WM_api.h | 4 | ||||
-rw-r--r-- | source/blender/windowmanager/WM_types.h | 4 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_event_system.c | 56 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_gesture.c | 14 | ||||
-rw-r--r-- | source/blender/windowmanager/intern/wm_gesture_ops.c | 2 | ||||
-rw-r--r-- | source/blender/windowmanager/wm.h | 2 |
6 files changed, 57 insertions, 25 deletions
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h index de6db8876f7..7eefbb4d648 100644 --- a/source/blender/windowmanager/WM_api.h +++ b/source/blender/windowmanager/WM_api.h @@ -604,6 +604,10 @@ void WM_event_print(const struct wmEvent *event); void WM_operator_region_active_win_set(struct bContext *C); +int WM_event_drag_threshold(const struct wmEvent *event); +bool WM_event_drag_test(const struct wmEvent *event, const int prev_xy[2]); +bool WM_event_drag_test_with_delta(const struct wmEvent *event, const int delta[2]); + /* drag and drop */ struct wmDrag *WM_event_start_drag( struct bContext *C, int icon, int type, void *poin, double value, unsigned int flags); diff --git a/source/blender/windowmanager/WM_types.h b/source/blender/windowmanager/WM_types.h index f3771ea22a4..20bb5935c8e 100644 --- a/source/blender/windowmanager/WM_types.h +++ b/source/blender/windowmanager/WM_types.h @@ -528,7 +528,9 @@ typedef struct wmEvent { /** * Values below are considered a click, above are considered a drag. */ -#define WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD (U.tweak_threshold * U.dpi_fac) +int WM_event_cursor_click_drag_threshold_from_event_(const wmEvent *event); + +bool WM_event_cursor_click_drag_threshold_met(const wmEvent *event); /** * Values below are ignored when detecting if the user interntionally moved the cursor. diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c index 04ff29b6fbf..7aad89a4bdb 100644 --- a/source/blender/windowmanager/intern/wm_event_system.c +++ b/source/blender/windowmanager/intern/wm_event_system.c @@ -2869,10 +2869,7 @@ static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers) if (wm_action_not_handled(action)) { if (event->check_drag) { wmWindow *win = CTX_wm_window(C); - if ((abs(event->x - win->eventstate->prevclickx)) >= - WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD || - (abs(event->y - win->eventstate->prevclicky)) >= - WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD) { + if (WM_event_drag_test(event, &win->eventstate->prevclickx)) { int x = event->x; int y = event->y; short val = event->val; @@ -2928,10 +2925,11 @@ static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers) if ((event->val == KM_RELEASE) && (win->eventstate->prevval == KM_PRESS) && (win->eventstate->check_click == true)) { - if ((abs(event->x - win->eventstate->prevclickx)) < - WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD && - (abs(event->y - win->eventstate->prevclicky)) < - WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD) { + if (WM_event_drag_test(event, &win->eventstate->prevclickx)) { + win->eventstate->check_click = 0; + win->eventstate->check_drag = 0; + } + else { /* Position is where the actual click happens, for more * accurate selecting in case the mouse drifts a little. */ int x = event->x; @@ -2949,10 +2947,6 @@ static int wm_handlers_do(bContext *C, wmEvent *event, ListBase *handlers) event->x = x; event->y = y; } - else { - win->eventstate->check_click = 0; - win->eventstate->check_drag = 0; - } } else if (event->val == KM_DBL_CLICK) { /* The underlying event is a press, so try and handle this. */ @@ -4254,13 +4248,14 @@ static wmWindow *wm_event_cursor_other_windows(wmWindowManager *wm, wmWindow *wi return NULL; } -static bool wm_event_is_double_click(wmEvent *event, const wmEvent *event_state) +static bool wm_event_is_double_click(const wmEvent *event, const wmEvent *event_state) { if ((event->type == event_state->prevtype) && (event_state->prevval == KM_RELEASE) && (event->val == KM_PRESS)) { - if ((ISMOUSE(event->type) == false) || - ((abs(event->x - event_state->prevclickx)) < WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD && - (abs(event->y - event_state->prevclicky)) < WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD)) { + if (ISMOUSE(event->type) && WM_event_drag_test(event, &event_state->prevclickx)) { + /* pass */ + } + else { if ((PIL_check_seconds_timer() - event_state->prevclicktime) * 1000 < U.dbl_click_time) { return true; } @@ -5188,3 +5183,32 @@ bool WM_window_modal_keymap_status_draw(bContext *UNUSED(C), wmWindow *win, uiLa } /** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Event Click/Drag Checks + * + * Values under this limit are detected as clicks. + * + * \{ */ + +int WM_event_drag_threshold(const struct wmEvent *UNUSED(event)) +{ + return (int)((float)U.tweak_threshold * U.dpi_fac); +} + +bool WM_event_drag_test_with_delta(const wmEvent *event, const int drag_delta[2]) +{ + const int drag_threshold = WM_event_drag_threshold(event); + return abs(drag_delta[0]) > drag_threshold || abs(drag_delta[1]) > drag_threshold; +} + +bool WM_event_drag_test(const wmEvent *event, const int prev_xy[2]) +{ + const int drag_delta[2] = { + prev_xy[0] - event->x, + prev_xy[1] - event->y, + }; + return WM_event_drag_test_with_delta(event, drag_delta); +} + +/** \} */ diff --git a/source/blender/windowmanager/intern/wm_gesture.c b/source/blender/windowmanager/intern/wm_gesture.c index bc985087475..ea92409d528 100644 --- a/source/blender/windowmanager/intern/wm_gesture.c +++ b/source/blender/windowmanager/intern/wm_gesture.c @@ -128,15 +128,17 @@ bool WM_gesture_is_modal_first(const wmGesture *gesture) } /* tweak and line gestures */ -int wm_gesture_evaluate(wmGesture *gesture) +int wm_gesture_evaluate(wmGesture *gesture, const wmEvent *event) { if (gesture->type == WM_GESTURE_TWEAK) { rcti *rect = gesture->customdata; - int dx = BLI_rcti_size_x(rect); - int dy = BLI_rcti_size_y(rect); - const int drag_threshold = WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD; - if (abs(dx) >= drag_threshold || abs(dy) >= drag_threshold) { - int theta = round_fl_to_int(4.0f * atan2f((float)dy, (float)dx) / (float)M_PI); + const int delta[2] = { + BLI_rcti_size_x(rect), + BLI_rcti_size_y(rect), + }; + + if (WM_event_drag_test_with_delta(event, delta)) { + int theta = round_fl_to_int(4.0f * atan2f((float)delta[1], (float)delta[0]) / (float)M_PI); int val = EVT_GESTURE_W; if (theta == 0) { diff --git a/source/blender/windowmanager/intern/wm_gesture_ops.c b/source/blender/windowmanager/intern/wm_gesture_ops.c index 9161c97374c..8273f7059cc 100644 --- a/source/blender/windowmanager/intern/wm_gesture_ops.c +++ b/source/blender/windowmanager/intern/wm_gesture_ops.c @@ -475,7 +475,7 @@ static void gesture_tweak_modal(bContext *C, const wmEvent *event) rect->xmax = event->x - gesture->winrct.xmin; rect->ymax = event->y - gesture->winrct.ymin; - if ((val = wm_gesture_evaluate(gesture))) { + if ((val = wm_gesture_evaluate(gesture, event))) { wmEvent tevent; wm_event_init_from_window(window, &tevent); diff --git a/source/blender/windowmanager/wm.h b/source/blender/windowmanager/wm.h index 5cbaea4f99b..fa375efb469 100644 --- a/source/blender/windowmanager/wm.h +++ b/source/blender/windowmanager/wm.h @@ -61,7 +61,7 @@ void wm_operatortypes_register(void); /* wm_gesture.c */ void wm_gesture_draw(struct wmWindow *win); -int wm_gesture_evaluate(wmGesture *gesture); +int wm_gesture_evaluate(wmGesture *gesture, const struct wmEvent *event); void wm_gesture_tag_redraw(bContext *C); /* wm_gesture_ops.c */ |