Blender V4.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
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 "MEM_guardedalloc.h"
25
26#include "WM_api.hh"
27#include "WM_types.hh"
28
29static CLG_LogRef LOG = {"ed.undo.curves"};
30
31namespace blender::ed::curves {
32namespace undo {
33
34/* -------------------------------------------------------------------- */
40struct StepObject {
41 UndoRefID_Object obedit_ref = {};
42 bke::CurvesGeometry geometry = {};
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);
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 = *static_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 = *static_cast<Curves *>(object.obedit_ref.ptr->data);
98
99 /* Overwrite the curves geometry. */
100 curves_id.geometry.wrap() = object.geometry;
101
103 }
104
106 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, ((UndoRefID *)&us->scene_ref));
126 for (const StepObject &object : us->objects) {
127 foreach_ID_ref_fn(user_data, ((UndoRefID *)&object.obedit_ref));
128 }
129}
130
133} // namespace undo
134
136{
137 ut->name = "Edit Curves";
139 ut->step_encode = undo::step_encode;
140 ut->step_decode = undo::step_decode;
141 ut->step_free = undo::step_free;
142
143 ut->step_foreach_ID_ref = undo::foreach_ID_ref;
144
146
147 ut->step_size = sizeof(undo::CurvesUndoStep);
148}
149
150} // namespace blender::ed::curves
Scene * CTX_data_scene(const bContext *C)
wmWindowManager * CTX_wm_manager(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
Low-level operations for curves.
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
#define BLI_assert(a)
Definition BLI_assert.h:50
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1041
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 CLG_LogRef LOG
#define LOG(severity)
Definition log.h:33
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:95
CurvesGeometry geometry
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:165
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)