Fix T61272: Undo fails to track multi-edit mode enter/exit
[blender.git] / source / blender / editors / lattice / editlattice_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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup edlattice
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26
27 #include "MEM_guardedalloc.h"
28
29 #include "CLG_log.h"
30
31 #include "BLI_utildefines.h"
32 #include "BLI_array_utils.h"
33
34 #include "DNA_curve_types.h"
35 #include "DNA_lattice_types.h"
36 #include "DNA_object_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BKE_context.h"
40 #include "BKE_layer.h"
41 #include "BKE_undo_system.h"
42
43 #include "DEG_depsgraph.h"
44
45 #include "ED_object.h"
46 #include "ED_lattice.h"
47 #include "ED_undo.h"
48 #include "ED_util.h"
49
50 #include "WM_types.h"
51 #include "WM_api.h"
52
53 #include "lattice_intern.h"
54
55 /** We only need this locally. */
56 static CLG_LogRef LOG = {"ed.undo.lattice"};
57
58 /* -------------------------------------------------------------------- */
59 /** \name Undo Conversion
60  * \{ */
61
62 typedef struct UndoLattice {
63         BPoint *def;
64         int pntsu, pntsv, pntsw, actbp;
65         size_t undo_size;
66 } UndoLattice;
67
68 static void undolatt_to_editlatt(UndoLattice *ult, EditLatt *editlatt)
69 {
70         int len = editlatt->latt->pntsu * editlatt->latt->pntsv * editlatt->latt->pntsw;
71
72         memcpy(editlatt->latt->def, ult->def, sizeof(BPoint) * len);
73         editlatt->latt->actbp = ult->actbp;
74 }
75
76 static void *undolatt_from_editlatt(UndoLattice *ult, EditLatt *editlatt)
77 {
78         BLI_assert(BLI_array_is_zeroed(ult, 1));
79
80         ult->def = MEM_dupallocN(editlatt->latt->def);
81         ult->pntsu = editlatt->latt->pntsu;
82         ult->pntsv = editlatt->latt->pntsv;
83         ult->pntsw = editlatt->latt->pntsw;
84         ult->actbp = editlatt->latt->actbp;
85
86         ult->undo_size += sizeof(*ult->def) * ult->pntsu * ult->pntsv * ult->pntsw;
87
88         return ult;
89 }
90
91 static void undolatt_free_data(UndoLattice *ult)
92 {
93         if (ult->def) {
94                 MEM_freeN(ult->def);
95         }
96 }
97
98 #if 0
99 static int validate_undoLatt(void *data, void *edata)
100 {
101         UndoLattice *ult = (UndoLattice *)data;
102         EditLatt *editlatt = (EditLatt *)edata;
103
104         return (ult->pntsu == editlatt->latt->pntsu &&
105                 ult->pntsv == editlatt->latt->pntsv &&
106                 ult->pntsw == editlatt->latt->pntsw);
107 }
108 #endif
109
110 static Object *editlatt_object_from_context(bContext *C)
111 {
112         Object *obedit = CTX_data_edit_object(C);
113         if (obedit && obedit->type == OB_LATTICE) {
114                 Lattice *lt = obedit->data;
115                 if (lt->editlatt != NULL) {
116                         return obedit;
117                 }
118         }
119
120         return NULL;
121 }
122
123 /** \} */
124
125 /* -------------------------------------------------------------------- */
126 /** \name Implements ED Undo System
127  *
128  * \note This is similar for all edit-mode types.
129  * \{ */
130
131 typedef struct LatticeUndoStep_Elem {
132         UndoRefID_Object obedit_ref;
133         UndoLattice data;
134 } LatticeUndoStep_Elem;
135
136 typedef struct LatticeUndoStep {
137         UndoStep step;
138         LatticeUndoStep_Elem *elems;
139         uint                  elems_len;
140 } LatticeUndoStep;
141
142 static bool lattice_undosys_poll(bContext *C)
143 {
144         return editlatt_object_from_context(C) != NULL;
145 }
146
147 static bool lattice_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
148 {
149         LatticeUndoStep *us = (LatticeUndoStep *)us_p;
150
151         /* Important not to use the 3D view when getting objects because all objects
152          * outside of this list will be moved out of edit-mode when reading back undo steps. */
153         ViewLayer *view_layer = CTX_data_view_layer(C);
154         uint objects_len = 0;
155         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, NULL, &objects_len);
156
157         us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
158         us->elems_len = objects_len;
159
160         for (uint i = 0; i < objects_len; i++) {
161                 Object *ob = objects[i];
162                 LatticeUndoStep_Elem *elem = &us->elems[i];
163
164                 elem->obedit_ref.ptr = ob;
165                 Lattice *lt = ob->data;
166                 undolatt_from_editlatt(&elem->data, lt->editlatt);
167                 us->step.data_size += elem->data.undo_size;
168         }
169         MEM_freeN(objects);
170         return true;
171 }
172
173 static void lattice_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
174 {
175         LatticeUndoStep *us = (LatticeUndoStep *)us_p;
176
177         /* Load all our objects  into edit-mode, clear everything else. */
178         ED_undo_object_editmode_restore_helper(C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
179
180         BLI_assert(lattice_undosys_poll(C));
181
182         for (uint i = 0; i < us->elems_len; i++) {
183                 LatticeUndoStep_Elem *elem = &us->elems[i];
184                 Object *obedit = elem->obedit_ref.ptr;
185                 Lattice *lt = obedit->data;
186                 if (lt->editlatt == NULL) {
187                         /* Should never fail, may not crash but can give odd behavior. */
188                         CLOG_ERROR(&LOG, "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
189                                    us_p->name, obedit->id.name);
190                         continue;
191                 }
192                 undolatt_to_editlatt(&elem->data, lt->editlatt);
193                 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
194         }
195
196         /* The first element is always active */
197         ED_undo_object_set_active_or_warn(CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
198
199         WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
200 }
201
202 static void lattice_undosys_step_free(UndoStep *us_p)
203 {
204         LatticeUndoStep *us = (LatticeUndoStep *)us_p;
205
206         for (uint i = 0; i < us->elems_len; i++) {
207                 LatticeUndoStep_Elem *elem = &us->elems[i];
208                 undolatt_free_data(&elem->data);
209         }
210         MEM_freeN(us->elems);
211 }
212
213 static void lattice_undosys_foreach_ID_ref(
214         UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
215 {
216         LatticeUndoStep *us = (LatticeUndoStep *)us_p;
217
218         for (uint i = 0; i < us->elems_len; i++) {
219                 LatticeUndoStep_Elem *elem = &us->elems[i];
220                 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
221         }
222 }
223
224 /* Export for ED_undo_sys. */
225 void ED_lattice_undosys_type(UndoType *ut)
226 {
227         ut->name = "Edit Lattice";
228         ut->poll = lattice_undosys_poll;
229         ut->step_encode = lattice_undosys_step_encode;
230         ut->step_decode = lattice_undosys_step_decode;
231         ut->step_free = lattice_undosys_step_free;
232
233         ut->step_foreach_ID_ref = lattice_undosys_foreach_ID_ref;
234
235         ut->use_context = true;
236
237         ut->step_size = sizeof(LatticeUndoStep);
238 }
239
240 /** \} */