Blender V4.3
editmball_undo.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include <cmath>
10#include <cstring>
11
12#include "MEM_guardedalloc.h"
13
14#include "CLG_log.h"
15
16#include "BLI_array_utils.h"
17#include "BLI_listbase.h"
18#include "BLI_utildefines.h"
19
20#include "DNA_defs.h"
21#include "DNA_layer_types.h"
22#include "DNA_meta_types.h"
23#include "DNA_object_types.h"
24#include "DNA_scene_types.h"
25
26#include "BKE_context.hh"
27#include "BKE_layer.hh"
28#include "BKE_main.hh"
29#include "BKE_object.hh"
30#include "BKE_undo_system.hh"
31
32#include "DEG_depsgraph.hh"
33
34#include "ED_mball.hh"
35#include "ED_object.hh"
36#include "ED_undo.hh"
37#include "ED_util.hh"
38
39#include "WM_api.hh"
40#include "WM_types.hh"
41
43static CLG_LogRef LOG = {"ed.undo.mball"};
44
45/* -------------------------------------------------------------------- */
54
55/* free all MetaElems from ListBase */
56static void freeMetaElemlist(ListBase *lb)
57{
58 if (lb == nullptr) {
59 return;
60 }
61
62 while (MetaElem *ml = static_cast<MetaElem *>(BLI_pophead(lb))) {
63 MEM_freeN(ml);
64 }
65}
66
68{
70 mb->lastelem = nullptr;
71
72 /* copy 'undo' MetaElems to 'edit' MetaElems */
73 int index = 0;
74 for (MetaElem *ml_undo = static_cast<MetaElem *>(umb->editelems.first); ml_undo;
75 ml_undo = ml_undo->next, index += 1)
76 {
77 MetaElem *ml_edit = static_cast<MetaElem *>(MEM_dupallocN(ml_undo));
78 BLI_addtail(mb->editelems, ml_edit);
79 if (index == umb->lastelem_index) {
80 mb->lastelem = ml_edit;
81 }
82 }
83}
84
86{
88
89 /* allocate memory for undo ListBase */
90 umb->lastelem_index = -1;
91
92 /* copy contents of current ListBase to the undo ListBase */
93 int index = 0;
94 for (MetaElem *ml_edit = static_cast<MetaElem *>(mb->editelems->first); ml_edit;
95 ml_edit = ml_edit->next, index += 1)
96 {
97 MetaElem *ml_undo = static_cast<MetaElem *>(MEM_dupallocN(ml_edit));
98 BLI_addtail(&umb->editelems, ml_undo);
99 if (ml_edit == mb->lastelem) {
100 umb->lastelem_index = index;
101 }
102 umb->undo_size += sizeof(MetaElem);
103 }
104
105 return umb;
106}
107
108/* free undo ListBase of MetaElems */
110{
112}
113
115{
116 Scene *scene = CTX_data_scene(C);
117 ViewLayer *view_layer = CTX_data_view_layer(C);
118 BKE_view_layer_synced_ensure(scene, view_layer);
119 Object *obedit = BKE_view_layer_edit_object_get(view_layer);
120 if (obedit && obedit->type == OB_MBALL) {
121 MetaBall *mb = static_cast<MetaBall *>(obedit->data);
122 if (mb->editelems != nullptr) {
123 return obedit;
124 }
125 }
126 return nullptr;
127}
128
131/* -------------------------------------------------------------------- */
138 UndoRefID_Object obedit_ref;
140};
141
149
151{
152 return editmball_object_from_context(C) != nullptr;
153}
154
155static bool mball_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
156{
157 MBallUndoStep *us = (MBallUndoStep *)us_p;
158
159 /* Important not to use the 3D view when getting objects because all objects
160 * outside of this list will be moved out of edit-mode when reading back undo steps. */
161 Scene *scene = CTX_data_scene(C);
162 ViewLayer *view_layer = CTX_data_view_layer(C);
164
165 us->scene_ref.ptr = scene;
166 us->elems = static_cast<MBallUndoStep_Elem *>(
167 MEM_callocN(sizeof(*us->elems) * objects.size(), __func__));
168 us->elems_len = objects.size();
169
170 for (uint i = 0; i < objects.size(); i++) {
171 Object *ob = objects[i];
172 MBallUndoStep_Elem *elem = &us->elems[i];
173
174 elem->obedit_ref.ptr = ob;
175 MetaBall *mb = static_cast<MetaBall *>(ob->data);
176 editmball_from_undomball(&elem->data, mb);
177 mb->needs_flush_to_id = 1;
178 us->step.data_size += elem->data.undo_size;
179 }
180
181 bmain->is_memfile_undo_flush_needed = true;
182
183 return true;
184}
185
187 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
188{
189 MBallUndoStep *us = (MBallUndoStep *)us_p;
190 Scene *scene = CTX_data_scene(C);
191 ViewLayer *view_layer = CTX_data_view_layer(C);
192
194 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
196 scene, view_layer, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
198
199 for (uint i = 0; i < us->elems_len; i++) {
200 MBallUndoStep_Elem *elem = &us->elems[i];
201 Object *obedit = elem->obedit_ref.ptr;
202 MetaBall *mb = static_cast<MetaBall *>(obedit->data);
203 if (mb->editelems == nullptr) {
204 /* Should never fail, may not crash but can give odd behavior. */
206 "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
207 us_p->name,
208 obedit->id.name);
209 continue;
210 }
211 undomball_to_editmball(&elem->data, mb);
212 mb->needs_flush_to_id = 1;
214 }
215
216 /* The first element is always active */
218 scene, view_layer, us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
219
220 /* Check after setting active (unless undoing into another scene). */
221 BLI_assert(mball_undosys_poll(C) || (scene != CTX_data_scene(C)));
222
223 bmain->is_memfile_undo_flush_needed = true;
224
226}
227
229{
230 MBallUndoStep *us = (MBallUndoStep *)us_p;
231
232 for (uint i = 0; i < us->elems_len; i++) {
233 MBallUndoStep_Elem *elem = &us->elems[i];
235 }
236 MEM_freeN(us->elems);
237}
238
240 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
241 void *user_data)
242{
243 MBallUndoStep *us = (MBallUndoStep *)us_p;
244
245 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->scene_ref));
246 for (uint i = 0; i < us->elems_len; i++) {
247 MBallUndoStep_Elem *elem = &us->elems[i];
248 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
249 }
250}
251
266
Scene * CTX_data_scene(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_edit_object_get(const ViewLayer *view_layer)
General operations, lookup, etc. for blender objects.
bool BKE_object_is_in_editmode(const Object *ob)
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
eUndoStepDir
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition BLI_assert.h:50
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:110
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:251
unsigned int uint
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:182
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
struct MetaElem MetaElem
Object is a sort of wrapper for general info.
@ OB_MBALL
void ED_undo_object_set_active_or_warn(Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:790
void ED_undo_object_editmode_restore_helper(Scene *scene, ViewLayer *view_layer, Object **object_array, uint object_array_len, uint object_array_stride)
Definition ed_undo.cc:826
blender::Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:871
void ED_undo_object_editmode_validate_scene_from_windows(wmWindowManager *wm, const Scene *scene_ref, Scene **scene_p, ViewLayer **view_layer_p)
Definition ed_undo.cc:809
Read Guarded memory(de)allocation.
#define NC_GEOM
Definition WM_types.hh:360
#define ND_DATA
Definition WM_types.hh:475
static void mball_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
static void undomball_free_data(UndoMBall *umb)
static Object * editmball_object_from_context(bContext *C)
static void freeMetaElemlist(ListBase *lb)
static void * editmball_from_undomball(UndoMBall *umb, MetaBall *mb)
static void mball_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static bool mball_undosys_poll(bContext *C)
static void undomball_to_editmball(UndoMBall *umb, MetaBall *mb)
static bool mball_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
static CLG_LogRef LOG
void ED_mball_undosys_type(UndoType *ut)
static void mball_undosys_step_free(UndoStep *us_p)
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
void *(* MEM_dupallocN)(const void *vmemh)
Definition mallocn.cc:39
char name[66]
Definition DNA_ID.h:425
void * first
UndoRefID_Object obedit_ref
MBallUndoStep_Elem * elems
UndoRefID_Scene scene_ref
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:165
MetaElem * lastelem
ListBase * editelems
char needs_flush_to_id
ListBase editelems
size_t undo_size
size_t data_size
char name[64]
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(* step_decode)(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
bool(* step_encode)(bContext *C, Main *bmain, UndoStep *us)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)