[Bf-blender-cvs] [1f7a791a539] master: Ruler: Implement Snap Gizmo

Germano Cavalcante noreply at git.blender.org
Wed May 27 05:17:26 CEST 2020


Commit: 1f7a791a539287c8f1cabc0f20f4c727e0fb36dc
Author: Germano Cavalcante
Date:   Wed May 27 00:16:32 2020 -0300
Branches: master
https://developer.blender.org/rB1f7a791a539287c8f1cabc0f20f4c727e0fb36dc

Ruler: Implement Snap Gizmo

The snap point can now be viewed when activating the tool.

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

M	source/blender/editors/space_view3d/view3d_gizmo_ruler.c

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

diff --git a/source/blender/editors/space_view3d/view3d_gizmo_ruler.c b/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
index 36c4b32e318..5d00a9ff950 100644
--- a/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
+++ b/source/blender/editors/space_view3d/view3d_gizmo_ruler.c
@@ -40,6 +40,7 @@
 #include "DNA_object_types.h"
 #include "DNA_view3d_types.h"
 
+#include "ED_gizmo_library.h"
 #include "ED_gizmo_utils.h"
 #include "ED_gpencil.h"
 #include "ED_screen.h"
@@ -56,6 +57,7 @@
 #include "WM_api.h"
 #include "WM_toolsystem.h"
 #include "WM_types.h"
+#include "wm.h"
 
 #include "view3d_intern.h" /* own include */
 
@@ -95,10 +97,6 @@ enum {
   RULER_STATE_DRAG,
 };
 
-enum {
-  RULER_SNAP_OK = (1 << 0),
-};
-
 struct RulerItem;
 
 typedef struct RulerInfo {
@@ -107,19 +105,25 @@ typedef struct RulerInfo {
   int snap_flag;
   int state;
 
-  struct SnapObjectContext *snap_context;
-
   /* wm state */
+  wmWindowManager *wm;
   wmWindow *win;
   ScrArea *area;
   ARegion *region; /* re-assigned every modal update */
 
   /* Track changes in state. */
   struct {
+#ifndef USE_SNAP_DETECT_FROM_KEYMAP_HACK
     bool do_snap;
+#endif
     bool do_thickness;
   } drag_state_prev;
 
+  struct {
+    wmGizmo *gizmo;
+    PropertyRNA *prop_prevpoint;
+  } snap_data;
+
 } RulerInfo;
 
 /* -------------------------------------------------------------------- */
@@ -270,26 +274,17 @@ static bool view3d_ruler_pick(wmGizmoGroup *gzgroup,
  * Ensure the 'snap_context' is only cached while dragging,
  * needed since the user may toggle modes between tool use.
  */
-static void ruler_state_set(bContext *C, RulerInfo *ruler_info, int state)
+static void ruler_state_set(RulerInfo *ruler_info, int state)
 {
-  Main *bmain = CTX_data_main(C);
   if (state == ruler_info->state) {
     return;
   }
 
-  /* always remove */
-  if (ruler_info->snap_context) {
-    ED_transform_snap_object_context_destroy(ruler_info->snap_context);
-    ruler_info->snap_context = NULL;
-  }
-
   if (state == RULER_STATE_NORMAL) {
     /* pass */
   }
   else if (state == RULER_STATE_DRAG) {
     memset(&ruler_info->drag_state_prev, 0x0, sizeof(ruler_info->drag_state_prev));
-    ruler_info->snap_context = ED_transform_snap_object_context_create_view3d(
-        bmain, CTX_data_scene(C), 0, ruler_info->region, CTX_wm_view3d(C));
   }
   else {
     BLI_assert(0);
@@ -308,13 +303,18 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
                                         RulerInfo *ruler_info,
                                         RulerItem *ruler_item,
                                         const int mval[2],
-                                        const bool do_thickness,
-                                        const bool do_snap)
+                                        const bool do_thickness
+#ifndef USE_SNAP_DETECT_FROM_KEYMAP_HACK
+                                        ,
+                                        const bool do_snap
+#endif
+)
 {
+  wmGizmo *snap_gizmo = ruler_info->snap_data.gizmo;
   const float eps_bias = 0.0002f;
   float dist_px = MVAL_MAX_PX_DIST * U.pixelsize; /* snap dist */
 
-  ruler_info->snap_flag &= ~RULER_SNAP_OK;
+  WM_gizmo_set_flag(snap_gizmo, WM_GIZMO_HIDDEN, true);
 
   if (ruler_item) {
     RulerInteraction *inter = ruler_item->gz.interaction_data;
@@ -325,6 +325,7 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
     if (do_thickness && inter->co_index != 1) {
       // Scene *scene = CTX_data_scene(C);
       // View3D *v3d = ruler_info->area->spacedata.first;
+      SnapObjectContext *snap_context = ED_gizmotypes_snap_3d_context_get(snap_gizmo);
       const float mval_fl[2] = {UNPACK2(mval)};
       float ray_normal[3];
       float ray_start[3];
@@ -332,7 +333,7 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
 
       co_other = ruler_item->co[inter->co_index == 0 ? 2 : 0];
 
-      if (ED_transform_snap_object_project_view3d(ruler_info->snap_context,
+      if (ED_transform_snap_object_project_view3d(snap_context,
                                                   depsgraph,
                                                   SCE_SNAP_MODE_FACE,
                                                   &(const struct SnapObjectParams){
@@ -347,7 +348,7 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
         negate_v3(ray_normal);
         /* add some bias */
         madd_v3_v3v3fl(ray_start, co, ray_normal, eps_bias);
-        ED_transform_snap_object_project_ray(ruler_info->snap_context,
+        ED_transform_snap_object_project_ray(snap_context,
                                              depsgraph,
                                              &(const struct SnapObjectParams){
                                                  .snap_select = SNAP_ALL,
@@ -360,7 +361,12 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
                                              NULL);
       }
     }
-    else if (do_snap) {
+    else
+#ifndef USE_SNAP_DETECT_FROM_KEYMAP_HACK
+        if (do_snap)
+#endif
+    {
+      View3D *v3d = ruler_info->area->spacedata.first;
       const float mval_fl[2] = {UNPACK2(mval)};
       float *prev_point = NULL;
 
@@ -375,23 +381,19 @@ static bool view3d_ruler_item_mousemove(struct Depsgraph *depsgraph,
           prev_point = ruler_item->co[0];
         }
       }
+      if (prev_point != NULL) {
+        RNA_property_float_set_array(
+            snap_gizmo->ptr, ruler_info->snap_data.prop_prevpoint, prev_point);
+      }
+
+      short snap_elem = ED_gizmotypes_snap_3d_update(
+          snap_gizmo, depsgraph, ruler_info->region, v3d, ruler_info->wm, mval_fl, co, NULL);
+
+      if (snap_elem) {
+        WM_gizmo_set_flag(snap_gizmo, WM_GIZMO_HIDDEN, false);
 
-      if (ED_transform_snap_object_project_view3d(
-              ruler_info->snap_context,
-              depsgraph,
-              (SCE_SNAP_MODE_VERTEX | SCE_SNAP_MODE_EDGE | SCE_SNAP_MODE_FACE |
-               SCE_SNAP_MODE_EDGE_MIDPOINT | SCE_SNAP_MODE_EDGE_PERPENDICULAR),
-              &(const struct SnapObjectParams){
-                  .snap_select = SNAP_ALL,
-                  .use_object_edit_cage = true,
-                  .use_occlusion_test = true,
-              },
-              mval_fl,
-              prev_point,
-              &dist_px,
-              co,
-              NULL)) {
-        ruler_info->snap_flag |= RULER_SNAP_OK;
+        /* Highlight snap_gizmo so that it is drawn. */
+        wm_gizmomap_highlight_set(snap_gizmo->parent_gzgroup->parent_gzmap, NULL, snap_gizmo, 0);
       }
     }
     return true;
@@ -418,6 +420,15 @@ static bGPDlayer *view3d_ruler_layer_get(bGPdata *gpd)
   return NULL;
 }
 
+static RulerItem *gzgroup_ruler_item_first_get(wmGizmoGroup *gzgroup)
+{
+#ifndef NDEBUG
+  RulerInfo *ruler_info = gzgroup->customdata;
+  BLI_assert(gzgroup->gizmos.first == ruler_info->snap_data.gizmo);
+#endif
+  return (RulerItem *)((wmGizmo *)gzgroup->gizmos.first)->next;
+}
+
 #define RULER_ID "RulerData3D"
 static bool view3d_ruler_to_gpencil(bContext *C, wmGizmoGroup *gzgroup)
 {
@@ -449,7 +460,7 @@ static bool view3d_ruler_to_gpencil(bContext *C, wmGizmoGroup *gzgroup)
   gpf = BKE_gpencil_layer_frame_get(gpl, CFRA, GP_GETFRAME_ADD_NEW);
   BKE_gpencil_free_strokes(gpf);
 
-  for (ruler_item = gzgroup->gizmos.first; ruler_item;
+  for (ruler_item = gzgroup_ruler_item_first_get(gzgroup); ruler_item;
        ruler_item = (RulerItem *)ruler_item->gz.next) {
     bGPDspoint *pt;
     int j;
@@ -557,6 +568,12 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz)
   uchar color_wire[3];
   float color_back[4] = {1.0f, 1.0f, 1.0f, 0.5f};
 
+  /* Pixel Space. */
+  GPU_matrix_push_projection();
+  GPU_matrix_push();
+  GPU_matrix_identity_set();
+  wmOrtho2_region_pixelspace(region);
+
   /* anti-aliased lines for more consistent appearance */
   GPU_line_smooth(true);
   GPU_line_width(1.0f);
@@ -886,27 +903,10 @@ static void gizmo_ruler_draw(const bContext *C, wmGizmo *gz)
 
   BLF_disable(blf_mono_font, BLF_ROTATION);
 
-#undef ARC_STEPS
-
-  /* draw snap */
-  if ((ruler_info->snap_flag & RULER_SNAP_OK) && (ruler_info->state == RULER_STATE_DRAG) &&
-      (ruler_item->gz.interaction_data != NULL)) {
-    RulerInteraction *inter = ruler_item->gz.interaction_data;
-    /* size from drawSnapping */
-    const float size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
-    float co_ss_snap[3];
-    ED_view3d_project_float_global(
-        region, ruler_item->co[inter->co_index], co_ss_snap, V3D_PROJ_TEST_NOP);
-
-    uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
-
-    immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
-    immUniformThemeColor3(TH_GIZMO_VIEW_ALIGN);
-
-    imm_draw_circle_wire_2d(pos, co_ss_snap[0], co_ss_snap[1], size * U.pixelsize, 32);
+  GPU_matrix_pop();
+  GPU_matrix_pop_projection();
 
-    immUnbindProgram();
-  }
+#undef ARC_STEPS
 }
 
 static int gizmo_ruler_test_select(bContext *UNUSED(C), wmGizmo *gz, const int mval[2])
@@ -939,35 +939,36 @@ static int gizmo_ruler_modal(bContext *C,
   RulerInfo *ruler_info = gz->parent_gzgroup->customdata;
   RulerItem *ruler_item = (RulerItem *)gz;
   ARegion *region = CTX_wm_region(C);
-  bool do_cursor_update = false;
+  bool do_cursor_update = (event->val == KM_RELEASE) || (event->type == MOUSEMOVE);
 
   ruler_info->region = region;
 
-  switch (event->type) {
-    case MOUSEMOVE: {
-      do_cursor_update = true;
-      break;
-    }
-  }
-
-  const bool do_snap = tweak_flag & WM_GIZMO_TWEAK_SNAP;
+#ifndef USE_SNAP_DETECT_FROM_KEYMAP_HACK
+  const bool do_snap = !(tweak_flag & WM_GIZMO_TWEAK_SNAP);
+#endif
   const bool do_thickness = tweak_flag & WM_GIZMO_TWEAK_PRECISE;
-  if ((ruler_info->drag_state_prev.do_snap != do_snap) ||
-      (ruler_info->drag_state_prev.do_thickness != do_thickness)) {
+  if ((ruler_info->drag_state_prev.do_thickness != do_thickness)) {
     do_cursor_update = true;
   }
 
   if (do_cursor_update) {
     if (ruler_info->state == RULER_STATE_DRAG) {
       struct Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
-      if (view3d_ru

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list