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.
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.
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.
17 /** \file blender/editors/sculpt_paint/paint_curve_undo.c
23 #include "MEM_guardedalloc.h"
25 #include "DNA_brush_types.h"
26 #include "DNA_space_types.h"
28 #include "BLI_array_utils.h"
30 #include "BKE_context.h"
31 #include "BKE_paint.h"
32 #include "BKE_undo_system.h"
40 #include "paint_intern.h"
42 /* -------------------------------------------------------------------- */
43 /** \name Undo Conversion
46 typedef struct UndoCurve {
47 PaintCurvePoint *points; /* points of curve */
52 static void undocurve_from_paintcurve(UndoCurve *uc, const PaintCurve *pc)
54 BLI_assert(BLI_array_is_zeroed(uc, 1));
55 uc->points = MEM_dupallocN(pc->points);
56 uc->tot_points = pc->tot_points;
57 uc->add_index = pc->add_index;
60 static void undocurve_to_paintcurve(const UndoCurve *uc, PaintCurve *pc)
62 MEM_SAFE_FREE(pc->points);
63 pc->points = MEM_dupallocN(uc->points);
64 pc->tot_points = uc->tot_points;
65 pc->add_index = uc->add_index;
68 static void undocurve_free_data(UndoCurve *uc)
70 MEM_SAFE_FREE(uc->points);
75 /* -------------------------------------------------------------------- */
76 /** \name Implements ED Undo System
79 typedef struct PaintCurveUndoStep {
85 static bool paintcurve_undosys_poll(bContext *C)
87 Paint *p = BKE_paint_get_active_from_context(C);
88 return (p->brush && p->brush->paint_curve);
91 static void paintcurve_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
93 /* XXX, use to set the undo type only. */
97 static bool paintcurve_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
99 Paint *p = BKE_paint_get_active_from_context(C);
100 PaintCurve *pc = p ? (p->brush ? p->brush->paint_curve : NULL) : NULL;
105 PaintCurveUndoStep *us = (PaintCurveUndoStep *)us_p;
106 BLI_assert(us->step.data_size == 0);
109 undocurve_from_paintcurve(&us->data, pc);
114 static void paintcurve_undosys_step_decode(struct bContext *UNUSED(C), struct Main *UNUSED(bmain), UndoStep *us_p, int UNUSED(dir))
116 PaintCurveUndoStep *us = (PaintCurveUndoStep *)us_p;
117 undocurve_to_paintcurve(&us->data, us->pc);
120 static void paintcurve_undosys_step_free(UndoStep *us_p)
122 PaintCurveUndoStep *us = (PaintCurveUndoStep *)us_p;
123 undocurve_free_data(&us->data);
126 /* Export for ED_undo_sys. */
127 void ED_paintcurve_undosys_type(UndoType *ut)
129 ut->name = "Paint Curve";
130 /* don't poll for now */
131 ut->poll = paintcurve_undosys_poll;
132 ut->step_encode_init = paintcurve_undosys_step_encode_init;
133 ut->step_encode = paintcurve_undosys_step_encode;
134 ut->step_decode = paintcurve_undosys_step_decode;
135 ut->step_free = paintcurve_undosys_step_free;
137 ut->use_context = false;
139 ut->step_size = sizeof(PaintCurveUndoStep);
145 /* -------------------------------------------------------------------- */
149 void ED_paintcurve_undo_push_begin(const char *name)
151 UndoStack *ustack = ED_undo_stack_get();
152 bContext *C = NULL; /* special case, we never read from this. */
153 BKE_undosys_step_push_init_with_type(ustack, C, name, BKE_UNDOSYS_TYPE_PAINTCURVE);
156 void ED_paintcurve_undo_push_end(void)
158 UndoStack *ustack = ED_undo_stack_get();
159 BKE_undosys_step_push(ustack, NULL, NULL);