4 * Undo system for painting and sculpting.
6 * ***** BEGIN GPL LICENSE BLOCK *****
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * ***** END GPL LICENSE BLOCK *****
28 #include "MEM_guardedalloc.h"
30 #include "DNA_userdef_types.h"
32 #include "BLI_listbase.h"
34 #include "BKE_context.h"
35 #include "BKE_global.h"
37 #include "ED_sculpt.h"
39 #include "paint_intern.h"
41 #define MAXUNDONAME 64
43 typedef struct UndoElem {
44 struct UndoElem *next, *prev;
45 char name[MAXUNDONAME];
50 UndoRestoreCb restore;
54 typedef struct UndoStack {
60 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
61 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
65 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
67 if(uel && uel->restore)
68 uel->restore(C, &uel->elems);
71 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
73 if(uel && uel->free) {
74 uel->free(&uel->elems);
75 BLI_freelistN(&uel->elems);
79 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
84 /* Undo push is split up in begin and end, the reason is that as painting
85 * happens more tiles/nodes are added to the list, and at the very end we
86 * know how much memory the undo used to remove old undo elements */
88 /* remove all undos after (also when stack->current==NULL) */
89 while(stack->elems.last != stack->current) {
90 uel= stack->elems.last;
91 undo_elem_free(stack, uel);
92 BLI_freelinkN(&stack->elems, uel);
96 stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
97 uel->restore= restore;
99 BLI_addtail(&stack->elems, uel);
101 /* name can be a dynamic string */
102 strncpy(uel->name, name, MAXUNDONAME-1);
104 /* limit amount to the maximum amount*/
106 uel= stack->elems.last;
109 if(nr==U.undosteps) break;
113 while(stack->elems.first!=uel) {
114 UndoElem *first= stack->elems.first;
115 undo_elem_free(stack, first);
116 BLI_freelinkN(&stack->elems, first);
121 static void undo_stack_push_end(UndoStack *stack)
124 uintptr_t totmem, maxmem;
126 if(U.undomemory != 0) {
127 /* limit to maximum memory (afterwards, we can't know in advance) */
129 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
131 uel= stack->elems.last;
133 totmem+= uel->undosize;
134 if(totmem>maxmem) break;
139 while(stack->elems.first!=uel) {
140 UndoElem *first= stack->elems.first;
141 undo_elem_free(stack, first);
142 BLI_freelinkN(&stack->elems, first);
148 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
153 if(stack->current==NULL);
155 if(!name || strcmp(stack->current->name, name) == 0) {
156 if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
157 undo_restore(C, stack, stack->current);
158 stack->current= stack->current->prev;
164 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
166 if(!name || strcmp(stack->current->name, name) == 0) {
167 undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
168 undo_restore(C, stack, undo);
169 stack->current= undo;
170 if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
179 static void undo_stack_free(UndoStack *stack)
183 for(uel=stack->elems.first; uel; uel=uel->next)
184 undo_elem_free(stack, uel);
186 BLI_freelistN(&stack->elems);
187 stack->current= NULL;
190 /* Exported Functions */
192 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
194 if(type == UNDO_PAINT_IMAGE)
195 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
196 else if(type == UNDO_PAINT_MESH)
197 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
200 ListBase *undo_paint_push_get_list(int type)
202 if(type == UNDO_PAINT_IMAGE) {
203 if(ImageUndoStack.current)
204 return &ImageUndoStack.current->elems;
206 else if(type == UNDO_PAINT_MESH) {
207 if(MeshUndoStack.current)
208 return &MeshUndoStack.current->elems;
214 void undo_paint_push_count_alloc(int type, int size)
216 if(type == UNDO_PAINT_IMAGE)
217 ImageUndoStack.current->undosize += size;
218 else if(type == UNDO_PAINT_MESH)
219 MeshUndoStack.current->undosize += size;
222 void undo_paint_push_end(int type)
224 if(type == UNDO_PAINT_IMAGE)
225 undo_stack_push_end(&ImageUndoStack);
226 else if(type == UNDO_PAINT_MESH)
227 undo_stack_push_end(&MeshUndoStack);
230 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
232 if(type == UNDO_PAINT_IMAGE)
233 return undo_stack_step(C, &ImageUndoStack, step, name);
234 else if(type == UNDO_PAINT_MESH)
235 return undo_stack_step(C, &MeshUndoStack, step, name);
240 void ED_undo_paint_free(void)
242 undo_stack_free(&ImageUndoStack);
243 undo_stack_free(&MeshUndoStack);