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