Blender V5.0
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
27static CLG_LogRef LOG = {"undo.curves"};
28
29namespace blender::ed::curves {
30namespace undo {
31
32/* -------------------------------------------------------------------- */
37
38struct StepObject {
39 UndoRefID_Object obedit_ref = {};
41};
42
49
50static bool step_encode(bContext *C, Main *bmain, UndoStep *us_p)
51{
52 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
53
54 Scene *scene = CTX_data_scene(C);
55 ViewLayer *view_layer = CTX_data_view_layer(C);
57
58 us->scene_ref.ptr = scene;
59 new (&us->objects) Array<StepObject>(objects.size());
60
61 threading::parallel_for(us->objects.index_range(), 8, [&](const IndexRange range) {
62 for (const int i : range) {
63 Object *ob = objects[i];
64 const Curves &curves_id = *static_cast<Curves *>(ob->data);
65 StepObject &object = us->objects[i];
66
67 object.obedit_ref.ptr = ob;
68 object.geometry = curves_id.geometry.wrap();
69 }
70 });
71
72 bmain->is_memfile_undo_flush_needed = true;
73
74 return true;
75}
76
77static void step_decode(
78 bContext *C, Main *bmain, UndoStep *us_p, const eUndoStepDir /*dir*/, bool /*is_final*/)
79{
80 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
81 Scene *scene = CTX_data_scene(C);
82 ViewLayer *view_layer = CTX_data_view_layer(C);
83
85 CTX_wm_manager(C), us->scene_ref.ptr, &scene, &view_layer);
87 view_layer,
88 &us->objects.first().obedit_ref.ptr,
89 us->objects.size(),
90 sizeof(decltype(us->objects)::value_type));
91
92 BLI_assert(BKE_object_is_in_editmode(us->objects.first().obedit_ref.ptr));
93
94 for (const StepObject &object : us->objects) {
95 Curves &curves_id = *static_cast<Curves *>(object.obedit_ref.ptr->data);
96
97 /* Overwrite the curves geometry. */
98 curves_id.geometry.wrap() = object.geometry;
99
101 }
102
104 scene, view_layer, us->objects.first().obedit_ref.ptr, us_p->name, &LOG);
105
106 bmain->is_memfile_undo_flush_needed = true;
107
109}
110
111static void step_free(UndoStep *us_p)
112{
113 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
114 us->objects.~Array();
115}
116
117static void foreach_ID_ref(UndoStep *us_p,
118 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
119 void *user_data)
120{
121 CurvesUndoStep *us = reinterpret_cast<CurvesUndoStep *>(us_p);
122
123 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->scene_ref));
124 for (const StepObject &object : us->objects) {
125 foreach_ID_ref_fn(user_data, ((UndoRefID *)&object.obedit_ref));
126 }
127}
128
130
131} // namespace undo
132
147
148} // 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:46
void DEG_id_tag_update(ID *id, unsigned int flags)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
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
#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
#define LOG(level)
Definition log.h:97
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:93
CurvesGeometry geometry
bool is_memfile_undo_flush_needed
Definition BKE_main.hh:213
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)