From b670e7a82c9b26e93cc005e7e0a553f2a768e176 Mon Sep 17 00:00:00 2001
From: Christoph Lendenfeld <chris.lenden@gmail.com>
Date: Thu, 9 Nov 2023 14:38:30 +0100
Subject: [PATCH] Fix #114588: Graph Editor increment snap not working

Restoring the behavior while transforming keys and pressing `Ctrl`

The regular snapping code has the feature that
when you press `Ctrl` you can toggle the snapping on and off.
Prior to 4.0 the snapping for the Graph Editor used a
completely different system, including a different flag to toggle snapping.

Because of that, the flag that the regular snapping
code uses was never set by default. So the system thought it was disabled.

Now when you press `Ctrl` you would enable that
flag and the snapping code would run.
It would snap to increments because that is the mode that
is returned for the Graph Editor space type.
(Note that this is the mode of the generic snapping
system, not of the Graph Editor specific one)

However at the same time, doing that would disable the
Graph Editor specific snapping code.
So the snapped values from the original system would bubble through.

This did not occur in the Dope Sheet and NLA Editor,
because those never returned a snapping mode.
Those still have a different behavior to 3.6 where they
now snap to seconds, instead of not snapping at all.

Pull Request: https://projects.blender.org/blender/blender/pulls/114607
---
 .../transform/transform_convert_graph.cc      | 16 ---------------
 .../editors/transform/transform_snap.cc       | 20 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/source/blender/editors/transform/transform_convert_graph.cc b/source/blender/editors/transform/transform_convert_graph.cc
index 127b49c50ab..c2e8b613b6d 100644
--- a/source/blender/editors/transform/transform_convert_graph.cc
+++ b/source/blender/editors/transform/transform_convert_graph.cc
@@ -644,18 +644,6 @@ static bool fcu_test_selected(FCurve *fcu)
   return false;
 }
 
-static void invert_snap(eSnapMode &snap_mode)
-{
-  if (snap_mode & SCE_SNAP_TO_FRAME) {
-    snap_mode &= ~SCE_SNAP_TO_FRAME;
-    snap_mode |= SCE_SNAP_TO_SECOND;
-  }
-  else if (snap_mode & SCE_SNAP_TO_SECOND) {
-    snap_mode &= ~SCE_SNAP_TO_SECOND;
-    snap_mode |= SCE_SNAP_TO_FRAME;
-  }
-}
-
 /* This function is called on recalc_data to apply the transforms applied
  * to the transdata on to the actual keyframe data
  */
@@ -668,10 +656,6 @@ static void flushTransGraphData(TransInfo *t)
 
   eSnapMode snap_mode = t->tsnap.mode;
 
-  if (t->modifiers & MOD_SNAP_INVERT) {
-    invert_snap(snap_mode);
-  }
-
   TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
   /* flush to 2d vector from internally used 3d vector */
   for (a = 0,
diff --git a/source/blender/editors/transform/transform_snap.cc b/source/blender/editors/transform/transform_snap.cc
index 136bf630cbf..5418124bc27 100644
--- a/source/blender/editors/transform/transform_snap.cc
+++ b/source/blender/editors/transform/transform_snap.cc
@@ -129,10 +129,28 @@ bool validSnap(const TransInfo *t)
 
 void transform_snap_flag_from_modifiers_set(TransInfo *t)
 {
-  if (ELEM(t->spacetype, SPACE_GRAPH, SPACE_ACTION, SPACE_NLA)) {
+  if (ELEM(t->spacetype, SPACE_ACTION, SPACE_NLA)) {
     /* Those space-types define their own invert behavior instead of toggling it on/off. */
     return;
   }
+  if (t->spacetype == SPACE_GRAPH) {
+    /* This is to stay consistent with the behavior from 3.6. */
+    if (t->modifiers & MOD_SNAP_INVERT) {
+      t->tsnap.mode |= SCE_SNAP_TO_INCREMENT;
+    }
+    else {
+      t->tsnap.mode &= ~SCE_SNAP_TO_INCREMENT;
+    }
+    /* In 3.6 when snapping was disabled, pressing the invert button would turn on snapping.
+     * But it wouldn't turn it off when it was enabled. */
+    if ((t->modifiers & MOD_SNAP) || (t->modifiers & MOD_SNAP_INVERT)) {
+      t->tsnap.flag |= SCE_SNAP;
+    }
+    else {
+      t->tsnap.flag &= ~SCE_SNAP;
+    }
+    return;
+  }
   SET_FLAG_FROM_TEST(t->tsnap.flag,
                      (((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
                       ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT)),
-- 
GitLab