Fix #36748 Sculpting/image painting does not respect undo steps limit.
[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 #include <stdlib.h>
27 #include <string.h>
28
29 #include "MEM_guardedalloc.h"
30
31 #include "BLI_listbase.h"
32 #include "BLI_utildefines.h"
33 #include "BLI_string.h"
34
35 #include "DNA_userdef_types.h"
36
37 #include "BKE_blender.h"
38 #include "BKE_context.h"
39 #include "BKE_global.h"
40
41 #include "ED_sculpt.h"
42
43 #include "paint_intern.h"
44
45 typedef struct UndoElem {
46         struct UndoElem *next, *prev;
47         char name[BKE_UNDO_STR_MAX];
48         uintptr_t undosize;
49
50         ListBase elems;
51
52         UndoRestoreCb restore;
53         UndoFreeCb free;
54 } UndoElem;
55
56 typedef struct UndoStack {
57         int type;
58         ListBase elems;
59         UndoElem *current;
60 } UndoStack;
61
62 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
63 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
64
65 /* Generic */
66
67 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
68 {
69         if (uel && uel->restore)
70                 uel->restore(C, &uel->elems);
71 }
72
73 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
74 {
75         if (uel && uel->free) {
76                 uel->free(&uel->elems);
77                 BLI_freelistN(&uel->elems);
78         }
79 }
80
81 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
82 {
83         UndoElem *uel;
84         int nr;
85         
86         /* Undo push is split up in begin and end, the reason is that as painting
87          * happens more tiles/nodes are added to the list, and at the very end we
88          * know how much memory the undo used to remove old undo elements */
89
90         /* remove all undos after (also when stack->current==NULL) */
91         while (stack->elems.last != stack->current) {
92                 uel = stack->elems.last;
93                 undo_elem_free(stack, uel);
94                 BLI_freelinkN(&stack->elems, uel);
95         }
96         
97         /* make new */
98         stack->current = uel = MEM_callocN(sizeof(UndoElem), "undo file");
99         uel->restore = restore;
100         uel->free = free;
101         BLI_addtail(&stack->elems, uel);
102
103         /* name can be a dynamic string */
104         BLI_strncpy(uel->name, name, sizeof(uel->name));
105         
106         /* limit amount to the maximum amount*/
107         nr = 0;
108         uel = stack->elems.last;
109         while (uel) {
110                 nr++;
111                 if (nr == U.undosteps) break;
112                 uel = uel->prev;
113         }
114         if (uel) {
115                 while (stack->elems.first != uel) {
116                         UndoElem *first = stack->elems.first;
117                         undo_elem_free(stack, first);
118                         BLI_freelinkN(&stack->elems, first);
119                 }
120         }
121 }
122
123 static void undo_stack_push_end(UndoStack *stack)
124 {
125         UndoElem *uel;
126         uintptr_t totmem, maxmem;
127         int totundo = 0;
128
129         /* first limit to undo steps */
130         uel = stack->elems.last;
131
132         while (uel) {
133                 totundo++;
134                 if (totundo > U.undosteps) break;
135                 uel = uel->prev;
136         }
137
138         if (uel) {
139                 UndoElem *first;
140
141                 /* in case the undo steps are zero, the current pointer will be invalid */
142                 if (uel == stack->current)
143                         stack->current = NULL;
144
145                 do {
146                         first = stack->elems.first;
147                         undo_elem_free(stack, first);
148                         BLI_freelinkN(&stack->elems, first);
149                 } while (first != uel);
150         }
151
152         if (U.undomemory != 0) {
153                 /* limit to maximum memory (afterwards, we can't know in advance) */
154                 totmem = 0;
155                 maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
156
157                 uel = stack->elems.last;
158                 while (uel) {
159                         totmem += uel->undosize;
160                         if (totmem > maxmem) break;
161                         uel = uel->prev;
162                 }
163
164                 if (uel) {
165                         while (stack->elems.first != uel) {
166                                 UndoElem *first = stack->elems.first;
167                                 undo_elem_free(stack, first);
168                                 BLI_freelinkN(&stack->elems, first);
169                         }
170                 }
171         }
172 }
173
174 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
175 {
176         UndoElem *undo;
177
178         if (step == 1) {
179                 if (stack->current == NULL) {
180                         /* pass */
181                 }
182                 else {
183                         if (!name || strcmp(stack->current->name, name) == 0) {
184                                 if (G.debug & G_DEBUG_WM) {
185                                         printf("%s: undo '%s'\n", __func__, stack->current->name);
186                                 }
187                                 undo_restore(C, stack, stack->current);
188                                 stack->current = stack->current->prev;
189                                 return 1;
190                         }
191                 }
192         }
193         else if (step == -1) {
194                 if ((stack->current != NULL && stack->current->next == NULL) || stack->elems.first == NULL) {
195                         /* pass */
196                 }
197                 else {
198                         if (!name || strcmp(stack->current->name, name) == 0) {
199                                 undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
200                                 undo_restore(C, stack, undo);
201                                 stack->current = undo;
202                                 if (G.debug & G_DEBUG_WM) {
203                                         printf("%s: redo %s\n", __func__, undo->name);
204                                 }
205                                 return 1;
206                         }
207                 }
208         }
209
210         return 0;
211 }
212
213 static void undo_stack_free(UndoStack *stack)
214 {
215         UndoElem *uel;
216         
217         for (uel = stack->elems.first; uel; uel = uel->next)
218                 undo_elem_free(stack, uel);
219
220         BLI_freelistN(&stack->elems);
221         stack->current = NULL;
222 }
223
224 /* Exported Functions */
225
226 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
227 {
228         if (type == UNDO_PAINT_IMAGE)
229                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
230         else if (type == UNDO_PAINT_MESH)
231                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
232 }
233
234 ListBase *undo_paint_push_get_list(int type)
235 {
236         if (type == UNDO_PAINT_IMAGE) {
237                 if (ImageUndoStack.current)
238                         return &ImageUndoStack.current->elems;
239         }
240         else if (type == UNDO_PAINT_MESH) {
241                 if (MeshUndoStack.current)
242                         return &MeshUndoStack.current->elems;
243         }
244         
245         return NULL;
246 }
247
248 void undo_paint_push_count_alloc(int type, int size)
249 {
250         if (type == UNDO_PAINT_IMAGE)
251                 ImageUndoStack.current->undosize += size;
252         else if (type == UNDO_PAINT_MESH)
253                 MeshUndoStack.current->undosize += size;
254 }
255
256 void undo_paint_push_end(int type)
257 {
258         if (type == UNDO_PAINT_IMAGE)
259                 undo_stack_push_end(&ImageUndoStack);
260         else if (type == UNDO_PAINT_MESH)
261                 undo_stack_push_end(&MeshUndoStack);
262 }
263
264 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
265 {
266         if (type == UNDO_PAINT_IMAGE)
267                 return undo_stack_step(C, &ImageUndoStack, step, name);
268         else if (type == UNDO_PAINT_MESH)
269                 return undo_stack_step(C, &MeshUndoStack, step, name);
270         
271         return 0;
272 }
273
274 int ED_undo_paint_valid(int type, const char *name)
275 {
276         UndoStack *stack;
277         
278         if (type == UNDO_PAINT_IMAGE)
279                 stack = &ImageUndoStack;
280         else if (type == UNDO_PAINT_MESH)
281                 stack = &MeshUndoStack;
282         else 
283                 return 0;
284         
285         if (stack->current == NULL) {
286                 /* pass */
287         }
288         else {
289                 if (name && strcmp(stack->current->name, name) == 0)
290                         return 1;
291                 else
292                         return stack->elems.first != stack->elems.last;
293         }
294         return 0;
295 }
296
297 void ED_undo_paint_free(void)
298 {
299         undo_stack_free(&ImageUndoStack);
300         undo_stack_free(&MeshUndoStack);
301 }