doxygen: add newline after \file
[blender.git] / source / blender / editors / mask / mask_shapekey.c
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  * The Original Code is Copyright (C) 2012 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edmask
22  */
23
24 #include <stdlib.h>
25
26 #include "BLI_utildefines.h"
27 #include "BLI_listbase.h"
28 #include "BLI_math.h"
29
30 #include "BKE_context.h"
31 #include "BKE_mask.h"
32
33 #include "DNA_object_types.h"
34 #include "DNA_mask_types.h"
35 #include "DNA_scene_types.h"
36
37 #include "DEG_depsgraph.h"
38
39 #include "RNA_access.h"
40 #include "RNA_define.h"
41
42 #include "WM_api.h"
43 #include "WM_types.h"
44
45 #include "ED_mask.h"  /* own include */
46
47 #include "mask_intern.h"  /* own include */
48
49 static int mask_shape_key_insert_exec(bContext *C, wmOperator *UNUSED(op))
50 {
51         Scene *scene = CTX_data_scene(C);
52         const int frame = CFRA;
53         Mask *mask = CTX_data_edit_mask(C);
54         MaskLayer *masklay;
55         bool changed = false;
56
57         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
58                 MaskLayerShape *masklay_shape;
59
60                 if (!ED_mask_layer_select_check(masklay)) {
61                         continue;
62                 }
63
64                 masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame);
65                 BKE_mask_layer_shape_from_mask(masklay, masklay_shape);
66                 changed = true;
67         }
68
69         if (changed) {
70                 WM_event_add_notifier(C, NC_MASK | ND_DATA, mask);
71                 DEG_id_tag_update(&mask->id, 0);
72
73                 return OPERATOR_FINISHED;
74         }
75         else {
76                 return OPERATOR_CANCELLED;
77         }
78 }
79
80 void MASK_OT_shape_key_insert(wmOperatorType *ot)
81 {
82         /* identifiers */
83         ot->name = "Insert Shape Key";
84         ot->idname = "MASK_OT_shape_key_insert";
85
86         /* api callbacks */
87         ot->exec = mask_shape_key_insert_exec;
88         ot->poll = ED_maskedit_mask_poll;
89
90         /* flags */
91         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
92 }
93
94 static int mask_shape_key_clear_exec(bContext *C, wmOperator *UNUSED(op))
95 {
96         Scene *scene = CTX_data_scene(C);
97         const int frame = CFRA;
98         Mask *mask = CTX_data_edit_mask(C);
99         MaskLayer *masklay;
100         bool changed = false;
101
102         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
103                 MaskLayerShape *masklay_shape;
104
105                 if (!ED_mask_layer_select_check(masklay)) {
106                         continue;
107                 }
108
109                 masklay_shape = BKE_mask_layer_shape_find_frame(masklay, frame);
110
111                 if (masklay_shape) {
112                         BKE_mask_layer_shape_unlink(masklay, masklay_shape);
113                         changed = true;
114                 }
115         }
116
117         if (changed) {
118                 WM_event_add_notifier(C, NC_MASK | ND_DATA, mask);
119                 DEG_id_tag_update(&mask->id, ID_RECALC_GEOMETRY);
120
121                 return OPERATOR_FINISHED;
122         }
123         else {
124                 return OPERATOR_CANCELLED;
125         }
126 }
127
128 void MASK_OT_shape_key_clear(wmOperatorType *ot)
129 {
130         /* identifiers */
131         ot->name = "Clear Shape Key";
132         ot->idname = "MASK_OT_shape_key_clear";
133
134         /* api callbacks */
135         ot->exec = mask_shape_key_clear_exec;
136         ot->poll = ED_maskedit_mask_poll;
137
138         /* flags */
139         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
140 }
141
142 static int mask_shape_key_feather_reset_exec(bContext *C, wmOperator *UNUSED(op))
143 {
144         Scene *scene = CTX_data_scene(C);
145         const int frame = CFRA;
146         Mask *mask = CTX_data_edit_mask(C);
147         MaskLayer *masklay;
148         bool changed = false;
149
150         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
151
152                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
153                         continue;
154                 }
155
156                 if (masklay->splines_shapes.first) {
157                         MaskLayerShape *masklay_shape_reset;
158                         MaskLayerShape *masklay_shape;
159
160                         /* get the shapekey of the current state */
161                         masklay_shape_reset = BKE_mask_layer_shape_alloc(masklay, frame);
162                         /* initialize from mask - as if inseting a keyframe */
163                         BKE_mask_layer_shape_from_mask(masklay, masklay_shape_reset);
164
165                         for (masklay_shape = masklay->splines_shapes.first;
166                              masklay_shape;
167                              masklay_shape = masklay_shape->next)
168                         {
169
170                                 if (masklay_shape_reset->tot_vert == masklay_shape->tot_vert) {
171                                         int i_abs = 0;
172                                         int i;
173                                         MaskSpline *spline;
174                                         MaskLayerShapeElem *shape_ele_src;
175                                         MaskLayerShapeElem *shape_ele_dst;
176
177                                         shape_ele_src = (MaskLayerShapeElem *)masklay_shape_reset->data;
178                                         shape_ele_dst = (MaskLayerShapeElem *)masklay_shape->data;
179
180                                         for (spline = masklay->splines.first; spline; spline = spline->next) {
181                                                 for (i = 0; i < spline->tot_point; i++) {
182                                                         MaskSplinePoint *point = &spline->points[i];
183
184                                                         if (MASKPOINT_ISSEL_ANY(point)) {
185                                                                 /* TODO - nicer access here */
186                                                                 shape_ele_dst->value[6] = shape_ele_src->value[6];
187                                                         }
188
189                                                         shape_ele_src++;
190                                                         shape_ele_dst++;
191
192                                                         i_abs++;
193                                                 }
194                                         }
195
196                                 }
197                                 else {
198                                         // printf("%s: skipping\n", __func__);
199                                 }
200
201                                 changed = true;
202                         }
203
204                         BKE_mask_layer_shape_free(masklay_shape_reset);
205                 }
206         }
207
208         if (changed) {
209                 WM_event_add_notifier(C, NC_MASK | ND_DATA, mask);
210                 DEG_id_tag_update(&mask->id, 0);
211
212                 return OPERATOR_FINISHED;
213         }
214         else {
215                 return OPERATOR_CANCELLED;
216         }
217 }
218
219 void MASK_OT_shape_key_feather_reset(wmOperatorType *ot)
220 {
221         /* identifiers */
222         ot->name = "Feather Reset Animation";
223         ot->description = "Reset feather weights on all selected points animation values";
224         ot->idname = "MASK_OT_shape_key_feather_reset";
225
226         /* api callbacks */
227         ot->exec = mask_shape_key_feather_reset_exec;
228         ot->poll = ED_maskedit_mask_poll;
229
230         /* flags */
231         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
232 }
233
234 /*
235  * - loop over selected shapekeys.
236  * - find firstsel/lastsel pairs.
237  * - move these into a temp list.
238  * - re-key all the original shapes.
239  * - copy unselected values back from the original.
240  * - free the original.
241  */
242 static int mask_shape_key_rekey_exec(bContext *C, wmOperator *op)
243 {
244         Scene *scene = CTX_data_scene(C);
245         const int frame = CFRA;
246         Mask *mask = CTX_data_edit_mask(C);
247         MaskLayer *masklay;
248         bool changed = false;
249
250         const bool do_feather  = RNA_boolean_get(op->ptr, "feather");
251         const bool do_location = RNA_boolean_get(op->ptr, "location");
252
253         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
254
255                 if (masklay->restrictflag & (MASK_RESTRICT_VIEW | MASK_RESTRICT_SELECT)) {
256                         continue;
257                 }
258
259                 /* we need at least one point selected here to bother re-interpolating */
260                 if (!ED_mask_layer_select_check(masklay)) {
261                         continue;
262                 }
263
264                 if (masklay->splines_shapes.first) {
265                         MaskLayerShape *masklay_shape, *masklay_shape_next;
266                         MaskLayerShape *masklay_shape_lastsel = NULL;
267
268                         for (masklay_shape = masklay->splines_shapes.first;
269                              masklay_shape;
270                              masklay_shape = masklay_shape_next)
271                         {
272                                 MaskLayerShape *masklay_shape_a = NULL;
273                                 MaskLayerShape *masklay_shape_b = NULL;
274
275                                 masklay_shape_next = masklay_shape->next;
276
277                                 /* find contiguous selections */
278                                 if (masklay_shape->flag & MASK_SHAPE_SELECT) {
279                                         if (masklay_shape_lastsel == NULL) {
280                                                 masklay_shape_lastsel = masklay_shape;
281                                         }
282                                         if ((masklay_shape->next == NULL) ||
283                                             (((MaskLayerShape *)masklay_shape->next)->flag & MASK_SHAPE_SELECT) == 0)
284                                         {
285                                                 masklay_shape_a = masklay_shape_lastsel;
286                                                 masklay_shape_b = masklay_shape;
287                                                 masklay_shape_lastsel = NULL;
288
289                                                 /* this will be freed below, step over selection */
290                                                 masklay_shape_next = masklay_shape->next;
291                                         }
292                                 }
293
294                                 /* we have a from<>to? - re-interpolate! */
295                                 if (masklay_shape_a && masklay_shape_b) {
296                                         ListBase shapes_tmp = {NULL, NULL};
297                                         MaskLayerShape *masklay_shape_tmp;
298                                         MaskLayerShape *masklay_shape_tmp_next;
299                                         MaskLayerShape *masklay_shape_tmp_last = masklay_shape_b->next;
300                                         MaskLayerShape *masklay_shape_tmp_rekey;
301
302                                         /* move keys */
303                                         for (masklay_shape_tmp = masklay_shape_a;
304                                              masklay_shape_tmp && (masklay_shape_tmp != masklay_shape_tmp_last);
305                                              masklay_shape_tmp = masklay_shape_tmp_next)
306                                         {
307                                                 masklay_shape_tmp_next = masklay_shape_tmp->next;
308                                                 BLI_remlink(&masklay->splines_shapes, masklay_shape_tmp);
309                                                 BLI_addtail(&shapes_tmp, masklay_shape_tmp);
310                                         }
311
312                                         /* re-key, note: cant modify the keys here since it messes uop */
313                                         for (masklay_shape_tmp = shapes_tmp.first;
314                                              masklay_shape_tmp;
315                                              masklay_shape_tmp = masklay_shape_tmp->next)
316                                         {
317                                                 BKE_mask_layer_evaluate(masklay, masklay_shape_tmp->frame, true);
318                                                 masklay_shape_tmp_rekey = BKE_mask_layer_shape_verify_frame(masklay, masklay_shape_tmp->frame);
319                                                 BKE_mask_layer_shape_from_mask(masklay, masklay_shape_tmp_rekey);
320                                                 masklay_shape_tmp_rekey->flag = masklay_shape_tmp->flag & MASK_SHAPE_SELECT;
321                                         }
322
323                                         /* restore unselected points and free copies */
324                                         for (masklay_shape_tmp = shapes_tmp.first;
325                                              masklay_shape_tmp;
326                                              masklay_shape_tmp = masklay_shape_tmp_next)
327                                         {
328                                                 /* restore */
329                                                 int i_abs = 0;
330                                                 int i;
331                                                 MaskSpline *spline;
332                                                 MaskLayerShapeElem *shape_ele_src;
333                                                 MaskLayerShapeElem *shape_ele_dst;
334
335                                                 masklay_shape_tmp_next = masklay_shape_tmp->next;
336
337                                                 /* we know this exists, added above */
338                                                 masklay_shape_tmp_rekey = BKE_mask_layer_shape_find_frame(masklay, masklay_shape_tmp->frame);
339
340                                                 shape_ele_src = (MaskLayerShapeElem *)masklay_shape_tmp->data;
341                                                 shape_ele_dst = (MaskLayerShapeElem *)masklay_shape_tmp_rekey->data;
342
343                                                 for (spline = masklay->splines.first; spline; spline = spline->next) {
344                                                         for (i = 0; i < spline->tot_point; i++) {
345                                                                 MaskSplinePoint *point = &spline->points[i];
346
347                                                                 /* not especially efficient but makes this easier to follow */
348                                                                 SWAP(MaskLayerShapeElem, *shape_ele_src, *shape_ele_dst);
349
350                                                                 if (MASKPOINT_ISSEL_ANY(point)) {
351                                                                         if (do_location) {
352                                                                                 memcpy(shape_ele_dst->value, shape_ele_src->value, sizeof(float) * 6);
353                                                                         }
354                                                                         if (do_feather) {
355                                                                                 shape_ele_dst->value[6] = shape_ele_src->value[6];
356                                                                         }
357                                                                 }
358
359                                                                 shape_ele_src++;
360                                                                 shape_ele_dst++;
361
362                                                                 i_abs++;
363                                                         }
364                                                 }
365
366                                                 BKE_mask_layer_shape_free(masklay_shape_tmp);
367                                         }
368
369                                         changed = true;
370                                 }
371                         }
372
373                         /* re-evaluate */
374                         BKE_mask_layer_evaluate(masklay, frame, true);
375                 }
376         }
377
378         if (changed) {
379                 WM_event_add_notifier(C, NC_MASK | ND_DATA, mask);
380                 DEG_id_tag_update(&mask->id, 0);
381
382                 return OPERATOR_FINISHED;
383         }
384         else {
385                 return OPERATOR_CANCELLED;
386         }
387 }
388
389 void MASK_OT_shape_key_rekey(wmOperatorType *ot)
390 {
391         /* identifiers */
392         ot->name = "Re-Key Points of Selected Shapes";
393         ot->description = "Recalculate animation data on selected points for frames selected in the dopesheet";
394         ot->idname = "MASK_OT_shape_key_rekey";
395
396         /* api callbacks */
397         ot->exec = mask_shape_key_rekey_exec;
398         ot->poll = ED_maskedit_mask_poll;
399
400         /* flags */
401         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
402
403         /* properties */
404         RNA_def_boolean(ot->srna, "location", true, "Location", "");
405         RNA_def_boolean(ot->srna, "feather", true, "Feather", "");
406 }
407
408
409 /* *** Shape Key Utils *** */
410
411 void ED_mask_layer_shape_auto_key(MaskLayer *masklay, const int frame)
412 {
413         MaskLayerShape *masklay_shape;
414
415         masklay_shape = BKE_mask_layer_shape_verify_frame(masklay, frame);
416         BKE_mask_layer_shape_from_mask(masklay, masklay_shape);
417 }
418
419 bool ED_mask_layer_shape_auto_key_all(Mask *mask, const int frame)
420 {
421         MaskLayer *masklay;
422         bool changed = false;
423
424         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
425                 ED_mask_layer_shape_auto_key(masklay, frame);
426                 changed = true;
427         }
428
429         return changed;
430 }
431
432 bool ED_mask_layer_shape_auto_key_select(Mask *mask, const int frame)
433 {
434         MaskLayer *masklay;
435         bool changed = false;
436
437         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
438
439                 if (!ED_mask_layer_select_check(masklay)) {
440                         continue;
441                 }
442
443                 ED_mask_layer_shape_auto_key(masklay, frame);
444                 changed = true;
445         }
446
447         return changed;
448 }