2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17 * All rights reserved.
20 /** \file \ingroup edarmature
23 #include "MEM_guardedalloc.h"
28 #include "DNA_armature_types.h"
29 #include "DNA_object_types.h"
31 #include "BLI_array_utils.h"
33 #include "BKE_context.h"
34 #include "BKE_layer.h"
35 #include "BKE_undo_system.h"
37 #include "DEG_depsgraph.h"
39 #include "ED_armature.h"
40 #include "ED_object.h"
47 /** We only need this locally. */
48 static CLG_LogRef LOG = {"ed.undo.armature"};
50 /* -------------------------------------------------------------------- */
51 /** \name Undo Conversion
54 typedef struct UndoArmature {
60 static void undoarm_to_editarm(UndoArmature *uarm, bArmature *arm)
64 ED_armature_ebone_listbase_free(arm->edbo);
65 ED_armature_ebone_listbase_copy(arm->edbo, &uarm->lb);
68 if (uarm->act_edbone) {
69 ebone = uarm->act_edbone;
70 arm->act_edbone = ebone->temp.ebone;
73 arm->act_edbone = NULL;
76 ED_armature_ebone_listbase_temp_clear(arm->edbo);
79 static void *undoarm_from_editarm(UndoArmature *uarm, bArmature *arm)
81 BLI_assert(BLI_array_is_zeroed(uarm, 1));
83 /* TODO: include size of ID-properties. */
86 ED_armature_ebone_listbase_copy(&uarm->lb, arm->edbo);
89 if (arm->act_edbone) {
90 EditBone *ebone = arm->act_edbone;
91 uarm->act_edbone = ebone->temp.ebone;
94 ED_armature_ebone_listbase_temp_clear(&uarm->lb);
96 for (EditBone *ebone = uarm->lb.first; ebone; ebone = ebone->next) {
97 uarm->undo_size += sizeof(EditBone);
103 static void undoarm_free_data(UndoArmature *uarm)
105 ED_armature_ebone_listbase_free(&uarm->lb);
108 static Object *editarm_object_from_context(bContext *C)
110 Object *obedit = CTX_data_edit_object(C);
111 if (obedit && obedit->type == OB_ARMATURE) {
112 bArmature *arm = obedit->data;
113 if (arm->edbo != NULL) {
122 /* -------------------------------------------------------------------- */
123 /** \name Implements ED Undo System
125 * \note This is similar for all edit-mode types.
128 typedef struct ArmatureUndoStep_Elem {
129 struct ArmatureUndoStep_Elem *next, *prev;
130 UndoRefID_Object obedit_ref;
132 } ArmatureUndoStep_Elem;
134 typedef struct ArmatureUndoStep {
136 ArmatureUndoStep_Elem *elems;
140 static bool armature_undosys_poll(bContext *C)
142 return editarm_object_from_context(C) != NULL;
145 static bool armature_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
147 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
149 /* Important not to use the 3D view when getting objects because all objects
150 * outside of this list will be moved out of edit-mode when reading back undo steps. */
151 ViewLayer *view_layer = CTX_data_view_layer(C);
152 uint objects_len = 0;
153 Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, NULL, &objects_len);
155 us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
156 us->elems_len = objects_len;
158 for (uint i = 0; i < objects_len; i++) {
159 Object *ob = objects[i];
160 ArmatureUndoStep_Elem *elem = &us->elems[i];
162 elem->obedit_ref.ptr = ob;
163 bArmature *arm = elem->obedit_ref.ptr->data;
164 undoarm_from_editarm(&elem->data, arm);
165 us->step.data_size += elem->data.undo_size;
171 static void armature_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
173 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
175 /* Load all our objects into edit-mode, clear everything else. */
176 ED_undo_object_editmode_restore_helper(C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
178 BLI_assert(armature_undosys_poll(C));
180 for (uint i = 0; i < us->elems_len; i++) {
181 ArmatureUndoStep_Elem *elem = &us->elems[i];
182 Object *obedit = elem->obedit_ref.ptr;
183 bArmature *arm = obedit->data;
184 if (arm->edbo == NULL) {
185 /* Should never fail, may not crash but can give odd behavior. */
186 CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid", us_p->name, obedit->id.name);
189 undoarm_to_editarm(&elem->data, arm);
190 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
193 /* The first element is always active */
194 ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
196 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
199 static void armature_undosys_step_free(UndoStep *us_p)
201 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
203 for (uint i = 0; i < us->elems_len; i++) {
204 ArmatureUndoStep_Elem *elem = &us->elems[i];
205 undoarm_free_data(&elem->data);
207 MEM_freeN(us->elems);
210 static void armature_undosys_foreach_ID_ref(
211 UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
213 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
215 for (uint i = 0; i < us->elems_len; i++) {
216 ArmatureUndoStep_Elem *elem = &us->elems[i];
217 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
221 /* Export for ED_undo_sys. */
222 void ED_armature_undosys_type(UndoType *ut)
224 ut->name = "Edit Armature";
225 ut->poll = armature_undosys_poll;
226 ut->step_encode = armature_undosys_step_encode;
227 ut->step_decode = armature_undosys_step_decode;
228 ut->step_free = armature_undosys_step_free;
230 ut->step_foreach_ID_ref = armature_undosys_foreach_ID_ref;
232 ut->use_context = true;
234 ut->step_size = sizeof(ArmatureUndoStep);