* \brief Undo system for painting and sculpting.
*/
-
#include <stdlib.h>
#include <string.h>
#include "DNA_userdef_types.h"
-
+#include "BKE_blender.h"
#include "BKE_context.h"
#include "BKE_global.h"
-#include "ED_sculpt.h"
+#include "ED_paint.h"
#include "paint_intern.h"
-#define MAXUNDONAME 64
-
typedef struct UndoElem {
struct UndoElem *next, *prev;
- char name[MAXUNDONAME];
+ char name[BKE_UNDO_STR_MAX];
uintptr_t undosize;
ListBase elems;
UndoRestoreCb restore;
UndoFreeCb free;
+ UndoCleanupCb cleanup;
} UndoElem;
typedef struct UndoStack {
}
}
-static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free)
+static void undo_stack_push_begin(UndoStack *stack, const char *name, UndoRestoreCb restore, UndoFreeCb free, UndoCleanupCb cleanup)
{
UndoElem *uel;
int nr;
stack->current = uel = MEM_callocN(sizeof(UndoElem), "undo file");
uel->restore = restore;
uel->free = free;
+ uel->cleanup = cleanup;
BLI_addtail(&stack->elems, uel);
/* name can be a dynamic string */
{
UndoElem *uel;
uintptr_t totmem, maxmem;
+ int totundo = 0;
+
+ /* first limit to undo steps */
+ uel = stack->elems.last;
+
+ while (uel) {
+ totundo++;
+ if (totundo > U.undosteps) break;
+ uel = uel->prev;
+ }
+
+ if (uel) {
+ UndoElem *first;
+
+ /* in case the undo steps are zero, the current pointer will be invalid */
+ if (uel == stack->current)
+ stack->current = NULL;
+
+ do {
+ first = stack->elems.first;
+ undo_elem_free(stack, first);
+ BLI_freelinkN(&stack->elems, first);
+ } while (first != uel);
+ }
if (U.undomemory != 0) {
/* limit to maximum memory (afterwards, we can't know in advance) */
}
}
+static void undo_stack_cleanup(UndoStack *stack, bContext *C)
+{
+ UndoElem *uel = stack->elems.first;
+ bool stack_reset = false;
+
+ while (uel) {
+ if (uel->cleanup && uel->cleanup(C, &uel->elems)) {
+ UndoElem *uel_tmp = uel->next;
+ if (stack->current == uel) {
+ stack->current = NULL;
+ stack_reset = true;
+ }
+ undo_elem_free(stack, uel);
+ BLI_freelinkN(&stack->elems, uel);
+ uel = uel_tmp;
+ }
+ else
+ uel = uel->next;
+ }
+ if (stack_reset) {
+ stack->current = stack->elems.last;
+ }
+
+}
+
static int undo_stack_step(bContext *C, UndoStack *stack, int step, const char *name)
{
UndoElem *undo;
+ /* first cleanup any old undo steps that may belong to invalid data */
+ undo_stack_cleanup(stack, C);
+
if (step == 1) {
- if (stack->current == NULL) ;
+ if (stack->current == NULL) {
+ /* pass */
+ }
else {
if (!name || strcmp(stack->current->name, name) == 0) {
if (G.debug & G_DEBUG_WM) {
}
}
else if (step == -1) {
- if ((stack->current != NULL && stack->current->next == NULL) || stack->elems.first == NULL) ;
+ if ((stack->current != NULL && stack->current->next == NULL) || BLI_listbase_is_empty(&stack->elems)) {
+ /* pass */
+ }
else {
if (!name || strcmp(stack->current->name, name) == 0) {
undo = (stack->current && stack->current->next) ? stack->current->next : stack->elems.first;
/* Exported Functions */
-void undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free)
+void ED_undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free, UndoCleanupCb cleanup)
{
if (type == UNDO_PAINT_IMAGE)
- undo_stack_push_begin(&ImageUndoStack, name, restore, free);
+ undo_stack_push_begin(&ImageUndoStack, name, restore, free, cleanup);
else if (type == UNDO_PAINT_MESH)
- undo_stack_push_begin(&MeshUndoStack, name, restore, free);
+ undo_stack_push_begin(&MeshUndoStack, name, restore, free, cleanup);
}
ListBase *undo_paint_push_get_list(int type)
{
if (type == UNDO_PAINT_IMAGE) {
- if (ImageUndoStack.current)
+ if (ImageUndoStack.current) {
return &ImageUndoStack.current->elems;
+ }
}
else if (type == UNDO_PAINT_MESH) {
- if (MeshUndoStack.current)
+ if (MeshUndoStack.current) {
return &MeshUndoStack.current->elems;
+ }
}
return NULL;
MeshUndoStack.current->undosize += size;
}
-void undo_paint_push_end(int type)
+void ED_undo_paint_push_end(int type)
{
if (type == UNDO_PAINT_IMAGE)
undo_stack_push_end(&ImageUndoStack);
return 0;
}
+static void undo_step_num(bContext *C, UndoStack *stack, int step)
+{
+ UndoElem *uel;
+ int a = 0;
+ int curnum = BLI_findindex(&stack->elems, stack->current);
+
+ for (uel = stack->elems.first; uel; uel = uel->next, a++) {
+ if (a == step) break;
+ }
+
+ if (curnum > a) {
+ while (a++ != curnum)
+ undo_stack_step(C, stack, 1, NULL);
+ }
+ else if (curnum < a) {
+ while (a-- != curnum)
+ undo_stack_step(C, stack, -1, NULL);
+ }
+}
+
+void ED_undo_paint_step_num(bContext *C, int type, int step)
+{
+ if (type == UNDO_PAINT_IMAGE)
+ undo_step_num(C, &ImageUndoStack, step);
+ else if (type == UNDO_PAINT_MESH)
+ undo_step_num(C, &MeshUndoStack, step);
+}
+
+static char *undo_stack_get_name(UndoStack *stack, int nr, int *active)
+{
+ UndoElem *uel;
+
+ if (active) *active = 0;
+
+ uel = BLI_findlink(&stack->elems, nr);
+ if (uel) {
+ if (active && uel == stack->current)
+ *active = 1;
+ return uel->name;
+ }
+
+ return NULL;
+}
+
+const char *ED_undo_paint_get_name(bContext *C, int type, int nr, int *active)
+{
+
+ if (type == UNDO_PAINT_IMAGE) {
+ undo_stack_cleanup(&ImageUndoStack, C);
+ return undo_stack_get_name(&ImageUndoStack, nr, active);
+ }
+ else if (type == UNDO_PAINT_MESH) {
+ undo_stack_cleanup(&MeshUndoStack, C);
+ return undo_stack_get_name(&MeshUndoStack, nr, active);
+ }
+ return NULL;
+}
+
+bool ED_undo_paint_empty(int type)
+{
+ UndoStack *stack;
+
+ if (type == UNDO_PAINT_IMAGE)
+ stack = &ImageUndoStack;
+ else if (type == UNDO_PAINT_MESH)
+ stack = &MeshUndoStack;
+ else
+ return true;
+
+ if (stack->current == NULL) {
+ return true;
+ }
+
+ return false;
+}
+
int ED_undo_paint_valid(int type, const char *name)
{
UndoStack *stack;
else
return 0;
- if (stack->current == NULL) ;
+ if (stack->current == NULL) {
+ /* pass */
+ }
else {
if (name && strcmp(stack->current->name, name) == 0)
return 1;
undo_stack_free(&ImageUndoStack);
undo_stack_free(&MeshUndoStack);
}
-