Blender  V3.3
gpencil_edit_curve.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. */
3 
9 #include <math.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "DNA_gpencil_types.h"
16 #include "DNA_view3d_types.h"
17 
18 #include "BKE_context.h"
19 #include "BKE_gpencil.h"
20 #include "BKE_gpencil_curve.h"
21 #include "BKE_gpencil_geom.h"
22 
23 #include "BLI_listbase.h"
24 #include "BLI_math.h"
25 
26 #include "RNA_access.h"
27 #include "RNA_define.h"
28 
29 #include "WM_api.h"
30 #include "WM_types.h"
31 
32 #include "ED_gpencil.h"
33 
34 #include "DEG_depsgraph.h"
35 
36 #include "gpencil_intern.h"
37 
38 /* -------------------------------------------------------------------- */
42 /* Poll callback for checking if there is an active layer and we are in curve edit mode. */
44 {
46  if ((ob == NULL) || (ob->type != OB_GPENCIL)) {
47  return false;
48  }
49  bGPdata *gpd = (bGPdata *)ob->data;
51  return false;
52  }
53 
55  return (gpl != NULL);
56 }
57 
59 {
61  bGPdata *gpd = ob->data;
62 
63  float error_threshold = RNA_float_get(op->ptr, "error_threshold");
64  gpd->curve_edit_threshold = error_threshold;
65 
66  if (ELEM(NULL, gpd)) {
67  return OPERATOR_CANCELLED;
68  }
69 
70  LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
71  LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
72  if (gpf == gpl->actframe) {
73  LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
74  /* only allow selected and non-converted strokes to be transformed */
75  if ((gps->flag & GP_STROKE_SELECT && gps->editcurve == NULL) ||
76  (gps->editcurve != NULL && gps->editcurve->flag & GP_CURVE_NEEDS_STROKE_UPDATE)) {
78  /* Update the selection from the stroke to the curve. */
79  BKE_gpencil_editcurve_stroke_sync_selection(gpd, gps, gps->editcurve);
80  gps->flag |= GP_STROKE_NEEDS_CURVE_UPDATE;
82  }
83  }
84  }
85  }
86  }
87 
89 
90  /* notifiers */
93 
94  return OPERATOR_FINISHED;
95 }
96 
98 {
99  PropertyRNA *prop;
100 
101  /* identifiers */
102  ot->name = "Enter curve edit mode";
103  ot->idname = "GPENCIL_OT_stroke_enter_editcurve_mode";
104  ot->description = "Called to transform a stroke into a curve";
105 
106  /* api callbacks */
109 
110  /* flags */
112 
113  /* properties */
114  prop = RNA_def_float(ot->srna,
115  "error_threshold",
116  0.1f,
117  FLT_MIN,
118  100.0f,
119  "Error Threshold",
120  "Threshold on the maximum deviation from the actual stroke",
121  FLT_MIN,
122  10.0f);
123  RNA_def_property_ui_range(prop, FLT_MIN, 10.0f, 0.1f, 5);
124 }
125 
128 /* -------------------------------------------------------------------- */
133 {
135  bGPdata *gpd = ob->data;
136  const int handle_type = RNA_enum_get(op->ptr, "type");
137 
138  if (ELEM(NULL, gpd)) {
139  return OPERATOR_CANCELLED;
140  }
141 
142  GP_EDITABLE_CURVES_BEGIN(gps_iter, C, gpl, gps, gpc)
143  {
144  for (int i = 0; i < gpc->tot_curve_points; i++) {
145  bGPDcurve_point *gpc_pt = &gpc->curve_points[i];
146 
147  if (gpc_pt->flag & GP_CURVE_POINT_SELECT) {
148  BezTriple *bezt = &gpc_pt->bezt;
149 
150  if (bezt->f2 & SELECT) {
151  bezt->h1 = handle_type;
152  bezt->h2 = handle_type;
153  }
154  else {
155  if (bezt->f1 & SELECT) {
156  bezt->h1 = handle_type;
157  }
158  if (bezt->f3 & SELECT) {
159  bezt->h2 = handle_type;
160  }
161  }
162  }
163  }
164 
166  gps->flag |= GP_STROKE_NEEDS_CURVE_UPDATE;
168  }
169  GP_EDITABLE_CURVES_END(gps_iter);
170 
171  /* notifiers */
174 
175  return OPERATOR_FINISHED;
176 }
177 
179 {
180  static const EnumPropertyItem editcurve_handle_type_items[] = {
181  {HD_FREE, "FREE", 0, "Free", ""},
182  {HD_AUTO, "AUTOMATIC", 0, "Automatic", ""},
183  {HD_VECT, "VECTOR", 0, "Vector", ""},
184  {HD_ALIGN, "ALIGNED", 0, "Aligned", ""},
185  {0, NULL, 0, NULL, NULL},
186  };
187 
188  /* identifiers */
189  ot->name = "Set handle type";
190  ot->idname = "GPENCIL_OT_stroke_editcurve_set_handle_type";
191  ot->description = "Set the type of a edit curve handle";
192 
193  /* api callbacks */
197 
198  /* flags */
200 
201  /* properties */
202  ot->prop = RNA_def_enum(ot->srna, "type", editcurve_handle_type_items, 1, "Type", "Spline type");
203 }
204 
struct Object * CTX_data_active_object(const bContext *C)
Definition: context.c:1353
struct bGPDlayer * BKE_gpencil_layer_active_get(struct bGPdata *gpd)
Definition: gpencil.c:1558
void BKE_gpencil_stroke_editcurve_update(struct bGPdata *gpd, struct bGPDlayer *gpl, struct bGPDstroke *gps)
void BKE_gpencil_editcurve_stroke_sync_selection(struct bGPdata *gpd, struct bGPDstroke *gps, struct bGPDcurve *gpc)
void BKE_gpencil_editcurve_recalculate_handles(struct bGPDstroke *gps)
void BKE_gpencil_stroke_geometry_update(struct bGPdata *gpd, struct bGPDstroke *gps)
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define ELEM(...)
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:771
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
@ HD_VECT
@ HD_FREE
@ HD_AUTO
@ HD_ALIGN
@ GP_CURVE_NEEDS_STROKE_UPDATE
@ GP_STROKE_NEEDS_CURVE_UPDATE
@ GP_STROKE_SELECT
@ GP_DATA_CURVE_EDIT_MODE
#define GPENCIL_CURVE_EDIT_SESSIONS_ON(gpd)
@ GP_CURVE_POINT_SELECT
@ OB_GPENCIL
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define ND_DATA
Definition: WM_types.h:456
#define NA_EDITED
Definition: WM_types.h:523
#define NC_GPENCIL
Definition: WM_types.h:349
#define SELECT
void GPENCIL_OT_stroke_enter_editcurve_mode(wmOperatorType *ot)
static bool gpencil_curve_edit_mode_poll(bContext *C)
void GPENCIL_OT_stroke_editcurve_set_handle_type(wmOperatorType *ot)
static int gpencil_stroke_enter_editcurve_mode_exec(bContext *C, wmOperator *op)
static int gpencil_editcurve_set_handle_type_exec(bContext *C, wmOperator *op)
#define GP_EDITABLE_CURVES_END(gpstroke_iter)
#define GP_EDITABLE_CURVES_BEGIN(gpstroke_iter, C, gpl, gps, gpc)
bool gpencil_active_layer_poll(struct bContext *C)
float RNA_float_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4957
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5004
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, float default_value, float hardmin, float hardmax, const char *ui_name, const char *ui_description, float softmin, float softmax)
Definition: rna_define.c:3836
void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, int precision)
Definition: rna_define.c:1664
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
uint8_t h1
uint8_t f3
uint8_t f1
uint8_t f2
uint8_t h2
void * data
ListBase layers
float curve_edit_threshold
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479
int WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))