svn merge ^/trunk/blender -r44562:45364
[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.debug & G_DEBUG_WM) {
164                                         printf("%s: undo '%s'\n", __func__, stack->current->name);
165                                 }
166                                 undo_restore(C, stack, stack->current);
167                                 stack->current = stack->current->prev;
168                                 return 1;
169                         }
170                 }
171         }
172         else if (step == -1) {
173                 if ((stack->current != NULL && stack->current->next == NULL) || stack->elems.first == NULL) ;
174                 else {
175                         if (!name || strcmp(stack->current->name, name) == 0) {
176                                 undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
177                                 undo_restore(C, stack, undo);
178                                 stack->current = undo;
179                                 if (G.debug & G_DEBUG_WM) {
180                                         printf("%s: redo %s\n", __func__, undo->name);
181                                 }
182                                 return 1;
183                         }
184                 }
185         }
186
187         return 0;
188 }
189
190 static void undo_stack_free(UndoStack *stack)
191 {
192         UndoElem *uel;
193         
194         for (uel = stack->elems.first; uel; uel = uel->next)
195                 undo_elem_free(stack, uel);
196
197         BLI_freelistN(&stack->elems);
198         stack->current = NULL;
199 }
200
201 /* Exported Functions */
202
203 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
204 {
205         if (type == UNDO_PAINT_IMAGE)
206                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
207         else if (type == UNDO_PAINT_MESH)
208                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
209 }
210
211 ListBase *undo_paint_push_get_list(int type)
212 {
213         if (type == UNDO_PAINT_IMAGE) {
214                 if (ImageUndoStack.current)
215                         return &ImageUndoStack.current->elems;
216         }
217         else if (type == UNDO_PAINT_MESH) {
218                 if (MeshUndoStack.current)
219                         return &MeshUndoStack.current->elems;
220         }
221         
222         return NULL;
223 }
224
225 void undo_paint_push_count_alloc(int type, int size)
226 {
227         if (type == UNDO_PAINT_IMAGE)
228                 ImageUndoStack.current->undosize += size;
229         else if (type == UNDO_PAINT_MESH)
230                 MeshUndoStack.current->undosize += size;
231 }
232
233 void undo_paint_push_end(int type)
234 {
235         if (type == UNDO_PAINT_IMAGE)
236                 undo_stack_push_end(&ImageUndoStack);
237         else if (type == UNDO_PAINT_MESH)
238                 undo_stack_push_end(&MeshUndoStack);
239 }
240
241 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
242 {
243         if (type == UNDO_PAINT_IMAGE)
244                 return undo_stack_step(C, &ImageUndoStack, step, name);
245         else if (type == UNDO_PAINT_MESH)
246                 return undo_stack_step(C, &MeshUndoStack, step, name);
247         
248         return 0;
249 }
250
251 int ED_undo_paint_valid(int type, const char *name)
252 {
253         UndoStack *stack;
254         
255         if (type == UNDO_PAINT_IMAGE)
256                 stack = &ImageUndoStack;
257         else if (type == UNDO_PAINT_MESH)
258                 stack = &MeshUndoStack;
259         else 
260                 return 0;
261         
262         if (stack->current == NULL) ;
263         else {
264                 if (name && strcmp(stack->current->name, name) == 0)
265                         return 1;
266                 else
267                         return stack->elems.first != stack->elems.last;
268         }
269         return 0;
270 }
271
272 void ED_undo_paint_free(void)
273 {
274         undo_stack_free(&ImageUndoStack);
275         undo_stack_free(&MeshUndoStack);
276 }
277