[Bf-blender-cvs] [442b08aac0c] master: Snap Gizmo: support the WM_GIZMO_HIDDEN flag

Germano Cavalcante noreply at git.blender.org
Wed Jan 11 22:28:10 CET 2023


Commit: 442b08aac0c77c5167b31e651b260dfa04155c1c
Author: Germano Cavalcante
Date:   Wed Jan 11 18:20:05 2023 -0300
Branches: master
https://developer.blender.org/rB442b08aac0c77c5167b31e651b260dfa04155c1c

Snap Gizmo: support the WM_GIZMO_HIDDEN flag

Support snap cursor removal in poll callback, this makes it possible to
delay cursor removal if the gizmo is hidden.

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

M	source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/space_view3d/view3d_cursor_snap.c
M	source/blender/editors/space_view3d/view3d_placement.c
M	source/blender/windowmanager/intern/wm_event_system.cc

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

diff --git a/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c b/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
index 5cac8b93b88..8f2cb016f86 100644
--- a/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
+++ b/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
@@ -227,23 +227,64 @@ static void gizmo_snap_rna_snap_elem_index_get_fn(struct PointerRNA *UNUSED(ptr)
 /** \} */
 
 /* -------------------------------------------------------------------- */
-/** \name GIZMO_GT_snap_3d
+/** \name Snap Cursor Utils
  * \{ */
 
-static void snap_gizmo_setup(wmGizmo *gz)
+static void snap_cursor_free(SnapGizmo3D *snap_gizmo)
+{
+  if (snap_gizmo->snap_state) {
+    ED_view3d_cursor_snap_deactive(snap_gizmo->snap_state);
+    snap_gizmo->snap_state = NULL;
+  }
+}
+
+/* XXX: Delayed snap cursor removal. */
+static bool snap_cursor_poll(ARegion *region, void *data)
+{
+  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)data;
+  if (snap_gizmo->gizmo.flag & WM_GIZMO_HIDDEN) {
+    snap_cursor_free(snap_gizmo);
+    return false;
+  }
+
+  if (!WM_gizmomap_group_find_ptr(region->gizmo_map, snap_gizmo->gizmo.parent_gzgroup->type)) {
+    /* Wrong viewport. */
+    snap_cursor_free(snap_gizmo);
+    return false;
+  }
+  return true;
+}
+
+static void snap_cursor_init(SnapGizmo3D *snap_gizmo)
 {
-  gz->flag |= WM_GIZMO_NO_TOOLTIP;
-  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
   snap_gizmo->snap_state = ED_view3d_cursor_snap_active();
-  snap_gizmo->snap_state->gzgrp_type = gz->parent_gzgroup->type;
   snap_gizmo->snap_state->draw_point = true;
   snap_gizmo->snap_state->draw_plane = false;
 
-  rgba_float_to_uchar(snap_gizmo->snap_state->color_point, gz->color);
+  rgba_float_to_uchar(snap_gizmo->snap_state->color_point, snap_gizmo->gizmo.color);
+
+  snap_gizmo->snap_state->poll = snap_cursor_poll;
+  snap_gizmo->snap_state->poll_data = snap_gizmo;
 }
 
-static void snap_gizmo_draw(const bContext *UNUSED(C), wmGizmo *UNUSED(gz))
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name GIZMO_GT_snap_3d
+ * \{ */
+
+static void snap_gizmo_setup(wmGizmo *gz)
 {
+  gz->flag |= WM_GIZMO_NO_TOOLTIP;
+  snap_cursor_init((SnapGizmo3D *)gz);
+}
+
+static void snap_gizmo_draw(const bContext *UNUSED(C), wmGizmo *gz)
+{
+  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
+  if (snap_gizmo->snap_state == NULL) {
+    snap_cursor_init(snap_gizmo);
+  }
   /* All drawing is handled at the paint cursor. */
 }
 
@@ -292,10 +333,7 @@ static int snap_gizmo_invoke(bContext *UNUSED(C),
 
 static void snap_gizmo_free(wmGizmo *gz)
 {
-  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
-  if (snap_gizmo->snap_state) {
-    ED_view3d_cursor_snap_deactive(snap_gizmo->snap_state);
-  }
+  snap_cursor_free((SnapGizmo3D *)gz);
 }
 
 static void GIZMO_GT_snap_3d(wmGizmoType *gzt)
diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h
index f475eeee335..12313b3d244 100644
--- a/source/blender/editors/include/ED_view3d.h
+++ b/source/blender/editors/include/ED_view3d.h
@@ -320,7 +320,6 @@ typedef struct V3DSnapCursorState {
   uchar color_line[4];
   uchar color_point[4];
   uchar color_box[4];
-  struct wmGizmoGroupType *gzgrp_type; /* Force cursor to be drawn only when gizmo is available. */
   float *prevpoint;
   float box_dimensions[3];
   eSnapMode snap_elem_force; /* If SCE_SNAP_MODE_NONE, use scene settings. */
@@ -329,6 +328,9 @@ typedef struct V3DSnapCursorState {
   bool draw_point;
   bool draw_plane;
   bool draw_box;
+
+  bool (*poll)(struct ARegion *region, void *custom_poll_data);
+  void *poll_data;
 } V3DSnapCursorState;
 
 void ED_view3d_cursor_snap_state_default_set(V3DSnapCursorState *state);
diff --git a/source/blender/editors/space_view3d/view3d_cursor_snap.c b/source/blender/editors/space_view3d/view3d_cursor_snap.c
index 771a964dbcd..875097b4827 100644
--- a/source/blender/editors/space_view3d/view3d_cursor_snap.c
+++ b/source/blender/editors/space_view3d/view3d_cursor_snap.c
@@ -826,14 +826,10 @@ static bool v3d_cursor_snap_poll_fn(bContext *C)
     return false;
   };
 
+  /* Call this callback last and don't reuse the `state` as the caller can free the cursor. */
   V3DSnapCursorState *state = ED_view3d_cursor_snap_state_get();
-  if (state->gzgrp_type) {
-    /* Check the respective gizmo group is in the region. */
-    wmGizmoMap *gzmap = region->gizmo_map;
-    if (WM_gizmomap_group_find_ptr(gzmap, state->gzgrp_type) == NULL) {
-      /* Wrong viewport. */
-      return false;
-    }
+  if (state->poll && !state->poll(region, state->poll_data)) {
+    return false;
   }
 
   return true;
@@ -959,10 +955,11 @@ void ED_view3d_cursor_snap_state_default_set(V3DSnapCursorState *state)
   /* These values are temporarily set by the tool.
    * They are not convenient as default values.
    * So reset to null. */
-  g_data_intern.state_default.gzgrp_type = NULL;
   g_data_intern.state_default.prevpoint = NULL;
   g_data_intern.state_default.draw_plane = false;
   g_data_intern.state_default.draw_box = false;
+  g_data_intern.state_default.poll = NULL;
+  g_data_intern.state_default.poll_data = NULL;
 }
 
 V3DSnapCursorState *ED_view3d_cursor_snap_active(void)
diff --git a/source/blender/editors/space_view3d/view3d_placement.c b/source/blender/editors/space_view3d/view3d_placement.c
index 7588ad8d2c1..dfa4ba04e54 100644
--- a/source/blender/editors/space_view3d/view3d_placement.c
+++ b/source/blender/editors/space_view3d/view3d_placement.c
@@ -1515,11 +1515,21 @@ static void preview_plane_free_fn(void *customdata)
   ED_view3d_cursor_snap_deactive(snap_state);
 }
 
+static bool snap_cursor_poll(ARegion *region, void *data)
+{
+  if (WM_gizmomap_group_find_ptr(region->gizmo_map, (wmGizmoGroupType *)data) == NULL) {
+    /* Wrong viewport. */
+    return false;
+  }
+  return true;
+}
+
 static void WIDGETGROUP_placement_setup(const bContext *UNUSED(C), wmGizmoGroup *gzgroup)
 {
   V3DSnapCursorState *snap_state = ED_view3d_cursor_snap_active();
   if (snap_state) {
-    snap_state->gzgrp_type = gzgroup->type;
+    snap_state->poll = snap_cursor_poll;
+    snap_state->poll_data = gzgroup->type;
     snap_state->draw_plane = true;
 
     gzgroup->customdata = snap_state;
diff --git a/source/blender/windowmanager/intern/wm_event_system.cc b/source/blender/windowmanager/intern/wm_event_system.cc
index ba8f13dd5c3..14812c4335d 100644
--- a/source/blender/windowmanager/intern/wm_event_system.cc
+++ b/source/blender/windowmanager/intern/wm_event_system.cc
@@ -3573,10 +3573,10 @@ static ARegion *region_event_inside(bContext *C, const int xy[2])
   return nullptr;
 }
 
-static void wm_paintcursor_tag(bContext *C, wmPaintCursor *pc, ARegion *region)
+static void wm_paintcursor_tag(bContext *C, wmWindowManager *wm, ARegion *region)
 {
   if (region) {
-    for (; pc; pc = pc->next) {
+    LISTBASE_FOREACH_MUTABLE (wmPaintCursor *, pc, &wm->paintcursors) {
       if (pc->poll == nullptr || pc->poll(C)) {
         wmWindow *win = CTX_wm_window(C);
         WM_paint_cursor_tag_redraw(win, region);
@@ -3598,7 +3598,7 @@ static void wm_paintcursor_test(bContext *C, const wmEvent *event)
     ARegion *region = CTX_wm_region(C);
 
     if (region) {
-      wm_paintcursor_tag(C, static_cast<wmPaintCursor *>(wm->paintcursors.first), region);
+      wm_paintcursor_tag(C, wm, region);
     }
 
     /* If previous position was not in current region, we have to set a temp new context. */
@@ -3608,8 +3608,7 @@ static void wm_paintcursor_test(bContext *C, const wmEvent *event)
       CTX_wm_area_set(C, area_event_inside(C, event->prev_xy));
       CTX_wm_region_set(C, region_event_inside(C, event->prev_xy));
 
-      wm_paintcursor_tag(
-          C, static_cast<wmPaintCursor *>(wm->paintcursors.first), CTX_wm_region(C));
+      wm_paintcursor_tag(C, wm, CTX_wm_region(C));
 
       CTX_wm_area_set(C, area);
       CTX_wm_region_set(C, region);



More information about the Bf-blender-cvs mailing list