[Bf-blender-cvs] [f623b1a5eca] transform-snap-base: Transform: New feature to set a custom 'Snap With'

Germano Cavalcante noreply at git.blender.org
Tue Nov 17 14:39:34 CET 2020


Commit: f623b1a5eca87e335ea7ebd56266115c2b9bdf9f
Author: Germano Cavalcante
Date:   Mon Nov 16 08:38:17 2020 -0300
Branches: transform-snap-base
https://developer.blender.org/rBf623b1a5eca87e335ea7ebd56266115c2b9bdf9f

Transform: New feature to set a custom 'Snap With'

Ref T66484

Basically the idea is to use a modal keymap to change the current Base Point
defined by `Snap With` by one that can be chosen through snapping.

{F9170047}

I prefer to avoid using any of the modifier buttons (Shift, Alt, Ctrl) as
they can be used in the future to allow navigation during transform operations.

So the shortcut chosen in this patch is the {key B}.

Note:
This feature is not enabled if the scene snap option is Incremental or grid.

Maniphest Tasks: T66484

Differential Revision: https://developer.blender.org/D9415

# Conflicts:
#	source/tools

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

M	release/scripts/presets/keyconfig/keymap_data/blender_default.py
M	release/scripts/presets/keyconfig/keymap_data/industry_compatible_data.py
M	source/blender/editors/transform/transform.c
M	source/blender/editors/transform/transform.h
M	source/blender/editors/transform/transform_snap.c
M	source/blender/editors/transform/transform_snap.h
M	source/blender/editors/transform/transform_snap_object.c

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

diff --git a/release/scripts/presets/keyconfig/keymap_data/blender_default.py b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
index d66d7c45dd4..f8090625bf2 100644
--- a/release/scripts/presets/keyconfig/keymap_data/blender_default.py
+++ b/release/scripts/presets/keyconfig/keymap_data/blender_default.py
@@ -5039,6 +5039,7 @@ def km_transform_modal_map(_params):
         ("INSERTOFS_TOGGLE_DIR", {"type": 'T', "value": 'PRESS'}, None),
         ("AUTOCONSTRAIN", {"type": 'MIDDLEMOUSE', "value": 'PRESS'}, None),
         ("AUTOCONSTRAINPLANE", {"type": 'MIDDLEMOUSE', "value": 'PRESS', "shift": True}, None),
+        ("EDIT_SNAPWITH", {"type": 'B', "value": 'PRESS', "repeat": True}, None),
     ])
 
     return keymap
diff --git a/release/scripts/presets/keyconfig/keymap_data/industry_compatible_data.py b/release/scripts/presets/keyconfig/keymap_data/industry_compatible_data.py
index c79a59145cf..337ee0b4fef 100644
--- a/release/scripts/presets/keyconfig/keymap_data/industry_compatible_data.py
+++ b/release/scripts/presets/keyconfig/keymap_data/industry_compatible_data.py
@@ -3944,6 +3944,7 @@ def km_transform_modal_map(_params):
         ("INSERTOFS_TOGGLE_DIR", {"type": 'T', "value": 'PRESS'}, None),
         ("AUTOCONSTRAIN", {"type": 'MIDDLEMOUSE', "value": 'PRESS'}, None),
         ("AUTOCONSTRAINPLANE", {"type": 'MIDDLEMOUSE', "value": 'PRESS', "shift": True}, None),
+        ("EDIT_SNAPWITH", {"type": 'B', "value": 'PRESS', "repeat": True}, None),
     ])
 
     return keymap
diff --git a/source/blender/editors/transform/transform.c b/source/blender/editors/transform/transform.c
index 77ce9c10c77..4419cd6fa9f 100644
--- a/source/blender/editors/transform/transform.c
+++ b/source/blender/editors/transform/transform.c
@@ -636,6 +636,15 @@ static bool transform_modal_item_poll(const wmOperator *op, int value)
       }
       break;
     }
+    case TFM_MODAL_EDIT_SNAPWITH: {
+      if (t->spacetype != SPACE_VIEW3D) {
+        return false;
+      }
+      if (!(t->tsnap.mode & ~(SCE_SNAP_MODE_GRID | SCE_SNAP_MODE_INCREMENT))) {
+        return false;
+      }
+      break;
+    }
   }
   return true;
 }
@@ -685,6 +694,7 @@ wmKeyMap *transform_modal_keymap(wmKeyConfig *keyconf)
       {TFM_MODAL_RESIZE, "RESIZE", 0, "Resize", ""},
       {TFM_MODAL_AUTOCONSTRAINT, "AUTOCONSTRAIN", 0, "Automatic Constraint", ""},
       {TFM_MODAL_AUTOCONSTRAINTPLANE, "AUTOCONSTRAINPLANE", 0, "Automatic Constraint Plane", ""},
+      {TFM_MODAL_EDIT_SNAPWITH, "EDIT_SNAPWITH", 0, "Edit snap with", ""},
       {0, NULL, 0, NULL, NULL},
   };
 
@@ -1108,6 +1118,9 @@ int transformEvent(TransInfo *t, const wmEvent *event)
           handled = true;
         }
         break;
+      case TFM_MODAL_EDIT_SNAPWITH:
+        tranform_snap_snapwith_init(t);
+        break;
       /* Those two are only handled in transform's own handler, see T44634! */
       case TFM_MODAL_EDGESLIDE_UP:
       case TFM_MODAL_EDGESLIDE_DOWN:
@@ -1206,12 +1219,15 @@ int transformEvent(TransInfo *t, const wmEvent *event)
       default: {
         /* Disable modifiers. */
         int modifiers = t->modifiers;
-        modifiers &= ~MOD_CONSTRAINT_SELECT;
-        modifiers &= ~MOD_CONSTRAINT_PLANE;
+        modifiers &= ~(MOD_CONSTRAINT_SELECT | MOD_CONSTRAINT_PLANE | MOD_EDIT_SNAPWITH);
         if (modifiers != t->modifiers) {
           if (t->modifiers & (MOD_CONSTRAINT_SELECT | MOD_CONSTRAINT_PLANE)) {
             postSelectConstraint(t);
           }
+          else {
+            BLI_assert(t->modifiers & MOD_EDIT_SNAPWITH);
+            tranform_snap_snapwith_end(t);
+          }
           t->modifiers = modifiers;
           t->redraw |= TREDRAW_HARD;
           handled = true;
@@ -1913,14 +1929,18 @@ void transformApply(bContext *C, TransInfo *t)
 
   if ((t->redraw & TREDRAW_HARD) || (t->draw_handle_apply == NULL && (t->redraw & TREDRAW_SOFT))) {
     selectConstraint(t);
-    if (t->transform) {
+    if (t->modifiers & MOD_EDIT_SNAPWITH) {
+      tranform_snap_snapwith_update(t);
+    }
+    else if (t->transform) {
       t->transform(t, t->mval); /* calls recalcData() */
-      viewRedrawForce(C, t);
+      t->redraw |= TREDRAW_SOFT;
     }
-    t->redraw = TREDRAW_NOTHING;
   }
-  else if (t->redraw & TREDRAW_SOFT) {
+
+  if (t->redraw & TREDRAW_SOFT) {
     viewRedrawForce(C, t);
+    t->redraw = TREDRAW_NOTHING;
   }
 
   /* If auto confirm is on, break after one pass */
diff --git a/source/blender/editors/transform/transform.h b/source/blender/editors/transform/transform.h
index 227330e8524..97541add558 100644
--- a/source/blender/editors/transform/transform.h
+++ b/source/blender/editors/transform/transform.h
@@ -86,9 +86,9 @@ typedef struct TransSnap {
   /* Snapped Element Type (currently for objects only). */
   char snapElem;
   /** snapping from this point (in global-space). */
-  float snapPoint[3];
-  /** to this point (in global-space). */
   float snapTarget[3];
+  /** to this point (in global-space). */
+  float snapPoint[3];
   float snapNormal[3];
   char snapNodeBorder;
   ListBase points;
@@ -503,6 +503,7 @@ enum {
   MOD_SNAP = 1 << 2,
   MOD_SNAP_INVERT = 1 << 3,
   MOD_CONSTRAINT_PLANE = 1 << 4,
+  MOD_EDIT_SNAPWITH = 1 << 5,
 };
 
 /* use node center for transform instead of upper-left corner.
@@ -589,6 +590,8 @@ enum {
 
   TFM_MODAL_AUTOCONSTRAINT = 28,
   TFM_MODAL_AUTOCONSTRAINTPLANE = 29,
+
+  TFM_MODAL_EDIT_SNAPWITH = 30,
 };
 
 /** \} */
diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c
index f1c4c243780..a2b10041e09 100644
--- a/source/blender/editors/transform/transform_snap.c
+++ b/source/blender/editors/transform/transform_snap.c
@@ -128,6 +128,9 @@ bool validSnap(const TransInfo *t)
 
 bool activeSnap(const TransInfo *t)
 {
+  if (t->modifiers & MOD_EDIT_SNAPWITH) {
+    return true;
+  }
   return ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
          ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT);
 }
@@ -177,8 +180,9 @@ void drawSnapping(const struct bContext *C, TransInfo *t)
   activeCol[3] = 192;
 
   if (t->spacetype == SPACE_VIEW3D) {
-    bool draw_target = (t->tsnap.status & TARGET_INIT) &&
-                       (t->scene->toolsettings->snap_mode & SCE_SNAP_MODE_EDGE_PERPENDICULAR);
+    bool draw_target = (t->modifiers & MOD_EDIT_SNAPWITH) ||
+                       (t->tsnap.status & TARGET_INIT) &&
+                           (t->scene->toolsettings->snap_mode & SCE_SNAP_MODE_EDGE_PERPENDICULAR);
 
     if (draw_target || validSnap(t)) {
       const float *loc_cur = NULL;
@@ -1115,6 +1119,11 @@ static void TargetSnapClosest(TransInfo *t)
   }
 }
 
+static void TargetSnapCustom(TransInfo *t)
+{
+  t->tsnap.status |= TARGET_INIT;
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
@@ -1130,7 +1139,7 @@ short snapObjectsTransform(
       t->depsgraph,
       t->settings->snap_mode,
       &(const struct SnapObjectParams){
-          .snap_select = t->tsnap.modeSelect,
+          .snap_select = t->modifiers & MOD_EDIT_SNAPWITH ? SNAP_ALL : t->tsnap.modeSelect,
           .use_object_edit_cage = (t->flag & T_EDIT) != 0,
           .use_occlusion_test = t->settings->snap_mode != SCE_SNAP_MODE_FACE,
           .use_backface_culling = t->tsnap.use_backface_culling,
@@ -1546,6 +1555,42 @@ bool transform_snap_increment(TransInfo *t, float *val)
   return true;
 }
 
+void tranform_snap_snapwith_init(TransInfo *t)
+{
+  t->modifiers |= MOD_EDIT_SNAPWITH;
+  t->tsnap.targetSnap = TargetSnapCustom;
+  t->tsnap.targetSnap(t);
+  restoreTransObjects(t);
+}
+
+void tranform_snap_snapwith_update(TransInfo *t)
+{
+  BLI_assert(t->modifiers & MOD_EDIT_SNAPWITH);
+  double current = PIL_check_seconds_timer();
+
+  /* Time base quirky code to go around findnearest slowness */
+  /* TODO: add exception for object mode, no need to slow it down then. */
+  if (current - t->tsnap.last >= 0.01) {
+    t->tsnap.calcSnap(t, NULL);
+    t->tsnap.last = current;
+  }
+
+  if (validSnap(t)) {
+    copy_v3_v3(t->tsnap.snapTarget, t->tsnap.snapPoint);
+    t->redraw |= TREDRAW_SOFT;
+  }
+}
+
+void tranform_snap_snapwith_end(TransInfo *t)
+{
+  BLI_assert(t->modifiers & MOD_EDIT_SNAPWITH);
+  t->modifiers &= ~MOD_EDIT_SNAPWITH;
+
+  /* Force a reinit with a current #t->mval. */
+  initMouseInput(t, &t->mouse, t->center2d, t->mval, false);
+  applyMouseInput(t, &t->mouse, t->mval, t->values);
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/editors/transform/transform_snap.h b/source/blender/editors/transform/transform_snap.h
index 5bee572c603..732c8ab8dbd 100644
--- a/source/blender/editors/transform/transform_snap.h
+++ b/source/blender/editors/transform/transform_snap.h
@@ -59,6 +59,10 @@ bool transformModeUseSnap(const TransInfo *t);
 bool transform_snap_increment(TransInfo *t, float *val);
 bool transform_snap_grid(TransInfo *t, float *val);
 
+void tranform_snap_snapwith_init(TransInfo *t);
+void tranform_snap_snapwith_update(TransInfo *t);
+void tranform_snap_snapwith_end(TransInfo *t);
+
 void snapSequenceBounds(TransInfo *t, const int mval[2]);
 
 bool activeSnap(const TransInfo *t);
diff --git a/source/blender/editors/transform/transform_snap_object.c b/source/blender/editors/transform/transform_snap_object.c
index 50b7c6d147b..9603692770b 100644
--- a/source/blender/editors/transform/transform_snap_object.c
+++ b/source/blender/editors/transform/transform_snap_object.c
@@ -395,7 +395,7 @@ static void iter_snap_objects(SnapObjectContext *sctx,
       continue;
     }
 
-    if (base->flag_legacy & BA_TRANSFORM_LOCKED_IN_PLACE) {
+    if (snap_select == SNAP_ALL || base->flag_legacy & BA_TRANSFORM_LOCKED_IN_PLACE) {
       /* pass */
     }
     else if (base->flag_legacy & BA_SNAP_FIX_DEPS_FIASCO) {



More information about the Bf-blender-cvs mailing list