[Bf-blender-cvs] [76cdcc2bcad] master: Cleanup/Refactor: Use flags instead of bool to configure the snap gizmo

Germano Cavalcante noreply at git.blender.org
Thu Apr 1 17:48:38 CEST 2021


Commit: 76cdcc2bcadc8a8fecd2254ec767169b34893c6d
Author: Germano Cavalcante
Date:   Thu Apr 1 12:32:59 2021 -0300
Branches: master
https://developer.blender.org/rB76cdcc2bcadc8a8fecd2254ec767169b34893c6d

Cleanup/Refactor: Use flags instead of bool to configure the snap gizmo

This simplifies the addition of future improvements.

Also make it more practical to expose as a parameter of gizmo for Python.

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

M	source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
M	source/blender/editors/include/ED_gizmo_library.h
M	source/blender/editors/space_view3d/view3d_placement.c

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

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 c21535de4d5..1d18c82d896 100644
--- a/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
+++ b/source/blender/editors/gizmo_library/gizmo_types/snap3d_gizmo.c
@@ -78,9 +78,9 @@ typedef struct SnapGizmo3D {
 #endif
 
   /* Setup. */
+  eSnapGizmo flag;
   float *prevpoint;
   float prevpoint_stack[3];
-  int use_snap_override;
   short snap_elem_force;
 
   /* Return values. */
@@ -286,26 +286,32 @@ SnapObjectContext *ED_gizmotypes_snap_3d_context_ensure(Scene *scene,
   return snap_gizmo->snap_context_v3d;
 }
 
-bool ED_gizmotypes_snap_3d_invert_snap_get(struct wmGizmo *gz)
+void ED_gizmotypes_snap_3d_flag_set(struct wmGizmo *gz, eSnapGizmo flag)
 {
-#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
   SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
-  return snap_gizmo->invert_snap;
-#else
-  return false;
-#endif
+  snap_gizmo->flag |= flag;
 }
 
-void ED_gizmotypes_snap_3d_toggle_set(wmGizmo *gz, bool enable)
+void ED_gizmotypes_snap_3d_flag_clear(struct wmGizmo *gz, eSnapGizmo flag)
 {
   SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
-  snap_gizmo->use_snap_override = (int)enable;
+  snap_gizmo->flag &= ~flag;
 }
 
-void ED_gizmotypes_snap_3d_toggle_clear(wmGizmo *gz)
+bool ED_gizmotypes_snap_3d_flag_test(struct wmGizmo *gz, eSnapGizmo flag)
 {
   SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
-  snap_gizmo->use_snap_override = -1;
+  return (snap_gizmo->flag & flag) != 0;
+}
+
+bool ED_gizmotypes_snap_3d_invert_snap_get(struct wmGizmo *gz)
+{
+#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
+  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
+  return snap_gizmo->invert_snap;
+#else
+  return false;
+#endif
 }
 
 bool ED_gizmotypes_snap_3d_is_enabled(wmGizmo *gz)
@@ -324,22 +330,11 @@ short ED_gizmotypes_snap_3d_update(wmGizmo *gz,
   SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
   snap_gizmo->is_enabled = false;
 
-  if (snap_gizmo->use_snap_override != -1) {
-    if (snap_gizmo->use_snap_override == false) {
-      snap_gizmo->snap_elem = 0;
-      return 0;
-    }
-  }
-
-#ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
-  snap_gizmo->invert_snap = invert_snap(snap_gizmo, wm);
-#endif
-
-  eventstate_save(snap_gizmo, wm);
   Scene *scene = DEG_get_input_scene(depsgraph);
 
 #ifdef USE_SNAP_DETECT_FROM_KEYMAP_HACK
-  if (snap_gizmo->use_snap_override == -1) {
+  if ((snap_gizmo->flag & ED_SNAPGIZMO_TOGGLE_ALWAYS_TRUE) == 0) {
+    snap_gizmo->invert_snap = invert_snap(snap_gizmo, wm);
     const ToolSettings *ts = scene->toolsettings;
     if (snap_gizmo->invert_snap != !(ts->snap_flag & SCE_SNAP)) {
       snap_gizmo->snap_elem = 0;
@@ -348,6 +343,8 @@ short ED_gizmotypes_snap_3d_update(wmGizmo *gz,
   }
 #endif
 
+  eventstate_save(snap_gizmo, wm);
+
   snap_gizmo->is_enabled = true;
 
   float co[3], no[3];
@@ -555,9 +552,6 @@ static void gizmo_snap_rna_snap_elem_index_get_fn(struct PointerRNA *ptr,
 
 static void snap_gizmo_setup(wmGizmo *gz)
 {
-  SnapGizmo3D *snap_gizmo = (SnapGizmo3D *)gz;
-  snap_gizmo->use_snap_override = -1;
-
   /* Flags. */
   gz->flag |= WM_GIZMO_NO_TOOLTIP;
 }
diff --git a/source/blender/editors/include/ED_gizmo_library.h b/source/blender/editors/include/ED_gizmo_library.h
index 1c795896f86..58b2cb80bbe 100644
--- a/source/blender/editors/include/ED_gizmo_library.h
+++ b/source/blender/editors/include/ED_gizmo_library.h
@@ -261,9 +261,15 @@ struct SnapObjectContext *ED_gizmotypes_snap_3d_context_ensure(struct Scene *sce
                                                                const struct View3D *v3d,
                                                                struct wmGizmo *gz);
 
+typedef enum {
+  ED_SNAPGIZMO_TOGGLE_ALWAYS_TRUE = 1 << 0,
+} eSnapGizmo;
+
+void ED_gizmotypes_snap_3d_flag_set(struct wmGizmo *gz, eSnapGizmo flag);
+void ED_gizmotypes_snap_3d_flag_clear(struct wmGizmo *gz, eSnapGizmo flag);
+bool ED_gizmotypes_snap_3d_flag_test(struct wmGizmo *gz, eSnapGizmo flag);
+
 bool ED_gizmotypes_snap_3d_invert_snap_get(struct wmGizmo *gz);
-void ED_gizmotypes_snap_3d_toggle_set(struct wmGizmo *gz, bool enable);
-void ED_gizmotypes_snap_3d_toggle_clear(struct wmGizmo *gz);
 bool ED_gizmotypes_snap_3d_is_enabled(struct wmGizmo *gz);
 
 short ED_gizmotypes_snap_3d_update(struct wmGizmo *gz,
diff --git a/source/blender/editors/space_view3d/view3d_placement.c b/source/blender/editors/space_view3d/view3d_placement.c
index b0dfd4af7c5..8172742a756 100644
--- a/source/blender/editors/space_view3d/view3d_placement.c
+++ b/source/blender/editors/space_view3d/view3d_placement.c
@@ -1499,7 +1499,7 @@ static int view3d_interactive_add_modal(bContext *C, wmOperator *op, const wmEve
     ipd->is_snap_found = false;
     if (ipd->use_snap) {
       if (ipd->snap_gizmo != NULL) {
-        ED_gizmotypes_snap_3d_toggle_set(ipd->snap_gizmo, ipd->use_snap);
+        ED_gizmotypes_snap_3d_flag_set(ipd->snap_gizmo, ED_SNAPGIZMO_TOGGLE_ALWAYS_TRUE);
         if (ED_gizmotypes_snap_3d_update(ipd->snap_gizmo,
                                          CTX_data_ensure_evaluated_depsgraph(C),
                                          ipd->region,
@@ -1509,7 +1509,7 @@ static int view3d_interactive_add_modal(bContext *C, wmOperator *op, const wmEve
           ED_gizmotypes_snap_3d_data_get(ipd->snap_gizmo, ipd->snap_co, NULL, NULL, NULL);
           ipd->is_snap_found = true;
         }
-        ED_gizmotypes_snap_3d_toggle_clear(ipd->snap_gizmo);
+        ED_gizmotypes_snap_3d_flag_clear(ipd->snap_gizmo, ED_SNAPGIZMO_TOGGLE_ALWAYS_TRUE);
       }
     }



More information about the Bf-blender-cvs mailing list