[Bf-blender-cvs] [82e8e5c871f] master: Cleanup: move click/drag events to functions

Campbell Barton noreply at git.blender.org
Thu May 30 07:29:42 CEST 2019


Commit: 82e8e5c871f86e65f332cdde3bd484a55e7e8572
Author: Campbell Barton
Date:   Thu May 30 14:17:39 2019 +1000
Branches: master
https://developer.blender.org/rB82e8e5c871f86e65f332cdde3bd484a55e7e8572

Cleanup: move click/drag events to functions

Simplifies future changes to dragging checks and avoids
each check for drag using slightly different logic.

===================================================================

M	source/blender/editors/interface/interface_handlers.c
M	source/blender/editors/space_node/node_select.c
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/WM_types.h
M	source/blender/windowmanager/intern/wm_event_system.c
M	source/blender/windowmanager/intern/wm_gesture.c
M	source/blender/windowmanager/intern/wm_gesture_ops.c
M	source/blender/windowmanager/wm.h

===================================================================

diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c
index f08829c6556..85e5ea1aee4 100644
--- a/source/blender/editors/interface/interface_handlers.c
+++ b/source/blender/editors/interface/interface_handlers.c
@@ -1762,7 +1762,7 @@ static bool ui_but_drag_init(bContext *C,
   /* Clamp the maximum to half the UI unit size so a high user preference
    * doesn't require the user to drag more then half the default button height. */
   const int drag_threshold = min_ii(
-      WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD,
+      WM_event_drag_threshold(event),
       (int)((UI_UNIT_Y / 2) * ui_block_to_window_scale(data->region, but->block)));
 
   if (ABS(data->dragstartx - event->x) + ABS(data->dragstarty - event->y) > drag_threshold) {
diff --git a/source/blender/editors/space_node/node_select.c b/source/blender/editors/space_node/node_select.c
index 11462358d88..78f36719880 100644
--- a/source/blender/editors/space_node/node_select.c
+++ b/source/blender/editors/space_node/node_select.c
@@ -611,13 +611,14 @@ static int node_select_modal(bContext *C, wmOperator *op, const wmEvent *event)
     return ret_value | OPERATOR_PASS_THROUGH;
   }
   else if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
-    const int dx = mval[0] - event->mval[0];
-    const int dy = mval[1] - event->mval[1];
-    const int drag_threshold = WM_EVENT_CURSOR_CLICK_DRAG_THRESHOLD;
+    const int drag_delta[2] = {
+        mval[0] - event->mval[0],
+        mval[1] - event->mval[1],
+    };
     /* If user moves mouse more than defined threshold, we consider select operator as
      * finished. Otherwise, it is still running until we get an 'release' event. In any
      * case, we pass through event, but select op is not finished yet. */
-    if (abs(dx) >= drag_threshold || abs(dy) >= drag_threshold) {
+    if (WM_event_drag_test_with_delta(event, drag_delta)) {
       return OPERATOR_FINISHED | OPERATOR_PASS_THROUGH;
     }
     else {
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 */



More information about the Bf-blender-cvs mailing list