merge with trunk at r31523
[blender-staging.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 "DNA_userdef_types.h"
31
32 #include "BLI_listbase.h"
33
34 #include "BKE_context.h"
35 #include "BKE_global.h"
36
37 #include "ED_sculpt.h"
38
39 #include "paint_intern.h"
40
41 #define MAXUNDONAME     64
42
43 typedef struct UndoElem {
44         struct UndoElem *next, *prev;
45         char name[MAXUNDONAME];
46         uintptr_t undosize;
47
48         ListBase elems;
49
50         UndoRestoreCb restore;
51         UndoFreeCb free;
52 } UndoElem;
53
54 typedef struct UndoStack {
55         int type;
56         ListBase elems;
57         UndoElem *current;
58 } UndoStack;
59
60 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
61 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
62
63 /* Generic */
64
65 static void undo_restore(bContext *C, UndoStack *stack, UndoElem *uel)
66 {
67         if(uel && uel->restore)
68                 uel->restore(C, &uel->elems);
69 }
70
71 static void undo_elem_free(UndoStack *stack, UndoElem *uel)
72 {
73         if(uel && uel->free) {
74                 uel->free(&uel->elems);
75                 BLI_freelistN(&uel->elems);
76         }
77 }
78
79 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
80 {
81         UndoElem *uel;
82         int nr;
83         
84         /* Undo push is split up in begin and end, the reason is that as painting
85          * happens more tiles/nodes are added to the list, and at the very end we
86          * know how much memory the undo used to remove old undo elements */
87
88         /* remove all undos after (also when stack->current==NULL) */
89         while(stack->elems.last != stack->current) {
90                 uel= stack->elems.last;
91                 undo_elem_free(stack, uel);
92                 BLI_freelinkN(&stack->elems, uel);
93         }
94         
95         /* make new */
96         stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
97         uel->restore= restore;
98         uel->free= free;
99         BLI_addtail(&stack->elems, uel);
100
101         /* name can be a dynamic string */
102         strncpy(uel->name, name, MAXUNDONAME-1);
103         
104         /* limit amount to the maximum amount*/
105         nr= 0;
106         uel= stack->elems.last;
107         while(uel) {
108                 nr++;
109                 if(nr==U.undosteps) break;
110                 uel= uel->prev;
111         }
112         if(uel) {
113                 while(stack->elems.first!=uel) {
114                         UndoElem *first= stack->elems.first;
115                         undo_elem_free(stack, first);
116                         BLI_freelinkN(&stack->elems, first);
117                 }
118         }
119 }
120
121 static void undo_stack_push_end(UndoStack *stack)
122 {
123         UndoElem *uel;
124         uintptr_t totmem, maxmem;
125
126         if(U.undomemory != 0) {
127                 /* limit to maximum memory (afterwards, we can't know in advance) */
128                 totmem= 0;
129                 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
130
131                 uel= stack->elems.last;
132                 while(uel) {
133                         totmem+= uel->undosize;
134                         if(totmem>maxmem) break;
135                         uel= uel->prev;
136                 }
137
138                 if(uel) {
139                         while(stack->elems.first!=uel) {
140                                 UndoElem *first= stack->elems.first;
141                                 undo_elem_free(stack, first);
142                                 BLI_freelinkN(&stack->elems, first);
143                         }
144                 }
145         }
146 }
147
148 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
149 {
150         UndoElem *undo;
151
152         if(step==1) {
153                 if(stack->current==NULL);
154                 else {
155                         if(!name || strcmp(stack->current->name, name) == 0) {
156                                 if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
157                                 undo_restore(C, stack, stack->current);
158                                 stack->current= stack->current->prev;
159                                 return 1;
160                         }
161                 }
162         }
163         else if(step==-1) {
164                 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
165                 else {
166                         if(!name || strcmp(stack->current->name, name) == 0) {
167                                 undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
168                                 undo_restore(C, stack, undo);
169                                 stack->current= undo;
170                                 if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
171                                 return 1;
172                         }
173                 }
174         }
175
176         return 0;
177 }
178
179 static void undo_stack_free(UndoStack *stack)
180 {
181         UndoElem *uel;
182         
183         for(uel=stack->elems.first; uel; uel=uel->next)
184                 undo_elem_free(stack, uel);
185
186         BLI_freelistN(&stack->elems);
187         stack->current= NULL;
188 }
189
190 /* Exported Functions */
191
192 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
193 {
194         if(type == UNDO_PAINT_IMAGE)
195                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
196         else if(type == UNDO_PAINT_MESH)
197                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
198 }
199
200 ListBase *undo_paint_push_get_list(int type)
201 {
202         if(type == UNDO_PAINT_IMAGE) {
203                 if(ImageUndoStack.current)
204                         return &ImageUndoStack.current->elems;
205         }
206         else if(type == UNDO_PAINT_MESH) {
207                 if(MeshUndoStack.current)
208                         return &MeshUndoStack.current->elems;
209         }
210         
211         return NULL;
212 }
213
214 void undo_paint_push_count_alloc(int type, int size)
215 {
216         if(type == UNDO_PAINT_IMAGE)
217                 ImageUndoStack.current->undosize += size;
218         else if(type == UNDO_PAINT_MESH)
219                 MeshUndoStack.current->undosize += size;
220 }
221
222 void undo_paint_push_end(int type)
223 {
224         if(type == UNDO_PAINT_IMAGE)
225                 undo_stack_push_end(&ImageUndoStack);
226         else if(type == UNDO_PAINT_MESH)
227                 undo_stack_push_end(&MeshUndoStack);
228 }
229
230 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
231 {
232         if(type == UNDO_PAINT_IMAGE)
233                 return undo_stack_step(C, &ImageUndoStack, step, name);
234         else if(type == UNDO_PAINT_MESH)
235                 return undo_stack_step(C, &MeshUndoStack, step, name);
236         
237         return 0;
238 }
239
240 void ED_undo_paint_free(void)
241 {
242         undo_stack_free(&ImageUndoStack);
243         undo_stack_free(&MeshUndoStack);
244 }
245