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