Blender V5.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
8
9#include <cstring>
10
11#include "MEM_guardedalloc.h"
12
13#include "CLG_log.h"
14
15#include "BLI_array_utils_c.hh"
16#include "BLI_listbase.hh"
17
18#include "DNA_layer_types.h"
19#include "DNA_meta_types.h"
20#include "DNA_object_types.h"
21#include "DNA_scene_types.h"
22
23#include "BKE_context.hh"
24#include "BKE_layer.hh"
25#include "BKE_main.hh"
26#include "BKE_object.hh"
27#include "BKE_undo_system.hh"
28
29#include "DEG_depsgraph.hh"
30
31#include "ED_mball.hh"
32#include "ED_object.hh"
33#include "ED_undo.hh"
34#include "ED_util.hh"
35
36#include "WM_api.hh"
37#include "WM_types.hh"
38
39namespace blender {
40
42static CLG_LogRef LOG = {"undo.mball"};
43
44/* -------------------------------------------------------------------- */
47
48struct UndoMBall {
49 ListBaseT<MetaElem> editelems;
51 size_t undo_size;
52};
53
54/* free all MetaElems from ListBaseT */
55static void freeMetaElemlist(ListBaseT<MetaElem> *lb)
56{
57 if (lb == nullptr) {
58 return;
59 }
60
61 while (MetaElem *ml = static_cast<MetaElem *>(BLI_pophead(lb))) {
62 MEM_delete(ml);
63 }
64}
65
67{
69 mb->lastelem = nullptr;
70
71 /* copy 'undo' MetaElems to 'edit' MetaElems */
72 int index = 0;
73 for (MetaElem *ml_undo = umb->editelems.first(); ml_undo; ml_undo = ml_undo->next, index += 1) {
74 MetaElem *ml_edit = MEM_dupalloc(ml_undo);
75 BLI_addtail(mb->editelems, ml_edit);
76 if (index == umb->lastelem_index) {
77 mb->lastelem = ml_edit;
78 }
79 }
80}
81
83{
85
86 /* allocate memory for undo ListBaseT */
87 umb->lastelem_index = -1;
88
89 /* copy contents of current ListBaseT to the undo ListBaseT */
90 int index = 0;
91 for (MetaElem *ml_edit = mb->editelems->first(); ml_edit; ml_edit = ml_edit->next, index += 1) {
92 MetaElem *ml_undo = MEM_dupalloc(ml_edit);
93 BLI_addtail(&umb->editelems, ml_undo);
94 if (ml_edit == mb->lastelem) {
95 umb->lastelem_index = index;
96 }
97 umb->undo_size += sizeof(MetaElem);
98 }
99
100 return umb;
101}
102
103/* free undo ListBaseT of MetaElems */
105{
107}
108
110{
111 const Main *bmain = CTX_data_main(C);
112 Scene *scene = CTX_data_scene(C);
113 ViewLayer *view_layer = CTX_data_view_layer(C);
114 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
115 Object *obedit = BKE_view_layer_edit_object_get(view_layer);
116 if (obedit && obedit->type == OB_MBALL) {
117 MetaBall *mb = id_cast<MetaBall *>(obedit->data);
118 if (mb->editelems != nullptr) {
119 return obedit;
120 }
121 }
122 return nullptr;
123}
124
126
127/* -------------------------------------------------------------------- */
132
134 UndoRefID_Object obedit_ref;
136};
137
145
147{
148 return editmball_object_from_context(C) != nullptr;
149}
150
152{
153 MBallUndoStep *us = reinterpret_cast<MBallUndoStep *>(us_p);
154
155 /* Important not to use the 3D view when getting objects because all objects
156 * outside of this list will be moved out of edit-mode when reading back undo steps. */
157 Scene *scene = CTX_data_scene(C);
158 ViewLayer *view_layer = CTX_data_view_layer(C);
159 Vector<Object *> objects = ED_undo_editmode_objects_from_view_layer(*bmain, scene, view_layer);
160
161 us->scene_ref.ptr = scene;
162 us->elems = MEM_new_array_zeroed<MBallUndoStep_Elem>(objects.size(), __func__);
163 us->elems_len = objects.size();
164
165 for (uint i = 0; i < objects.size(); i++) {
166 Object *ob = objects[i];
167 MBallUndoStep_Elem *elem = &us->elems[i];
168
169 elem->obedit_ref.ptr = ob;
170 MetaBall *mb = id_cast<MetaBall *>(ob->data);
171 editmball_from_undomball(&elem->data, mb);
172 mb->needs_flush_to_id = 1;
173 us->step.data_size += elem->data.undo_size;
174 }
175
176 bmain->is_memfile_undo_flush_needed = true;
177
178 return true;
179}
180
182 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
183{
184 MBallUndoStep *us = reinterpret_cast<MBallUndoStep *>(us_p);
185 Scene *scene = CTX_data_scene(C);
186 ViewLayer *view_layer = CTX_data_view_layer(C);
187
189 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
191 scene, view_layer, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
193
194 for (uint i = 0; i < us->elems_len; i++) {
195 MBallUndoStep_Elem *elem = &us->elems[i];
196 Object *obedit = elem->obedit_ref.ptr;
197 MetaBall *mb = id_cast<MetaBall *>(obedit->data);
198 if (mb->editelems == nullptr) {
199 /* Should never fail, may not crash but can give odd behavior. */
201 "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
202 us_p->name,
203 obedit->id.name);
204 continue;
205 }
206 undomball_to_editmball(&elem->data, mb);
207 mb->needs_flush_to_id = 1;
209 }
210
211 /* The first element is always active */
213 *bmain, scene, view_layer, us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
214
215 /* Check after setting active (unless undoing into another scene). */
217
218 bmain->is_memfile_undo_flush_needed = true;
219
221}
222
224{
225 MBallUndoStep *us = reinterpret_cast<MBallUndoStep *>(us_p);
226
227 for (uint i = 0; i < us->elems_len; i++) {
228 MBallUndoStep_Elem *elem = &us->elems[i];
230 }
231 MEM_delete(us->elems);
232}
233
235 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
236 void *user_data)
237{
238 MBallUndoStep *us = reinterpret_cast<MBallUndoStep *>(us_p);
239
240 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->scene_ref)));
241 for (uint i = 0; i < us->elems_len; i++) {
242 MBallUndoStep_Elem *elem = &us->elems[i];
243 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&elem->obedit_ref)));
244 }
245}
246
261
263
264} // namespace blender
General operations, lookup, etc. for blender objects.
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
Object is a sort of wrapper for general info.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:406
#define ND_DATA
Definition WM_types.hh:552
int64_t size() const
BMesh Iterators.
static void undomball_free_data(UndoMBall *umb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
static void freeMetaElemlist(ListBaseT< MetaElem > *lb)
void ED_mball_undosys_type(UndoType *ut)
static bool mball_undosys_step_encode(bContext *C, Main *bmain, UndoStep *us_p)
void ED_undo_object_set_active_or_warn(const Main &bmain, Scene *scene, ViewLayer *view_layer, Object *ob, const char *info, CLG_LogRef *log)
Definition ed_undo.cc:1024
static void mball_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static void mball_undosys_step_free(UndoStep *us_p)
Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:1109
ViewLayer * CTX_data_view_layer(const bContext *C)
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:259
static Object * editmball_object_from_context(bContext *C)
static void * editmball_from_undomball(UndoMBall *umb, MetaBall *mb)
Object * BKE_view_layer_edit_object_get(const ViewLayer *view_layer)
void DEG_id_tag_update(ID *id, unsigned int flags)
wmWindowManager * CTX_wm_manager(const bContext *C)
static void mball_undosys_step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
static bool mball_undosys_poll(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool BKE_object_is_in_editmode(const Object *ob)
Definition object.cc:1898
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
static void undomball_to_editmball(UndoMBall *umb, MetaBall *mb)
static CLG_LogRef LOG
Definition logging.cpp:12
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:1064
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
Scene * CTX_data_scene(const bContext *C)
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:1047
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
char name[258]
Definition DNA_ID.h:433
MBallUndoStep_Elem * elems
UndoRefID_Scene scene_ref
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
ListBaseT< MetaElem > * editelems
ListBaseT< MetaElem > editelems
bool(*) poll(bContext *C)
void(*) step_free(UndoStep *us)
void(*) step_decode(bContext *C, Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
void(*) step_foreach_ID_ref(UndoStep *us, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
bool(*) step_encode(bContext *C, Main *bmain, UndoStep *us)
const char * identifier