Blender  V2.93
editlattice_undo.c
Go to the documentation of this file.
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 
24 #include <math.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "CLG_log.h"
31 
32 #include "BLI_array_utils.h"
33 #include "BLI_utildefines.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_main.h"
43 #include "BKE_object.h"
44 #include "BKE_undo_system.h"
45 
46 #include "DEG_depsgraph.h"
47 
48 #include "ED_lattice.h"
49 #include "ED_object.h"
50 #include "ED_undo.h"
51 #include "ED_util.h"
52 
53 #include "WM_api.h"
54 #include "WM_types.h"
55 
56 #include "lattice_intern.h"
57 
59 static CLG_LogRef LOG = {"ed.undo.lattice"};
60 
61 /* -------------------------------------------------------------------- */
65 /* TODO(Campbell): this could contain an entire 'Lattice' struct. */
66 typedef struct UndoLattice {
69  char typeu, typev, typew;
70  float fu, fv, fw;
71  float du, dv, dw;
72  size_t undo_size;
74 
75 static void undolatt_to_editlatt(UndoLattice *ult, EditLatt *editlatt)
76 {
77  const int len_src = ult->pntsu * ult->pntsv * ult->pntsw;
78  const int len_dst = editlatt->latt->pntsu * editlatt->latt->pntsv * editlatt->latt->pntsw;
79  if (len_src != len_dst) {
80  MEM_freeN(editlatt->latt->def);
81  editlatt->latt->def = MEM_dupallocN(ult->def);
82  }
83  else {
84  memcpy(editlatt->latt->def, ult->def, sizeof(BPoint) * len_src);
85  }
86 
87  editlatt->latt->pntsu = ult->pntsu;
88  editlatt->latt->pntsv = ult->pntsv;
89  editlatt->latt->pntsw = ult->pntsw;
90  editlatt->latt->actbp = ult->actbp;
91 
92  editlatt->latt->typeu = ult->typeu;
93  editlatt->latt->typev = ult->typev;
94  editlatt->latt->typew = ult->typew;
95 
96  editlatt->latt->fu = ult->fu;
97  editlatt->latt->fv = ult->fv;
98  editlatt->latt->fw = ult->fw;
99  editlatt->latt->du = ult->du;
100  editlatt->latt->dv = ult->dv;
101  editlatt->latt->dw = ult->dw;
102 }
103 
104 static void *undolatt_from_editlatt(UndoLattice *ult, EditLatt *editlatt)
105 {
107 
108  ult->def = MEM_dupallocN(editlatt->latt->def);
109  ult->pntsu = editlatt->latt->pntsu;
110  ult->pntsv = editlatt->latt->pntsv;
111  ult->pntsw = editlatt->latt->pntsw;
112  ult->actbp = editlatt->latt->actbp;
113 
114  ult->typeu = editlatt->latt->typeu;
115  ult->typev = editlatt->latt->typev;
116  ult->typew = editlatt->latt->typew;
117 
118  ult->fu = editlatt->latt->fu;
119  ult->fv = editlatt->latt->fv;
120  ult->fw = editlatt->latt->fw;
121  ult->du = editlatt->latt->du;
122  ult->dv = editlatt->latt->dv;
123  ult->dw = editlatt->latt->dw;
124 
125  ult->undo_size += sizeof(*ult->def) * ult->pntsu * ult->pntsv * ult->pntsw;
126 
127  return ult;
128 }
129 
131 {
132  if (ult->def) {
133  MEM_freeN(ult->def);
134  }
135 }
136 
137 #if 0
138 static int validate_undoLatt(void *data, void *edata)
139 {
140  UndoLattice *ult = (UndoLattice *)data;
141  EditLatt *editlatt = (EditLatt *)edata;
142 
143  return (ult->pntsu == editlatt->latt->pntsu && ult->pntsv == editlatt->latt->pntsv &&
144  ult->pntsw == editlatt->latt->pntsw);
145 }
146 #endif
147 
149 {
150  ViewLayer *view_layer = CTX_data_view_layer(C);
151  Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
152  if (obedit && obedit->type == OB_LATTICE) {
153  Lattice *lt = obedit->data;
154  if (lt->editlatt != NULL) {
155  return obedit;
156  }
157  }
158 
159  return NULL;
160 }
161 
164 /* -------------------------------------------------------------------- */
170 typedef struct LatticeUndoStep_Elem {
171  UndoRefID_Object obedit_ref;
174 
175 typedef struct LatticeUndoStep {
180 
182 {
184 }
185 
186 static bool lattice_undosys_step_encode(struct bContext *C, Main *bmain, UndoStep *us_p)
187 {
188  LatticeUndoStep *us = (LatticeUndoStep *)us_p;
189 
190  /* Important not to use the 3D view when getting objects because all objects
191  * outside of this list will be moved out of edit-mode when reading back undo steps. */
192  ViewLayer *view_layer = CTX_data_view_layer(C);
193  uint objects_len = 0;
194  Object **objects = ED_undo_editmode_objects_from_view_layer(view_layer, &objects_len);
195 
196  us->elems = MEM_callocN(sizeof(*us->elems) * objects_len, __func__);
197  us->elems_len = objects_len;
198 
199  for (uint i = 0; i < objects_len; i++) {
200  Object *ob = objects[i];
201  LatticeUndoStep_Elem *elem = &us->elems[i];
202 
203  elem->obedit_ref.ptr = ob;
204  Lattice *lt = ob->data;
205  undolatt_from_editlatt(&elem->data, lt->editlatt);
206  lt->editlatt->needs_flush_to_id = 1;
207  us->step.data_size += elem->data.undo_size;
208  }
209  MEM_freeN(objects);
210 
211  bmain->is_memfile_undo_flush_needed = true;
212 
213  return true;
214 }
215 
217  struct Main *bmain,
218  UndoStep *us_p,
219  const eUndoStepDir UNUSED(dir),
220  bool UNUSED(is_final))
221 {
222  LatticeUndoStep *us = (LatticeUndoStep *)us_p;
223 
225  C, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
226 
228 
229  for (uint i = 0; i < us->elems_len; i++) {
230  LatticeUndoStep_Elem *elem = &us->elems[i];
231  Object *obedit = elem->obedit_ref.ptr;
232  Lattice *lt = obedit->data;
233  if (lt->editlatt == NULL) {
234  /* Should never fail, may not crash but can give odd behavior. */
235  CLOG_ERROR(&LOG,
236  "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
237  us_p->name,
238  obedit->id.name);
239  continue;
240  }
241  undolatt_to_editlatt(&elem->data, lt->editlatt);
242  lt->editlatt->needs_flush_to_id = 1;
244  }
245 
246  /* The first element is always active */
248  CTX_data_scene(C), CTX_data_view_layer(C), us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
249 
250  /* Check after setting active. */
252 
253  bmain->is_memfile_undo_flush_needed = true;
254 
256 }
257 
259 {
260  LatticeUndoStep *us = (LatticeUndoStep *)us_p;
261 
262  for (uint i = 0; i < us->elems_len; i++) {
263  LatticeUndoStep_Elem *elem = &us->elems[i];
264  undolatt_free_data(&elem->data);
265  }
266  MEM_freeN(us->elems);
267 }
268 
270  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
271  void *user_data)
272 {
273  LatticeUndoStep *us = (LatticeUndoStep *)us_p;
274 
275  for (uint i = 0; i < us->elems_len; i++) {
276  LatticeUndoStep_Elem *elem = &us->elems[i];
277  foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
278  }
279 }
280 
281 /* Export for ED_undo_sys. */
283 {
284  ut->name = "Edit Lattice";
289 
291 
293 
294  ut->step_size = sizeof(LatticeUndoStep);
295 }
296 
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const struct Object *ob)
eUndoStepDir
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
void(* UndoTypeForEachIDRefFn)(void *user_data, struct UndoRefID *id_ref)
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned int uint
Definition: BLI_sys_types.h:83
#define UNUSED(x)
#define CLOG_ERROR(clg_ref,...)
Definition: CLG_log.h:204
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
Object is a sort of wrapper for general info.
@ OB_LATTICE
#define OBEDIT_FROM_VIEW_LAYER(view_layer)
void ED_undo_object_editmode_restore_helper(struct bContext *C, struct Object **object_array, uint object_array_len, uint object_array_stride)
Definition: ed_undo.c:897
void ED_undo_object_set_active_or_warn(struct Scene *scene, struct ViewLayer *view_layer, struct Object *ob, const char *info, struct CLG_LogRef *log)
Definition: ed_undo.c:877
struct Object ** ED_undo_editmode_objects_from_view_layer(struct ViewLayer *view_layer, uint *r_len)
Definition: ed_undo.c:968
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define NC_GEOM
Definition: WM_types.h:294
#define ND_DATA
Definition: WM_types.h:408
void * user_data
struct LatticeUndoStep_Elem LatticeUndoStep_Elem
static void undolatt_to_editlatt(UndoLattice *ult, EditLatt *editlatt)
static bool lattice_undosys_poll(bContext *C)
static bool lattice_undosys_step_encode(struct bContext *C, Main *bmain, UndoStep *us_p)
static void lattice_undosys_step_free(UndoStep *us_p)
struct UndoLattice UndoLattice
void ED_lattice_undosys_type(UndoType *ut)
static void * undolatt_from_editlatt(UndoLattice *ult, EditLatt *editlatt)
static void undolatt_free_data(UndoLattice *ult)
static void lattice_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
struct LatticeUndoStep LatticeUndoStep
static CLG_LogRef LOG
static Object * editlatt_object_from_context(bContext *C)
static void lattice_undosys_step_decode(struct bContext *C, struct Main *bmain, UndoStep *us_p, const eUndoStepDir UNUSED(dir), bool UNUSED(is_final))
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
char needs_flush_to_id
struct Lattice * latt
char name[66]
Definition: DNA_ID.h:283
UndoRefID_Object obedit_ref
LatticeUndoStep_Elem * elems
struct EditLatt * editlatt
struct BPoint * def
Definition: BKE_main.h:116
char is_memfile_undo_flush_needed
Definition: BKE_main.h:130
void * data
size_t data_size
char name[64]
size_t step_size
void(* step_decode)(struct bContext *C, struct Main *bmain, UndoStep *us, const eUndoStepDir dir, bool is_final)
bool(* step_encode)(struct bContext *C, struct Main *bmain, UndoStep *us)
void(* step_foreach_ID_ref)(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
const char * name
void(* step_free)(UndoStep *us)
bool(* poll)(struct bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)