Blender  V2.93
mask_editaction.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  * The Original Code is Copyright (C) 2008, Blender Foundation
17  * This is a new part of Blender
18  */
19 
24 #include <math.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "MEM_guardedalloc.h"
31 
32 #include "BLI_blenlib.h"
33 #include "BLI_utildefines.h"
34 
35 #include "DNA_mask_types.h"
36 #include "DNA_scene_types.h"
37 
38 #include "BKE_fcurve.h"
39 #include "BKE_mask.h"
40 
41 #include "ED_anim_api.h"
42 #include "ED_keyframes_edit.h"
43 #include "ED_markers.h"
44 #include "ED_mask.h" /* own include */
45 
46 /* ***************************************** */
47 /* NOTE ABOUT THIS FILE:
48  * This file contains code for editing Mask data in the Action Editor
49  * as a 'keyframes', so that a user can adjust the timing of Mask shapekeys.
50  * Therefore, this file mostly contains functions for selecting Mask frames (shapekeys).
51  */
52 /* ***************************************** */
53 /* Generics - Loopers */
54 
55 /* Loops over the mask-frames for a mask-layer, and applies the given callback */
57  Scene *scene,
58  bool (*mask_layer_shape_cb)(MaskLayerShape *, Scene *))
59 {
60  MaskLayerShape *mask_layer_shape;
61 
62  /* error checker */
63  if (mask_layer == NULL) {
64  return false;
65  }
66 
67  /* do loop */
68  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
69  mask_layer_shape = mask_layer_shape->next) {
70  /* execute callback */
71  if (mask_layer_shape_cb(mask_layer_shape, scene)) {
72  return true;
73  }
74  }
75 
76  /* nothing to return */
77  return false;
78 }
79 
80 /* ****************************************** */
81 /* Data Conversion Tools */
82 
83 /* make a listing all the mask-frames in a layer as cfraelems */
84 void ED_masklayer_make_cfra_list(MaskLayer *mask_layer, ListBase *elems, bool onlysel)
85 {
86  MaskLayerShape *mask_layer_shape;
87  CfraElem *ce;
88 
89  /* error checking */
90  if (ELEM(NULL, mask_layer, elems)) {
91  return;
92  }
93 
94  /* loop through mask-frames, adding */
95  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
96  mask_layer_shape = mask_layer_shape->next) {
97  if ((onlysel == false) || (mask_layer_shape->flag & MASK_SHAPE_SELECT)) {
98  ce = MEM_callocN(sizeof(CfraElem), "CfraElem");
99 
100  ce->cfra = (float)mask_layer_shape->frame;
101  ce->sel = (mask_layer_shape->flag & MASK_SHAPE_SELECT) ? 1 : 0;
102 
103  BLI_addtail(elems, ce);
104  }
105  }
106 }
107 
108 /* ***************************************** */
109 /* Selection Tools */
110 
111 /* check if one of the frames in this layer is selected */
113 {
114  MaskLayerShape *mask_layer_shape;
115 
116  /* error checking */
117  if (mask_layer == NULL) {
118  return 0;
119  }
120 
121  /* stop at the first one found */
122  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
123  mask_layer_shape = mask_layer_shape->next) {
124  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
125  return true;
126  }
127  }
128 
129  /* not found */
130  return false;
131 }
132 
133 /* helper function - select mask-frame based on SELECT_* mode */
134 static void mask_layer_shape_select(MaskLayerShape *mask_layer_shape, short select_mode)
135 {
136  if (mask_layer_shape == NULL) {
137  return;
138  }
139 
140  switch (select_mode) {
141  case SELECT_ADD:
142  mask_layer_shape->flag |= MASK_SHAPE_SELECT;
143  break;
144  case SELECT_SUBTRACT:
145  mask_layer_shape->flag &= ~MASK_SHAPE_SELECT;
146  break;
147  case SELECT_INVERT:
148  mask_layer_shape->flag ^= MASK_SHAPE_SELECT;
149  break;
150  }
151 }
152 
153 /* set all/none/invert select (like above, but with SELECT_* modes) */
154 void ED_mask_select_frames(MaskLayer *mask_layer, short select_mode)
155 {
156  MaskLayerShape *mask_layer_shape;
157 
158  /* error checking */
159  if (mask_layer == NULL) {
160  return;
161  }
162 
163  /* handle according to mode */
164  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
165  mask_layer_shape = mask_layer_shape->next) {
166  mask_layer_shape_select(mask_layer_shape, select_mode);
167  }
168 }
169 
170 /* set all/none/invert select */
171 void ED_masklayer_frame_select_set(MaskLayer *mask_layer, short mode)
172 {
173  /* error checking */
174  if (mask_layer == NULL) {
175  return;
176  }
177 
178  /* now call the standard function */
179  ED_mask_select_frames(mask_layer, mode);
180 }
181 
182 /* select the frame in this layer that occurs on this frame (there should only be one at most) */
183 void ED_mask_select_frame(MaskLayer *mask_layer, int selx, short select_mode)
184 {
185  MaskLayerShape *mask_layer_shape;
186 
187  if (mask_layer == NULL) {
188  return;
189  }
190 
191  mask_layer_shape = BKE_mask_layer_shape_find_frame(mask_layer, selx);
192 
193  if (mask_layer_shape) {
194  mask_layer_shape_select(mask_layer_shape, select_mode);
195  }
196 }
197 
198 /* select the frames in this layer that occur within the bounds specified */
199 void ED_masklayer_frames_select_box(MaskLayer *mask_layer, float min, float max, short select_mode)
200 {
201  MaskLayerShape *mask_layer_shape;
202 
203  if (mask_layer == NULL) {
204  return;
205  }
206 
207  /* only select those frames which are in bounds */
208  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
209  mask_layer_shape = mask_layer_shape->next) {
210  if (IN_RANGE(mask_layer_shape->frame, min, max)) {
211  mask_layer_shape_select(mask_layer_shape, select_mode);
212  }
213  }
214 }
215 
216 /* select the frames in this layer that occur within the lasso/circle region specified */
218  MaskLayer *mask_layer,
219  short tool,
220  short select_mode)
221 {
222  MaskLayerShape *mask_layer_shape;
223 
224  if (mask_layer == NULL) {
225  return;
226  }
227 
228  /* only select frames which are within the region */
229  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
230  mask_layer_shape = mask_layer_shape->next) {
231  /* construct a dummy point coordinate to do this testing with */
232  float pt[2] = {0};
233 
234  pt[0] = mask_layer_shape->frame;
235  pt[1] = ked->channel_y;
236 
237  /* check the necessary regions */
238  if (tool == BEZT_OK_CHANNEL_LASSO) {
239  /* Lasso */
240  if (keyframe_region_lasso_test(ked->data, pt)) {
241  mask_layer_shape_select(mask_layer_shape, select_mode);
242  }
243  }
244  else if (tool == BEZT_OK_CHANNEL_CIRCLE) {
245  /* Circle */
246  if (keyframe_region_circle_test(ked->data, pt)) {
247  mask_layer_shape_select(mask_layer_shape, select_mode);
248  }
249  }
250  }
251 }
252 
253 /* ***************************************** */
254 /* Frame Editing Tools */
255 
256 /* Delete selected frames */
258 {
259  MaskLayerShape *mask_layer_shape, *mask_layer_shape_next;
260  bool changed = false;
261 
262  /* error checking */
263  if (mask_layer == NULL) {
264  return false;
265  }
266 
267  /* check for frames to delete */
268  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
269  mask_layer_shape = mask_layer_shape_next) {
270  mask_layer_shape_next = mask_layer_shape->next;
271 
272  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
273  BKE_mask_layer_shape_unlink(mask_layer, mask_layer_shape);
274  changed = true;
275  }
276  }
277 
278  return changed;
279 }
280 
281 /* Duplicate selected frames from given mask-layer */
283 {
284  MaskLayerShape *mask_layer_shape, *gpfn;
285 
286  /* error checking */
287  if (mask_layer == NULL) {
288  return;
289  }
290 
291  /* duplicate selected frames */
292  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
293  mask_layer_shape = gpfn) {
294  gpfn = mask_layer_shape->next;
295 
296  /* duplicate this frame */
297  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
298  MaskLayerShape *mask_shape_dupe;
299 
300  /* duplicate frame, and deselect self */
301  mask_shape_dupe = BKE_mask_layer_shape_duplicate(mask_layer_shape);
302  mask_layer_shape->flag &= ~MASK_SHAPE_SELECT;
303 
304  /* XXX - how to handle duplicate frames? */
305  BLI_insertlinkafter(&mask_layer->splines_shapes, mask_layer_shape, mask_shape_dupe);
306  }
307  }
308 }
309 
310 /* -------------------------------------- */
311 /* Snap Tools */
312 
313 static bool snap_mask_layer_nearest(MaskLayerShape *mask_layer_shape, Scene *UNUSED(scene))
314 {
315  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
316  mask_layer_shape->frame = (int)(floor(mask_layer_shape->frame + 0.5));
317  }
318  return false;
319 }
320 
321 static bool snap_mask_layer_nearestsec(MaskLayerShape *mask_layer_shape, Scene *scene)
322 {
323  float secf = (float)FPS;
324  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
325  mask_layer_shape->frame = (int)(floorf(mask_layer_shape->frame / secf + 0.5f) * secf);
326  }
327  return false;
328 }
329 
330 static bool snap_mask_layer_cframe(MaskLayerShape *mask_layer_shape, Scene *scene)
331 {
332  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
333  mask_layer_shape->frame = (int)CFRA;
334  }
335  return false;
336 }
337 
338 static bool snap_mask_layer_nearmarker(MaskLayerShape *mask_layer_shape, Scene *scene)
339 {
340  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
341  mask_layer_shape->frame = (int)ED_markers_find_nearest_marker_time(
342  &scene->markers, (float)mask_layer_shape->frame);
343  }
344  return false;
345 }
346 
347 /* snap selected frames to ... */
348 void ED_masklayer_snap_frames(MaskLayer *mask_layer, Scene *scene, short mode)
349 {
350  switch (mode) {
351  case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */
353  break;
354  case SNAP_KEYS_CURFRAME: /* snap to current frame */
356  break;
357  case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */
359  break;
360  case SNAP_KEYS_NEARSEC: /* snap to nearest second */
362  break;
363  default: /* just in case */
364  break;
365  }
366 }
typedef float(TangentPoint)[2]
struct MaskLayerShape * BKE_mask_layer_shape_find_frame(struct MaskLayer *masklay, const int frame)
Definition: mask.c:1744
void BKE_mask_layer_shape_unlink(struct MaskLayer *masklay, struct MaskLayerShape *masklay_shape)
Definition: mask.c:1831
struct MaskLayerShape * BKE_mask_layer_shape_duplicate(struct MaskLayerShape *masklay_shape)
Definition: mask.c:1818
void BLI_insertlinkafter(struct ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition: listbase.c:352
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
#define IN_RANGE(a, b, c)
#define UNUSED(x)
#define ELEM(...)
@ MASK_SHAPE_SELECT
#define CFRA
#define FPS
@ BEZT_OK_CHANNEL_CIRCLE
@ BEZT_OK_CHANNEL_LASSO
@ SNAP_KEYS_CURFRAME
@ SNAP_KEYS_NEARFRAME
@ SNAP_KEYS_NEARMARKER
@ SNAP_KEYS_NEARSEC
@ SELECT_INVERT
@ SELECT_SUBTRACT
@ SELECT_ADD
Read Guarded memory(de)allocation.
int ED_markers_find_nearest_marker_time(ListBase *markers, float x)
Definition: anim_markers.c:193
Scene scene
#define floorf(x)
bool keyframe_region_lasso_test(const KeyframeEdit_LassoData *data_lasso, const float xy[2])
bool keyframe_region_circle_test(const KeyframeEdit_CircleData *data_circle, const float xy[2])
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static bool snap_mask_layer_nearest(MaskLayerShape *mask_layer_shape, Scene *UNUSED(scene))
static void mask_layer_shape_select(MaskLayerShape *mask_layer_shape, short select_mode)
void ED_mask_select_frame(MaskLayer *mask_layer, int selx, short select_mode)
bool ED_masklayer_frame_select_check(MaskLayer *mask_layer)
void ED_mask_select_frames(MaskLayer *mask_layer, short select_mode)
void ED_masklayer_snap_frames(MaskLayer *mask_layer, Scene *scene, short mode)
void ED_masklayer_frames_select_region(KeyframeEditData *ked, MaskLayer *mask_layer, short tool, short select_mode)
bool ED_masklayer_frames_delete(MaskLayer *mask_layer)
void ED_masklayer_frames_select_box(MaskLayer *mask_layer, float min, float max, short select_mode)
bool ED_masklayer_frames_looper(MaskLayer *mask_layer, Scene *scene, bool(*mask_layer_shape_cb)(MaskLayerShape *, Scene *))
void ED_masklayer_frames_duplicate(MaskLayer *mask_layer)
void ED_masklayer_make_cfra_list(MaskLayer *mask_layer, ListBase *elems, bool onlysel)
static bool snap_mask_layer_nearmarker(MaskLayerShape *mask_layer_shape, Scene *scene)
static bool snap_mask_layer_nearestsec(MaskLayerShape *mask_layer_shape, Scene *scene)
void ED_masklayer_frame_select_set(MaskLayer *mask_layer, short mode)
static bool snap_mask_layer_cframe(MaskLayerShape *mask_layer_shape, Scene *scene)
#define min(a, b)
Definition: sort.c:51
float cfra
Definition: BKE_fcurve.h:56
int sel
Definition: BKE_fcurve.h:57
void * first
Definition: DNA_listBase.h:47
struct MaskLayerShape * next
ListBase splines_shapes
ListBase markers
float max
ccl_device_inline float2 floor(const float2 &a)