Fix build error on Windows 32 bit.
[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_undo.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         UndoCleanupCb cleanup;
55 } UndoElem;
56
57 typedef struct UndoStack {
58         int type;
59         ListBase elems;
60         UndoElem *current;
61 } UndoStack;
62
63 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
64 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
65
66 /* Generic */
67
68 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
69 {
70         if (uel && uel->restore)
71                 uel->restore(C, &uel->elems);
72 }
73
74 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
75 {
76         if (uel && uel->free) {
77                 uel->free(&uel->elems);
78                 BLI_freelistN(&uel->elems);
79         }
80 }
81
82 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free, UndoCleanupCb cleanup)
83 {
84         UndoElem *uel;
85         int nr;
86         
87         /* Undo push is split up in begin and end, the reason is that as painting
88          * happens more tiles/nodes are added to the list, and at the very end we
89          * know how much memory the undo used to remove old undo elements */
90
91         /* remove all undos after (also when stack->current==NULL) */
92         while (stack->elems.last != stack->current) {
93                 uel = stack->elems.last;
94                 undo_elem_free(stack, uel);
95                 BLI_freelinkN(&stack->elems, uel);
96         }
97         
98         /* make new */
99         stack->current = uel = MEM_callocN(sizeof(UndoElem), "undo file");
100         uel->restore = restore;
101         uel->free = free;
102         uel->cleanup = cleanup;
103         BLI_addtail(&stack->elems, uel);
104
105         /* name can be a dynamic string */
106         BLI_strncpy(uel->name, name, sizeof(uel->name));
107         
108         /* limit amount to the maximum amount*/
109         nr = 0;
110         uel = stack->elems.last;
111         while (uel) {
112                 nr++;
113                 if (nr == U.undosteps) break;
114                 uel = uel->prev;
115         }
116         if (uel) {
117                 while (stack->elems.first != uel) {
118                         UndoElem *first = stack->elems.first;
119                         undo_elem_free(stack, first);
120                         BLI_freelinkN(&stack->elems, first);
121                 }
122         }
123 }
124
125 static void undo_stack_push_end(UndoStack *stack)
126 {
127         UndoElem *uel;
128         uintptr_t totmem, maxmem;
129         int totundo = 0;
130
131         /* first limit to undo steps */
132         uel = stack->elems.last;
133
134         while (uel) {
135                 totundo++;
136                 if (totundo > U.undosteps) break;
137                 uel = uel->prev;
138         }
139
140         if (uel) {
141                 UndoElem *first;
142
143                 /* in case the undo steps are zero, the current pointer will be invalid */
144                 if (uel == stack->current)
145                         stack->current = NULL;
146
147                 do {
148                         first = stack->elems.first;
149                         undo_elem_free(stack, first);
150                         BLI_freelinkN(&stack->elems, first);
151                 } while (first != uel);
152         }
153
154         if (U.undomemory != 0) {
155                 /* limit to maximum memory (afterwards, we can't know in advance) */
156                 totmem = 0;
157                 maxmem = ((uintptr_t)U.undomemory) * 1024 * 1024;
158
159                 uel = stack->elems.last;
160                 while (uel) {
161                         totmem += uel->undosize;
162                         if (totmem > maxmem) break;
163                         uel = uel->prev;
164                 }
165
166                 if (uel) {
167                         while (stack->elems.first != uel) {
168                                 UndoElem *first = stack->elems.first;
169                                 undo_elem_free(stack, first);
170                                 BLI_freelinkN(&stack->elems, first);
171                         }
172                 }
173         }
174 }
175
176 static void undo_stack_cleanup(UndoStack *stack, bContext *C)
177 {
178         UndoElem *uel = stack->elems.first;
179         bool stack_reset = false;
180
181         while (uel) {
182                 if (uel->cleanup && uel->cleanup(C, &uel->elems)) {
183                         UndoElem *uel_tmp = uel->next;
184                         if (stack->current == uel) {
185                                 stack->current = NULL;
186                                 stack_reset = true;
187                         }
188                         undo_elem_free(stack, uel);
189                         BLI_freelinkN(&stack->elems, uel);
190                         uel = uel_tmp;
191                 }
192                 else
193                         uel = uel->next;
194         }
195         if (stack_reset) {
196                 stack->current = stack->elems.last;
197         }
198
199 }
200
201 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
202 {
203         UndoElem *undo;
204
205         /* first cleanup any old undo steps that may belong to invalid data */
206         undo_stack_cleanup(stack, C);
207
208         if (step == 1) {
209                 if (stack->current == NULL) {
210                         /* pass */
211                 }
212                 else {
213                         if (!name || STREQ(stack->current->name, name)) {
214                                 if (G.debug & G_DEBUG_WM) {
215                                         printf("%s: undo '%s'\n", __func__, stack->current->name);
216                                 }
217                                 undo_restore(C, stack, stack->current);
218                                 stack->current = stack->current->prev;
219                                 return 1;
220                         }
221                 }
222         }
223         else if (step == -1) {
224                 if ((stack->current != NULL && stack->current->next == NULL) || BLI_listbase_is_empty(&stack->elems)) {
225                         /* pass */
226                 }
227                 else {
228                         if (!name || STREQ(stack->current->name, name)) {
229                                 undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
230                                 undo_restore(C, stack, undo);
231                                 stack->current = undo;
232                                 if (G.debug & G_DEBUG_WM) {
233                                         printf("%s: redo %s\n", __func__, undo->name);
234                                 }
235                                 return 1;
236                         }
237                 }
238         }
239
240         return 0;
241 }
242
243 static void undo_stack_free(UndoStack *stack)
244 {
245         UndoElem *uel;
246         
247         for (uel = stack->elems.first; uel; uel = uel->next)
248                 undo_elem_free(stack, uel);
249
250         BLI_freelistN(&stack->elems);
251         stack->current = NULL;
252 }
253
254 /* Exported Functions */
255
256 void ED_undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free, UndoCleanupCb cleanup)
257 {
258         if (type == UNDO_PAINT_IMAGE)
259                 undo_stack_push_begin(&ImageUndoStack, name, restore, free, cleanup);
260         else if (type == UNDO_PAINT_MESH)
261                 undo_stack_push_begin(&MeshUndoStack, name, restore, free, cleanup);
262 }
263
264 ListBase *undo_paint_push_get_list(int type)
265 {
266         if (type == UNDO_PAINT_IMAGE) {
267                 if (ImageUndoStack.current) {
268                         return &ImageUndoStack.current->elems;
269                 }
270         }
271         else if (type == UNDO_PAINT_MESH) {
272                 if (MeshUndoStack.current) {
273                         return &MeshUndoStack.current->elems;
274                 }
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, bool *r_active)
335 {
336         UndoElem *uel;
337
338         if (r_active) *r_active = false;
339
340         uel = BLI_findlink(&stack->elems, nr);
341         if (uel) {
342                 if (r_active && (uel == stack->current)) {
343                         *r_active = true;
344                 }
345                 return uel->name;
346         }
347
348         return NULL;
349 }
350
351 const char *ED_undo_paint_get_name(bContext *C, int type, int nr, bool *r_active)
352 {
353
354         if (type == UNDO_PAINT_IMAGE) {
355                 undo_stack_cleanup(&ImageUndoStack, C);
356                 return undo_stack_get_name(&ImageUndoStack, nr, r_active);
357         }
358         else if (type == UNDO_PAINT_MESH) {
359                 undo_stack_cleanup(&MeshUndoStack, C);
360                 return undo_stack_get_name(&MeshUndoStack, nr, r_active);
361         }
362         return NULL;
363 }
364
365 bool ED_undo_paint_empty(int type)
366 {
367         UndoStack *stack;
368
369         if (type == UNDO_PAINT_IMAGE)
370                 stack = &ImageUndoStack;
371         else if (type == UNDO_PAINT_MESH)
372                 stack = &MeshUndoStack;
373         else
374                 return true;
375
376         if (stack->current == NULL) {
377                 return true;
378         }
379
380         return false;
381 }
382
383 bool ED_undo_paint_is_valid(int type, const char *name)
384 {
385         UndoStack *stack;
386         
387         if (type == UNDO_PAINT_IMAGE)
388                 stack = &ImageUndoStack;
389         else if (type == UNDO_PAINT_MESH)
390                 stack = &MeshUndoStack;
391         else 
392                 return 0;
393         
394         if (stack->current == NULL) {
395                 /* pass */
396         }
397         else {
398                 if (name && STREQ(stack->current->name, name))
399                         return 1;
400                 else
401                         return stack->elems.first != stack->elems.last;
402         }
403         return 0;
404 }
405
406 void ED_undo_paint_free(void)
407 {
408         undo_stack_free(&ImageUndoStack);
409         undo_stack_free(&MeshUndoStack);
410 }