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