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