Merge from trunk -r 25003:25745.
[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., 59 Temple Place - Suite 330, Boston, MA        02111-1307, 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, 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 void undo_stack_step(bContext *C, UndoStack *stack, int step)
149 {
150         UndoElem *undo;
151
152         if(step==1) {
153                 if(stack->current==NULL);
154                 else {
155                         if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
156                         undo_restore(C, stack, stack->current);
157                         stack->current= stack->current->prev;
158                 }
159         }
160         else if(step==-1) {
161                 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
162                 else {
163                         undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
164                         undo_restore(C, stack, undo);
165                         stack->current= undo;
166                         if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
167                 }
168         }
169 }
170
171 static void undo_stack_free(UndoStack *stack)
172 {
173         UndoElem *uel;
174         
175         for(uel=stack->elems.first; uel; uel=uel->next)
176                 undo_elem_free(stack, uel);
177
178         BLI_freelistN(&stack->elems);
179         stack->current= NULL;
180 }
181
182 /* Exported Functions */
183
184 void undo_paint_push_begin(int type, char *name, UndoRestoreCb restore, UndoFreeCb free)
185 {
186         if(type == UNDO_PAINT_IMAGE)
187                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
188         else if(type == UNDO_PAINT_MESH)
189                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
190 }
191
192 ListBase *undo_paint_push_get_list(int type)
193 {
194         if(type == UNDO_PAINT_IMAGE) {
195                 if(ImageUndoStack.current)
196                         return &ImageUndoStack.current->elems;
197         }
198         else if(type == UNDO_PAINT_MESH) {
199                 if(MeshUndoStack.current)
200                         return &MeshUndoStack.current->elems;
201         }
202         
203         return NULL;
204 }
205
206 void undo_paint_push_count_alloc(int type, int size)
207 {
208         if(type == UNDO_PAINT_IMAGE)
209                 ImageUndoStack.current->undosize += size;
210         else if(type == UNDO_PAINT_MESH)
211                 MeshUndoStack.current->undosize += size;
212 }
213
214 void undo_paint_push_end(int type)
215 {
216         if(type == UNDO_PAINT_IMAGE)
217                 undo_stack_push_end(&ImageUndoStack);
218         else if(type == UNDO_PAINT_MESH)
219                 undo_stack_push_end(&MeshUndoStack);
220 }
221
222 void ED_undo_paint_step(bContext *C, int type, int step)
223 {
224         if(type == UNDO_PAINT_IMAGE)
225                 undo_stack_step(C, &ImageUndoStack, step);
226         else if(type == UNDO_PAINT_MESH)
227                 undo_stack_step(C, &MeshUndoStack, step);
228 }
229
230 void ED_undo_paint_free(void)
231 {
232         undo_stack_free(&ImageUndoStack);
233         undo_stack_free(&MeshUndoStack);
234 }
235