From dac81ad71b88d1ffa2ea2a4a22ede06dc4b56adb Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 5 Jul 2021 23:37:11 +1000 Subject: EditMesh: extract restore logic out of EDBM_redo_state_free Split mesh restore logic into a new function: `EDBM_redo_state_restore_and_free`. --- source/blender/editors/include/ED_mesh.h | 12 ++++++--- source/blender/editors/mesh/editmesh_bevel.c | 6 ++--- source/blender/editors/mesh/editmesh_bisect.c | 8 +++--- source/blender/editors/mesh/editmesh_inset.c | 6 ++--- source/blender/editors/mesh/editmesh_tools.c | 4 +-- source/blender/editors/mesh/editmesh_utils.c | 38 ++++++++++++++------------- 6 files changed, 40 insertions(+), 34 deletions(-) diff --git a/source/blender/editors/include/ED_mesh.h b/source/blender/editors/include/ED_mesh.h index 7fcae2349db..667540b8f63 100644 --- a/source/blender/editors/include/ED_mesh.h +++ b/source/blender/editors/include/ED_mesh.h @@ -27,6 +27,8 @@ extern "C" { #endif +#include "BLI_compiler_attrs.h" + struct ARegion; struct BMBVHTree; struct BMEdge; @@ -455,12 +457,14 @@ typedef struct BMBackup { struct BMesh *bmcopy; } BMBackup; -/* save a copy of the bmesh for restoring later */ struct BMBackup EDBM_redo_state_store(struct BMEditMesh *em); /* restore a bmesh from backup */ -void EDBM_redo_state_restore(struct BMBackup, struct BMEditMesh *em, int recalctess); -/* delete the backup, optionally flushing it to an editmesh */ -void EDBM_redo_state_free(struct BMBackup *, struct BMEditMesh *em, int recalctess); +void EDBM_redo_state_restore(struct BMBackup *backup, struct BMEditMesh *em, bool recalc_looptri) + ATTR_NONNULL(1, 2); +void EDBM_redo_state_restore_and_free(struct BMBackup *backup, + struct BMEditMesh *em, + bool recalc_looptri) ATTR_NONNULL(1, 2); +void EDBM_redo_state_free(struct BMBackup *backup) ATTR_NONNULL(1); /* *** meshtools.c *** */ int ED_mesh_join_objects_exec(struct bContext *C, struct wmOperator *op); diff --git a/source/blender/editors/mesh/editmesh_bevel.c b/source/blender/editors/mesh/editmesh_bevel.c index 0388e133534..01736f2919a 100644 --- a/source/blender/editors/mesh/editmesh_bevel.c +++ b/source/blender/editors/mesh/editmesh_bevel.c @@ -347,7 +347,7 @@ static bool edbm_bevel_calc(wmOperator *op) /* revert to original mesh */ if (opdata->is_modal) { - EDBM_redo_state_restore(opdata->ob_store[ob_index].mesh_backup, em, false); + EDBM_redo_state_restore(&opdata->ob_store[ob_index].mesh_backup, em, false); } const int material = CLAMPIS(material_init, -1, obedit->totcol - 1); @@ -436,7 +436,7 @@ static void edbm_bevel_exit(bContext *C, wmOperator *op) View3D *v3d = CTX_wm_view3d(C); ARegion *region = CTX_wm_region(C); for (uint ob_index = 0; ob_index < opdata->ob_store_len; ob_index++) { - EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup, NULL, false); + EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup); } ED_region_draw_cb_exit(region->type, opdata->draw_handle_pixel); if (v3d) { @@ -456,7 +456,7 @@ static void edbm_bevel_cancel(bContext *C, wmOperator *op) for (uint ob_index = 0; ob_index < opdata->ob_store_len; ob_index++) { Object *obedit = opdata->ob_store[ob_index].ob; BMEditMesh *em = BKE_editmesh_from_object(obedit); - EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup, em, true); + EDBM_redo_state_restore_and_free(&opdata->ob_store[ob_index].mesh_backup, em, true); EDBM_update(obedit->data, &(const struct EDBMUpdate_Params){ .calc_looptri = false, diff --git a/source/blender/editors/mesh/editmesh_bisect.c b/source/blender/editors/mesh/editmesh_bisect.c index 8afbc61272f..3c8afe8e7db 100644 --- a/source/blender/editors/mesh/editmesh_bisect.c +++ b/source/blender/editors/mesh/editmesh_bisect.c @@ -67,7 +67,7 @@ typedef struct { /* Aligned with objects array. */ struct { - BMBackup mesh; + BMBackup mesh_backup; bool is_valid; bool is_dirty; } * backup; @@ -160,7 +160,7 @@ static int mesh_bisect_invoke(bContext *C, wmOperator *op, const wmEvent *event) if (em->bm->totedgesel != 0) { opdata->backup[ob_index].is_valid = true; - opdata->backup[ob_index].mesh = EDBM_redo_state_store(em); + opdata->backup[ob_index].mesh_backup = EDBM_redo_state_store(em); } } @@ -184,7 +184,7 @@ static void edbm_bisect_exit(bContext *C, BisectData *opdata) for (int ob_index = 0; ob_index < opdata->backup_len; ob_index++) { if (opdata->backup[ob_index].is_valid) { - EDBM_redo_state_free(&opdata->backup[ob_index].mesh, NULL, false); + EDBM_redo_state_free(&opdata->backup[ob_index].mesh_backup); } } MEM_freeN(opdata->backup); @@ -301,7 +301,7 @@ static int mesh_bisect_exec(bContext *C, wmOperator *op) if (opdata != NULL) { if (opdata->backup[ob_index].is_dirty) { - EDBM_redo_state_restore(opdata->backup[ob_index].mesh, em, false); + EDBM_redo_state_restore(&opdata->backup[ob_index].mesh_backup, em, false); opdata->backup[ob_index].is_dirty = false; } } diff --git a/source/blender/editors/mesh/editmesh_inset.c b/source/blender/editors/mesh/editmesh_inset.c index 1c27ab00715..18f51ae9df2 100644 --- a/source/blender/editors/mesh/editmesh_inset.c +++ b/source/blender/editors/mesh/editmesh_inset.c @@ -209,7 +209,7 @@ static void edbm_inset_exit(bContext *C, wmOperator *op) View3D *v3d = CTX_wm_view3d(C); ARegion *region = CTX_wm_region(C); for (uint ob_index = 0; ob_index < opdata->ob_store_len; ob_index++) { - EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup, NULL, false); + EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup); } ED_region_draw_cb_exit(region->type, opdata->draw_handle_pixel); if (v3d) { @@ -235,7 +235,7 @@ static void edbm_inset_cancel(bContext *C, wmOperator *op) for (uint ob_index = 0; ob_index < opdata->ob_store_len; ob_index++) { Object *obedit = opdata->ob_store[ob_index].ob; BMEditMesh *em = BKE_editmesh_from_object(obedit); - EDBM_redo_state_free(&opdata->ob_store[ob_index].mesh_backup, em, true); + EDBM_redo_state_restore_and_free(&opdata->ob_store[ob_index].mesh_backup, em, true); EDBM_update(obedit->data, &(const struct EDBMUpdate_Params){ .calc_looptri = false, @@ -276,7 +276,7 @@ static bool edbm_inset_calc(wmOperator *op) BMEditMesh *em = BKE_editmesh_from_object(obedit); if (opdata->is_modal) { - EDBM_redo_state_restore(opdata->ob_store[ob_index].mesh_backup, em, false); + EDBM_redo_state_restore(&opdata->ob_store[ob_index].mesh_backup, em, false); } if (use_individual) { diff --git a/source/blender/editors/mesh/editmesh_tools.c b/source/blender/editors/mesh/editmesh_tools.c index ed368b4ee6e..03f94e9f753 100644 --- a/source/blender/editors/mesh/editmesh_tools.c +++ b/source/blender/editors/mesh/editmesh_tools.c @@ -1284,7 +1284,7 @@ static bool edbm_connect_vert_pair(BMEditMesh *em, struct Mesh *me, wmOperator * } else if (failure) { len = 0; - EDBM_redo_state_free(&em_backup, em, true); + EDBM_redo_state_restore_and_free(&em_backup, em, true); em_backup_free = false; } else { @@ -1302,7 +1302,7 @@ static bool edbm_connect_vert_pair(BMEditMesh *em, struct Mesh *me, wmOperator * } if (em_backup_free) { - EDBM_redo_state_free(&em_backup, NULL, false); + EDBM_redo_state_free(&em_backup); } } MEM_freeN(verts); diff --git a/source/blender/editors/mesh/editmesh_utils.c b/source/blender/editors/mesh/editmesh_utils.c index 12d85c88084..4f416c94f62 100644 --- a/source/blender/editors/mesh/editmesh_utils.c +++ b/source/blender/editors/mesh/editmesh_utils.c @@ -68,6 +68,9 @@ * just as the undo stack would. * So leaving this as an interface for further work */ +/** + * Save a copy of the #BMesh for restoring later. + */ BMBackup EDBM_redo_state_store(BMEditMesh *em) { BMBackup backup; @@ -75,42 +78,41 @@ BMBackup EDBM_redo_state_store(BMEditMesh *em) return backup; } -void EDBM_redo_state_restore(BMBackup backup, BMEditMesh *em, int recalctess) +void EDBM_redo_state_restore(BMBackup *backup, BMEditMesh *em, bool recalc_looptri) { BMesh *tmpbm; - if (!em || !backup.bmcopy) { - return; - } BM_mesh_data_free(em->bm); - tmpbm = BM_mesh_copy(backup.bmcopy); + tmpbm = BM_mesh_copy(backup->bmcopy); *em->bm = *tmpbm; MEM_freeN(tmpbm); tmpbm = NULL; - if (recalctess) { + if (recalc_looptri) { BKE_editmesh_looptri_calc(em); } } -void EDBM_redo_state_free(BMBackup *backup, BMEditMesh *em, int recalctess) +/** + * Delete the backup, flushing it to an edit-mesh. + */ +void EDBM_redo_state_restore_and_free(BMBackup *backup, BMEditMesh *em, bool recalc_looptri) { - if (em && backup->bmcopy) { - BM_mesh_data_free(em->bm); - *em->bm = *backup->bmcopy; - } - else if (backup->bmcopy) { - BM_mesh_data_free(backup->bmcopy); + BM_mesh_data_free(em->bm); + *em->bm = *backup->bmcopy; + MEM_freeN(backup->bmcopy); + backup->bmcopy = NULL; + if (recalc_looptri) { + BKE_editmesh_looptri_calc(em); } +} +void EDBM_redo_state_free(BMBackup *backup) +{ if (backup->bmcopy) { + BM_mesh_data_free(backup->bmcopy); MEM_freeN(backup->bmcopy); } - backup->bmcopy = NULL; - - if (recalctess && em) { - BKE_editmesh_looptri_calc(em); - } } /** \} */ -- cgit v1.2.3