Fix dashed line drawing
[blender.git] / source / blender / windowmanager / intern / wm_gesture.c
index b264f6a570d6e38a20ae90ce01299ab13624766c..b79de4370d032a964c62273fbeaf5d02de7a3d54 100644 (file)
@@ -62,7 +62,7 @@ wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
   gesture->type = type;
   gesture->event_type = event->type;
   gesture->winrct = ar->winrct;
-  gesture->userdata_free = true; /* Free if userdata is set. */
+  gesture->user_data.use_free = true; /* Free if userdata is set. */
   gesture->modal_state = GESTURE_MODAL_NOP;
 
   if (ELEM(type,
@@ -106,9 +106,7 @@ void WM_gesture_end(bContext *C, wmGesture *gesture)
   }
   BLI_remlink(&win->gesture, gesture);
   MEM_freeN(gesture->customdata);
-  if (gesture->userdata && gesture->userdata_free) {
-    MEM_freeN(gesture->userdata);
-  }
+  WM_generic_user_data_free(&gesture->user_data);
   MEM_freeN(gesture);
 }
 
@@ -130,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);
-    float tweak_threshold = U.tweak_threshold * U.dpi_fac;
-    if (abs(dx) + abs(dy) > tweak_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) {
@@ -165,14 +165,30 @@ int wm_gesture_evaluate(wmGesture *gesture)
 
 #if 0
       /* debug */
-      if (val == 1) printf("tweak north\n");
-      if (val == 2) printf("tweak north-east\n");
-      if (val == 3) printf("tweak east\n");
-      if (val == 4) printf("tweak south-east\n");
-      if (val == 5) printf("tweak south\n");
-      if (val == 6) printf("tweak south-west\n");
-      if (val == 7) printf("tweak west\n");
-      if (val == 8) printf("tweak north-west\n");
+      if (val == 1) {
+        printf("tweak north\n");
+      }
+      if (val == 2) {
+        printf("tweak north-east\n");
+      }
+      if (val == 3) {
+        printf("tweak east\n");
+      }
+      if (val == 4) {
+        printf("tweak south-east\n");
+      }
+      if (val == 5) {
+        printf("tweak south\n");
+      }
+      if (val == 6) {
+        printf("tweak south-west\n");
+      }
+      if (val == 7) {
+        printf("tweak west\n");
+      }
+      if (val == 8) {
+        printf("tweak north-west\n");
+      }
 #endif
       return val;
     }
@@ -199,6 +215,7 @@ static void wm_gesture_draw_line(wmGesture *gt)
   immUniformArray4fv(
       "colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
   immUniform1f("dash_width", 8.0f);
+  immUniform1f("dash_factor", 0.5f);
 
   float xmin = (float)rect->xmin;
   float ymin = (float)rect->ymin;
@@ -241,13 +258,15 @@ static void wm_gesture_draw_rect(wmGesture *gt)
   immUniformArray4fv(
       "colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
   immUniform1f("dash_width", 8.0f);
+  immUniform1f("dash_factor", 0.5f);
 
   imm_draw_box_wire_2d(
       shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, (float)rect->ymax);
 
   immUnbindProgram();
 
-  // wm_gesture_draw_line(gt); // draws a diagonal line in the lined box to test wm_gesture_draw_line
+  /* draws a diagonal line in the lined box to test wm_gesture_draw_line */
+  // wm_gesture_draw_line(gt);
 }
 
 static void wm_gesture_draw_circle(wmGesture *gt)
@@ -278,6 +297,7 @@ static void wm_gesture_draw_circle(wmGesture *gt)
   immUniformArray4fv(
       "colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
   immUniform1f("dash_width", 4.0f);
+  immUniform1f("dash_factor", 0.5f);
 
   imm_draw_circle_wire_2d(shdr_pos, (float)rect->xmin, (float)rect->ymin, (float)rect->xmax, 40);
 
@@ -401,6 +421,7 @@ static void wm_gesture_draw_lasso(wmGesture *gt, bool filled)
   immUniformArray4fv(
       "colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
   immUniform1f("dash_width", 2.0f);
+  immUniform1f("dash_factor", 0.5f);
 
   immBegin((gt->type == WM_GESTURE_LASSO) ? GPU_PRIM_LINE_LOOP : GPU_PRIM_LINE_STRIP, numverts);
 
@@ -434,6 +455,7 @@ static void wm_gesture_draw_cross(wmWindow *win, wmGesture *gt)
   immUniformArray4fv(
       "colors", (float *)(float[][4]){{0.4f, 0.4f, 0.4f, 1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}, 2);
   immUniform1f("dash_width", 8.0f);
+  immUniform1f("dash_factor", 0.5f);
 
   immBegin(GPU_PRIM_LINES, 4);
 
@@ -471,8 +493,11 @@ void wm_gesture_draw(wmWindow *win)
     if (gt->type == WM_GESTURE_RECT) {
       wm_gesture_draw_rect(gt);
     }
-    //      else if (gt->type == WM_GESTURE_TWEAK)
-    //          wm_gesture_draw_line(gt);
+#if 0
+    else if (gt->type == WM_GESTURE_TWEAK) {
+      wm_gesture_draw_line(gt);
+    }
+#endif
     else if (gt->type == WM_GESTURE_CIRCLE) {
       wm_gesture_draw_circle(gt);
     }