diff options
Diffstat (limited to 'source/blender/editors/gpencil')
-rw-r--r-- | source/blender/editors/gpencil/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/editors/gpencil/drawgpencil.c | 2 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_edit.c | 4 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_intern.h | 7 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_ops.c | 3 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_paint.c | 263 | ||||
-rw-r--r-- | source/blender/editors/gpencil/gpencil_undo.c | 168 |
7 files changed, 364 insertions, 84 deletions
diff --git a/source/blender/editors/gpencil/CMakeLists.txt b/source/blender/editors/gpencil/CMakeLists.txt index 7a2f196fd6d..b312f397939 100644 --- a/source/blender/editors/gpencil/CMakeLists.txt +++ b/source/blender/editors/gpencil/CMakeLists.txt @@ -42,6 +42,7 @@ set(SRC gpencil_edit.c gpencil_ops.c gpencil_paint.c + gpencil_undo.c gpencil_intern.h ) diff --git a/source/blender/editors/gpencil/drawgpencil.c b/source/blender/editors/gpencil/drawgpencil.c index 440d5ee7c4d..cfa9585868e 100644 --- a/source/blender/editors/gpencil/drawgpencil.c +++ b/source/blender/editors/gpencil/drawgpencil.c @@ -644,7 +644,7 @@ static void gp_draw_data (bGPdata *gpd, int offsx, int offsy, int winx, int winy /* Check if may need to draw the active stroke cache, only if this layer is the active layer * that is being edited. (Stroke buffer is currently stored in gp-data) */ - if ((G.f & G_GREASEPENCIL) && (gpl->flag & GP_LAYER_ACTIVE) && + if (ED_gpencil_session_active() && (gpl->flag & GP_LAYER_ACTIVE) && (gpf->flag & GP_FRAME_PAINT)) { /* Buffer stroke needs to be drawn with a different linestyle to help differentiate them from normal strokes. */ diff --git a/source/blender/editors/gpencil/gpencil_edit.c b/source/blender/editors/gpencil/gpencil_edit.c index 9dc764b7aac..1cd8b1f05db 100644 --- a/source/blender/editors/gpencil/gpencil_edit.c +++ b/source/blender/editors/gpencil/gpencil_edit.c @@ -511,8 +511,8 @@ static void gp_stroke_to_bezier (bContext *C, bGPDlayer *gpl, bGPDstroke *gps, C copy_v3_v3(p3d_prev, p3d_cur); copy_v3_v3(p3d_cur, p3d_next); - if (i + 1 < tot) { - gp_strokepoint_convertcoords(C, gps, pt+1, p3d_next, subrect); + if (i + 2 < tot) { + gp_strokepoint_convertcoords(C, gps, pt + 2, p3d_next, subrect); } } diff --git a/source/blender/editors/gpencil/gpencil_intern.h b/source/blender/editors/gpencil/gpencil_intern.h index c31de8d30a7..8000af54f53 100644 --- a/source/blender/editors/gpencil/gpencil_intern.h +++ b/source/blender/editors/gpencil/gpencil_intern.h @@ -37,6 +37,7 @@ /* ***************************************************** */ /* Operator Defines */ +struct bGPdata; struct wmOperatorType; /* drawing ---------- */ @@ -48,6 +49,7 @@ typedef enum eGPencil_PaintModes { GP_PAINTMODE_DRAW = 0, GP_PAINTMODE_ERASER, GP_PAINTMODE_DRAW_STRAIGHT, + GP_PAINTMODE_DRAW_POLY } eGPencil_PaintModes; /* buttons editing --- */ @@ -61,6 +63,11 @@ void GPENCIL_OT_active_frame_delete(struct wmOperatorType *ot); void GPENCIL_OT_convert(struct wmOperatorType *ot); +/* undo stack ---------- */ + +void gpencil_undo_init(struct bGPdata *gpd); +void gpencil_undo_push(struct bGPdata *gpd); +void gpencil_undo_finish(void); /******************************************************* */ /* FILTERED ACTION DATA - TYPES ---> XXX DEPRECEATED OLD ANIM SYSTEM CODE! */ diff --git a/source/blender/editors/gpencil/gpencil_ops.c b/source/blender/editors/gpencil/gpencil_ops.c index e1e4c8d5457..150e0ba90e4 100644 --- a/source/blender/editors/gpencil/gpencil_ops.c +++ b/source/blender/editors/gpencil/gpencil_ops.c @@ -59,6 +59,9 @@ void ED_keymap_gpencil(wmKeyConfig *keyconf) /* draw - straight lines */ kmi=WM_keymap_add_item(keymap, "GPENCIL_OT_draw", LEFTMOUSE, KM_PRESS, KM_CTRL, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_STRAIGHT); + /* draw - poly lines */ + kmi=WM_keymap_add_item(keymap, "GPENCIL_OT_draw", RIGHTMOUSE, KM_PRESS, KM_CTRL, DKEY); + RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_DRAW_POLY); /* erase */ kmi=WM_keymap_add_item(keymap, "GPENCIL_OT_draw", RIGHTMOUSE, KM_PRESS, 0, DKEY); RNA_enum_set(kmi->ptr, "mode", GP_PAINTMODE_ERASER); diff --git a/source/blender/editors/gpencil/gpencil_paint.c b/source/blender/editors/gpencil/gpencil_paint.c index 04565eab155..a23f2064a9e 100644 --- a/source/blender/editors/gpencil/gpencil_paint.c +++ b/source/blender/editors/gpencil/gpencil_paint.c @@ -124,6 +124,7 @@ enum { /* Runtime flags */ enum { GP_PAINTFLAG_FIRSTRUN = (1<<0), /* operator just started */ + GP_PAINTFLAG_STROKEADDED = (1<<1) /* stroke was already added during draw session */ }; /* ------ */ @@ -152,7 +153,7 @@ static int gpencil_draw_poll (bContext *C) /* check if current context can support GPencil data */ if (gpencil_data_get_pointers(C, NULL) != NULL) { /* check if Grease Pencil isn't already running */ - if ((G.f & G_GREASEPENCIL) == 0) + if (ED_gpencil_session_active() == 0) return 1; else CTX_wm_operator_poll_msg_set(C, "Grease Pencil operator is already active"); @@ -375,6 +376,52 @@ static short gp_stroke_addpoint (tGPsdata *p, const int mval[2], float pressure) else return GP_STROKEADD_NORMAL; } + else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) { + /* get pointer to destination point */ + pt= (tGPspoint *)(gpd->sbuffer); + + /* store settings */ + pt->x= mval[0]; + pt->y= mval[1]; + pt->pressure= pressure; + + /* if there's stroke fir this poly line session add (or replace last) point + to stroke. This allows to draw lines more interactively (see new segment + during mouse slide, i.e.) */ + if (p->flags & GP_PAINTFLAG_STROKEADDED) { + bGPDstroke *gps= p->gpf->strokes.last; + bGPDspoint *pts; + + /* first time point is adding to temporary buffer -- need to allocate new point in stroke */ + if (gpd->sbuffer_size == 0) { + gps->points = MEM_reallocN(gps->points, sizeof(bGPDspoint)*(gps->totpoints+1)); + gps->totpoints++; + } + + pts = &gps->points[gps->totpoints-1]; + + /* special case for poly lines: normally, depth is needed only when creating new stroke from buffer, + but poly lines are converting to stroke instantly, so initialize depth buffer before converting coordinates */ + if (gpencil_project_check(p)) { + View3D *v3d= p->sa->spacedata.first; + + view3d_region_operator_needs_opengl(p->win, p->ar); + ED_view3d_autodist_init(p->scene, p->ar, v3d, (p->gpd->flag & GP_DATA_DEPTH_STROKE) ? 1:0); + } + + /* convert screen-coordinates to appropriate coordinates (and store them) */ + gp_stroke_convertcoords(p, &pt->x, &pts->x, NULL); + + /* copy pressure */ + pts->pressure= pt->pressure; + } + + /* increment counters */ + if (gpd->sbuffer_size == 0) + gpd->sbuffer_size++; + + return GP_STROKEADD_NORMAL; + } /* return invalid state for now... */ return GP_STROKEADD_INVALID; @@ -395,7 +442,7 @@ static void gp_stroke_smooth (tGPsdata *p) int i=0, cmx=gpd->sbuffer_size; /* only smooth if smoothing is enabled, and we're not doing a straight line */ - if (!(U.gp_settings & GP_PAINT_DOSMOOTH) || (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT)) + if (!(U.gp_settings & GP_PAINT_DOSMOOTH) || ELEM(p->paintmode, GP_PAINTMODE_DRAW_STRAIGHT, GP_PAINTMODE_DRAW_POLY)) return; /* don't try if less than 2 points in buffer */ @@ -527,17 +574,28 @@ static void gp_stroke_newfrombuffer (tGPsdata *p) return; } + /* special case for poly line -- for already added stroke during session + coordinates are getting added to stroke immediatelly to allow more + interactive behavior */ + if (p->paintmode == GP_PAINTMODE_DRAW_POLY) { + if (p->flags & GP_PAINTFLAG_STROKEADDED) + return; + } + /* allocate memory for a new stroke */ gps= MEM_callocN(sizeof(bGPDstroke), "gp_stroke"); - /* allocate enough memory for a continuous array for storage points */ - pt= gps->points= MEM_callocN(sizeof(bGPDspoint)*totelem, "gp_stroke_points"); - /* copy appropriate settings for stroke */ gps->totpoints= totelem; gps->thickness= p->gpl->thickness; gps->flag= gpd->sbuffer_sflag; + /* allocate enough memory for a continuous array for storage points */ + gps->points= MEM_callocN(sizeof(bGPDspoint)*gps->totpoints, "gp_stroke_points"); + + /* set pointer to first non-initialized point */ + pt= gps->points + (gps->totpoints - totelem); + /* copy points from the buffer to the stroke */ if (p->paintmode == GP_PAINTMODE_DRAW_STRAIGHT) { /* straight lines only -> only endpoints */ @@ -565,6 +623,16 @@ static void gp_stroke_newfrombuffer (tGPsdata *p) pt->pressure= ptc->pressure; } } + else if (p->paintmode == GP_PAINTMODE_DRAW_POLY) { + /* first point */ + ptc= gpd->sbuffer; + + /* convert screen-coordinates to appropriate coordinates (and store them) */ + gp_stroke_convertcoords(p, &ptc->x, &pt->x, NULL); + + /* copy pressure */ + pt->pressure= ptc->pressure; + } else { float *depth_arr= NULL; @@ -643,6 +711,8 @@ static void gp_stroke_newfrombuffer (tGPsdata *p) MEM_freeN(depth_arr); } + p->flags |= GP_PAINTFLAG_STROKEADDED; + /* add stroke to frame */ BLI_addtail(&p->gpf->strokes, gps); } @@ -891,10 +961,14 @@ static void gp_session_validatebuffer (tGPsdata *p) bGPdata *gpd= p->gpd; /* clear memory of buffer (or allocate it if starting a new session) */ - if (gpd->sbuffer) + if (gpd->sbuffer) { + //printf("\t\tGP - reset sbuffer\n"); memset(gpd->sbuffer, 0, sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX); - else + } + else { + //printf("\t\tGP - allocate sbuffer\n"); gpd->sbuffer= MEM_callocN(sizeof(tGPspoint)*GP_STROKE_BUFFER_MAX, "gp_session_strokebuffer"); + } /* reset indices */ gpd->sbuffer_size = 0; @@ -903,24 +977,21 @@ static void gp_session_validatebuffer (tGPsdata *p) gpd->sbuffer_sflag= 0; } -/* init new painting session */ -static tGPsdata *gp_session_initpaint (bContext *C) +/* (re)init new painting data */ +static int gp_session_initdata (bContext *C, tGPsdata *p) { - tGPsdata *p = NULL; bGPdata **gpd_ptr = NULL; ScrArea *curarea= CTX_wm_area(C); ARegion *ar= CTX_wm_region(C); /* make sure the active view (at the starting time) is a 3d-view */ if (curarea == NULL) { + p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: No active view for painting \n"); - return NULL; + return 0; } - /* create new context data */ - p= MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data"); - /* pass on current scene and window */ p->scene= CTX_data_scene(C); p->win= CTX_wm_window(C); @@ -942,7 +1013,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: 3D-View active region doesn't have any region data, so cannot be drawable \n"); - return p; + return 0; } #if 0 // XXX will this sort of antiquated stuff be restored? @@ -951,7 +1022,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: In active view, Grease Pencil not shown \n"); - return p; + return 0; } #endif } @@ -972,7 +1043,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: In active view, Grease Pencil not shown \n"); - return; + return 0; } #endif } @@ -992,13 +1063,13 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: In active view (sequencer), active mode doesn't support Grease Pencil \n"); - return; + return 0; } if ((sseq->flag & SEQ_DRAW_GPENCIL)==0) { p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: In active view, Grease Pencil not shown \n"); - return; + return 0; } } break; @@ -1019,7 +1090,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: In active view, Grease Pencil not shown \n"); - return p; + return 0; } #endif } @@ -1031,7 +1102,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: Active view not appropriate for Grease Pencil drawing \n"); - return p; + return 0; } break; } @@ -1042,7 +1113,7 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->status= GP_STATUS_ERROR; if (G.f & G_DEBUG) printf("Error: Current context doesn't allow for any Grease Pencil data \n"); - return p; + return 0; } else { /* if no existing GPencil block exists, add one */ @@ -1051,8 +1122,11 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->gpd= *gpd_ptr; } - /* set edit flags - so that buffer will get drawn */ - G.f |= G_GREASEPENCIL; + if(ED_gpencil_session_active()==0) { + /* initialize undo stack, + also, existing undo stack would make buffer drawn */ + gpencil_undo_init(p->gpd); + } /* clear out buffer (stored in gp-data), in case something contaminated it */ gp_session_validatebuffer(p); @@ -1062,6 +1136,19 @@ static tGPsdata *gp_session_initpaint (bContext *C) p->im2d_settings.sizex= 1; p->im2d_settings.sizey= 1; #endif + + return 1; +} + +/* init new painting session */ +static tGPsdata *gp_session_initpaint (bContext *C) +{ + tGPsdata *p = NULL; + + /* create new context data */ + p= MEM_callocN(sizeof(tGPsdata), "GPencil Drawing Data"); + + gp_session_initdata(C, p); /* return context data for running paint operator */ return p; @@ -1078,6 +1165,7 @@ static void gp_session_cleanup (tGPsdata *p) /* free stroke buffer */ if (gpd->sbuffer) { + //printf("\t\tGP - free sbuffer\n"); MEM_freeN(gpd->sbuffer); gpd->sbuffer= NULL; } @@ -1247,7 +1335,8 @@ static void gp_paint_strokeend (tGPsdata *p) static void gp_paint_cleanup (tGPsdata *p) { /* finish off a stroke */ - gp_paint_strokeend(p); + if(p->gpd) + gp_paint_strokeend(p); /* "unlock" frame */ if (p->gpf) @@ -1260,8 +1349,8 @@ static void gpencil_draw_exit (bContext *C, wmOperator *op) { tGPsdata *p= op->customdata; - /* clear edit flags */ - G.f &= ~G_GREASEPENCIL; + /* clear undo stack */ + gpencil_undo_finish(); /* restore cursor to indicate end of drawing */ WM_cursor_restore(CTX_wm_window(C)); @@ -1592,6 +1681,7 @@ static int gpencil_draw_invoke (bContext *C, wmOperator *op, wmEvent *event) //printf("\tGP - hotkey invoked... waiting for click-drag\n"); } + WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL, NULL); /* add a modal handler for this operator, so that we can then draw continuous strokes */ WM_event_add_modal_handler(C, op); return OPERATOR_RUNNING_MODAL; @@ -1609,16 +1699,57 @@ static int gpencil_area_exists(bContext *C, ScrArea *satest) return 0; } +static tGPsdata *gpencil_stroke_begin(bContext *C, wmOperator *op) +{ + tGPsdata *p= op->customdata; + + /* we must check that we're still within the area that we're set up to work from + * otherwise we could crash (see bug #20586) + */ + if (CTX_wm_area(C) != p->sa) { + printf("\t\t\tGP - wrong area execution abort! \n"); + p->status= GP_STATUS_ERROR; + } + + //printf("\t\tGP - start stroke \n"); + + /* we may need to set up paint env again if we're resuming */ + // XXX: watch it with the paintmode! in future, it'd be nice to allow changing paint-mode when in sketching-sessions + // XXX: with tablet events, we may event want to check for eraser here, for nicer tablet support + + if (gp_session_initdata(C, p)) + gp_paint_initstroke(p, p->paintmode); + + p= op->customdata; + + if(p->status != GP_STATUS_ERROR) + p->status= GP_STATUS_PAINTING; + + return op->customdata; +} + +static void gpencil_stroke_end(wmOperator *op) +{ + tGPsdata *p= op->customdata; + + gp_paint_cleanup(p); + + gpencil_undo_push(p->gpd); + + gp_session_cleanup(p); + + p->status= GP_STATUS_IDLING; + + p->gpd= NULL; + p->gpl= NULL; + p->gpf= NULL; +} + /* events handling during interactive drawing part of operator */ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) { tGPsdata *p= op->customdata; - //int estate = OPERATOR_PASS_THROUGH; /* default exit state - not handled, so let others have a share of the pie */ - /* currently, grease pencil conflicts with such operators as undo and set object mode - which makes behavior of operator totally unpredictable and crash for some cases. - the only way to solve this proper is to ger rid of pointers to data which can - chage stored in operator custom data (sergey) */ - int estate = OPERATOR_RUNNING_MODAL; + int estate = OPERATOR_PASS_THROUGH; /* default exit state - not handled, so let others have a share of the pie */ // if (event->type == NDOF_MOTION) // return OPERATOR_PASS_THROUGH; @@ -1646,17 +1777,24 @@ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) if (ELEM(event->type, LEFTMOUSE, RIGHTMOUSE)) { /* if painting, end stroke */ if (p->status == GP_STATUS_PAINTING) { + int sketch= 0; /* basically, this should be mouse-button up = end stroke * BUT what happens next depends on whether we 'painting sessions' is enabled */ - if (GPENCIL_SKETCH_SESSIONS_ON(p->scene)) { + sketch|= GPENCIL_SKETCH_SESSIONS_ON(p->scene); + /* polyline drawig is also 'sketching' -- all knots should be added during one session */ + sketch|= p->paintmode == GP_PAINTMODE_DRAW_POLY; + + if (sketch) { /* end stroke only, and then wait to resume painting soon */ //printf("\t\tGP - end stroke only\n"); - gp_paint_cleanup(p); - p->status= GP_STATUS_IDLING; + gpencil_stroke_end(op); /* we've just entered idling state, so this event was processed (but no others yet) */ estate = OPERATOR_RUNNING_MODAL; + + /* stroke could be smoothed, send notifier to refresh screen */ + WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); } else { //printf("\t\tGP - end of stroke + op\n"); @@ -1664,35 +1802,19 @@ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) estate = OPERATOR_FINISHED; } } - else { + else if (event->val == KM_PRESS) { /* not painting, so start stroke (this should be mouse-button down) */ - /* we must check that we're still within the area that we're set up to work from - * otherwise we could crash (see bug #20586) - */ - if (CTX_wm_area(C) != p->sa) { - //printf("\t\t\tGP - wrong area execution abort! \n"); - p->status= GP_STATUS_ERROR; + p= gpencil_stroke_begin(C, op); + + if (p->status == GP_STATUS_ERROR) { estate = OPERATOR_CANCELLED; } - else { - //printf("\t\tGP - start stroke \n"); - p->status= GP_STATUS_PAINTING; - - /* we may need to set up paint env again if we're resuming */ - // XXX: watch it with the paintmode! in future, it'd be nice to allow changing paint-mode when in sketching-sessions - // XXX: with tablet events, we may event want to check for eraser here, for nicer tablet support - gp_paint_initstroke(p, p->paintmode); - - if (p->status == GP_STATUS_ERROR) { - estate = OPERATOR_CANCELLED; - } - } + } else { + p->status = GP_STATUS_IDLING; } } - - /* handle mode-specific events */ if (p->status == GP_STATUS_PAINTING) { /* handle painting mouse-movements? */ @@ -1704,7 +1826,7 @@ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) /* finish painting operation if anything went wrong just now */ if (p->status == GP_STATUS_ERROR) { - //printf("\t\t\t\tGP - add error done! \n"); + printf("\t\t\t\tGP - add error done! \n"); estate = OPERATOR_CANCELLED; } else { @@ -1721,28 +1843,6 @@ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) estate = OPERATOR_RUNNING_MODAL; } } - else if (p->status == GP_STATUS_IDLING) { - /* standard undo/redo shouldn't be allowed to execute or else it causes crashes, so catch it here */ - // FIXME: this is a hardcoded hotkey that can't be changed - // TODO: catch redo as well, but how? - if (event->type == ZKEY && event->val == KM_RELEASE) { - /* oskey = cmd key on macs as they seem to use cmd-z for undo as well? */ - if ((event->ctrl) || (event->oskey)) { - /* just delete last stroke, which will look like undo to the end user */ - //printf("caught attempted undo event... deleting last stroke \n"); - gpencil_frame_delete_laststroke(p->gpl, p->gpf); - /* undoing the last line can free p->gpf - * note, could do this in a bit more of an elegant way then a search but it at least prevents a crash */ - if(BLI_findindex(&p->gpl->frames, p->gpf) == -1) { - p->gpf= NULL; - } - - /* event handled, so force refresh */ - ED_region_tag_redraw(p->ar); /* just active area for now, since doing whole screen is too slow */ - estate = OPERATOR_RUNNING_MODAL; - } - } - } /* gpencil modal operator stores area, which can be removed while using it (like fullscreen) */ if(0==gpencil_area_exists(C, p->sa)) @@ -1778,6 +1878,7 @@ static int gpencil_draw_modal (bContext *C, wmOperator *op, wmEvent *event) static EnumPropertyItem prop_gpencil_drawmodes[] = { {GP_PAINTMODE_DRAW, "DRAW", 0, "Draw Freehand", ""}, {GP_PAINTMODE_DRAW_STRAIGHT, "DRAW_STRAIGHT", 0, "Draw Straight Lines", ""}, + {GP_PAINTMODE_DRAW_POLY, "DRAW_POLY", 0, "Dtaw Poly Line", ""}, {GP_PAINTMODE_ERASER, "ERASER", 0, "Eraser", ""}, {0, NULL, 0, NULL, NULL} }; diff --git a/source/blender/editors/gpencil/gpencil_undo.c b/source/blender/editors/gpencil/gpencil_undo.c new file mode 100644 index 00000000000..1154975e3cc --- /dev/null +++ b/source/blender/editors/gpencil/gpencil_undo.c @@ -0,0 +1,168 @@ +/* + * $Id$ + * + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2011 Blender Foundation. + * All rights reserved. + * + * + * Contributor(s): Blender Foundation, + * Sergey Sharybin + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include <stdlib.h> +#include <string.h> + +#include "MEM_guardedalloc.h" + +#include "DNA_gpencil_types.h" +#include "DNA_listBase.h" +#include "DNA_windowmanager_types.h" + +#include "BKE_context.h" +#include "BKE_gpencil.h" + +#include "BLI_listbase.h" + +#include "ED_gpencil.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "gpencil_intern.h" + +#define MAXUNDONAME 64 + +typedef struct bGPundonode { + struct bGPundonode *next, *prev; + + char name[MAXUNDONAME]; + struct bGPdata *gpd; +} bGPundonode; + +static ListBase undo_nodes = {NULL, NULL}; +static bGPundonode *cur_node = NULL; + +int ED_gpencil_session_active(void) +{ + return undo_nodes.first != NULL; +} + +int ED_undo_gpencil_step(bContext *C, int step, const char *name) +{ + bGPdata **gpd_ptr= NULL, *new_gpd= NULL; + + gpd_ptr= gpencil_data_get_pointers(C, NULL); + + if(step==1) { /* undo */ + //printf("\t\tGP - undo step\n"); + if(cur_node->prev) { + if(!name || strcmp(cur_node->name, name) == 0) { + cur_node= cur_node->prev; + new_gpd= cur_node->gpd; + } + } + } + else if (step==-1) { + //printf("\t\tGP - redo step\n"); + if(cur_node->next) { + if(!name || strcmp(cur_node->name, name) == 0) { + cur_node= cur_node->next; + new_gpd= cur_node->gpd; + } + } + } + + if(new_gpd) { + if(gpd_ptr) { + if(*gpd_ptr) { + bGPdata *gpd= *gpd_ptr; + bGPDlayer *gpl, *gpld; + + free_gpencil_layers(&gpd->layers); + + /* copy layers */ + gpd->layers.first= gpd->layers.last= NULL; + + for (gpl= new_gpd->layers.first; gpl; gpl= gpl->next) { + /* make a copy of source layer and its data */ + gpld= gpencil_layer_duplicate(gpl); + BLI_addtail(&gpd->layers, gpld); + } + } + } + } + + WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL); + + return OPERATOR_FINISHED; +} + +void gpencil_undo_init(bGPdata *gpd) +{ + gpencil_undo_push(gpd); +} + +void gpencil_undo_push(bGPdata *gpd) +{ + bGPundonode *undo_node; + + //printf("\t\tGP - undo push\n"); + + if(cur_node) { + /* remove all un-done nodes from stack */ + undo_node= cur_node->next; + + while(undo_node) { + bGPundonode *next_node= undo_node->next; + + free_gpencil_data(undo_node->gpd); + MEM_freeN(undo_node->gpd); + + BLI_freelinkN(&undo_nodes, undo_node); + + undo_node= next_node; + } + } + + /* create new undo node */ + undo_node= MEM_callocN(sizeof(bGPundonode), "gpencil undo node"); + undo_node->gpd= gpencil_data_duplicate(gpd); + + cur_node= undo_node; + + BLI_addtail(&undo_nodes, undo_node); +} + +void gpencil_undo_finish(void) +{ + bGPundonode *undo_node= undo_nodes.first; + + while(undo_node) { + free_gpencil_data(undo_node->gpd); + MEM_freeN(undo_node->gpd); + + undo_node= undo_node->next; + } + + BLI_freelistN(&undo_nodes); + + cur_node= NULL; +} |