Blender V5.0
paint_curve_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 "DNA_brush_types.h"
14#include "DNA_userdef_types.h"
15
16#include "BKE_paint.hh"
17#include "BKE_undo_system.hh"
18
19#include "ED_paint.hh"
20#include "ED_undo.hh"
21
22#include "WM_api.hh"
23
24#include "paint_intern.hh"
25
26#ifndef NDEBUG
27# include "BLI_array_utils.h" /* #BLI_array_is_zeroed */
28#endif
29
30/* -------------------------------------------------------------------- */
33
34namespace {
35
36struct UndoCurve {
37 PaintCurvePoint *points; /* points of curve */
38 int tot_points;
39 int add_index;
40};
41
42} // namespace
43
44static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
45{
47 uc->points = static_cast<PaintCurvePoint *>(MEM_dupallocN(pc->points));
48 uc->tot_points = pc->tot_points;
49 uc->add_index = pc->add_index;
50}
51
52static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
53{
55 pc->points = static_cast<PaintCurvePoint *>(MEM_dupallocN(uc->points));
56 pc->tot_points = uc->tot_points;
57 pc->add_index = uc->add_index;
58}
59
60static void undocurve_free_data(UndoCurve *uc)
61{
62 MEM_SAFE_FREE(uc->points);
63}
64
66
67/* -------------------------------------------------------------------- */
70
73
74 UndoRefID_PaintCurve pc_ref;
75
76 UndoCurve data;
77};
78
80{
81 if (C == nullptr || !paint_curve_poll(C)) {
82 return false;
83 }
85 Brush *brush = BKE_paint_brush(paint);
86 return (brush && brush->paint_curve);
87}
88
90{
91 /* XXX, use to set the undo type only. */
92 UNUSED_VARS(C, us_p);
93}
94
95static bool paintcurve_undosys_step_encode(bContext *C, Main * /*bmain*/, UndoStep *us_p)
96{
97 /* FIXME Double check this, it should not be needed here at all? undo system is supposed to
98 * ensure that. */
99 if (!paint_curve_poll(C)) {
100 return false;
101 }
102
104 Brush *brush = BKE_paint_brush(paint);
105 PaintCurve *pc = paint ? (brush ? brush->paint_curve : nullptr) : nullptr;
106 if (pc == nullptr) {
107 return false;
108 }
109
111 BLI_assert(us->step.data_size == 0);
112
113 us->pc_ref.ptr = pc;
115
116 return true;
117}
118
120 Main * /*bmain*/,
121 UndoStep *us_p,
122 const eUndoStepDir /*dir*/,
123 bool /*is_final*/)
124{
126 undocurve_to_paintcurve(&us->data, us->pc_ref.ptr);
127}
128
134
136 UndoTypeForEachIDRefFn foreach_ID_ref_fn,
137 void *user_data)
138{
140 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->pc_ref));
141}
142
158
160
161/* -------------------------------------------------------------------- */
164
166{
167 UndoStack *ustack = ED_undo_stack_get();
168 bContext *C = nullptr; /* special case, we never read from this. */
170}
171
179
Paint * BKE_paint_get_active_from_context(const bContext *C)
Definition paint.cc:476
Brush * BKE_paint_brush(Paint *paint)
Definition paint.cc:645
void(*)(void *user_data, UndoRefID *id_ref) UndoTypeForEachIDRefFn
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, bContext *C, const char *name)
eUndoStepDir
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, bContext *C, const char *name, const UndoType *ut)
#define BKE_undosys_stack_limit_steps_and_memory_defaults(ustack)
const UndoType * BKE_UNDOSYS_TYPE_PAINTCURVE
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition BLI_assert.h:46
#define UNUSED_VARS(...)
struct PaintCurvePoint PaintCurvePoint
UndoStack * ED_undo_stack_get()
Definition ed_undo.cc:442
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition RandGen.cpp:29
void * MEM_dupallocN(const void *vmemh)
Definition mallocn.cc:143
bool paint_curve_poll(bContext *C)
static bool paintcurve_undosys_poll(bContext *C)
void ED_paintcurve_undosys_type(UndoType *ut)
static void paintcurve_undosys_foreach_ID_ref(UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
static void paintcurve_undosys_step_free(UndoStep *us_p)
static void paintcurve_undosys_step_encode_init(bContext *C, UndoStep *us_p)
static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
void ED_paintcurve_undo_push_begin(const char *name)
static bool paintcurve_undosys_step_encode(bContext *C, Main *, UndoStep *us_p)
static void undocurve_free_data(UndoCurve *uc)
void ED_paintcurve_undo_push_end(bContext *C)
static void paintcurve_undosys_step_decode(bContext *, Main *, UndoStep *us_p, const eUndoStepDir, bool)
const char * name
struct PaintCurve * paint_curve
UndoRefID_PaintCurve pc_ref
PaintCurvePoint * points
size_t data_size
void(* step_encode_init)(bContext *C, UndoStep *us)
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_file_tag_modified()
Definition wm_files.cc:177