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