Blender V5.3
curves_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 "BLI_task.hh"
10
11#include "BKE_context.hh"
12#include "BKE_curves.hh"
13#include "BKE_main.hh"
14#include "BKE_object.hh"
15#include "BKE_undo_system.hh"
16
17#include "CLG_log.h"
18
19#include "DEG_depsgraph.hh"
20
21#include "ED_curves.hh"
22#include "ED_undo.hh"
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27namespace blender {
28
29static CLG_LogRef LOG = {"undo.curves"};
30
31namespace ed::curves {
32namespace undo {
33
34/* -------------------------------------------------------------------- */
39
40struct StepObject {
41 UndoRefID_Object obedit_ref = {};
43};
44
51
52static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
53{
54 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
55
56 Scene *scene = CTX_data_scene(C);
57 ViewLayer *view_layer = CTX_data_view_layer(C);
58 Vector<Object *> objects = ED_undo_editmode_objects_from_view_layer(*bmain, scene, view_layer);
59
60 us->scene_ref.ptr = scene;
61 new (&us->objects) Array<StepObject>(objects.size());
62
63 threading::parallel_for(us->objects.index_range(), 8, [&](const IndexRange range) {
64 for (const int i : range) {
65 Object *ob = objects[i];
66 const Curves &curves_id = *id_cast<Curves *>(ob->data);
67 StepObject &object = us->objects[i];
68
69 object.obedit_ref.ptr = ob;
70 object.geometry = curves_id.geometry.wrap();
71 }
72 });
73
74 bmain->is_memfile_undo_flush_needed = true;
75
76 return true;
77}
78
79static void step_decode(
80 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
81{
82 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
83 Scene *scene = CTX_data_scene(C);
84 ViewLayer *view_layer = CTX_data_view_layer(C);
85
87 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
89 view_layer,
90 &us->objects.first().obedit_ref.ptr,
91 us->objects.size(),
92 sizeof(decltype(us->objects)::value_type));
93
94 BLI_assert(BKE_object_is_in_editmode(us->objects.first().obedit_ref.ptr));
95
96 for (const StepObject &object : us->objects) {
97 Curves &curves_id = *id_cast<Curves *>(object.obedit_ref.ptr->data);
98
99 /* Overwrite the curves geometry. */
100 curves_id.geometry.wrap() = object.geometry;
101
103 }
104
106 *bmain, scene, view_layer, us->objects.first().obedit_ref.ptr, us_p->name, &LOG);
107
108 bmain->is_memfile_undo_flush_needed = true;
109
111}
112
113static void step_free(UndoStep *us_p)
114{
115 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
116 us->objects.~Array();
117}
118
119static void foreach_ID_ref(UndoStep *us_p,
120 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
121 void *user_data)
122{
123 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
124
125 foreach_ID_ref_fn(user_data, (reinterpret_cast<UndoRefID *>(&us->scene_ref)));
126 for (const StepObject &object : us->objects) {
127 foreach_ID_ref_fn(
128 user_data,
129 (reinterpret_cast<UndoRefID *>(const_cast<UndoRefID_Object *>(&object.obedit_ref))));
130 }
131}
132
134
135} // namespace undo
136
151
152} // namespace ed::curves
153} // namespace blender
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
#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
static void foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
static void step_free(UndoStep *us_p)
static void step_decode(bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir, bool)
bool editable_curves_in_edit_mode_poll(bContext *C)
void undosys_type_register(UndoType *ut)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ UNDOTYPE_FLAG_NEED_CONTEXT_FOR_ENCODE
@ UNDOTYPE_FLAG_ENCODE_PRE_MEMFILE_SUPPORTED
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
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 DEG_id_tag_update(ID *id, unsigned int flags)
wmWindowManager * CTX_wm_manager(const 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
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
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
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:302
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