[Bf-blender-cvs] [baff05ad1c1] master: UI: Add Free Handle Types to CurveProfile Widget

Hans Goudey noreply at git.blender.org
Wed Jun 24 17:50:12 CEST 2020


Commit: baff05ad1c156ca477375c440e2c5b92cad214e9
Author: Hans Goudey
Date:   Wed Jun 24 11:50:01 2020 -0400
Branches: master
https://developer.blender.org/rBbaff05ad1c156ca477375c440e2c5b92cad214e9

UI: Add Free Handle Types to CurveProfile Widget

Under the hood the CurveProfile widget (used for bevel custom profiles)
uses a bezier curve, but right now though it only supports two of the
bezier curve handle types, vector and auto. This patch adds support for
free handles and adds all of the logic for editing them.

This is the first step to the ability to import and export curve objects
in the widget.

There's some code cleanup in curveprofile.c. Movement for handles and
control points is abstracted to functions there rather than happening
in interface_handlers.c.

An "Apply Preset" button is also added, which solves a confusing issue
where you apply a preset, then change the number of samples and the
preset doesn't change. The button makes it clear that the preset needs
to be reapplied.

Reviewed By: Severin

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

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

M	source/blender/blenkernel/BKE_curveprofile.h
M	source/blender/blenkernel/intern/curveprofile.c
M	source/blender/editors/interface/interface_draw.c
M	source/blender/editors/interface/interface_handlers.c
M	source/blender/editors/interface/interface_templates.c
M	source/blender/makesdna/DNA_curveprofile_types.h
M	source/blender/makesrna/intern/rna_curveprofile.c

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

diff --git a/source/blender/blenkernel/BKE_curveprofile.h b/source/blender/blenkernel/BKE_curveprofile.h
index bf50cde1efc..877ab887138 100644
--- a/source/blender/blenkernel/BKE_curveprofile.h
+++ b/source/blender/blenkernel/BKE_curveprofile.h
@@ -45,6 +45,16 @@ void BKE_curveprofile_copy_data(struct CurveProfile *target, const struct CurveP
 
 struct CurveProfile *BKE_curveprofile_copy(const struct CurveProfile *profile);
 
+bool BKE_curveprofile_move_handle(struct CurveProfilePoint *point,
+                                  const bool handle_1,
+                                  const bool snap,
+                                  const float delta[2]);
+
+bool BKE_curveprofile_move_point(struct CurveProfile *profile,
+                                 struct CurveProfilePoint *point,
+                                 const bool snap,
+                                 const float delta[2]);
+
 bool BKE_curveprofile_remove_point(struct CurveProfile *profile, struct CurveProfilePoint *point);
 
 void BKE_curveprofile_remove_by_flag(struct CurveProfile *profile, const short flag);
@@ -65,7 +75,12 @@ void BKE_curveprofile_create_samples(struct CurveProfile *profile,
 void BKE_curveprofile_initialize(struct CurveProfile *profile, short segments_len);
 
 /* Called for a complete update of the widget after modifications */
-void BKE_curveprofile_update(struct CurveProfile *profile, const bool rem_doubles);
+enum {
+  PROF_UPDATE_NONE = 0,
+  PROF_UPDATE_REMOVE_DOUBLES = (1 << 0),
+  PROF_UPDATE_CLIP = (1 << 1),
+};
+void BKE_curveprofile_update(struct CurveProfile *profile, const int update_flags);
 
 /* Need to find the total length of the curve to sample a portion of it */
 float BKE_curveprofile_total_length(const struct CurveProfile *profile);
diff --git a/source/blender/blenkernel/intern/curveprofile.c b/source/blender/blenkernel/intern/curveprofile.c
index f43e0355eaa..7b7cadfcb1b 100644
--- a/source/blender/blenkernel/intern/curveprofile.c
+++ b/source/blender/blenkernel/intern/curveprofile.c
@@ -65,6 +65,11 @@ void BKE_curveprofile_copy_data(CurveProfile *target, const CurveProfile *profil
   target->path = MEM_dupallocN(profile->path);
   target->table = MEM_dupallocN(profile->table);
   target->segments = MEM_dupallocN(profile->segments);
+
+  /* Update the reference the points have to the profile. */
+  for (int i = 0; i < target->path_len; i++) {
+    target->path[i].profile = target;
+  }
 }
 
 CurveProfile *BKE_curveprofile_copy(const CurveProfile *profile)
@@ -77,6 +82,101 @@ CurveProfile *BKE_curveprofile_copy(const CurveProfile *profile)
   return NULL;
 }
 
+/**
+ * Move a point's handle, accounting for the alignment of handles with the HD_ALIGN type.
+ *
+ * \param handle_1 Whether to move the 1st or 2nd control point.
+ * \param new_location The *relative* change in the handle's position.
+ * \note Requires #BKE_curveprofile_update call after.
+ * \return Whether the handle moved from its start position.
+ */
+bool BKE_curveprofile_move_handle(struct CurveProfilePoint *point,
+                                  const bool handle_1,
+                                  const bool snap,
+                                  const float delta[2])
+{
+  short handle_type = (handle_1) ? point->h1 : point->h2;
+  float *handle_location = (handle_1) ? &point->h1_loc[0] : &point->h2_loc[0];
+
+  float start_position[2];
+  copy_v2_v2(start_position, handle_location);
+
+  /* Don't move the handle if it's not a free handle type. */
+  if (!ELEM(handle_type, HD_FREE, HD_ALIGN)) {
+    return false;
+  }
+
+  /* Move the handle. */
+  handle_location[0] += delta ? delta[0] : 0.0f;
+  handle_location[1] += delta ? delta[1] : 0.0f;
+  if (snap) {
+    handle_location[0] = 0.125f * roundf(8.0f * handle_location[0]);
+    handle_location[1] = 0.125f * roundf(8.0f * handle_location[1]);
+  }
+
+  /* Move the other handle if they are aligned. */
+  if (handle_type == HD_ALIGN) {
+    short other_handle_type = (handle_1) ? point->h2 : point->h1;
+    if (other_handle_type == HD_ALIGN) {
+      float *other_handle_location = (handle_1) ? &point->h2_loc[0] : &point->h1_loc[0];
+      other_handle_location[0] = 2.0f * point->x - handle_location[0];
+      other_handle_location[1] = 2.0f * point->y - handle_location[1];
+    }
+  }
+
+  if (!equals_v2v2(handle_location, start_position)) {
+    return true;
+  }
+  return false;
+}
+
+/**
+ * Moves a control point, accounting for clipping and snapping, and moving free handles.
+ *
+ * \param snap Whether to snap the point to the grid
+ * \param new_location The *relative* change of the point's location.
+ * \return Whether the point moved from its start position.
+ * \note Requires #BKE_curveprofile_update call after.
+ */
+bool BKE_curveprofile_move_point(struct CurveProfile *profile,
+                                 struct CurveProfilePoint *point,
+                                 const bool snap,
+                                 const float delta[2])
+{
+  float origx = point->x;
+  float origy = point->y;
+
+  point->x += delta[0];
+  point->y += delta[1];
+  if (snap) {
+    point->x = 0.125f * roundf(8.0f * point->x);
+    point->y = 0.125f * roundf(8.0f * point->y);
+  }
+
+  /* Clip here instead to test clipping here to stop handles from moving too. */
+  if (profile->flag & PROF_USE_CLIP) {
+    point->x = max_ff(point->x, profile->clip_rect.xmin);
+    point->x = min_ff(point->x, profile->clip_rect.xmax);
+    point->y = max_ff(point->y, profile->clip_rect.ymin);
+    point->y = min_ff(point->y, profile->clip_rect.ymax);
+  }
+
+  /* Also move free handles even when they aren't selected. */
+  if (ELEM(point->h1, HD_FREE, HD_ALIGN)) {
+    point->h1_loc[0] += point->x - origx;
+    point->h1_loc[1] += point->y - origy;
+  }
+  if (ELEM(point->h2, HD_FREE, HD_ALIGN)) {
+    point->h2_loc[0] += point->x - origx;
+    point->h2_loc[1] += point->y - origy;
+  }
+
+  if (point->x != origx || point->y != origy) {
+    return true;
+  }
+  return false;
+}
+
 /**
  * Removes a specific point from the path of control points.
  * \note Requires #BKE_curveprofile_update call after.
@@ -100,8 +200,10 @@ bool BKE_curveprofile_remove_point(CurveProfile *profile, CurveProfilePoint *poi
   uint i_delete = (uint)(point - profile->path);
 
   /* Copy the before and after the deleted point. */
-  memcpy(pts, profile->path, i_delete);
-  memcpy(pts + i_delete, profile->path + i_delete + 1, (size_t)profile->path_len - i_delete - 1);
+  memcpy(pts, profile->path, sizeof(CurveProfilePoint) * i_delete);
+  memcpy(pts + i_delete,
+         profile->path + i_delete + 1,
+         sizeof(CurveProfilePoint) * (profile->path_len - i_delete - 1));
 
   MEM_freeN(profile->path);
   profile->path = pts;
@@ -180,12 +282,9 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
                                            "profile path");
   for (int i_new = 0, i_old = 0; i_new < profile->path_len; i_new++) {
     if (i_new != i_insert) {
-      /* Insert old points */
-      new_pts[i_new].x = profile->path[i_old].x;
-      new_pts[i_new].y = profile->path[i_old].y;
-      new_pts[i_new].flag = profile->path[i_old].flag & ~PROF_SELECT; /* Deselect old points. */
-      new_pts[i_new].h1 = profile->path[i_old].h1;
-      new_pts[i_new].h2 = profile->path[i_old].h2;
+      /* Insert old points. */
+      memcpy(&new_pts[i_new], &profile->path[i_old], sizeof(CurveProfilePoint));
+      new_pts[i_new].flag &= ~PROF_SELECT; /* Deselect old points. */
       i_old++;
     }
     else {
@@ -201,6 +300,8 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
       else {
         new_pt->h1 = new_pt->h2 = HD_AUTO;
       }
+      /* Give new point a reference to the profile. */
+      new_pt->profile = profile;
     }
   }
 
@@ -212,35 +313,19 @@ CurveProfilePoint *BKE_curveprofile_insert(CurveProfile *profile, float x, float
 
 /**
  * Sets the handle type of the selected control points.
- * \param type_1, type_2: Either HD_VECT or HD_AUTO. Handle types for the first and second handles.
- *
+ * \param type_* Handle type for the first handle. HD_VECT, HD_AUTO, HD_FREE, or HD_ALIGN.
  * \note Requires #BKE_curveprofile_update call after.
  */
 void BKE_curveprofile_selected_handle_set(CurveProfile *profile, int type_1, int type_2)
 {
   for (int i = 0; i < profile->path_len; i++) {
-    if (profile->path[i].flag & PROF_SELECT) {
-      switch (type_1) {
-        case HD_AUTO:
-          profile->path[i].h1 = HD_AUTO;
-          break;
-        case HD_VECT:
-          profile->path[i].h1 = HD_VECT;
-          break;
-        default:
-          profile->path[i].h1 = HD_AUTO;
-          break;
-      }
-      switch (type_2) {
-        case HD_AUTO:
-          profile->path[i].h2 = HD_AUTO;
-          break;
-        case HD_VECT:
-          profile->path[i].h2 = HD_VECT;
-          break;
-        default:
-          profile->path[i].h1 = HD_AUTO;
-          break;
+    if (ELEM(profile->path[i].flag, PROF_SELECT, PROF_H1_SELECT, PROF_H2_SELECT)) {
+      profile->path[i].h1 = type_1;
+      profile->path[i].h2 = type_2;
+
+      if (type_1 == HD_ALIGN && type_2 == HD_ALIGN) {
+        /* Align the handles. */
+        BKE_curveprofile_move_handle(&profile->path[i], true, false, NULL);
       }
     }
   }
@@ -261,11 +346,24 @@ void BKE_curveprofile_reverse(CurveProfile *profile)
                                            "profile path");
   /* Mirror the new points across the y = x line */
   for (int i = 0; i < profile->path_len; i++) {
-    new_pts[profile->path_len - i - 1].x = profile->path[i].y;
-    new_pts[profile->path_len - i - 1].y = profile->path[i].x;
-    new_pts[profile->path_len - i - 1].flag = profile->path[i].flag;
-    new_pts[profile->path_len - i - 1].h1 = profile->path[i].h1;
-    new_pts[profile->path_len - i - 1].h2 = profile->path[i].h2;
+    int i_reversed = profile->path_len - i - 1;
+    BLI_assert(i_reversed >= 0);
+    new_pts[i_reversed].x = profile->path[i].y;
+    new_pts[i_reversed].y = profile->path[i].x;
+    new_pts[i_reversed].flag = profile->path[i].flag;
+    new_pts[i_reversed].h1 = profile->path[i].h2;
+    new_pts[i_reversed].h2 = profil

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list