From 3ea4c6b9c9cb7bc6e9efa00bf86759b8a7fedc68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 16 Jul 2021 15:24:55 +0200 Subject: Pose backup: convert from C to C++ Convert `pose_backup.c` (in C) to `pose_backup.cc` (in C++). This will make future improvements easier. For now, it's the same code with just some additional explicit casts (C++ doesn't allow implicitly casting `void *`), `NULL` changed into `nullptr`, and some other simple changes. No functional changes. --- source/blender/editors/armature/CMakeLists.txt | 2 +- source/blender/editors/armature/pose_backup.c | 139 ------------------------ source/blender/editors/armature/pose_backup.cc | 140 +++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 140 deletions(-) delete mode 100644 source/blender/editors/armature/pose_backup.c create mode 100644 source/blender/editors/armature/pose_backup.cc (limited to 'source/blender') diff --git a/source/blender/editors/armature/CMakeLists.txt b/source/blender/editors/armature/CMakeLists.txt index e942bcf2902..aff5803f037 100644 --- a/source/blender/editors/armature/CMakeLists.txt +++ b/source/blender/editors/armature/CMakeLists.txt @@ -44,7 +44,7 @@ set(SRC armature_utils.c editarmature_undo.c meshlaplacian.c - pose_backup.c + pose_backup.cc pose_edit.c pose_group.c pose_lib.c diff --git a/source/blender/editors/armature/pose_backup.c b/source/blender/editors/armature/pose_backup.c deleted file mode 100644 index dffcd9bdc5a..00000000000 --- a/source/blender/editors/armature/pose_backup.c +++ /dev/null @@ -1,139 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -/** \file - * \ingroup edarmature - */ - -#include "ED_armature.h" - -#include - -#include "BLI_listbase.h" - -#include "MEM_guardedalloc.h" - -#include "DNA_action_types.h" -#include "DNA_armature_types.h" -#include "DNA_object_types.h" - -#include "BKE_action.h" -#include "BKE_armature.h" -#include "BKE_idprop.h" - -/* simple struct for storing backup info for one pose channel */ -typedef struct PoseChannelBackup { - struct PoseChannelBackup *next, *prev; - - struct bPoseChannel *pchan; /* Pose channel this backup is for. */ - - struct bPoseChannel olddata; /* Backup of pose channel. */ - struct IDProperty *oldprops; /* Backup copy (needs freeing) of pose channel's ID properties. */ -} PoseChannelBackup; - -typedef struct PoseBackup { - bool is_bone_selection_relevant; - ListBase /* PoseChannelBackup* */ backups; -} PoseBackup; - -static PoseBackup *pose_backup_create(const Object *ob, - const bAction *action, - const bool is_bone_selection_relevant) -{ - ListBase backups = {NULL, NULL}; - const bArmature *armature = ob->data; - - /* TODO(Sybren): reuse same approach as in `armature_pose.cc` in this function, as that doesn't - * have the assumption that action group names are bone names. */ - LISTBASE_FOREACH (bActionGroup *, agrp, &action->groups) { - bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, agrp->name); - if (pchan == NULL) { - continue; - } - - if (is_bone_selection_relevant && !PBONE_SELECTED(armature, pchan->bone)) { - continue; - } - - PoseChannelBackup *chan_bak = MEM_callocN(sizeof(*chan_bak), "PoseChannelBackup"); - chan_bak->pchan = pchan; - memcpy(&chan_bak->olddata, chan_bak->pchan, sizeof(chan_bak->olddata)); - - if (pchan->prop) { - chan_bak->oldprops = IDP_CopyProperty(pchan->prop); - } - - BLI_addtail(&backups, chan_bak); - } - - /* PoseBackup is constructed late, so that the above loop can use stack variables. */ - PoseBackup *pose_backup = MEM_callocN(sizeof(*pose_backup), __func__); - pose_backup->is_bone_selection_relevant = is_bone_selection_relevant; - pose_backup->backups = backups; - return pose_backup; -} - -PoseBackup *ED_pose_backup_create_all_bones(const Object *ob, const bAction *action) -{ - return pose_backup_create(ob, action, false); -} - -PoseBackup *ED_pose_backup_create_selected_bones(const Object *ob, const bAction *action) -{ - /* See if bone selection is relevant. */ - bool all_bones_selected = true; - bool no_bones_selected = true; - const bArmature *armature = ob->data; - LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) { - const bool is_selected = PBONE_SELECTED(armature, pchan->bone); - all_bones_selected &= is_selected; - no_bones_selected &= !is_selected; - } - - /* If no bones are selected, act as if all are. */ - const bool is_bone_selection_relevant = !all_bones_selected && !no_bones_selected; - return pose_backup_create(ob, action, is_bone_selection_relevant); -} - -bool ED_pose_backup_is_selection_relevant(const struct PoseBackup *pose_backup) -{ - return pose_backup->is_bone_selection_relevant; -} - -void ED_pose_backup_restore(const PoseBackup *pbd) -{ - LISTBASE_FOREACH (PoseChannelBackup *, chan_bak, &pbd->backups) { - memcpy(chan_bak->pchan, &chan_bak->olddata, sizeof(chan_bak->olddata)); - - if (chan_bak->oldprops) { - IDP_SyncGroupValues(chan_bak->pchan->prop, chan_bak->oldprops); - } - - /* TODO: constraints settings aren't restored yet, - * even though these could change (though not that likely) */ - } -} - -void ED_pose_backup_free(PoseBackup *pbd) -{ - LISTBASE_FOREACH_MUTABLE (PoseChannelBackup *, chan_bak, &pbd->backups) { - if (chan_bak->oldprops) { - IDP_FreeProperty(chan_bak->oldprops); - } - BLI_freelinkN(&pbd->backups, chan_bak); - } - MEM_freeN(pbd); -} diff --git a/source/blender/editors/armature/pose_backup.cc b/source/blender/editors/armature/pose_backup.cc new file mode 100644 index 00000000000..8d53bd8064b --- /dev/null +++ b/source/blender/editors/armature/pose_backup.cc @@ -0,0 +1,140 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/** \file + * \ingroup edarmature + */ + +#include "ED_armature.h" + +#include + +#include "BLI_listbase.h" + +#include "MEM_guardedalloc.h" + +#include "DNA_action_types.h" +#include "DNA_armature_types.h" +#include "DNA_object_types.h" + +#include "BKE_action.h" +#include "BKE_armature.h" +#include "BKE_idprop.h" + +/* simple struct for storing backup info for one pose channel */ +typedef struct PoseChannelBackup { + struct PoseChannelBackup *next, *prev; + + struct bPoseChannel *pchan; /* Pose channel this backup is for. */ + + struct bPoseChannel olddata; /* Backup of pose channel. */ + struct IDProperty *oldprops; /* Backup copy (needs freeing) of pose channel's ID properties. */ +} PoseChannelBackup; + +struct PoseBackup { + bool is_bone_selection_relevant; + ListBase /* PoseChannelBackup* */ backups; +}; + +static PoseBackup *pose_backup_create(const Object *ob, + const bAction *action, + const bool is_bone_selection_relevant) +{ + ListBase backups = {nullptr, nullptr}; + const bArmature *armature = static_cast(ob->data); + + /* TODO(Sybren): reuse same approach as in `armature_pose.cc` in this function, as that doesn't + * have the assumption that action group names are bone names. */ + LISTBASE_FOREACH (bActionGroup *, agrp, &action->groups) { + bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, agrp->name); + if (pchan == nullptr) { + continue; + } + + if (is_bone_selection_relevant && !PBONE_SELECTED(armature, pchan->bone)) { + continue; + } + + PoseChannelBackup *chan_bak = static_cast( + MEM_callocN(sizeof(*chan_bak), "PoseChannelBackup")); + chan_bak->pchan = pchan; + memcpy(&chan_bak->olddata, chan_bak->pchan, sizeof(chan_bak->olddata)); + + if (pchan->prop) { + chan_bak->oldprops = IDP_CopyProperty(pchan->prop); + } + + BLI_addtail(&backups, chan_bak); + } + + /* PoseBackup is constructed late, so that the above loop can use stack variables. */ + PoseBackup *pose_backup = static_cast(MEM_callocN(sizeof(*pose_backup), __func__)); + pose_backup->is_bone_selection_relevant = is_bone_selection_relevant; + pose_backup->backups = backups; + return pose_backup; +} + +PoseBackup *ED_pose_backup_create_all_bones(const Object *ob, const bAction *action) +{ + return pose_backup_create(ob, action, false); +} + +PoseBackup *ED_pose_backup_create_selected_bones(const Object *ob, const bAction *action) +{ + /* See if bone selection is relevant. */ + bool all_bones_selected = true; + bool no_bones_selected = true; + const bArmature *armature = static_cast(ob->data); + LISTBASE_FOREACH (bPoseChannel *, pchan, &ob->pose->chanbase) { + const bool is_selected = PBONE_SELECTED(armature, pchan->bone); + all_bones_selected &= is_selected; + no_bones_selected &= !is_selected; + } + + /* If no bones are selected, act as if all are. */ + const bool is_bone_selection_relevant = !all_bones_selected && !no_bones_selected; + return pose_backup_create(ob, action, is_bone_selection_relevant); +} + +bool ED_pose_backup_is_selection_relevant(const struct PoseBackup *pose_backup) +{ + return pose_backup->is_bone_selection_relevant; +} + +void ED_pose_backup_restore(const PoseBackup *pbd) +{ + LISTBASE_FOREACH (PoseChannelBackup *, chan_bak, &pbd->backups) { + memcpy(chan_bak->pchan, &chan_bak->olddata, sizeof(chan_bak->olddata)); + + if (chan_bak->oldprops) { + IDP_SyncGroupValues(chan_bak->pchan->prop, chan_bak->oldprops); + } + + /* TODO: constraints settings aren't restored yet, + * even though these could change (though not that likely) */ + } +} + +void ED_pose_backup_free(PoseBackup *pbd) +{ + LISTBASE_FOREACH_MUTABLE (PoseChannelBackup *, chan_bak, &pbd->backups) { + if (chan_bak->oldprops) { + IDP_FreeProperty(chan_bak->oldprops); + } + BLI_freelinkN(&pbd->backups, chan_bak); + } + MEM_freeN(pbd); +} -- cgit v1.2.3