[Bf-blender-cvs] [633086e9796] temp-gpencil-bezier-v2: GPencil: Set stroke type operator

Falk David noreply at git.blender.org
Fri Mar 12 20:14:11 CET 2021


Commit: 633086e979675f0abcdeec6785463929a8691013
Author: Falk David
Date:   Fri Mar 12 20:13:37 2021 +0100
Branches: temp-gpencil-bezier-v2
https://developer.blender.org/rB633086e979675f0abcdeec6785463929a8691013

GPencil: Set stroke type operator

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

M	source/blender/editors/gpencil/gpencil_edit_curve.c
M	source/blender/editors/gpencil/gpencil_intern.h
M	source/blender/editors/gpencil/gpencil_ops.c

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

diff --git a/source/blender/editors/gpencil/gpencil_edit_curve.c b/source/blender/editors/gpencil/gpencil_edit_curve.c
index b2cff613f40..5e35aea5211 100644
--- a/source/blender/editors/gpencil/gpencil_edit_curve.c
+++ b/source/blender/editors/gpencil/gpencil_edit_curve.c
@@ -35,6 +35,7 @@
 #include "BKE_gpencil.h"
 #include "BKE_gpencil_curve.h"
 #include "BKE_gpencil_geom.h"
+#include "BKE_report.h"
 
 #include "BLI_listbase.h"
 #include "BLI_math.h"
@@ -49,6 +50,9 @@
 
 #include "DEG_depsgraph.h"
 
+#include "UI_interface.h"
+#include "UI_resources.h"
+
 #include "gpencil_intern.h"
 
 /* -------------------------------------------------------------------- */
@@ -130,48 +134,76 @@ void GPENCIL_OT_stroke_editcurve_set_handle_type(wmOperatorType *ot)
 /** \} */
 
 /* -------------------------------------------------------------------- */
-/** \name Make curve from stroke operator
+/** \name Set stroke type operator
  * \{ */
 
-static int gpencil_stroke_make_curve_exec(bContext *C, wmOperator *op)
+static int gpencil_stroke_set_type_exec(bContext *C, wmOperator *op)
 {
   Object *ob = CTX_data_active_object(C);
   bGPdata *gpd = ob->data;
-  const float threshold = RNA_float_get(op->ptr, "threshold");
-  const float corner_angle = RNA_float_get(op->ptr, "corner_angle");
+  eGPStrokeType type = RNA_enum_get(op->ptr, "type");
+  const bool only_selected = RNA_boolean_get(op->ptr, "only_selected");
 
   if (ELEM(NULL, gpd)) {
     return OPERATOR_CANCELLED;
   }
 
   bool changed = false;
-  GP_EDITABLE_STROKES_BEGIN (gps_iter, C, gpl, gps) {
-    if (!GPENCIL_STROKE_TYPE_BEZIER(gps) && (gps->flag & GP_STROKE_SELECT)) {
-      BKE_gpencil_stroke_editcurve_update(gps, threshold, corner_angle, false);
-      if (gps->editcurve != NULL) {
-        bGPDcurve *gpc = gps->editcurve;
-        gps->flag |= GP_STROKE_NEEDS_CURVE_UPDATE;
-        BKE_gpencil_stroke_geometry_update(gpd, gps);
-
-        /* Select all curve points. */
-        for (uint32_t i = 0; i < gpc->tot_curve_points; i++) {
-          bGPDcurve_point *pt = &gpc->curve_points[i];
-          pt->flag |= GP_CURVE_POINT_SELECT;
-          BEZT_SEL_ALL(&pt->bezt);
+  switch (type) {
+    case STROKE_POLY: {
+      GP_EDITABLE_CURVES_BEGIN(gps_iter, C, gpl, gps, gpc)
+      {
+        if (only_selected && (gpc->flag & GP_CURVE_SELECT) == 0) {
+          continue;
         }
-        gpc->flag |= GP_CURVE_SELECT;
+        BKE_gpencil_stroke_editcurve_sync_selection(gpd, gps, gpc);
+        BKE_gpencil_free_stroke_editcurve(gps);
+        changed = true;
+      }
+      GP_EDITABLE_CURVES_END(gps_iter);
+    } break;
+
+    case STROKE_BEZIER: {
+      const float threshold = RNA_float_get(op->ptr, "threshold");
+      const float corner_angle = RNA_float_get(op->ptr, "corner_angle");
 
-        /* Deselect stroke points. */
-        for (uint32_t i = 0; i < gps->totpoints; i++) {
-          bGPDspoint *pt = &gps->points[i];
-          pt->flag &= ~GP_SPOINT_SELECT;
+      GP_EDITABLE_STROKES_BEGIN (gps_iter, C, gpl, gps) {
+        if (!GPENCIL_STROKE_TYPE_BEZIER(gps)) {
+          if (only_selected && (gps->flag & GP_STROKE_SELECT) == 0) {
+            continue;
+          }
+
+          BKE_gpencil_stroke_editcurve_update(gps, threshold, corner_angle, false);
+          if (gps->editcurve != NULL) {
+            bGPDcurve *gpc = gps->editcurve;
+            gps->flag |= GP_STROKE_NEEDS_CURVE_UPDATE;
+            BKE_gpencil_stroke_geometry_update(gpd, gps);
+
+            /* Select all curve points. */
+            for (uint32_t i = 0; i < gpc->tot_curve_points; i++) {
+              bGPDcurve_point *pt = &gpc->curve_points[i];
+              pt->flag |= GP_CURVE_POINT_SELECT;
+              BEZT_SEL_ALL(&pt->bezt);
+            }
+            gpc->flag |= GP_CURVE_SELECT;
+
+            /* Deselect stroke points. */
+            for (uint32_t i = 0; i < gps->totpoints; i++) {
+              bGPDspoint *pt = &gps->points[i];
+              pt->flag &= ~GP_SPOINT_SELECT;
+            }
+            gps->flag &= ~GP_STROKE_SELECT;
+            changed = true;
+          }
         }
-        gps->flag &= ~GP_STROKE_SELECT;
-        changed = true;
       }
+      GP_EDITABLE_STROKES_END(gps_iter);
+    } break;
+    default: {
+      BKE_report(op->reports, RPT_ERROR, "Unknown stroke type");
+      return OPERATOR_CANCELLED;
     }
   }
-  GP_EDITABLE_STROKES_END(gps_iter);
 
   if (changed) {
     /* notifiers */
@@ -182,90 +214,69 @@ static int gpencil_stroke_make_curve_exec(bContext *C, wmOperator *op)
   return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_stroke_make_curve(wmOperatorType *ot)
-{
-  /* identifiers */
-  ot->name = "Make curve";
-  ot->idname = "GPENCIL_OT_stroke_make_curve";
-  ot->description = "Convert the stroke to a curve";
-
-  /* api callbacks */
-  ot->exec = gpencil_stroke_make_curve_exec;
-  ot->poll = gpencil_active_layer_poll;
-
-  /* flags */
-  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
-
-  /* properties */
-  ot->prop = RNA_def_float(ot->srna,
-                           "threshold",
-                           GP_DEFAULT_CURVE_ERROR,
-                           0.0f,
-                           100.0f,
-                           "Threshold",
-                           "Curve conversion error threshold",
-                           0.0f,
-                           3.0f);
-
-  ot->prop = RNA_def_float(ot->srna,
-                           "corner_angle",
-                           GP_DEFAULT_CURVE_EDIT_CORNER_ANGLE,
-                           0.0f,
-                           DEG2RADF(180.0f),
-                           "Corner angle",
-                           "Angle threshold to be treated as corners",
-                           0.0f,
-                           DEG2RADF(180.0f));
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \name Clear curve from stroke operator
- * \{ */
-
-static int gpencil_stroke_clear_curve_exec(bContext *C)
+static void gpencil_stroke_set_type_ui(bContext *UNUSED(C), wmOperator *op)
 {
-  Object *ob = CTX_data_active_object(C);
-  bGPdata *gpd = ob->data;
-
-  if (ELEM(NULL, gpd)) {
-    return OPERATOR_CANCELLED;
-  }
-
-  bool changed = false;
-  GP_EDITABLE_CURVES_BEGIN(gps_iter, C, gpl, gps, gpc)
-  {
-    if (gpc->flag & GP_CURVE_SELECT) {
-      BKE_gpencil_stroke_editcurve_sync_selection(gpd, gps, gpc);
-      BKE_gpencil_free_stroke_editcurve(gps);
-      changed = true;
-    }
-  }
-  GP_EDITABLE_CURVES_END(gps_iter);
-
-  if (changed) {
-    /* notifiers */
-    DEG_id_tag_update(&gpd->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY);
-    WM_event_add_notifier(C, NC_GPENCIL | ND_DATA | NA_EDITED, NULL);
+  uiLayout *layout = op->layout;
+  PointerRNA ptr;
+  uiLayoutSetPropSep(layout, true);
+  RNA_pointer_create(NULL, op->type->srna, op->properties, &ptr);
+
+  uiItemR(layout, &ptr, "only_selected", 0, NULL, ICON_NONE);
+  eGPStrokeType type = RNA_enum_get(&ptr, "type");
+  if (type == STROKE_BEZIER) {
+    uiItemR(layout, &ptr, "threshold", 0, NULL, ICON_NONE);
+    uiItemR(layout, &ptr, "corner_angle", 0, NULL, ICON_NONE);
   }
-
-  return OPERATOR_FINISHED;
 }
 
-void GPENCIL_OT_stroke_clear_curve(wmOperatorType *ot)
+void GPENCIL_OT_stroke_set_type(wmOperatorType *ot)
 {
+  PropertyRNA *prop;
+  static const EnumPropertyItem stroke_types[] = {
+      {STROKE_POLY, "POLY", 0, "Poly", ""},
+      {STROKE_BEZIER, "BEZIER", 0, "Bezier", ""},
+      {0, NULL, 0, NULL, NULL},
+  };
+
   /* identifiers */
-  ot->name = "Clear curve";
-  ot->idname = "GPENCIL_OT_stroke_clear_curve";
-  ot->description = "Deletes the curve data and converts it to a stroke";
+  ot->name = "Set Stroke Type";
+  ot->idname = "GPENCIL_OT_stroke_set_type";
+  ot->description = "Set the type of the stroke";
 
   /* api callbacks */
-  ot->exec = gpencil_stroke_clear_curve_exec;
+  ot->exec = gpencil_stroke_set_type_exec;
   ot->poll = gpencil_active_layer_poll;
+  ot->ui = gpencil_stroke_set_type_ui;
 
   /* flags */
   ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
+
+  prop = RNA_def_enum(ot->srna, "type", stroke_types, STROKE_POLY, "Type", "Stroke type");
+  RNA_def_property_flag(prop, PROP_SKIP_SAVE);
+
+  prop = RNA_def_boolean(
+      ot->srna, "only_selected", true, "Selected Only", "Only set the type for selected strokes");
+
+  prop = RNA_def_float(ot->srna,
+                       "threshold",
+                       GP_DEFAULT_CURVE_ERROR,
+                       0.0f,
+                       100.0f,
+                       "Threshold",
+                       "Bézier curve fitting error threshold",
+                       0.0f,
+                       3.0f);
+
+  prop = RNA_def_float_distance(ot->srna,
+                                "corner_angle",
+                                GP_DEFAULT_CURVE_EDIT_CORNER_ANGLE,
+                                0.0f,
+                                M_PI,
+                                "Corner Angle",
+                                "Angle threshold to be treated as corners",
+                                0.0f,
+                                M_PI);
+  RNA_def_property_subtype(prop, PROP_ANGLE);
 }
 
 /** \} */
\ No newline at end of file
diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h
index eec96bb48e0..d355a2c3656 100644
--- a/source/blender/editors/gpencil/gpencil_intern.h
+++ b/source/blender/editors/gpencil/gpencil_intern.h
@@ -227,6 +227,12 @@ typedef struct tGPDprimitive {
 
 } tGPDprimitive;
 
+/* Stroke types enum definition. */
+typedef enum eGPStrokeType {
+  STROKE_POLY = 0,
+  STROKE_BEZIER = 1,
+} eGPStrokeType;
+
 bool gpencil_stroke_inside_circle(const float mval[2], int rad, int x0, int y0, int x1, int y1);
 
 void gpencil_point_conversion_init(struct bContext *C, GP_SpaceConversion *r_gsc);
@@ -398,8 +404,7 @@ void GPENCIL_OT_recalc_geometry(struct wmOperatorType *ot);
 /* stroke editcurve */
 
 void GPENCIL_OT_stroke_editcurve_set_handle_type(struct wmOperatorType *ot);
-void GPENCIL_OT_stroke_make_curve(struct wmOperatorType *ot);
-void GPENCIL_OT_stroke_clear_curve(struct wmOperatorType *ot);
+void GPENCIL_OT_stroke_set_type(struct wmOperatorType *ot);
 
 /* 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list