GSOC 2013 paint
[blender-staging.git] / source / blender / editors / sculpt_paint / sculpt_undo.c
index 0560801bc773164ffcb915a9013cda2b6e10c81c..614c1f3ef1d4c7b21938ef7725a52e073763d592 100644 (file)
@@ -62,7 +62,8 @@
 
 #include "GPU_buffers.h"
 
-#include "ED_sculpt.h"
+#include "ED_paint.h"
+
 #include "bmesh.h"
 #include "paint_intern.h"
 #include "sculpt_intern.h"
@@ -112,7 +113,7 @@ static int sculpt_undo_restore_coords(bContext *C, DerivedMesh *dm, SculptUndoNo
                        if (kb) {
                                ob->shapenr = BLI_findindex(&key->block, kb) + 1;
 
-                               sculpt_update_mesh_elements(scene, sd, ob, 0, false);
+                               BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, false);
                                WM_event_add_notifier(C, NC_OBJECT | ND_DATA, ob);
                        }
                        else {
@@ -195,9 +196,9 @@ static int sculpt_undo_restore_hidden(bContext *C, DerivedMesh *dm,
                
                for (i = 0; i < unode->totvert; i++) {
                        MVert *v = &mvert[unode->index[i]];
-                       int uval = BLI_BITMAP_GET(unode->vert_hidden, i);
+                       int uval = BLI_BITMAP_TEST(unode->vert_hidden, i);
 
-                       BLI_BITMAP_MODIFY(unode->vert_hidden, i,
+                       BLI_BITMAP_SET(unode->vert_hidden, i,
                                          v->flag & ME_HIDE);
                        if (uval)
                                v->flag |= ME_HIDE;
@@ -413,7 +414,7 @@ static void sculpt_undo_restore(bContext *C, ListBase *lb)
                }
        }
 
-       sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
+       BKE_sculpt_update_mesh_elements(scene, sd, ob, 0, need_mask);
 
        /* call _after_ sculpt_update_mesh_elements() which may update 'ob->derivedFinal' */
        dm = mesh_get_derived_final(scene, ob, 0);
@@ -462,29 +463,29 @@ static void sculpt_undo_restore(bContext *C, ListBase *lb)
        }
 
        if (update || rebuild) {
-               int tag_update = 0;
+               bool tag_update = false;
                /* we update all nodes still, should be more clever, but also
                 * needs to work correct when exiting/entering sculpt mode and
                 * the nodes get recreated, though in that case it could do all */
                BKE_pbvh_search_callback(ss->pbvh, NULL, NULL, update_cb, &rebuild);
                BKE_pbvh_update(ss->pbvh, PBVH_UpdateBB | PBVH_UpdateOriginalBB | PBVH_UpdateRedraw, NULL);
 
-               if (sculpt_multires_active(scene, ob)) {
+               if (BKE_sculpt_multires_active(scene, ob)) {
                        if (rebuild)
                                multires_mark_as_modified(ob, MULTIRES_HIDDEN_MODIFIED);
                        else
                                multires_mark_as_modified(ob, MULTIRES_COORDS_MODIFIED);
                }
 
-               tag_update = ((Mesh *)ob->data)->id.us > 1;
+               tag_update |= ((Mesh *)ob->data)->id.us > 1;
 
-               if (ss->modifiers_active) {
+               if (ss->kb || ss->modifiers_active) {
                        Mesh *mesh = ob->data;
                        BKE_mesh_calc_normals_tessface(mesh->mvert, mesh->totvert,
                                                       mesh->mface, mesh->totface, NULL);
 
-                       free_sculptsession_deformMats(ss);
-                       tag_update |= 1;
+                       BKE_free_sculptsession_deformMats(ss);
+                       tag_update |= true;
                }
 
                if (tag_update) {
@@ -526,9 +527,11 @@ static void sculpt_undo_free(ListBase *lb)
                }
                if (unode->mask)
                        MEM_freeN(unode->mask);
+
                if (unode->bm_entry) {
                        BM_log_entry_drop(unode->bm_entry);
                }
+
                if (unode->bm_enter_totvert)
                        CustomData_free(&unode->bm_enter_vdata, unode->bm_enter_totvert);
                if (unode->bm_enter_totedge)
@@ -540,6 +543,23 @@ static void sculpt_undo_free(ListBase *lb)
        }
 }
 
+static bool sculpt_undo_cleanup(bContext *C, ListBase *lb)
+{
+       Object *ob = CTX_data_active_object(C);
+       SculptUndoNode *unode;
+
+       unode = lb->first;
+
+       if (unode && strcmp(unode->idname, ob->id.name) != 0) {
+               if (unode->bm_entry)
+                       BM_log_cleanup_entry(unode->bm_entry);
+
+               return true;
+       }
+
+       return false;
+}
+
 SculptUndoNode *sculpt_undo_get_node(PBVHNode *node)
 {
        ListBase *lb = undo_paint_push_get_list(UNDO_PAINT_MESH);
@@ -681,7 +701,7 @@ static void sculpt_undo_store_hidden(Object *ob, SculptUndoNode *unode)
                BKE_pbvh_node_num_verts(pbvh, node, NULL, &allvert);
                BKE_pbvh_node_get_verts(pbvh, node, &vert_indices, &mvert);
                for (i = 0; i < allvert; i++) {
-                       BLI_BITMAP_MODIFY(unode->vert_hidden, i,
+                       BLI_BITMAP_SET(unode->vert_hidden, i,
                                          mvert[vert_indices[i]].flag & ME_HIDE);
                }
        }
@@ -859,7 +879,7 @@ SculptUndoNode *sculpt_undo_push_node(Object *ob, PBVHNode *node,
 void sculpt_undo_push_begin(const char *name)
 {
        ED_undo_paint_push_begin(UNDO_PAINT_MESH, name,
-                             sculpt_undo_restore, sculpt_undo_free);
+                                sculpt_undo_restore, sculpt_undo_free, sculpt_undo_cleanup);
 }
 
 void sculpt_undo_push_end(void)