Blender  V3.3
paint_curve_undo.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <string.h>
8 
9 #include "MEM_guardedalloc.h"
10 
11 #include "DNA_brush_types.h"
12 #include "DNA_space_types.h"
13 
14 #include "BLI_array_utils.h"
15 
16 #include "BKE_context.h"
17 #include "BKE_paint.h"
18 #include "BKE_undo_system.h"
19 
20 #include "ED_paint.h"
21 #include "ED_undo.h"
22 
23 #include "WM_api.h"
24 
25 #include "paint_intern.h"
26 
27 /* -------------------------------------------------------------------- */
31 typedef struct UndoCurve {
32  PaintCurvePoint *points; /* points of curve */
34  int add_index;
36 
37 static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
38 {
40  uc->points = MEM_dupallocN(pc->points);
41  uc->tot_points = pc->tot_points;
42  uc->add_index = pc->add_index;
43 }
44 
45 static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
46 {
47  MEM_SAFE_FREE(pc->points);
48  pc->points = MEM_dupallocN(uc->points);
49  pc->tot_points = uc->tot_points;
50  pc->add_index = uc->add_index;
51 }
52 
54 {
55  MEM_SAFE_FREE(uc->points);
56 }
57 
60 /* -------------------------------------------------------------------- */
64 typedef struct PaintCurveUndoStep {
66 
67  UndoRefID_PaintCurve pc_ref;
68 
71 
73 {
74  if (C == NULL || !paint_curve_poll(C)) {
75  return false;
76  }
78  return (p->brush && p->brush->paint_curve);
79 }
80 
82 {
83  /* XXX, use to set the undo type only. */
84  UNUSED_VARS(C, us_p);
85 }
86 
88  struct Main *UNUSED(bmain),
89  UndoStep *us_p)
90 {
91  /* FIXME Double check this, it should not be needed here at all? undo system is supposed to
92  * ensure that. */
93  if (!paint_curve_poll(C)) {
94  return false;
95  }
96 
98  PaintCurve *pc = p ? (p->brush ? p->brush->paint_curve : NULL) : NULL;
99  if (pc == NULL) {
100  return false;
101  }
102 
104  BLI_assert(us->step.data_size == 0);
105 
106  us->pc_ref.ptr = pc;
108 
109  return true;
110 }
111 
113  struct Main *UNUSED(bmain),
114  UndoStep *us_p,
115  const eUndoStepDir UNUSED(dir),
116  bool UNUSED(is_final))
117 {
119  undocurve_to_paintcurve(&us->data, us->pc_ref.ptr);
120 }
121 
123 {
126 }
127 
129  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
130  void *user_data)
131 {
133  foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->pc_ref));
134 }
135 
137 {
138  ut->name = "Paint Curve";
144 
146 
147  ut->flags = 0;
148 
149  ut->step_size = sizeof(PaintCurveUndoStep);
150 }
151 
154 /* -------------------------------------------------------------------- */
158 void ED_paintcurve_undo_push_begin(const char *name)
159 {
160  UndoStack *ustack = ED_undo_stack_get();
161  bContext *C = NULL; /* special case, we never read from this. */
163 }
164 
166 {
167  UndoStack *ustack = ED_undo_stack_get();
168  BKE_undosys_step_push(ustack, C, NULL);
171 }
172 
struct Paint * BKE_paint_get_active_from_context(const struct bContext *C)
UndoStep * BKE_undosys_step_push_init_with_type(UndoStack *ustack, struct bContext *C, const char *name, const UndoType *ut)
Definition: undo_system.c:449
eUndoPushReturn BKE_undosys_step_push(UndoStack *ustack, struct bContext *C, const char *name)
Definition: undo_system.c:593
eUndoStepDir
#define BKE_undosys_stack_limit_steps_and_memory_defaults(ustack)
const UndoType * BKE_UNDOSYS_TYPE_PAINTCURVE
Definition: undo_system.c:55
void(* UndoTypeForEachIDRefFn)(void *user_data, struct UndoRefID *id_ref)
Generic array manipulation API.
#define BLI_array_is_zeroed(arr, arr_len)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED_VARS(...)
#define UNUSED(x)
struct UndoStack * ED_undo_stack_get(void)
Definition: ed_undo.c:473
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition: RandGen.cpp:25
void * user_data
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
bool paint_curve_poll(bContext *C)
Definition: paint_curve.c:40
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)
struct UndoCurve UndoCurve
static void paintcurve_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
struct PaintCurveUndoStep PaintCurveUndoStep
static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
static void paintcurve_undosys_step_free(UndoStep *us_p)
static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
void ED_paintcurve_undo_push_begin(const char *name)
static void undocurve_free_data(UndoCurve *uc)
static bool paintcurve_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
void ED_paintcurve_undo_push_end(bContext *C)
static void paintcurve_undosys_step_decode(struct bContext *UNUSED(C), struct Main *UNUSED(bmain), UndoStep *us_p, const eUndoStepDir UNUSED(dir), bool UNUSED(is_final))
struct PaintCurve * paint_curve
Definition: BKE_main.h:121
UndoRefID_PaintCurve pc_ref
PaintCurvePoint * points
struct Brush * brush
PaintCurvePoint * points
size_t data_size
size_t step_size
void(* step_decode)(struct bContext *C, struct Main *bmain, UndoStep *us, eUndoStepDir dir, bool is_final)
bool(* step_encode)(struct bContext *C, struct Main *bmain, UndoStep *us)
void(* step_encode_init)(struct 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 WM_file_tag_modified(void)
Definition: wm_files.c:150