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.
24 #include "MEM_guardedalloc.h"
28 #include "DNA_armature_types.h"
29 #include "DNA_object_types.h"
31 #include "BLI_array_utils.h"
32 #include "BLI_listbase.h"
34 #include "BKE_context.h"
35 #include "BKE_layer.h"
37 #include "BKE_undo_system.h"
39 #include "DEG_depsgraph.h"
41 #include "ED_armature.h"
42 #include "ED_object.h"
49 /** We only need this locally. */
50 static CLG_LogRef LOG = {"ed.undo.armature"};
52 /* -------------------------------------------------------------------- */
53 /** \name Undo Conversion
56 typedef struct UndoArmature {
62 static void undoarm_to_editarm(UndoArmature *uarm, bArmature *arm)
66 ED_armature_ebone_listbase_free(arm->edbo);
67 ED_armature_ebone_listbase_copy(arm->edbo, &uarm->lb);
70 if (uarm->act_edbone) {
71 ebone = uarm->act_edbone;
72 arm->act_edbone = ebone->temp.ebone;
75 arm->act_edbone = NULL;
78 ED_armature_ebone_listbase_temp_clear(arm->edbo);
81 static void *undoarm_from_editarm(UndoArmature *uarm, bArmature *arm)
83 BLI_assert(BLI_array_is_zeroed(uarm, 1));
85 /* TODO: include size of ID-properties. */
88 ED_armature_ebone_listbase_copy(&uarm->lb, arm->edbo);
91 if (arm->act_edbone) {
92 EditBone *ebone = arm->act_edbone;
93 uarm->act_edbone = ebone->temp.ebone;
96 ED_armature_ebone_listbase_temp_clear(&uarm->lb);
98 LISTBASE_FOREACH (EditBone *, ebone, &uarm->lb) {
99 uarm->undo_size += sizeof(EditBone);
105 static void undoarm_free_data(UndoArmature *uarm)
107 ED_armature_ebone_listbase_free(&uarm->lb);
110 static Object *editarm_object_from_context(bContext *C)
112 ViewLayer *view_layer = CTX_data_view_layer(C);
113 Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
114 if (obedit && obedit->type == OB_ARMATURE) {
115 bArmature *arm = obedit->data;
116 if (arm->edbo != NULL) {
125 /* -------------------------------------------------------------------- */
126 /** \name Implements ED Undo System
128 * \note This is similar for all edit-mode types.
131 typedef struct ArmatureUndoStep_Elem {
132 struct ArmatureUndoStep_Elem *next, *prev;
133 UndoRefID_Object obedit_ref;
135 } ArmatureUndoStep_Elem;
137 typedef struct ArmatureUndoStep {
139 ArmatureUndoStep_Elem *elems;
143 static bool armature_undosys_poll(bContext *C)
145 return editarm_object_from_context(C) != NULL;
148 static bool armature_undosys_step_encode(struct bContext *C, struct Main *bmain, UndoStep *us_p)
150 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
152 /* Important not to use the 3D view when getting objects because all objects
153 * outside of this list will be moved out of edit-mode when reading back undo steps. */
154 ViewLayer *view_layer = CTX_data_view_layer(C);
155 uint objects_len = 0;
156 Object **objects = ED_undo_editmode_objects_from_view_layer(view_layer, &objects_len);
158 us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
159 us->elems_len = objects_len;
161 for (uint i = 0; i < objects_len; i++) {
162 Object *ob = objects[i];
163 ArmatureUndoStep_Elem *elem = &us->elems[i];
165 elem->obedit_ref.ptr = ob;
166 bArmature *arm = elem->obedit_ref.ptr->data;
167 undoarm_from_editarm(&elem->data, arm);
168 arm->needs_flush_to_id = 1;
169 us->step.data_size += elem->data.undo_size;
173 bmain->is_memfile_undo_flush_needed = true;
178 static void armature_undosys_step_decode(
179 struct bContext *C, struct Main *bmain, UndoStep *us_p, int UNUSED(dir), bool UNUSED(is_final))
181 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
183 /* Load all our objects into edit-mode, clear everything else. */
184 ED_undo_object_editmode_restore_helper(
185 C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
187 BLI_assert(armature_undosys_poll(C));
189 for (uint i = 0; i < us->elems_len; i++) {
190 ArmatureUndoStep_Elem *elem = &us->elems[i];
191 Object *obedit = elem->obedit_ref.ptr;
192 bArmature *arm = obedit->data;
193 if (arm->edbo == NULL) {
194 /* Should never fail, may not crash but can give odd behavior. */
196 "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
201 undoarm_to_editarm(&elem->data, arm);
202 arm->needs_flush_to_id = 1;
203 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
206 /* The first element is always active */
207 ED_undo_object_set_active_or_warn(
208 CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
210 bmain->is_memfile_undo_flush_needed = true;
212 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
215 static void armature_undosys_step_free(UndoStep *us_p)
217 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
219 for (uint i = 0; i < us->elems_len; i++) {
220 ArmatureUndoStep_Elem *elem = &us->elems[i];
221 undoarm_free_data(&elem->data);
223 MEM_freeN(us->elems);
226 static void armature_undosys_foreach_ID_ref(UndoStep *us_p,
227 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
230 ArmatureUndoStep *us = (ArmatureUndoStep *)us_p;
232 for (uint i = 0; i < us->elems_len; i++) {
233 ArmatureUndoStep_Elem *elem = &us->elems[i];
234 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
238 /* Export for ED_undo_sys. */
239 void ED_armature_undosys_type(UndoType *ut)
241 ut->name = "Edit Armature";
242 ut->poll = armature_undosys_poll;
243 ut->step_encode = armature_undosys_step_encode;
244 ut->step_decode = armature_undosys_step_decode;
245 ut->step_free = armature_undosys_step_free;
247 ut->step_foreach_ID_ref = armature_undosys_foreach_ID_ref;
249 ut->use_context = true;
251 ut->step_size = sizeof(ArmatureUndoStep);