[Bf-blender-cvs] [3dd7de55060] temp-gpencil-bezier-stroke-type: WIP curve pen

Falk David noreply at git.blender.org
Sat Mar 20 12:32:15 CET 2021


Commit: 3dd7de55060aba50cd3d6c215e2db0cd59fa0635
Author: Falk David
Date:   Sun Jan 24 19:17:54 2021 +0100
Branches: temp-gpencil-bezier-stroke-type
https://developer.blender.org/rB3dd7de55060aba50cd3d6c215e2db0cd59fa0635

WIP curve pen

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

M	release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
M	source/blender/blenkernel/intern/brush.c
M	source/blender/editors/gpencil/gpencil_curve_draw.c
M	source/blender/editors/gpencil/gpencil_ops.c
M	source/blender/makesdna/DNA_brush_enums.h
M	source/blender/makesrna/intern/rna_brush.c

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

diff --git a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
index ce1ef733c69..120c66288d7 100644
--- a/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_toolsystem_toolbar.py
@@ -1905,6 +1905,21 @@ class _defs_gpencil_paint:
             ),
         )
 
+    @ToolDef.from_fn
+    def curve_pen():
+        def draw_settings(context, layout, tool):
+            pass
+
+        return dict(
+            idname="builtin.curve_pen",
+            label="Curve Pen",
+            icon="",
+            cursor='DOT',
+            widget=None,
+            keymap=(),
+            draw_settings=draw_settings,
+        )
+
     @ToolDef.from_fn
     def cutter():
         def draw_settings(_context, layout, tool):
@@ -2914,12 +2929,11 @@ class VIEW3D_PT_tools_active(ToolSelectPanelHelper, Panel):
             _defs_view3d_generic.cursor,
             None,
             _defs_gpencil_paint.generate_from_brushes,
+            _defs_gpencil_paint.curve_pen,
             _defs_gpencil_paint.cutter,
             None,
             _defs_gpencil_paint.eyedropper,
             None,
-            _defs_gpencil_paint.curve_pen,
-            None,
             _defs_gpencil_paint.line,
             _defs_gpencil_paint.polyline,
             _defs_gpencil_paint.arc,
diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c
index 13ba1957a32..f4451e7c6e5 100644
--- a/source/blender/blenkernel/intern/brush.c
+++ b/source/blender/blenkernel/intern/brush.c
@@ -1056,6 +1056,25 @@ void BKE_gpencil_brush_preset_set(Main *bmain, Brush *brush, const short type)
       zero_v3(brush->secondary_rgb);
       break;
     }
+    case GP_BRUSH_PRESET_INK_CURVE: {
+      brush->size = 25.0f;
+      brush->gpencil_settings->flag &= ~GP_BRUSH_USE_PRESSURE;
+
+      brush->gpencil_settings->draw_strength = 1.0f;
+      brush->gpencil_settings->flag &= ~GP_BRUSH_USE_STRENGTH_PRESSURE;
+
+      brush->gpencil_settings->input_samples = 10;
+      brush->gpencil_settings->draw_angle = 0.0f;
+      brush->gpencil_settings->draw_angle_factor = 0.0f;
+      brush->gpencil_settings->hardeness = 1.0f;
+      copy_v2_fl(brush->gpencil_settings->aspect_ratio, 1.0f);
+
+      brush->gpencil_settings->icon_id = GP_BRUSH_ICON_PEN;
+      brush->gpencil_tool = GPAINT_TOOL_CURVE;
+
+      zero_v3(brush->secondary_rgb);
+      break;
+    }
     case GP_BRUSH_PRESET_VERTEX_DRAW: {
       brush->gpencil_settings->icon_id = GP_BRUSH_ICON_VERTEX_DRAW;
       brush->gpencil_vertex_tool = GPVERTEX_TOOL_DRAW;
@@ -1398,6 +1417,12 @@ void BKE_brush_gpencil_paint_presets(Main *bmain, ToolSettings *ts, const bool r
     BKE_gpencil_brush_preset_set(bmain, brush, GP_BRUSH_PRESET_TINT);
   }
 
+  /* Tint brush. */
+  brush = gpencil_brush_ensure(bmain, ts, "Curve pen", OB_MODE_PAINT_GPENCIL, &r_new);
+  if ((reset) || (r_new)) {
+    BKE_gpencil_brush_preset_set(bmain, brush, GPAINT_TOOL_CURVE);
+  }
+
   /* Set default Draw brush. */
   if (reset || brush_prev == NULL) {
     BKE_paint_brush_set(paint, deft_draw);
diff --git a/source/blender/editors/gpencil/gpencil_curve_draw.c b/source/blender/editors/gpencil/gpencil_curve_draw.c
index 37e0d7a450d..071d26eced0 100644
--- a/source/blender/editors/gpencil/gpencil_curve_draw.c
+++ b/source/blender/editors/gpencil/gpencil_curve_draw.c
@@ -68,7 +68,9 @@ typedef enum eGPDcurve_draw_state {
   IN_SET_VECTOR = 1,
   IN_DRAG_ALIGNED_HANDLE = 2,
   IN_DRAG_FREE_HANDLE = 3,
+  IN_SET_THICKNESS = 4,
 } eGPDcurve_draw_state;
+
 typedef struct tGPDcurve_draw {
   Scene *scene;
   ARegion *region;
@@ -80,6 +82,8 @@ typedef struct tGPDcurve_draw {
   bGPDcurve *gpc;
   int cframe;
 
+  Brush *brush;
+
   GP_SpaceConversion gsc;
 
   /* imval of current event */
@@ -93,6 +97,7 @@ typedef struct tGPDcurve_draw {
   bool is_mouse_down;
 
   bool is_cyclic;
+  float prev_pressure;
 
   eGPDcurve_draw_state state;
 } tGPDcurve_draw;
@@ -113,6 +118,8 @@ static void debug_print_state(tGPDcurve_draw *tcd)
       "VECTOR",
       "ALIGN",
       "FREE",
+      "THICK",
+      "ALPHA"
   };
   printf("State: %s\tMouse x=%d\ty=%d\tpressed:%s\n",
          state_str[tcd->state],
@@ -142,7 +149,8 @@ static void gpencil_project_mval_to_v3(
   }
 }
 
-static void gpencil_push_new_curve_point(tGPDcurve_draw *tcd)
+/* Helper: Add a new curve point at the end (duplicating the previous last) */
+static void gpencil_push_curve_point(bContext *C, tGPDcurve_draw *tcd)
 {
   bGPDcurve *gpc = tcd->gpc;
   int old_num_points = gpc->tot_curve_points;
@@ -161,7 +169,43 @@ static void gpencil_push_new_curve_point(tGPDcurve_draw *tcd)
   BEZT_DESEL_ALL(&old_last->bezt);
 
   BKE_gpencil_stroke_update_geometry_from_editcurve(
-      tcd->gps, tcd->gpd->curve_edit_resolution, true);
+      tcd->gps, tcd->gpd->curve_edit_resolution, false);
+}
+
+/* Helper: Remove the last curve point */
+static void gpencil_pop_curve_point(bContext *C, tGPDcurve_draw *tcd)
+{
+  bGPdata *gpd = tcd->gpd;
+  bGPDstroke *gps = tcd->gps;
+  bGPDcurve *gpc = tcd->gpc;
+  const int old_num_points = gpc->tot_curve_points;
+  const int new_num_points = old_num_points - 1;
+  printf("old: %d, new: %d\n", old_num_points, new_num_points);
+
+  /* Create new stroke and curve */
+  bGPDstroke *new_stroke = BKE_gpencil_stroke_duplicate(tcd->gps, false, false);
+  new_stroke->points = NULL;
+
+  bGPDcurve *new_curve = BKE_gpencil_stroke_editcurve_new(new_num_points);
+  new_curve->flag = gpc->flag;
+  memcpy(new_curve->curve_points, gpc->curve_points, sizeof(bGPDcurve_point) * new_num_points);
+  new_stroke->editcurve = new_curve;
+
+  BKE_gpencil_stroke_update_geometry_from_editcurve(new_stroke, gpd->curve_edit_resolution, false);
+
+  /* Remove and free old stroke and curve */
+  BLI_remlink(&tcd->gpf->strokes, gps);
+  BKE_gpencil_free_stroke(gps);
+
+  tcd->gps = new_stroke;
+  tcd->gpc = new_curve;
+
+  BLI_addtail(&tcd->gpf->strokes, new_stroke);
+  BKE_gpencil_stroke_geometry_update(gpd, new_stroke);
+
+  DEG_id_tag_update(&gpd->id, ID_RECALC_COPY_ON_WRITE);
+  DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
+  WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
 }
 
 static void gpencil_set_handle_type_last_point(tGPDcurve_draw *tcd, eBezTriple_Handle type)
@@ -171,6 +215,22 @@ static void gpencil_set_handle_type_last_point(tGPDcurve_draw *tcd, eBezTriple_H
   cpt->bezt.h1 = cpt->bezt.h2 = type;
 }
 
+static void gpencil_set_alpha_last_segment(tGPDcurve_draw *tcd, float alpha)
+{
+  bGPDstroke *gps = tcd->gps;
+  bGPDcurve *gpc = tcd->gpc;
+
+  if (gpc->tot_curve_points < 2) {
+    return;
+  }
+
+  bGPDcurve_point *old_last = &gpc->curve_points[gpc->tot_curve_points - 2];
+  for (uint32_t i = old_last->point_index; i < gps->totpoints; i++) {
+    bGPDspoint *pt = &gps->points[i];
+    pt->strength = alpha;
+  }
+}
+
 /* ------------------------------------------------------------------------- */
 /* Main drawing functions */
 
@@ -197,7 +257,7 @@ static void gpencil_curve_draw_init(bContext *C, wmOperator *op, const wmEvent *
   copy_v2_v2_int(tcd->imval, event->mval);
   copy_v2_v2_int(tcd->imval_prev, event->mval);
   tcd->is_mouse_down = (event->val == KM_PRESS);
-  tcd->state = IN_MOVE;
+  tcd->state = IN_SET_VECTOR;
 
   if ((paint->brush == NULL) || (paint->brush->gpencil_settings == NULL)) {
     BKE_brush_gpencil_paint_presets(bmain, ts, true);
@@ -207,7 +267,8 @@ static void gpencil_curve_draw_init(bContext *C, wmOperator *op, const wmEvent *
   BKE_brush_tool_set(brush, paint, 0);
   BKE_paint_brush_set(paint, brush);
   BrushGpencilSettings *brush_settings = brush->gpencil_settings;
-  // tcd->brush = brush;
+  tcd->brush = brush;
+  printf("Brush size: %d\n", brush->size);
 
   /* Get active layer or create a new one. */
   bGPDlayer *gpl = CTX_data_active_gpencil_layer(C);
@@ -255,12 +316,17 @@ static void gpencil_curve_draw_init(bContext *C, wmOperator *op, const wmEvent *
   tcd->gps = gps;
 
   /* Create editcurve. */
-  BKE_gpencil_stroke_editcurve_update(tcd->gpd, tcd->gpl, tcd->gps);
-  bGPDcurve_point *cpt = &gps->editcurve->curve_points[0];
+  bGPDcurve *gpc = BKE_gpencil_stroke_editcurve_new(1);
+  bGPDcurve_point *cpt = &gpc->curve_points[0];
+  copy_v3_v3(cpt->bezt.vec[0], first_pt);
+  copy_v3_v3(cpt->bezt.vec[1], first_pt);
+  copy_v3_v3(cpt->bezt.vec[2], first_pt);
+  cpt->pressure = 1.0f;
+  cpt->strength = 1.0f;
   cpt->flag |= GP_CURVE_POINT_SELECT;
   BEZT_SEL_ALL(&cpt->bezt);
-  gps->editcurve->flag |= GP_CURVE_SELECT;
-  tcd->gpc = tcd->gps->editcurve;
+  gps->editcurve = gpc;
+  tcd->gpc = gpc;
 
   /* Calc geometry data. */
   BKE_gpencil_stroke_geometry_update(tcd->gpd, gps);
@@ -281,35 +347,55 @@ static void gpencil_curve_draw_update(bContext *C, tGPDcurve_draw *tcd)
   int tot_points = gpc->tot_curve_points;
   bGPDcurve_point *cpt = &gpc->curve_points[tot_points - 1];
   BezTriple *bezt = &cpt->bezt;
+  Brush *brush = tcd->brush;
 
   float co[3];
   switch (tcd->state) {
     case IN_MOVE: {
       gpencil_project_mval_to_v3(tcd->scene, tcd->region, tcd->ob, tcd->imval, co);
-      copy_v3_v3(&bezt->vec[1], co);
-      BKE_gpencil_editcurve_recalculate_handles(gps);
+      copy_v3_v3(bezt->vec[0], co);
+      copy_v3_v3(bezt->vec[1], co);
+      copy_v3_v3(bezt->vec[2], co);
+
+      BKE_gpencil_stroke_update_geometry_from_editcurve(gps, gpd->curve_edit_resolution, true);
+      gpencil_set_alpha_last_segment(tcd, 0.1f);
       break;
     }
     case IN_DRAG_ALIGNED_HANDLE: {
       float vec[3];
       gpencil_project_mval_to_v3(tcd->scene, tcd->region, tcd->ob, tcd->imval, co);
-      sub_v3_v3v3(vec, &bezt->vec[1], co);
-      add_v3_v3(vec, &bezt->vec[1]);
-      copy_v3_v3(&bezt->vec[0], vec);
-      copy_v3_v3(&bezt->vec[2], co);
-      // BKE_gpencil_editcurve_recalculate_handles(gps);
+      sub_v3_v3v3(vec, bezt->vec[1], co);
+      add_v3_v3(vec, bezt->vec[1]);
+      copy_v3_v3(bezt->vec[0], vec);
+      copy_v3_v3(bezt->vec[2], co);
+
+      BKE_gpencil_stroke_update_geometry_from_editcurve(gps, gpd->curve_edit_resolution, true);
       break;
     }
     case IN_DRAG_FREE_HANDLE: {
  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list