Fix T39156 part 2: Add support for image paint operations in undo
authorAntony Riakiotakis <kalast@gmail.com>
Thu, 13 Mar 2014 20:35:26 +0000 (22:35 +0200)
committerAntony Riakiotakis <kalast@gmail.com>
Thu, 13 Mar 2014 20:36:16 +0000 (22:36 +0200)
history operator (Ctrl + Alt + Z).

This will only show paint operations now while in an image paint mode.
The caveat is that user can delete previous paint operations too (even
on images not on the canvas currently) so it needs some care. This is
consistent with regular undo behaviour though.

Sculpting also suffers from lack of Undo history support, this will be
added in a separate commit.

source/blender/editors/include/ED_sculpt.h
source/blender/editors/sculpt_paint/paint_image.c
source/blender/editors/sculpt_paint/paint_undo.c
source/blender/editors/util/undo.c

index ba69be5b4c0deebc7e9acf9ee6ac45331eabc546..6105db14d6a131dfc25f4e430367707c619e52c3 100644 (file)
@@ -67,8 +67,11 @@ typedef void (*UndoRestoreCb)(struct bContext *C, struct ListBase *lb);
 typedef void (*UndoFreeCb)(struct ListBase *lb);
 
 int ED_undo_paint_step(struct bContext *C, int type, int step, const char *name);
+void ED_undo_paint_step_num(struct bContext *C, int type, int num);
+const char *ED_undo_paint_get_name(int type, int nr, int *active);
 void ED_undo_paint_free(void);
 int ED_undo_paint_valid(int type, const char *name);
+bool ED_undo_paint_empty(int type);
 void ED_undo_paint_push_begin(int type, const char *name, UndoRestoreCb restore, UndoFreeCb free);
 void ED_undo_paint_push_end(int type);
 
index 1b2e708be0f598e7951a014643f8812ec2670af9..85010d1be52183ac278b1ca9a11fcbfcc12aa8b1 100644 (file)
@@ -686,7 +686,7 @@ void PAINT_OT_image_paint(wmOperatorType *ot)
        ot->cancel = paint_stroke_cancel;
 
        /* flags */
-       ot->flag = OPTYPE_UNDO | OPTYPE_BLOCKING;
+       ot->flag = OPTYPE_BLOCKING;
 
        RNA_def_enum(ot->srna, "mode", stroke_mode_items, BRUSH_STROKE_NORMAL,
                     "Paint Stroke Mode",
index fd8cc4df41ef439d6f2d740233d454e1e602cb7b..2b5c6d9421c2eb65ccc09cfe2f19925a670fe9bd 100644 (file)
@@ -271,6 +271,77 @@ int ED_undo_paint_step(bContext *C, int type, int step, const char *name)
        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)
+               return undo_step_num(C, &ImageUndoStack, step);
+       else if (type == UNDO_PAINT_MESH)
+               return 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(int type, int nr, int *active)
+{
+       if (type == UNDO_PAINT_IMAGE)
+               return undo_stack_get_name(&ImageUndoStack, nr, active);
+       else if (type == UNDO_PAINT_MESH)
+               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;
index af524fa18bc3a86a2d274118ff6257af62284ed0..60c761cc12d8083e7b620520f5e1e6fd4e13f560 100644 (file)
@@ -105,13 +105,13 @@ void ED_undo_push(bContext *C, const char *str)
        }
        else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
                if (U.undosteps == 0) return;
-               
+
                PE_undo_push(CTX_data_scene(C), str);
        }
        else {
                BKE_write_undo(C, str);
        }
-       
+
        if (wm->file_saved) {
                wm->file_saved = 0;
                /* notifier that data changed, for save-over warning or header */
@@ -433,13 +433,27 @@ void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_ev
 enum {
        UNDOSYSTEM_GLOBAL   = 1,
        UNDOSYSTEM_EDITMODE = 2,
-       UNDOSYSTEM_PARTICLE = 3
+       UNDOSYSTEM_PARTICLE = 3,
+       UNDOSYSTEM_IMAPAINT = 4
 };
 
 static int get_undo_system(bContext *C)
 {
+       Object *obact = CTX_data_active_object(C);
        Object *obedit = CTX_data_edit_object(C);
-       
+       ScrArea *sa = CTX_wm_area(C);
+
+       /* first check for editor undo */
+       if (sa && (sa->spacetype == SPACE_IMAGE)) {
+               SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
+
+               if ((obact && (obact->mode & OB_MODE_TEXTURE_PAINT)) || (sima->mode == SI_MODE_PAINT)) {
+                       if (!ED_undo_paint_empty(UNDO_PAINT_IMAGE))
+                               return UNDOSYSTEM_IMAPAINT;
+                       else
+                               return UNDOSYSTEM_GLOBAL;
+               }
+       }
        /* find out which undo system */
        if (obedit) {
                if (OB_TYPE_SUPPORT_EDITMODE(obedit->type)) {
@@ -449,8 +463,16 @@ static int get_undo_system(bContext *C)
        else {
                Object *obact = CTX_data_active_object(C);
                
-               if (obact && obact->mode & OB_MODE_PARTICLE_EDIT)
-                       return UNDOSYSTEM_PARTICLE;
+               if (obact) {
+                       if (obact->mode & OB_MODE_PARTICLE_EDIT)
+                               return UNDOSYSTEM_PARTICLE;
+                       else if (obact->mode & OB_MODE_TEXTURE_PAINT) {
+                               if (!ED_undo_paint_empty(UNDO_PAINT_IMAGE))
+                                       return UNDOSYSTEM_IMAPAINT;
+                               else
+                                       return UNDOSYSTEM_GLOBAL;
+                       }
+               }
                else if (U.uiflag & USER_GLOBALUNDO)
                        return UNDOSYSTEM_GLOBAL;
        }
@@ -473,6 +495,9 @@ static EnumPropertyItem *rna_undo_itemf(bContext *C, int undosys, int *totitem)
                else if (undosys == UNDOSYSTEM_EDITMODE) {
                        name = undo_editmode_get_name(C, i, &active);
                }
+               else if (undosys == UNDOSYSTEM_IMAPAINT) {
+                       name = ED_undo_paint_get_name(UNDO_PAINT_IMAGE, i, &active);
+               }
                else {
                        name = BKE_undo_get_name(i, &active);
                }
@@ -545,6 +570,9 @@ static int undo_history_exec(bContext *C, wmOperator *op)
                        undo_editmode_number(C, item + 1);
                        WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
                }
+               else if (undosys == UNDOSYSTEM_IMAPAINT) {
+                       ED_undo_paint_step_num(C, UNDO_PAINT_IMAGE, item );
+               }
                else {
                        ED_viewport_render_kill_jobs(C, true);
                        BKE_undo_number(C, item);