doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / sculpt_paint / paint_undo.c
1 /*
2  * $Id$
3  *
4  * Undo system for painting and sculpting.
5  * 
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "MEM_guardedalloc.h"
29
30 #include "BLI_listbase.h"
31 #include "BLI_utildefines.h"
32
33 #include "DNA_userdef_types.h"
34
35
36 #include "BKE_context.h"
37 #include "BKE_global.h"
38
39 #include "ED_sculpt.h"
40
41 #include "paint_intern.h"
42
43 #define MAXUNDONAME     64
44
45 typedef struct UndoElem {
46         struct UndoElem *next, *prev;
47         char name[MAXUNDONAME];
48         uintptr_t undosize;
49
50         ListBase elems;
51
52         UndoRestoreCb restore;
53         UndoFreeCb free;
54 } UndoElem;
55
56 typedef struct UndoStack {
57         int type;
58         ListBase elems;
59         UndoElem *current;
60 } UndoStack;
61
62 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
63 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
64
65 /* Generic */
66
67 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
68 {
69         if(uel && uel->restore)
70                 uel->restore(C, &uel->elems);
71 }
72
73 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
74 {
75         if(uel && uel->free) {
76                 uel->free(&uel->elems);
77                 BLI_freelistN(&uel->elems);
78         }
79 }
80
81 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
82 {
83         UndoElem *uel;
84         int nr;
85         
86         /* Undo push is split up in begin and end, the reason is that as painting
87          * happens more tiles/nodes are added to the list, and at the very end we
88          * know how much memory the undo used to remove old undo elements */
89
90         /* remove all undos after (also when stack->current==NULL) */
91         while(stack->elems.last != stack->current) {
92                 uel= stack->elems.last;
93                 undo_elem_free(stack, uel);
94                 BLI_freelinkN(&stack->elems, uel);
95         }
96         
97         /* make new */
98         stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
99         uel->restore= restore;
100         uel->free= free;
101         BLI_addtail(&stack->elems, uel);
102
103         /* name can be a dynamic string */
104         strncpy(uel->name, name, MAXUNDONAME-1);
105         
106         /* limit amount to the maximum amount*/
107         nr= 0;
108         uel= stack->elems.last;
109         while(uel) {
110                 nr++;
111                 if(nr==U.undosteps) break;
112                 uel= uel->prev;
113         }
114         if(uel) {
115                 while(stack->elems.first!=uel) {
116                         UndoElem *first= stack->elems.first;
117                         undo_elem_free(stack, first);
118                         BLI_freelinkN(&stack->elems, first);
119                 }
120         }
121 }
122
123 static void undo_stack_push_end(UndoStack *stack)
124 {
125         UndoElem *uel;
126         uintptr_t totmem, maxmem;
127
128         if(U.undomemory != 0) {
129                 /* limit to maximum memory (afterwards, we can't know in advance) */
130                 totmem= 0;
131                 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
132
133                 uel= stack->elems.last;
134                 while(uel) {
135                         totmem+= uel->undosize;
136                         if(totmem>maxmem) break;
137                         uel= uel->prev;
138                 }
139
140                 if(uel) {
141                         while(stack->elems.first!=uel) {
142                                 UndoElem *first= stack->elems.first;
143                                 undo_elem_free(stack, first);
144                                 BLI_freelinkN(&stack->elems, first);
145                         }
146                 }
147         }
148 }
149
150 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
151 {
152         UndoElem *undo;
153
154         if(step==1) {
155                 if(stack->current==NULL);
156                 else {
157                         if(!name || strcmp(stack->current->name, name) == 0) {
158                                 if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
159                                 undo_restore(C, stack, stack->current);
160                                 stack->current= stack->current->prev;
161                                 return 1;
162                         }
163                 }
164         }
165         else if(step==-1) {
166                 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
167                 else {
168                         if(!name || strcmp(stack->current->name, name) == 0) {
169                                 undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
170                                 undo_restore(C, stack, undo);
171                                 stack->current= undo;
172                                 if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
173                                 return 1;
174                         }
175                 }
176         }
177
178         return 0;
179 }
180
181 static void undo_stack_free(UndoStack *stack)
182 {
183         UndoElem *uel;
184         
185         for(uel=stack->elems.first; uel; uel=uel->next)
186                 undo_elem_free(stack, uel);
187
188         BLI_freelistN(&stack->elems);
189         stack->current= NULL;
190 }
191
192 /* Exported Functions */
193
194 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
195 {
196         if(type == UNDO_PAINT_IMAGE)
197                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
198         else if(type == UNDO_PAINT_MESH)
199                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
200 }
201
202 ListBase *undo_paint_push_get_list(int type)
203 {
204         if(type == UNDO_PAINT_IMAGE) {
205                 if(ImageUndoStack.current)
206                         return &ImageUndoStack.current->elems;
207         }
208         else if(type == UNDO_PAINT_MESH) {
209                 if(MeshUndoStack.current)
210                         return &MeshUndoStack.current->elems;
211         }
212         
213         return NULL;
214 }
215
216 void undo_paint_push_count_alloc(int type, int size)
217 {
218         if(type == UNDO_PAINT_IMAGE)
219                 ImageUndoStack.current->undosize += size;
220         else if(type == UNDO_PAINT_MESH)
221                 MeshUndoStack.current->undosize += size;
222 }
223
224 void undo_paint_push_end(int type)
225 {
226         if(type == UNDO_PAINT_IMAGE)
227                 undo_stack_push_end(&ImageUndoStack);
228         else if(type == UNDO_PAINT_MESH)
229                 undo_stack_push_end(&MeshUndoStack);
230 }
231
232 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
233 {
234         if(type == UNDO_PAINT_IMAGE)
235                 return undo_stack_step(C, &ImageUndoStack, step, name);
236         else if(type == UNDO_PAINT_MESH)
237                 return undo_stack_step(C, &MeshUndoStack, step, name);
238         
239         return 0;
240 }
241
242 int ED_undo_paint_valid(int type, const char *name)
243 {
244         UndoStack *stack;
245         
246         if(type == UNDO_PAINT_IMAGE)
247                 stack= &ImageUndoStack;
248         else if(type == UNDO_PAINT_MESH)
249                 stack= &MeshUndoStack;
250         else 
251                 return 0;
252         
253         if(stack->current==NULL);
254         else {
255                 if(name && strcmp(stack->current->name, name) == 0)
256                         return 1;
257                 else
258                         return stack->elems.first != stack->elems.last;
259         }
260         return 0;
261 }
262
263 void ED_undo_paint_free(void)
264 {
265         undo_stack_free(&ImageUndoStack);
266         undo_stack_free(&MeshUndoStack);
267 }
268