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.
20 /** \file blender/editors/lattice/editlattice_undo.c
28 #include "MEM_guardedalloc.h"
32 #include "BLI_utildefines.h"
33 #include "BLI_array_utils.h"
35 #include "DNA_curve_types.h"
36 #include "DNA_lattice_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_scene_types.h"
40 #include "BKE_context.h"
41 #include "BKE_layer.h"
42 #include "BKE_undo_system.h"
44 #include "DEG_depsgraph.h"
46 #include "ED_object.h"
47 #include "ED_lattice.h"
54 #include "lattice_intern.h"
56 /** We only need this locally. */
57 static CLG_LogRef LOG = {"ed.undo.lattice"};
59 /* -------------------------------------------------------------------- */
60 /** \name Undo Conversion
63 typedef struct UndoLattice {
65 int pntsu, pntsv, pntsw, actbp;
69 static void undolatt_to_editlatt(UndoLattice *ult, EditLatt *editlatt)
71 int len = editlatt->latt->pntsu * editlatt->latt->pntsv * editlatt->latt->pntsw;
73 memcpy(editlatt->latt->def, ult->def, sizeof(BPoint) * len);
74 editlatt->latt->actbp = ult->actbp;
77 static void *undolatt_from_editlatt(UndoLattice *ult, EditLatt *editlatt)
79 BLI_assert(BLI_array_is_zeroed(ult, 1));
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;
87 ult->undo_size += sizeof(*ult->def) * ult->pntsu * ult->pntsv * ult->pntsw;
92 static void undolatt_free_data(UndoLattice *ult)
100 static int validate_undoLatt(void *data, void *edata)
102 UndoLattice *ult = (UndoLattice *)data;
103 EditLatt *editlatt = (EditLatt *)edata;
105 return (ult->pntsu == editlatt->latt->pntsu &&
106 ult->pntsv == editlatt->latt->pntsv &&
107 ult->pntsw == editlatt->latt->pntsw);
111 static Object *editlatt_object_from_context(bContext *C)
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) {
126 /* -------------------------------------------------------------------- */
127 /** \name Implements ED Undo System
129 * \note This is similar for all edit-mode types.
132 typedef struct LatticeUndoStep_Elem {
133 UndoRefID_Object obedit_ref;
135 } LatticeUndoStep_Elem;
137 typedef struct LatticeUndoStep {
139 LatticeUndoStep_Elem *elems;
143 static bool lattice_undosys_poll(bContext *C)
145 return editlatt_object_from_context(C) != NULL;
148 static bool lattice_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
150 LatticeUndoStep *us = (LatticeUndoStep *)us_p;
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);
156 us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
157 us->elems_len = objects_len;
159 for (uint i = 0; i < objects_len; i++) {
160 Object *ob = objects[i];
161 LatticeUndoStep_Elem *elem = &us->elems[i];
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;
172 static void lattice_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
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));
178 LatticeUndoStep *us = (LatticeUndoStep *)us_p;
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);
190 undolatt_to_editlatt(&elem->data, lt->editlatt);
191 DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
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);
197 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
200 static void lattice_undosys_step_free(UndoStep *us_p)
202 LatticeUndoStep *us = (LatticeUndoStep *)us_p;
204 for (uint i = 0; i < us->elems_len; i++) {
205 LatticeUndoStep_Elem *elem = &us->elems[i];
206 undolatt_free_data(&elem->data);
208 MEM_freeN(us->elems);
211 static void lattice_undosys_foreach_ID_ref(
212 UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
214 LatticeUndoStep *us = (LatticeUndoStep *)us_p;
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));
222 /* Export for ED_undo_sys. */
223 void ED_lattice_undosys_type(UndoType *ut)
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;
231 ut->step_foreach_ID_ref = lattice_undosys_foreach_ID_ref;
233 ut->use_context = true;
235 ut->step_size = sizeof(LatticeUndoStep);