GPencil: Handle drawing on back for polygons
authorAntonioya <blendergit@gmail.com>
Sun, 7 Aug 2016 10:16:44 +0000 (12:16 +0200)
committerAntonioya <blendergit@gmail.com>
Sun, 7 Aug 2016 14:45:02 +0000 (16:45 +0200)
The polygons must move the stroke to back only when polygon is complete

source/blender/editors/gpencil/gpencil_paint.c

index cebcbfe9317fdb17bc840296470bc6e119d79d23..4e521b5909c9a76590efef0eed5edc2ad76256d3 100644 (file)
@@ -930,7 +930,7 @@ static void gp_stroke_newfrombuffer(tGPsdata *p)
        * because the drawing order is inverse and the head stroke is the first to draw. This is very useful for artist
        * when drawing the background
        */
-       if (ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) {
+       if ((ts->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode != GP_PAINTMODE_DRAW_POLY)) {
                BLI_addhead(&p->gpf->strokes, gps);
        }
        else {
@@ -2287,6 +2287,28 @@ static void gpencil_stroke_end(wmOperator *op)
        p->gpf = NULL;
 }
 
+/* if drawing polygon and draw on back is enabled, move the stroke below all previous strokes */
+static void gpencil_move_polygon_stroke_to_back(bContext *C)
+{
+       /* move last stroke (the polygon) to head of the listbase stroke to draw on back of all previous strokes */
+       bGPdata *gpd = ED_gpencil_data_get_active(C);
+       bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
+
+       /* sanity checks */
+       if (ELEM(NULL, gpd, gpl, gpl->actframe)) {
+               return;
+       }
+
+       bGPDframe *gpf = gpl->actframe;
+       bGPDstroke *gps = gpf->strokes.last;
+       if (ELEM(NULL, gps)) {
+               return;
+       }
+
+       BLI_remlink(&gpf->strokes, gps);
+       BLI_insertlinkbefore(&gpf->strokes, gpf->strokes.first, gps);
+}
+
 /* events handling during interactive drawing part of operator */
 static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
 {
@@ -2343,6 +2365,10 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
        if (ELEM(event->type, RETKEY, PADENTER, ESCKEY, SPACEKEY, EKEY)) {
                /* exit() ends the current stroke before cleaning up */
                /* printf("\t\tGP - end of paint op + end of stroke\n"); */
+               /* if drawing polygon and enable on back, must move stroke */
+               if ((p->scene->toolsettings->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
+                       gpencil_move_polygon_stroke_to_back(C);
+               }
                p->status = GP_STATUS_DONE;
                estate = OPERATOR_FINISHED;
        }
@@ -2399,6 +2425,10 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
                        }
                        else {
                                /* printf("\t\tGP - end of stroke + op\n"); */
+                               /* if drawing polygon and enable on back, must move stroke */
+                               if ((p->scene->toolsettings->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
+                                       gpencil_move_polygon_stroke_to_back(C);
+                               }
                                p->status = GP_STATUS_DONE;
                                estate = OPERATOR_FINISHED;
                        }
@@ -2479,6 +2509,10 @@ static int gpencil_draw_modal(bContext *C, wmOperator *op, const wmEvent *event)
                                 * NOTE: Don't eter this case if an error occurred while finding the
                                 *       region (as above)
                                 */
+                               /* if drawing polygon and enable on back, must move stroke */
+                               if ((p->scene->toolsettings->gpencil_flags & GP_TOOL_FLAG_PAINT_ONBACK) && (p->paintmode == GP_PAINTMODE_DRAW_POLY)) {
+                                       gpencil_move_polygon_stroke_to_back(C);
+                               }
                                p->status = GP_STATUS_DONE;
                                estate = OPERATOR_FINISHED;
                        }