Commit: 0cc0b89013270a9e2c349ac5e87e048275c51e2f
Author: Antonio Vazquez
Date:   Fri Feb 2 10:34:04 2018 +0100
Branches: greasepencil-object
https://developer.blender.org/rB0cc0b89013270a9e2c349ac5e87e048275c51e2f

Cleanup: Create function to duplicate stroke to avoid code duplication.

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

M       source/blender/blenkernel/BKE_gpencil.h
M       source/blender/blenkernel/intern/gpencil.c
M       source/blender/editors/gpencil/gpencil_edit.c
M       source/blender/editors/gpencil/gpencil_interpolate.c

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

diff --git a/source/blender/blenkernel/BKE_gpencil.h 
b/source/blender/blenkernel/BKE_gpencil.h
index 2a5a0915c27..7458ce502f2 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -81,6 +81,7 @@ struct bGPDframe *BKE_gpencil_frame_duplicate(const struct 
bGPDframe *gpf_src);
 struct bGPDframe *BKE_gpencil_frame_color_duplicate(const struct bContext *C, 
const struct bGPDframe *gpf_src);
 struct bGPDlayer *BKE_gpencil_layer_duplicate(const struct bGPDlayer *gpl_src);
 void BKE_gpencil_frame_copy_strokes(struct bGPDframe *gpf_src, struct 
bGPDframe *gpf_dst);
+struct bGPDstroke *BKE_gpencil_stroke_duplicate(struct bGPDstroke *gps_src);
 
 void BKE_gpencil_copy_data(struct Main *bmain, struct bGPdata *gpd_dst, const 
struct bGPdata *gpd_src, const int flag);
 struct bGPdata   *BKE_gpencil_copy(struct Main *bmain, const struct bGPdata 
*gpd);
diff --git a/source/blender/blenkernel/intern/gpencil.c 
b/source/blender/blenkernel/intern/gpencil.c
index fa7f101d9fa..05f3560a779 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -926,11 +926,28 @@ void BKE_gpencil_stroke_weights_duplicate(bGPDstroke 
*gps_src, bGPDstroke *gps_d
        }
 }
 
+/* make a copy of a given gpencil stroke */
+bGPDstroke *BKE_gpencil_stroke_duplicate(bGPDstroke *gps_src)
+{
+       bGPDstroke *gps_dst = NULL;
+
+       gps_dst = MEM_dupallocN(gps_src);
+       gps_dst->prev = gps_dst->next = NULL;
+
+       gps_dst->points = MEM_dupallocN(gps_src->points);
+       BKE_gpencil_stroke_weights_duplicate(gps_src, gps_dst);
+       gps_dst->triangles = NULL;
+       gps_dst->tot_triangles = 0;
+       gps_dst->flag |= GP_STROKE_RECALC_CACHES;
+
+       /* return new stroke */
+       return gps_dst;
+}
 
 /* make a copy of a given gpencil frame */
 bGPDframe *BKE_gpencil_frame_duplicate(const bGPDframe *gpf_src)
 {
-       bGPDstroke *gps_dst;
+       bGPDstroke *gps_dst = NULL;
        bGPDframe *gpf_dst;
        
        /* error checking */
@@ -945,13 +962,8 @@ bGPDframe *BKE_gpencil_frame_duplicate(const bGPDframe 
*gpf_src)
        /* copy strokes */
        BLI_listbase_clear(&gpf_dst->strokes);
        for (bGPDstroke *gps_src = gpf_src->strokes.first; gps_src; gps_src = 
gps_src->next) {
-               /* make copy of source stroke, then adjust pointer to points 
too */
-               gps_dst = MEM_dupallocN(gps_src);
-               gps_dst->points = MEM_dupallocN(gps_src->points);
-               BKE_gpencil_stroke_weights_duplicate(gps_src, gps_dst);
-
-               gps_dst->triangles = MEM_dupallocN(gps_src->triangles);
-               gps_dst->flag |= GP_STROKE_RECALC_CACHES;
+               /* make copy of source stroke */
+               gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
                BLI_addtail(&gpf_dst->strokes, gps_dst);
        }
        
@@ -962,7 +974,7 @@ bGPDframe *BKE_gpencil_frame_duplicate(const bGPDframe 
*gpf_src)
 /* make a copy of strokes between gpencil frames */
 void BKE_gpencil_frame_copy_strokes(bGPDframe *gpf_src, struct bGPDframe 
*gpf_dst)
 {
-       bGPDstroke *gps_dst;
+       bGPDstroke *gps_dst = NULL;
        /* error checking */
        if ((gpf_src == NULL) || (gpf_dst == NULL)) {
                return;
@@ -971,13 +983,8 @@ void BKE_gpencil_frame_copy_strokes(bGPDframe *gpf_src, 
struct bGPDframe *gpf_ds
        /* copy strokes */
        BLI_listbase_clear(&gpf_dst->strokes);
        for (bGPDstroke *gps_src = gpf_src->strokes.first; gps_src; gps_src = 
gps_src->next) {
-               /* make copy of source stroke, then adjust pointer to points 
too */
-               gps_dst = MEM_dupallocN(gps_src);
-               gps_dst->points = MEM_dupallocN(gps_src->points);
-               BKE_gpencil_stroke_weights_duplicate(gps_src, gps_dst);
-
-               gps_dst->triangles = MEM_dupallocN(gps_src->triangles);
-               gps_dst->flag |= GP_STROKE_RECALC_CACHES;
+               /* make copy of source stroke */
+               gps_dst = BKE_gpencil_stroke_duplicate(gps_src);
                BLI_addtail(&gpf_dst->strokes, gps_dst);
        }
 }
diff --git a/source/blender/editors/gpencil/gpencil_edit.c 
b/source/blender/editors/gpencil/gpencil_edit.c
index 1abd7ad0537..2f191b258ef 100644
--- a/source/blender/editors/gpencil/gpencil_edit.c
+++ b/source/blender/editors/gpencil/gpencil_edit.c
@@ -3158,11 +3158,7 @@ static int gp_stroke_separate_exec(bContext *C, 
wmOperator *op)
                                                        /* selected points mode 
*/
                                                        if (mode == 
GP_SEPARATE_POINT) {
                                                                /* make copy of 
source stroke */
-                                                               bGPDstroke 
*gps_dst = MEM_dupallocN(gps);
-                                                               gps_dst->points 
= MEM_dupallocN(gps->points);
-                                                               
BKE_gpencil_stroke_weights_duplicate(gps, gps_dst);
-                                                               
gps_dst->triangles = MEM_dupallocN(gps->triangles);
-                                                               gps_dst->flag 
|= GP_STROKE_RECALC_CACHES;
+                                                               bGPDstroke 
*gps_dst = BKE_gpencil_stroke_duplicate(gps);
                                                                
                                                                /* link to 
destination frame */
                                                                
BLI_addtail(&gpf_dst->strokes, gps_dst);
diff --git a/source/blender/editors/gpencil/gpencil_interpolate.c 
b/source/blender/editors/gpencil/gpencil_interpolate.c
index d5fc329a635..88347d16fbe 100644
--- a/source/blender/editors/gpencil/gpencil_interpolate.c
+++ b/source/blender/editors/gpencil/gpencil_interpolate.c
@@ -259,7 +259,7 @@ static void gp_interpolate_set_points(bContext *C, 
tGPDinterpolate *tgpi)
                        bGPDstroke *gps_to;
                        int fFrame;
                        
-                       bGPDstroke *new_stroke;
+                       bGPDstroke *new_stroke = NULL;
                        bool valid = true;
                        
                        
@@ -285,12 +285,7 @@ static void gp_interpolate_set_points(bContext *C, 
tGPDinterpolate *tgpi)
                        }
                        
                        /* create new stroke */
-                       new_stroke = MEM_dupallocN(gps_from);
-                       new_stroke->points = MEM_dupallocN(gps_from->points);
-                       BKE_gpencil_stroke_weights_duplicate(gps_from, 
new_stroke);
-                       new_stroke->triangles = 
MEM_dupallocN(gps_from->triangles);
-                       new_stroke->tot_triangles = 0;
-                       new_stroke->flag |= GP_STROKE_RECALC_CACHES;
+                       new_stroke = BKE_gpencil_stroke_duplicate(gps_from);
 
                        if (valid) {
                                /* if destination stroke is smaller, resize 
new_stroke to size of gps_to stroke */
@@ -966,7 +961,7 @@ static int gpencil_interpolate_seq_exec(bContext *C, 
wmOperator *op)
                        
                        /* create new strokes data with interpolated points 
reading original stroke */
                        for (gps_from = prevFrame->strokes.first; gps_from; 
gps_from = gps_from->next) {
-                               bGPDstroke *new_stroke;
+                               bGPDstroke *new_stroke = NULL;
                                
                                /* only selected */
                                if ((flag & 
GP_TOOLFLAG_INTERPOLATE_ONLY_SELECTED) && ((gps_from->flag & GP_STROKE_SELECT) 
== 0)) {
@@ -995,12 +990,7 @@ static int gpencil_interpolate_seq_exec(bContext *C, 
wmOperator *op)
                                }
                                
                                /* create new stroke */
-                               new_stroke = MEM_dupallocN(gps_from);
-                               new_stroke->points = 
MEM_dupallocN(gps_from->points);
-                               BKE_gpencil_stroke_weights_duplicate(gps_from, 
new_stroke);
-                               new_stroke->triangles = 
MEM_dupallocN(gps_from->triangles);
-                               new_stroke->tot_triangles = 0;
-                               new_stroke->flag |= GP_STROKE_RECALC_CACHES;
+                               new_stroke = 
BKE_gpencil_stroke_duplicate(gps_from);
 
                                /* if destination stroke is smaller, resize 
new_stroke to size of gps_to stroke */
                                if (gps_from->totpoints > gps_to->totpoints) {

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to