[Bf-blender-cvs] [e584bc320a9] temp-fcurve-active-keyframe-D7737: Fix T76595: Indicate the Active Keyframe in Graph Editor

Hans Goudey noreply at git.blender.org
Wed Oct 7 12:47:45 CEST 2020


Commit: e584bc320a9cf6be7a16fc159bb26b5ced5572fb
Author: Hans Goudey
Date:   Wed Oct 7 12:46:58 2020 +0200
Branches: temp-fcurve-active-keyframe-D7737
https://developer.blender.org/rBe584bc320a9cf6be7a16fc159bb26b5ced5572fb

Fix T76595: Indicate the Active Keyframe in Graph Editor

Currently there is a panel that says "Active Keyframe" for numerically editing one keyframe's values, but in the code there is no concept of the "active keyframe."

This patch adds an "active keyframe index" to each FCurve, and displays it with a theme color for the active vertex (which didn't exist before) if the FCurve is active.

{F8536092 size=full}

The active keyframe is not currently set for select operations other than basic click-select, which mirrors the behavior in the 3D view.

Reviewed By: Severin, looch

Maniphest Tasks: T76595

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

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

M	release/datafiles/userdef/userdef_default_theme.c
M	source/blender/blenkernel/BKE_fcurve.h
M	source/blender/blenkernel/intern/fcurve.c
M	source/blender/blenkernel/intern/fcurve_test.cc
M	source/blender/blenloader/intern/versioning_userdef.c
M	source/blender/editors/animation/keyframes_general.c
M	source/blender/editors/animation/keyframing.c
M	source/blender/editors/include/UI_resources.h
M	source/blender/editors/interface/resources.c
M	source/blender/editors/space_graph/graph_buttons.c
M	source/blender/editors/space_graph/graph_draw.c
M	source/blender/editors/space_graph/graph_select.c
M	source/blender/makesdna/DNA_anim_types.h
M	source/blender/makesdna/DNA_userdef_types.h
M	source/blender/makesrna/intern/rna_userdef.c

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

diff --git a/release/datafiles/userdef/userdef_default_theme.c b/release/datafiles/userdef/userdef_default_theme.c
index d3175a6fa56..dc81e4c70ac 100644
--- a/release/datafiles/userdef/userdef_default_theme.c
+++ b/release/datafiles/userdef/userdef_default_theme.c
@@ -439,6 +439,7 @@ const bTheme U_theme_default = {
     .group_active = RGBA(0x368024ff),
     .vertex = RGBA(0x000000ff),
     .vertex_select = RGBA(0xff8500ff),
+    .vertex_active = RGBA(0xffffffff),
     .cframe = RGBA(0x5680c2ff),
     .time_scrub_background = RGBA(0x292929e6),
     .time_marker_line = RGBA(0x00000060),
diff --git a/source/blender/blenkernel/BKE_fcurve.h b/source/blender/blenkernel/BKE_fcurve.h
index 9b6189612ba..e42d1cbbc78 100644
--- a/source/blender/blenkernel/BKE_fcurve.h
+++ b/source/blender/blenkernel/BKE_fcurve.h
@@ -244,6 +244,9 @@ bool BKE_fcurve_calc_bounds(struct FCurve *fcu,
                             const bool do_sel_only,
                             const bool include_handles);
 
+void BKE_fcurve_active_keyframe_set(struct FCurve *fcu, const struct BezTriple *active_bezt);
+int BKE_fcurve_active_keyframe_index(const struct FCurve *fcu);
+
 /* .............. */
 
 /* Are keyframes on F-Curve of any use (to final result, and to show in editors)? */
diff --git a/source/blender/blenkernel/intern/fcurve.c b/source/blender/blenkernel/intern/fcurve.c
index d0f23221ed0..856e0e872b3 100644
--- a/source/blender/blenkernel/intern/fcurve.c
+++ b/source/blender/blenkernel/intern/fcurve.c
@@ -829,6 +829,38 @@ bool BKE_fcurve_calc_range(
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name Active Keyframe
+ * \{ */
+
+/**
+ * Set the index that stores the FCurve's active keyframe, assuming that \a active_bezt
+ * is already part of `fcu->bezt`. If NULL, set active keyframe index to "none."
+ */
+void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
+{
+  fcu->active_keyframe_index = (active_bezt == NULL) ? FCURVE_ACTIVE_KEYFRAME_NONE :
+                                                       active_bezt - fcu->bezt;
+}
+
+/**
+ * Get the active keyframe index, with sanity checks for point bounds.
+ */
+int BKE_fcurve_active_keyframe_index(const FCurve *fcu)
+{
+  const int active_keyframe_index = fcu->active_keyframe_index;
+
+  /* Sanity checks. */
+  if ((fcu->bezt == NULL) || (active_keyframe_index >= fcu->totvert) ||
+      (active_keyframe_index < 0)) {
+    return FCURVE_ACTIVE_KEYFRAME_NONE;
+  }
+
+  return active_keyframe_index;
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Status Checks
  * \{ */
diff --git a/source/blender/blenkernel/intern/fcurve_test.cc b/source/blender/blenkernel/intern/fcurve_test.cc
index dd672df744b..7e3f64925d4 100644
--- a/source/blender/blenkernel/intern/fcurve_test.cc
+++ b/source/blender/blenkernel/intern/fcurve_test.cc
@@ -273,4 +273,38 @@ TEST(fcurve_subdivide, BKE_bezt_subdivide_handles)
   BKE_fcurve_free(fcu);
 }
 
+TEST(fcurve_active_keyframe, ActiveKeyframe)
+{
+  FCurve *fcu = BKE_fcurve_create();
+
+  /* There should be no active keyframe with no points. */
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
+
+  /* Check that adding new points sets the active index. */
+  EXPECT_EQ(insert_vert_fcurve(fcu, 1.0f, 7.5f, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_NO_USERPREF), 0);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), 0);
+  EXPECT_EQ(insert_vert_fcurve(fcu, 8.0f, 15.0f, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_NO_USERPREF), 1);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), 1);
+  EXPECT_EQ(insert_vert_fcurve(fcu, 14.0f, 8.2f, BEZT_KEYTYPE_KEYFRAME, INSERTKEY_NO_USERPREF), 2);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), 2);
+
+  /* Check clearing the index. */
+  BKE_fcurve_active_keyframe_set(fcu, NULL);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
+
+  /* Check a "normal" action. */
+  BKE_fcurve_active_keyframe_set(fcu, &fcu->bezt[2]);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), 2);
+
+  /* Check out of bounds. */
+  BKE_fcurve_active_keyframe_set(fcu, fcu->bezt - 20);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
+
+  /* Check out of bounds again. */
+  BKE_fcurve_active_keyframe_set(fcu, fcu->bezt + 4);
+  EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
+
+  BKE_fcurve_free(fcu);
+}
+
 }  // namespace blender::bke::tests
diff --git a/source/blender/blenloader/intern/versioning_userdef.c b/source/blender/blenloader/intern/versioning_userdef.c
index 04302935deb..e81735c698d 100644
--- a/source/blender/blenloader/intern/versioning_userdef.c
+++ b/source/blender/blenloader/intern/versioning_userdef.c
@@ -253,6 +253,8 @@ static void do_versions_theme(const UserDef *userdef, bTheme *btheme)
    */
   {
     /* Keep this block, even when empty. */
+
+    FROM_DEFAULT_V4_UCHAR(space_graph.vertex_active);
   }
 
 #undef FROM_DEFAULT_V4_UCHAR
diff --git a/source/blender/editors/animation/keyframes_general.c b/source/blender/editors/animation/keyframes_general.c
index ea032446fc6..0bec20c739e 100644
--- a/source/blender/editors/animation/keyframes_general.c
+++ b/source/blender/editors/animation/keyframes_general.c
@@ -117,6 +117,9 @@ bool delete_fcurve_keys(FCurve *fcu)
   /* Delete selected BezTriples */
   for (int i = 0; i < fcu->totvert; i++) {
     if (fcu->bezt[i].f2 & SELECT) {
+      if (i == fcu->active_keyframe_index) {
+        BKE_fcurve_active_keyframe_set(fcu, NULL);
+      }
       memmove(&fcu->bezt[i], &fcu->bezt[i + 1], sizeof(BezTriple) * (fcu->totvert - i - 1));
       fcu->totvert--;
       i--;
diff --git a/source/blender/editors/animation/keyframing.c b/source/blender/editors/animation/keyframing.c
index ed3e236fed3..e4b73ea65a0 100644
--- a/source/blender/editors/animation/keyframing.c
+++ b/source/blender/editors/animation/keyframing.c
@@ -606,6 +606,8 @@ int insert_vert_fcurve(
   /* add temp beztriple to keyframes */
   a = insert_bezt_fcurve(fcu, &beztr, flag);
 
+  fcu->active_keyframe_index = a;
+
   /* what if 'a' is a negative index?
    * for now, just exit to prevent any segfaults
    */
diff --git a/source/blender/editors/include/UI_resources.h b/source/blender/editors/include/UI_resources.h
index 31cb62117c5..a7b87d38472 100644
--- a/source/blender/editors/include/UI_resources.h
+++ b/source/blender/editors/include/UI_resources.h
@@ -92,6 +92,7 @@ typedef enum ThemeColorID {
   TH_TRANSFORM,
   TH_VERTEX,
   TH_VERTEX_SELECT,
+  TH_VERTEX_ACTIVE,
   TH_VERTEX_UNREFERENCED,
   TH_VERTEX_SIZE,
   TH_OUTLINE_WIDTH,
diff --git a/source/blender/editors/interface/resources.c b/source/blender/editors/interface/resources.c
index ec6f99f167d..e55f84916b3 100644
--- a/source/blender/editors/interface/resources.c
+++ b/source/blender/editors/interface/resources.c
@@ -362,6 +362,9 @@ const uchar *UI_ThemeGetColorPtr(bTheme *btheme, int spacetype, int colorid)
         case TH_VERTEX_SELECT:
           cp = ts->vertex_select;
           break;
+        case TH_VERTEX_ACTIVE:
+          cp = ts->vertex_active;
+          break;
         case TH_VERTEX_BEVEL:
           cp = ts->vertex_bevel;
           break;
diff --git a/source/blender/editors/space_graph/graph_buttons.c b/source/blender/editors/space_graph/graph_buttons.c
index 45c43a5853d..bbdca5280d4 100644
--- a/source/blender/editors/space_graph/graph_buttons.c
+++ b/source/blender/editors/space_graph/graph_buttons.c
@@ -235,40 +235,27 @@ static void graph_panel_properties(const bContext *C, Panel *panel)
 /* ******************* active Keyframe ************** */
 
 /* get 'active' keyframe for panel editing */
-static bool get_active_fcurve_keyframe_edit(FCurve *fcu,
+static bool get_active_fcurve_keyframe_edit(const FCurve *fcu,
                                             BezTriple **r_bezt,
                                             BezTriple **r_prevbezt)
 {
-  BezTriple *b;
-  int i;
-
   /* zero the pointers */
   *r_bezt = *r_prevbezt = NULL;
 
-  /* sanity checks */
-  if ((fcu->bezt == NULL) || (fcu->totvert == 0)) {
+  const int active_keyframe_index = BKE_fcurve_active_keyframe_index(fcu);
+  if (active_keyframe_index == FCURVE_ACTIVE_KEYFRAME_NONE) {
     return false;
   }
 
-  /* find first selected keyframe for now, and call it the active one
-   * - this is a reasonable assumption, given that whenever anyone
-   *   wants to edit numerically, there is likely to only be 1 vert selected
-   */
-  for (i = 0, b = fcu->bezt; i < fcu->totvert; i++, b++) {
-    if (BEZT_ISSEL_ANY(b)) {
-      /* found
-       * - 'previous' is either the one before, of the keyframe itself (which is still fine)
-       *   XXX: we can just make this null instead if needed
-       */
-      *r_prevbezt = (i > 0) ? b - 1 : b;
-      *r_bezt = b;
-
-      return true;
-    }
-  }
+  /* The active keyframe should be selected. */
+  BLI_assert(BEZT_ISSEL_ANY(&fcu->bezt[active_keyframe_index]));
+
+  *r_bezt = &fcu->bezt[active_keyframe_index];
+  /* Previous is either one before the active, or the point itself if it's the first. */
+  const int prev_index = max_ii(active_keyframe_index - 1, 0);
+  *r_prevbezt = &fcu->bezt[prev_index];
 
-  /* not found */
-  return false;
+  return true;
 }
 
 /* update callback for active keyframe properties - base updates stuff */
diff --git a/source/blender/editors/space_graph/graph_draw.c b/source/blender/editors/space_graph/graph_draw.c
index af7a7d9a4de..97a950b1ed5 100644
--- a/source/blender/editors/space_graph/graph_draw.c
+++ b/source/blender/editors/space_graph/graph_draw.c
@@ -211,6 +211,35 @@ static void draw_fcurve_selected_keyframe_vertices(
   immEnd();
 }
 
+/**
+ * Draw the extra indicator for the active point.
+ */
+static void draw_fcurve_active_vertex(const FCurve *fcu,
+                                      const View2D *v2d,
+                                      const bool edit,
+                                      const uint pos)
+{
+  const int active_keyframe_index = BKE_fcurve_active_keyframe_index(fcu);
+  if (!(fcu->flag & FCURVE_ACTIVE) || active_keyframe_index == FCURVE_ACTIVE_KEYFRAME_NONE) {
+    return;


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list