[Bf-blender-cvs] [86023928ba7] soc-2021-uv-editor-improvements: UV: Increment snapping based on new UV grid types

Siddhartha Jejurkar noreply at git.blender.org
Sun Jul 4 19:31:51 CEST 2021


Commit: 86023928ba794606f2372ce9f52c86c2b881041f
Author: Siddhartha Jejurkar
Date:   Sun Jul 4 22:51:34 2021 +0530
Branches: soc-2021-uv-editor-improvements
https://developer.blender.org/rB86023928ba794606f2372ce9f52c86c2b881041f

UV: Increment snapping based on new UV grid types

Since the default UV editor grid has been replaced with a new
subdividing grid and dynamic grid (T78389) has also been implemented,
increment snapping value needs to change according to the grid
configuration in use.

This commit ensures that the increment snapping value is changed
according to the grid dimensions currently in use.
Example - For a NxN grid the increment value is set as 1/N

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

M	source/blender/editors/include/ED_image.h
M	source/blender/editors/space_image/image_draw.c
M	source/blender/editors/transform/transform.c

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

diff --git a/source/blender/editors/include/ED_image.h b/source/blender/editors/include/ED_image.h
index aa923bcf245..a0d64037ab9 100644
--- a/source/blender/editors/include/ED_image.h
+++ b/source/blender/editors/include/ED_image.h
@@ -43,11 +43,12 @@ struct wmOperator;
 struct wmWindowManager;
 struct View2D;
 
-/* image_draw.c */
+/* image_draw.c (Utility functions - should probably be moved to a BKE header) */
 float ED_space_image_zoom_level(const struct View2D *v2d, const int grid_dimension);
 void ED_space_image_grid_steps(const int grid_dimension,
                                float grid_steps[8],
                                const bool is_dynamic_grid);
+float ED_space_image_increment_snap_value(const float grid_steps[8], const float zoom_factor);
 
 /* image_edit.c, exported for transform */
 struct Image *ED_space_image(struct SpaceImage *sima);
diff --git a/source/blender/editors/space_image/image_draw.c b/source/blender/editors/space_image/image_draw.c
index 5b5ec236890..88db49aaf49 100644
--- a/source/blender/editors/space_image/image_draw.c
+++ b/source/blender/editors/space_image/image_draw.c
@@ -1078,4 +1078,41 @@ void ED_space_image_grid_steps(const int grid_dimension,
       grid_steps[step] = powf(grid_dimension, step) * (1.0f / (powf(grid_dimension, 8)));
     }
   }
+}
+
+/* Calculate the increment snapping value for UV/image editor based on the zoom factor
+ * The code in here (except the offset part) is used in `grid_frag.glsl` (see `grid_res`) for
+ * drawing the grid overlay for the UV/Image editor */
+float ED_space_image_increment_snap_value(const float grid_steps[8], const float zoom_factor)
+{
+  /* Small offset on each grid_steps[] so that snapping value doesn't change until grid lines are
+   * significantly visible.
+   * Offset = 3/4 * (grid_steps[i] - grid_steps[i+1])
+   *
+   * Refer grid_frag.glsl to find out when grid lines actually start
+   * appearing */
+  if ((grid_steps[0] - ((9 * grid_steps[0]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[0];
+  }
+  else if ((grid_steps[1] - ((9 * grid_steps[1]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[1];
+  }
+  else if ((grid_steps[2] - ((9 * grid_steps[2]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[2];
+  }
+  else if ((grid_steps[3] - ((9 * grid_steps[3]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[3];
+  }
+  else if ((grid_steps[4] - ((9 * grid_steps[4]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[4];
+  }
+  else if ((grid_steps[5] - ((9 * grid_steps[5]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[5];
+  }
+  else if ((grid_steps[6] - ((9 * grid_steps[6]) / (4.0f * 4.0f))) > zoom_factor) {
+    return grid_steps[6];
+  }
+  else {
+    return grid_steps[7];
+  }
 }
\ No newline at end of file
diff --git a/source/blender/editors/transform/transform.c b/source/blender/editors/transform/transform.c
index 88f91d477b5..7072c06f364 100644
--- a/source/blender/editors/transform/transform.c
+++ b/source/blender/editors/transform/transform.c
@@ -28,6 +28,7 @@
 #include "DNA_gpencil_types.h"
 #include "DNA_mask_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_screen_types.h"
 
 #include "BLI_math.h"
 #include "BLI_rect.h"
@@ -1610,8 +1611,25 @@ static void initSnapSpatial(TransInfo *t, float r_snap[2])
     }
   }
   else if (t->spacetype == SPACE_IMAGE) {
-    r_snap[0] = 0.0625f;
-    r_snap[1] = 0.03125f;
+    /* Change the default value of 0.0625 since the UV editor grid is now dynamically subdividing
+     */
+    SpaceImage *sima = t->area->spacedata.first;
+    View2D *v2d = &t->region->v2d;
+    /* For a NxN grid. Keep in sync with value in overay_grid.c. Could be moved to View2D or
+     * SpaceImage to make it global indirectly */
+    int N = 4;
+    float zoom_factor = ED_space_image_zoom_level(v2d, N); /* Use a better name */
+    float grid_steps[8];
+
+    if (sima->flag & SI_DYNAMIC_GRID) {
+      ED_space_image_grid_steps(sima->dynamic_grid_size, grid_steps, true);
+    }
+    else {
+      ED_space_image_grid_steps(N, grid_steps, false);
+    }
+
+    r_snap[0] = ED_space_image_increment_snap_value(grid_steps, zoom_factor);
+    r_snap[1] = r_snap[0] / 2.0f;
   }
   else if (t->spacetype == SPACE_CLIP) {
     r_snap[0] = 0.125f;



More information about the Bf-blender-cvs mailing list