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 *****
25 /** \file blender/editors/sculpt_paint/paint_undo.c
33 #include "MEM_guardedalloc.h"
35 #include "BLI_listbase.h"
36 #include "BLI_utildefines.h"
38 #include "DNA_userdef_types.h"
41 #include "BKE_context.h"
42 #include "BKE_global.h"
44 #include "ED_sculpt.h"
46 #include "paint_intern.h"
48 #define MAXUNDONAME 64
50 typedef struct UndoElem {
51 struct UndoElem *next, *prev;
52 char name[MAXUNDONAME];
57 UndoRestoreCb restore;
61 typedef struct UndoStack {
67 static UndoStack ImageUndoStack = {UNDO_PAINT_IMAGE, {NULL, NULL}, NULL};
68 static UndoStack MeshUndoStack = {UNDO_PAINT_MESH, {NULL, NULL}, NULL};
72 static void undo_restore(bContext *C, UndoStack *UNUSED(stack), UndoElem *uel)
74 if(uel && uel->restore)
75 uel->restore(C, &uel->elems);
78 static void undo_elem_free(UndoStack *UNUSED(stack), UndoElem *uel)
80 if(uel && uel->free) {
81 uel->free(&uel->elems);
82 BLI_freelistN(&uel->elems);
86 static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
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 */
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);
103 stack->current= uel= MEM_callocN(sizeof(UndoElem), "undo file");
104 uel->restore= restore;
106 BLI_addtail(&stack->elems, uel);
108 /* name can be a dynamic string */
109 strncpy(uel->name, name, MAXUNDONAME-1);
111 /* limit amount to the maximum amount*/
113 uel= stack->elems.last;
116 if(nr==U.undosteps) break;
120 while(stack->elems.first!=uel) {
121 UndoElem *first= stack->elems.first;
122 undo_elem_free(stack, first);
123 BLI_freelinkN(&stack->elems, first);
128 static void undo_stack_push_end(UndoStack *stack)
131 uintptr_t totmem, maxmem;
133 if(U.undomemory != 0) {
134 /* limit to maximum memory (afterwards, we can't know in advance) */
136 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
138 uel= stack->elems.last;
140 totmem+= uel->undosize;
141 if(totmem>maxmem) break;
146 while(stack->elems.first!=uel) {
147 UndoElem *first= stack->elems.first;
148 undo_elem_free(stack, first);
149 BLI_freelinkN(&stack->elems, first);
155 static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
160 if(stack->current==NULL);
162 if(!name || strcmp(stack->current->name, name) == 0) {
163 if(G.f & G_DEBUG) printf("undo %s\n", stack->current->name);
164 undo_restore(C, stack, stack->current);
165 stack->current= stack->current->prev;
171 if((stack->current!=NULL && stack->current->next==NULL) || stack->elems.first==NULL);
173 if(!name || strcmp(stack->current->name, name) == 0) {
174 undo= (stack->current && stack->current->next)? stack->current->next: stack->elems.first;
175 undo_restore(C, stack, undo);
176 stack->current= undo;
177 if(G.f & G_DEBUG) printf("redo %s\n", undo->name);
186 static void undo_stack_free(UndoStack *stack)
190 for(uel=stack->elems.first; uel; uel=uel->next)
191 undo_elem_free(stack, uel);
193 BLI_freelistN(&stack->elems);
194 stack->current= NULL;
197 /* Exported Functions */
199 void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
201 if(type == UNDO_PAINT_IMAGE)
202 undo_stack_push_begin(&ImageUndoStack, name, restore, free);
203 else if(type == UNDO_PAINT_MESH)
204 undo_stack_push_begin(&MeshUndoStack, name, restore, free);
207 ListBase *undo_paint_push_get_list(int type)
209 if(type == UNDO_PAINT_IMAGE) {
210 if(ImageUndoStack.current)
211 return &ImageUndoStack.current->elems;
213 else if(type == UNDO_PAINT_MESH) {
214 if(MeshUndoStack.current)
215 return &MeshUndoStack.current->elems;
221 void undo_paint_push_count_alloc(int type, int size)
223 if(type == UNDO_PAINT_IMAGE)
224 ImageUndoStack.current->undosize += size;
225 else if(type == UNDO_PAINT_MESH)
226 MeshUndoStack.current->undosize += size;
229 void undo_paint_push_end(int type)
231 if(type == UNDO_PAINT_IMAGE)
232 undo_stack_push_end(&ImageUndoStack);
233 else if(type == UNDO_PAINT_MESH)
234 undo_stack_push_end(&MeshUndoStack);
237 int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
239 if(type == UNDO_PAINT_IMAGE)
240 return undo_stack_step(C, &ImageUndoStack, step, name);
241 else if(type == UNDO_PAINT_MESH)
242 return undo_stack_step(C, &MeshUndoStack, step, name);
247 int ED_undo_paint_valid(int type, const char *name)
251 if(type == UNDO_PAINT_IMAGE)
252 stack= &ImageUndoStack;
253 else if(type == UNDO_PAINT_MESH)
254 stack= &MeshUndoStack;
258 if(stack->current==NULL);
260 if(name && strcmp(stack->current->name, name) == 0)
263 return stack->elems.first != stack->elems.last;
268 void ED_undo_paint_free(void)
270 undo_stack_free(&ImageUndoStack);
271 undo_stack_free(&MeshUndoStack);