Blender V5.0
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.h"
16#include "BLI_listbase.h"
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
40static CLG_LogRef LOG = {"undo.mball"};
41
42/* -------------------------------------------------------------------- */
45
51
52/* free all MetaElems from ListBase */
53static void freeMetaElemlist(ListBase *lb)
54{
55 if (lb == nullptr) {
56 return;
57 }
58
59 while (MetaElem *ml = static_cast<MetaElem *>(BLI_pophead(lb))) {
60 MEM_freeN(ml);
61 }
62}
63
65{
67 mb->lastelem = nullptr;
68
69 /* copy 'undo' MetaElems to 'edit' MetaElems */
70 int index = 0;
71 for (MetaElem *ml_undo = static_cast<MetaElem *>(umb->editelems.first); ml_undo;
72 ml_undo = ml_undo->next, index += 1)
73 {
74 MetaElem *ml_edit = static_cast<MetaElem *>(MEM_dupallocN(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 ListBase */
87 umb->lastelem_index = -1;
88
89 /* copy contents of current ListBase to the undo ListBase */
90 int index = 0;
91 for (MetaElem *ml_edit = static_cast<MetaElem *>(mb->editelems->first); ml_edit;
92 ml_edit = ml_edit->next, index += 1)
93 {
94 MetaElem *ml_undo = static_cast<MetaElem *>(MEM_dupallocN(ml_edit));
95 BLI_addtail(&umb->editelems, ml_undo);
96 if (ml_edit == mb->lastelem) {
97 umb->lastelem_index = index;
98 }
99 umb->undo_size += sizeof(MetaElem);
100 }
101
102 return umb;
103}
104
105/* free undo ListBase of MetaElems */
107{
109}
110
112{
113 Scene *scene = CTX_data_scene(C);
114 ViewLayer *view_layer = CTX_data_view_layer(C);
115 BKE_view_layer_synced_ensure(scene, view_layer);
116 Object *obedit = BKE_view_layer_edit_object_get(view_layer);
117 if (obedit && obedit->type == OB_MBALL) {
118 MetaBall *mb = static_cast<MetaBall *>(obedit->data);
119 if (mb->editelems != nullptr) {
120 return obedit;
121 }
122 }
123 return nullptr;
124}
125
127
128/* -------------------------------------------------------------------- */
133
135 UndoRefID_Object obedit_ref;
137};
138
146
148{
149 return editmball_object_from_context(C) != nullptr;
150}
151
153{
154 MBallUndoStep *us = (MBallUndoStep *)us_p;
155
156 /* Important not to use the 3D view when getting objects because all objects
157 * outside of this list will be moved out of edit-mode when reading back undo steps. */
158 Scene *scene = CTX_data_scene(C);
159 ViewLayer *view_layer = CTX_data_view_layer(C);
161
162 us->scene_ref.ptr = scene;
163 us->elems = MEM_calloc_arrayN<MBallUndoStep_Elem>(objects.size(), __func__);
164 us->elems_len = objects.size();
165
166 for (uint i = 0; i < objects.size(); i++) {
167 Object *ob = objects[i];
168 MBallUndoStep_Elem *elem = &us->elems[i];
169
170 elem->obedit_ref.ptr = ob;
171 MetaBall *mb = static_cast<MetaBall *>(ob->data);
172 editmball_from_undomball(&elem->data, mb);
173 mb->needs_flush_to_id = 1;
174 us->step.data_size += elem->data.undo_size;
175 }
176
177 bmain->is_memfile_undo_flush_needed = true;
178
179 return true;
180}
181
183 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
184{
185 MBallUndoStep *us = (MBallUndoStep *)us_p;
186 Scene *scene = CTX_data_scene(C);
187 ViewLayer *view_layer = CTX_data_view_layer(C);
188
190 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
192 scene, view_layer, &us->elems[0].obedit_ref.ptr, us->elems_len, sizeof(*us->elems));
194
195 for (uint i = 0; i < us->elems_len; i++) {
196 MBallUndoStep_Elem *elem = &us->elems[i];
197 Object *obedit = elem->obedit_ref.ptr;
198 MetaBall *mb = static_cast<MetaBall *>(obedit->data);
199 if (mb->editelems == nullptr) {
200 /* Should never fail, may not crash but can give odd behavior. */
202 "name='%s', failed to enter edit-mode for object '%s', undo state invalid",
203 us_p->name,
204 obedit->id.name);
205 continue;
206 }
207 undomball_to_editmball(&elem->data, mb);
208 mb->needs_flush_to_id = 1;
210 }
211
212 /* The first element is always active */
214 scene, view_layer, us->elems[0].obedit_ref.ptr, us_p->name, &LOG);
215
216 /* Check after setting active (unless undoing into another scene). */
218
219 bmain->is_memfile_undo_flush_needed = true;
220
222}
223
225{
226 MBallUndoStep *us = (MBallUndoStep *)us_p;
227
228 for (uint i = 0; i < us->elems_len; i++) {
229 MBallUndoStep_Elem *elem = &us->elems[i];
231 }
232 MEM_freeN(us->elems);
233}
234
236 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
237 void *user_data)
238{
239 MBallUndoStep *us = (MBallUndoStep *)us_p;
240
241 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->scene_ref));
242 for (uint i = 0; i < us->elems_len; i++) {
243 MBallUndoStep_Elem *elem = &us->elems[i];
244 foreach_ID_ref_fn(user_data, ((UndoRefID *)&elem->obedit_ref));
245 }
246}
247
262
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:46
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:111
void * BLI_pophead(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:252
unsigned int uint
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
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:775
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:811
blender::Vector< Object * > ED_undo_editmode_objects_from_view_layer(const Scene *scene, ViewLayer *view_layer)
Definition ed_undo.cc:856
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:794
Read Guarded memory(de)allocation.
#define C
Definition RandGen.cpp:29
#define NC_GEOM
Definition WM_types.hh:393
#define ND_DATA
Definition WM_types.hh:509
int64_t size() const
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)
void ED_mball_undosys_type(UndoType *ut)
static void mball_undosys_step_free(UndoStep *us_p)
#define LOG(level)
Definition log.h:97
void * MEM_calloc_arrayN(size_t len, size_t size, const char *str)
Definition mallocn.cc:123
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
char name[258]
Definition DNA_ID.h:432
void * first
UndoRefID_Object obedit_ref
MBallUndoStep_Elem * elems
UndoRefID_Scene scene_ref
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:213
MetaElem * lastelem
ListBase * editelems
char needs_flush_to_id
struct MetaElem * next
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)
i
Definition text_draw.cc:230
void WM_event_add_notifier(const bContext *C, uint type, void *reference)