Fix T61272: Undo fails to track multi-edit mode enter/exit
[blender.git] / source / blender / editors / curve / editcurve_undo.c
1 /*
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.
6  *
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.
11  *
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.
15  */
16
17 /** \file \ingroup edcurve
18  */
19
20 #include "MEM_guardedalloc.h"
21
22 #include "CLG_log.h"
23
24 #include "DNA_object_types.h"
25 #include "DNA_scene_types.h"
26 #include "DNA_anim_types.h"
27
28 #include "BLI_blenlib.h"
29 #include "BLI_ghash.h"
30 #include "BLI_array_utils.h"
31
32 #include "BKE_animsys.h"
33 #include "BKE_context.h"
34 #include "BKE_curve.h"
35 #include "BKE_fcurve.h"
36 #include "BKE_layer.h"
37 #include "BKE_undo_system.h"
38
39 #include "DEG_depsgraph.h"
40
41 #include "ED_object.h"
42 #include "ED_undo.h"
43 #include "ED_util.h"
44 #include "ED_curve.h"
45
46 #include "WM_types.h"
47 #include "WM_api.h"
48
49 #include "curve_intern.h"
50
51 /** We only need this locally. */
52 static CLG_LogRef LOG = {"ed.undo.curve"};
53
54 /* -------------------------------------------------------------------- */
55 /** \name Undo Conversion
56  * \{ */
57
58 typedef struct {
59         ListBase nubase;
60         int actvert;
61         GHash *undoIndex;
62         ListBase fcurves, drivers;
63         int actnu;
64         int flag;
65
66         /* Stored in the object, needed since users may change the active key while in edit-mode. */
67         struct {
68                 short shapenr;
69         } obedit;
70
71         size_t undo_size;
72 } UndoCurve;
73
74 static void undocurve_to_editcurve(UndoCurve *ucu, Curve *cu, short *r_shapenr)
75 {
76         ListBase *undobase = &ucu->nubase;
77         ListBase *editbase = BKE_curve_editNurbs_get(cu);
78         Nurb *nu, *newnu;
79         EditNurb *editnurb = cu->editnurb;
80         AnimData *ad = BKE_animdata_from_id(&cu->id);
81
82         BKE_nurbList_free(editbase);
83
84         if (ucu->undoIndex) {
85                 BKE_curve_editNurb_keyIndex_free(&editnurb->keyindex);
86                 editnurb->keyindex = ED_curve_keyindex_hash_duplicate(ucu->undoIndex);
87         }
88
89         if (ad) {
90                 if (ad->action) {
91                         free_fcurves(&ad->action->curves);
92                         copy_fcurves(&ad->action->curves, &ucu->fcurves);
93                 }
94
95                 free_fcurves(&ad->drivers);
96                 copy_fcurves(&ad->drivers, &ucu->drivers);
97         }
98
99         /* copy  */
100         for (nu = undobase->first; nu; nu = nu->next) {
101                 newnu = BKE_nurb_duplicate(nu);
102
103                 if (editnurb->keyindex) {
104                         ED_curve_keyindex_update_nurb(editnurb, nu, newnu);
105                 }
106
107                 BLI_addtail(editbase, newnu);
108         }
109
110         cu->actvert = ucu->actvert;
111         cu->actnu = ucu->actnu;
112         cu->flag = ucu->flag;
113         *r_shapenr = ucu->obedit.shapenr;
114         ED_curve_updateAnimPaths(cu);
115 }
116
117 static void undocurve_from_editcurve(UndoCurve *ucu, Curve *cu, const short shapenr)
118 {
119         BLI_assert(BLI_array_is_zeroed(ucu, 1));
120         ListBase *nubase = BKE_curve_editNurbs_get(cu);
121         EditNurb *editnurb = cu->editnurb, tmpEditnurb;
122         Nurb *nu, *newnu;
123         AnimData *ad = BKE_animdata_from_id(&cu->id);
124
125         /* TODO: include size of fcurve & undoIndex */
126         // ucu->undo_size = 0;
127
128         if (editnurb->keyindex) {
129                 ucu->undoIndex = ED_curve_keyindex_hash_duplicate(editnurb->keyindex);
130                 tmpEditnurb.keyindex = ucu->undoIndex;
131         }
132
133         if (ad) {
134                 if (ad->action)
135                         copy_fcurves(&ucu->fcurves, &ad->action->curves);
136
137                 copy_fcurves(&ucu->drivers, &ad->drivers);
138         }
139
140         /* copy  */
141         for (nu = nubase->first; nu; nu = nu->next) {
142                 newnu = BKE_nurb_duplicate(nu);
143
144                 if (ucu->undoIndex) {
145                         ED_curve_keyindex_update_nurb(&tmpEditnurb, nu, newnu);
146                 }
147
148                 BLI_addtail(&ucu->nubase, newnu);
149
150                 ucu->undo_size += (
151                         (nu->bezt ? (sizeof(BezTriple) * nu->pntsu) : 0) +
152                         (nu->bp ? (sizeof(BPoint) * (nu->pntsu * nu->pntsv)) : 0) +
153                         (nu->knotsu ? (sizeof(float) * KNOTSU(nu)) : 0) +
154                         (nu->knotsv ? (sizeof(float) * KNOTSV(nu)) : 0) +
155                         sizeof(Nurb));
156         }
157
158         ucu->actvert = cu->actvert;
159         ucu->actnu = cu->actnu;
160         ucu->flag = cu->flag;
161
162         ucu->obedit.shapenr = shapenr;
163 }
164
165 static void undocurve_free_data(UndoCurve *uc)
166 {
167         BKE_nurbList_free(&uc->nubase);
168
169         BKE_curve_editNurb_keyIndex_free(&uc->undoIndex);
170
171         free_fcurves(&uc->fcurves);
172         free_fcurves(&uc->drivers);
173 }
174
175 static Object *editcurve_object_from_context(bContext *C)
176 {
177         Object *obedit = CTX_data_edit_object(C);
178         if (obedit && ELEM(obedit->type, OB_CURVE, OB_SURF)) {
179                 Curve *cu = obedit->data;
180                 if (BKE_curve_editNurbs_get(cu) != NULL) {
181                         return obedit;
182                 }
183         }
184         return NULL;
185 }
186
187 /** \} */
188
189 /* -------------------------------------------------------------------- */
190 /** \name Implements ED Undo System
191  *
192  * \note This is similar for all edit-mode types.
193  * \{ */
194
195 typedef struct CurveUndoStep_Elem {
196         UndoRefID_Object obedit_ref;
197         UndoCurve data;
198 } CurveUndoStep_Elem;
199
200 typedef struct CurveUndoStep {
201         UndoStep step;
202         CurveUndoStep_Elem *elems;
203         uint                elems_len;
204 } CurveUndoStep;
205
206 static bool curve_undosys_poll(bContext *C)
207 {
208         Object *obedit = editcurve_object_from_context(C);
209         return (obedit != NULL);
210 }
211
212 static bool curve_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
213 {
214         CurveUndoStep *us = (CurveUndoStep *)us_p;
215
216         /* Important not to use the 3D view when getting objects because all objects
217          * outside of this list will be moved out of edit-mode when reading back undo steps. */
218         ViewLayer *view_layer = CTX_data_view_layer(C);
219         uint objects_len = 0;
220         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, NULL, &objects_len);
221
222         us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
223         us->elems_len = objects_len;
224
225         for (uint i = 0; i < objects_len; i++) {
226                 Object *ob = objects[i];
227                 CurveUndoStep_Elem *elem = &us->elems[i];
228
229                 elem->obedit_ref.ptr = ob;
230                 undocurve_from_editcurve(&elem->data, ob->data, ob->shapenr);
231                 us->step.data_size += elem->data.undo_size;
232         }
233         MEM_freeN(objects);
234         return true;
235 }
236
237 static void curve_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
238 {
239         CurveUndoStep *us = (CurveUndoStep *)us_p;
240
241         /* Load all our objects  into edit-mode, clear everything else. */
242         ED_undo_object_editmode_restore_helper(
243                 C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
244
245         BLI_assert(curve_undosys_poll(C));
246
247         for (uint i = 0; i < us->elems_len; i++) {
248                 CurveUndoStep_Elem *elem = &us->elems[i];
249                 Object *obedit = elem->obedit_ref.ptr;
250                 Curve *cu = obedit->data;
251                 if (cu->editnurb == NULL) {
252                         /* Should never fail, may not crash but can give odd behavior. */
253                         CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
254                                    us_p->name, obedit->id.name);
255                         continue;
256                 }
257                 undocurve_to_editcurve(&elem->data, obedit->data, &obedit->shapenr);
258                 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
259         }
260
261         /* The first element is always active */
262         ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
263
264         WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
265 }
266
267 static void curve_undosys_step_free(UndoStep *us_p)
268 {
269         CurveUndoStep *us = (CurveUndoStep *)us_p;
270
271         for (uint i = 0; i < us->elems_len; i++) {
272                 CurveUndoStep_Elem *elem = &us->elems[i];
273                 undocurve_free_data(&elem->data);
274         }
275         MEM_freeN(us->elems);
276 }
277
278 static void curve_undosys_foreach_ID_ref(
279         UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
280 {
281         CurveUndoStep *us = (CurveUndoStep *)us_p;
282
283         for (uint i = 0; i < us->elems_len; i++) {
284                 CurveUndoStep_Elem *elem = &us->elems[i];
285                 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
286         }
287 }
288
289 /* Export for ED_undo_sys. */
290 void ED_curve_undosys_type(UndoType *ut)
291 {
292         ut->name = "Edit Curve";
293         ut->poll = curve_undosys_poll;
294         ut->step_encode = curve_undosys_step_encode;
295         ut->step_decode = curve_undosys_step_decode;
296         ut->step_free = curve_undosys_step_free;
297
298         ut->step_foreach_ID_ref = curve_undosys_foreach_ID_ref;
299
300         ut->use_context = true;
301
302         ut->step_size = sizeof(CurveUndoStep);
303 }
304
305 /** \} */