Blender  V2.93
paint_curve_undo.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <string.h>
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "DNA_brush_types.h"
26 #include "DNA_space_types.h"
27 
28 #include "BLI_array_utils.h"
29 
30 #include "BKE_context.h"
31 #include "BKE_paint.h"
32 #include "BKE_undo_system.h"
33 
34 #include "ED_paint.h"
35 #include "ED_undo.h"
36 
37 #include "WM_api.h"
38 
39 #include "paint_intern.h"
40 
41 /* -------------------------------------------------------------------- */
45 typedef struct UndoCurve {
46  PaintCurvePoint *points; /* points of curve */
48  int add_index;
50 
51 static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
52 {
54  uc->points = MEM_dupallocN(pc->points);
55  uc->tot_points = pc->tot_points;
56  uc->add_index = pc->add_index;
57 }
58 
59 static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
60 {
61  MEM_SAFE_FREE(pc->points);
62  pc->points = MEM_dupallocN(uc->points);
63  pc->tot_points = uc->tot_points;
64  pc->add_index = uc->add_index;
65 }
66 
68 {
69  MEM_SAFE_FREE(uc->points);
70 }
71 
74 /* -------------------------------------------------------------------- */
78 typedef struct PaintCurveUndoStep {
80 
81  UndoRefID_PaintCurve pc_ref;
82 
85 
87 {
88  if (C == NULL || !paint_curve_poll(C)) {
89  return false;
90  }
92  return (p->brush && p->brush->paint_curve);
93 }
94 
96 {
97  /* XXX, use to set the undo type only. */
98  UNUSED_VARS(C, us_p);
99 }
100 
102  struct Main *UNUSED(bmain),
103  UndoStep *us_p)
104 {
105  /* FIXME Double check this, it should not be needed here at all? undo system is supposed to
106  * ensure that. */
107  if (!paint_curve_poll(C)) {
108  return false;
109  }
110 
112  PaintCurve *pc = p ? (p->brush ? p->brush->paint_curve : NULL) : NULL;
113  if (pc == NULL) {
114  return false;
115  }
116 
118  BLI_assert(us->step.data_size == 0);
119 
120  us->pc_ref.ptr = pc;
122 
123  return true;
124 }
125 
127  struct Main *UNUSED(bmain),
128  UndoStep *us_p,
129  const eUndoStepDir UNUSED(dir),
130  bool UNUSED(is_final))
131 {
133  undocurve_to_paintcurve(&us->data, us->pc_ref.ptr);
134 }
135 
137 {
140 }
141 
143  UndoTypeForEachIDRefFn foreach_ID_ref_fn,
144  void *user_data)
145 {
147  foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->pc_ref));
148 }
149 
150 /* Export for ED_undo_sys. */
152 {
153  ut->name = "Paint Curve";
159 
161 
162  ut->flags = 0;
163 
164  ut->step_size = sizeof(PaintCurveUndoStep);
165 }
166 
169 /* -------------------------------------------------------------------- */
173 void ED_paintcurve_undo_push_begin(const char *name)
174 {
175  UndoStack *ustack = ED_undo_stack_get();
176  bContext *C = NULL; /* special case, we never read from this. */
178 }
179 
181 {
182  UndoStack *ustack = ED_undo_stack_get();
183  BKE_undosys_step_push(ustack, C, NULL);
186 }
187 
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:458
eUndoStepDir
#define BKE_undosys_stack_limit_steps_and_memory_defaults(ustack)
UndoPushReturn BKE_undosys_step_push(UndoStack *ustack, struct bContext *C, const char *name)
Definition: undo_system.c:605
const UndoType * BKE_UNDOSYS_TYPE_PAINTCURVE
Definition: undo_system.c:102
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:58
#define UNUSED_VARS(...)
#define UNUSED(x)
struct UndoStack * ED_undo_stack_get(void)
Definition: ed_undo.c:501
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition: RandGen.cpp:39
void * user_data
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
bool paint_curve_poll(bContext *C)
Definition: paint_curve.c:54
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:116
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, const 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:158