Add ED_paint.h, split out ED_sculpt.h
[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_paint.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 bool (*UndoCleanupCb)(struct bContext *C, ListBase *lb);
57
58 typedef struct UndoStack {
59         int type;
60         ListBase elems;
61         UndoElem *current;
62         UndoCleanupCb cleanup;
63 } UndoStack;
64
65 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL, NULL};
66 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL, sculpt_undo_cleanup};
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         int totundo = 0;
131
132         /* first limit to undo steps */
133         uel = stack->elems.last;
134
135         while (uel) {
136                 totundo++;
137                 if (totundo > U.undosteps) break;
138                 uel = uel->prev;
139         }
140
141         if (uel) {
142                 UndoElem *first;
143
144                 /* in case the undo steps are zero, the current pointer will be invalid */
145                 if (uel == stack->current)
146                         stack->current = NULL;
147
148                 do {
149                         first = stack->elems.first;
150                         undo_elem_free(stack, first);
151                         BLI_freelinkN(&stack->elems, first);
152                 } while (first != uel);
153         }
154
155         if (U.undomemory != 0) {
156                 /* limit to maximum memory (afterwards, we can't know in advance) */
157                 totmem = 0;
158                 maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
159
160                 uel = stack->elems.last;
161                 while (uel) {
162                         totmem += uel->undosize;
163                         if (totmem > maxmem) break;
164                         uel = uel->prev;
165                 }
166
167                 if (uel) {
168                         while (stack->elems.first != uel) {
169                                 UndoElem *first = stack->elems.first;
170                                 undo_elem_free(stack, first);
171                                 BLI_freelinkN(&stack->elems, first);
172                         }
173                 }
174         }
175 }
176
177 static void undo_stack_cleanup(UndoStack *stack, bContext *C)
178 {
179         UndoElem *uel = stack->elems.first;
180         bool stack_reset = false;
181
182         if (stack->cleanup) {
183                 while (uel) {
184                         if (stack->cleanup(C, &uel->elems)) {
185                                 UndoElem *uel_tmp = uel->next;
186                                 if (stack->current == uel) {
187                                         stack->current = NULL;
188                                         stack_reset = true;
189                                 }
190                                 undo_elem_free(stack, uel);
191                                 BLI_freelinkN(&stack->elems, uel);
192                                 uel = uel_tmp;
193                         }
194                         else
195                                 uel = uel->next;
196                 }
197                 if (stack_reset) {
198                         stack->current = stack->elems.last;
199                 }
200         }
201 }
202
203 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
204 {
205         UndoElem *undo;
206
207         /* first cleanup any old undo steps that may belong to invalid data */
208         undo_stack_cleanup(stack, C);
209
210         if (step == 1) {
211                 if (stack->current == NULL) {
212                         /* pass */
213                 }
214                 else {
215                         if (!name || strcmp(stack->current->name, name) == 0) {
216                                 if (G.debug & G_DEBUG_WM) {
217                                         printf("%s: undo '%s'\n", __func__, stack->current->name);
218                                 }
219                                 undo_restore(C, stack, stack->current);
220                                 stack->current = stack->current->prev;
221                                 return 1;
222                         }
223                 }
224         }
225         else if (step == -1) {
226                 if ((stack->current != NULL && stack->current->next == NULL) || BLI_listbase_is_empty(&stack->elems)) {
227                         /* pass */
228                 }
229                 else {
230                         if (!name || strcmp(stack->current->name, name) == 0) {
231                                 undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
232                                 undo_restore(C, stack, undo);
233                                 stack->current = undo;
234                                 if (G.debug & G_DEBUG_WM) {
235                                         printf("%s: redo %s\n", __func__, undo->name);
236                                 }
237                                 return 1;
238                         }
239                 }
240         }
241
242         return 0;
243 }
244
245 static void undo_stack_free(UndoStack *stack)
246 {
247         UndoElem *uel;
248         
249         for (uel = stack->elems.first; uel; uel = uel->next)
250                 undo_elem_free(stack, uel);
251
252         BLI_freelistN(&stack->elems);
253         stack->current = NULL;
254 }
255
256 /* Exported Functions */
257
258 void ED_undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
259 {
260         if (type == UNDO_PAINT_IMAGE)
261                 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
262         else if (type == UNDO_PAINT_MESH)
263                 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
264 }
265
266 ListBase *undo_paint_push_get_list(int type)
267 {
268         if (type == UNDO_PAINT_IMAGE) {
269                 if (ImageUndoStack.current)
270                         return &ImageUndoStack.current->elems;
271         }
272         else if (type == UNDO_PAINT_MESH) {
273                 if (MeshUndoStack.current)
274                         return &MeshUndoStack.current->elems;
275         }
276         
277         return NULL;
278 }
279
280 void undo_paint_push_count_alloc(int type, int size)
281 {
282         if (type == UNDO_PAINT_IMAGE)
283                 ImageUndoStack.current->undosize += size;
284         else if (type == UNDO_PAINT_MESH)
285                 MeshUndoStack.current->undosize += size;
286 }
287
288 void ED_undo_paint_push_end(int type)
289 {
290         if (type == UNDO_PAINT_IMAGE)
291                 undo_stack_push_end(&ImageUndoStack);
292         else if (type == UNDO_PAINT_MESH)
293                 undo_stack_push_end(&MeshUndoStack);
294 }
295
296 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
297 {
298         if (type == UNDO_PAINT_IMAGE)
299                 return undo_stack_step(C, &ImageUndoStack, step, name);
300         else if (type == UNDO_PAINT_MESH)
301                 return undo_stack_step(C, &MeshUndoStack, step, name);
302         
303         return 0;
304 }
305
306 static void undo_step_num(bContext *C, UndoStack *stack, int step)
307 {
308         UndoElem *uel;
309         int a = 0;
310         int curnum = BLI_findindex(&stack->elems, stack->current);
311
312         for (uel = stack->elems.first; uel; uel = uel->next, a++) {
313                 if (a == step) break;
314         }
315
316         if (curnum > a) {
317                 while (a++ != curnum)
318                         undo_stack_step(C, stack, 1, NULL);
319         }
320         else if (curnum < a) {
321                 while (a-- != curnum)
322                         undo_stack_step(C, stack, -1, NULL);
323         }
324 }
325
326 void ED_undo_paint_step_num(bContext *C, int type, int step)
327 {
328         if (type == UNDO_PAINT_IMAGE)
329                 undo_step_num(C, &ImageUndoStack, step);
330         else if (type == UNDO_PAINT_MESH)
331                 undo_step_num(C, &MeshUndoStack, step);
332 }
333
334 static char *undo_stack_get_name(UndoStack *stack, int nr, int *active)
335 {
336         UndoElem *uel;
337
338         if (active) *active = 0;
339
340         uel = BLI_findlink(&stack->elems, nr);
341         if (uel) {
342                 if (active && uel == stack->current)
343                         *active = 1;
344                 return uel->name;
345         }
346
347         return NULL;
348 }
349
350 const char *ED_undo_paint_get_name(bContext *C, int type, int nr, int *active)
351 {
352
353         if (type == UNDO_PAINT_IMAGE) {
354                 undo_stack_cleanup(&ImageUndoStack, C);
355                 return undo_stack_get_name(&ImageUndoStack, nr, active);
356         }
357         else if (type == UNDO_PAINT_MESH) {
358                 undo_stack_cleanup(&MeshUndoStack, C);
359                 return undo_stack_get_name(&MeshUndoStack, nr, active);
360         }
361         return NULL;
362 }
363
364 bool ED_undo_paint_empty(int type)
365 {
366         UndoStack *stack;
367
368         if (type == UNDO_PAINT_IMAGE)
369                 stack = &ImageUndoStack;
370         else if (type == UNDO_PAINT_MESH)
371                 stack = &MeshUndoStack;
372         else
373                 return true;
374
375         if (stack->current == NULL) {
376                 return true;
377         }
378
379         return false;
380 }
381
382 int ED_undo_paint_valid(int type, const char *name)
383 {
384         UndoStack *stack;
385         
386         if (type == UNDO_PAINT_IMAGE)
387                 stack = &ImageUndoStack;
388         else if (type == UNDO_PAINT_MESH)
389                 stack = &MeshUndoStack;
390         else 
391                 return 0;
392         
393         if (stack->current == NULL) {
394                 /* pass */
395         }
396         else {
397                 if (name && strcmp(stack->current->name, name) == 0)
398                         return 1;
399                 else
400                         return stack->elems.first != stack->elems.last;
401         }
402         return 0;
403 }
404
405 void ED_undo_paint_free(void)
406 {
407         undo_stack_free(&ImageUndoStack);
408         undo_stack_free(&MeshUndoStack);
409 }