Blender V2.61 - r43446

gpencil_undo.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2011 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  *
00022  * Contributor(s): Blender Foundation,
00023  *                 Sergey Sharybin
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 
00031 #include "MEM_guardedalloc.h"
00032 
00033 #include "DNA_gpencil_types.h"
00034 #include "DNA_listBase.h"
00035 #include "DNA_windowmanager_types.h"
00036 
00037 #include "BKE_context.h"
00038 #include "BKE_gpencil.h"
00039 
00040 #include "BLI_listbase.h"
00041 
00042 #include "ED_gpencil.h"
00043 
00044 #include "WM_api.h"
00045 #include "WM_types.h"
00046 
00047 #include "gpencil_intern.h"
00048 
00049 #define MAXUNDONAME 64
00050 
00051 typedef struct bGPundonode {
00052     struct bGPundonode *next, *prev;
00053 
00054     char name[MAXUNDONAME];
00055     struct bGPdata *gpd;
00056 } bGPundonode;
00057 
00058 static ListBase undo_nodes = {NULL, NULL};
00059 static bGPundonode *cur_node = NULL;
00060 
00061 int ED_gpencil_session_active(void)
00062 {
00063     return undo_nodes.first != NULL;
00064 }
00065 
00066 int ED_undo_gpencil_step(bContext *C, int step, const char *name)
00067 {
00068     bGPdata **gpd_ptr= NULL, *new_gpd= NULL;
00069 
00070     gpd_ptr= gpencil_data_get_pointers(C, NULL);
00071 
00072     if(step==1) {   /* undo */
00073         //printf("\t\tGP - undo step\n");
00074         if(cur_node->prev) {
00075             if(!name || strcmp(cur_node->name, name) == 0) {
00076                 cur_node= cur_node->prev;
00077                 new_gpd= cur_node->gpd;
00078             }
00079         }
00080     }
00081     else if (step==-1) {
00082         //printf("\t\tGP - redo step\n");
00083         if(cur_node->next) {
00084             if(!name || strcmp(cur_node->name, name) == 0) {
00085                 cur_node= cur_node->next;
00086                 new_gpd= cur_node->gpd;
00087             }
00088         }
00089     }
00090 
00091     if(new_gpd) {
00092         if(gpd_ptr) {
00093             if(*gpd_ptr) {
00094                 bGPdata *gpd= *gpd_ptr;
00095                 bGPDlayer *gpl, *gpld;
00096 
00097                 free_gpencil_layers(&gpd->layers);
00098 
00099                 /* copy layers */
00100                 gpd->layers.first= gpd->layers.last= NULL;
00101 
00102                 for (gpl= new_gpd->layers.first; gpl; gpl= gpl->next) {
00103                     /* make a copy of source layer and its data */
00104                     gpld= gpencil_layer_duplicate(gpl);
00105                     BLI_addtail(&gpd->layers, gpld);
00106                 }
00107             }
00108         }
00109     }
00110 
00111     WM_event_add_notifier(C, NC_SCREEN|ND_GPENCIL|NA_EDITED, NULL);
00112 
00113     return OPERATOR_FINISHED;
00114 }
00115 
00116 void gpencil_undo_init(bGPdata *gpd)
00117 {
00118     gpencil_undo_push(gpd);
00119 }
00120 
00121 void gpencil_undo_push(bGPdata *gpd)
00122 {
00123     bGPundonode *undo_node;
00124 
00125     //printf("\t\tGP - undo push\n");
00126 
00127     if(cur_node) {
00128         /* remove all un-done nodes from stack */
00129         undo_node= cur_node->next;
00130 
00131         while(undo_node) {
00132             bGPundonode *next_node= undo_node->next;
00133 
00134             free_gpencil_data(undo_node->gpd);
00135             MEM_freeN(undo_node->gpd);
00136 
00137             BLI_freelinkN(&undo_nodes, undo_node);
00138 
00139             undo_node= next_node;
00140         }
00141     }
00142 
00143     /* create new undo node */
00144     undo_node= MEM_callocN(sizeof(bGPundonode), "gpencil undo node");
00145     undo_node->gpd= gpencil_data_duplicate(gpd);
00146 
00147     cur_node= undo_node;
00148 
00149     BLI_addtail(&undo_nodes, undo_node);
00150 }
00151 
00152 void gpencil_undo_finish(void)
00153 {
00154     bGPundonode *undo_node= undo_nodes.first;
00155 
00156     while(undo_node) {
00157         free_gpencil_data(undo_node->gpd);
00158         MEM_freeN(undo_node->gpd);
00159 
00160         undo_node= undo_node->next;
00161     }
00162 
00163     BLI_freelistN(&undo_nodes);
00164 
00165     cur_node= NULL;
00166 }