Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mask_editaction.c « mask « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9efbef4b42b9899c794e1c0294c96a374ffae46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008, Blender Foundation
 * This is a new part of Blender
 */

/** \file
 * \ingroup edmask
 */

#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"

#include "DNA_mask_types.h"
#include "DNA_scene_types.h"

#include "BKE_fcurve.h"
#include "BKE_mask.h"

#include "ED_anim_api.h"
#include "ED_keyframes_edit.h"
#include "ED_markers.h"
#include "ED_mask.h" /* own include */

/* ***************************************** */
/* NOTE ABOUT THIS FILE:
 * This file contains code for editing Mask data in the Action Editor
 * as a 'keyframes', so that a user can adjust the timing of Mask shapekeys.
 * Therefore, this file mostly contains functions for selecting Mask frames (shapekeys).
 */
/* ***************************************** */
/* Generics - Loopers */

/* Loops over the mask-frames for a mask-layer, and applies the given callback */
bool ED_masklayer_frames_looper(MaskLayer *mask_layer,
                                Scene *scene,
                                bool (*mask_layer_shape_cb)(MaskLayerShape *, Scene *))
{
  MaskLayerShape *mask_layer_shape;

  /* error checker */
  if (mask_layer == NULL) {
    return false;
  }

  /* do loop */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    /* execute callback */
    if (mask_layer_shape_cb(mask_layer_shape, scene)) {
      return true;
    }
  }

  /* nothing to return */
  return false;
}

/* ****************************************** */
/* Data Conversion Tools */

/* make a listing all the mask-frames in a layer as cfraelems */
void ED_masklayer_make_cfra_list(MaskLayer *mask_layer, ListBase *elems, bool onlysel)
{
  MaskLayerShape *mask_layer_shape;
  CfraElem *ce;

  /* error checking */
  if (ELEM(NULL, mask_layer, elems)) {
    return;
  }

  /* loop through mask-frames, adding */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    if ((onlysel == false) || (mask_layer_shape->flag & MASK_SHAPE_SELECT)) {
      ce = MEM_callocN(sizeof(CfraElem), "CfraElem");

      ce->cfra = (float)mask_layer_shape->frame;
      ce->sel = (mask_layer_shape->flag & MASK_SHAPE_SELECT) ? 1 : 0;

      BLI_addtail(elems, ce);
    }
  }
}

/* ***************************************** */
/* Selection Tools */

/* check if one of the frames in this layer is selected */
bool ED_masklayer_frame_select_check(const MaskLayer *mask_layer)
{
  MaskLayerShape *mask_layer_shape;

  /* error checking */
  if (mask_layer == NULL) {
    return 0;
  }

  /* stop at the first one found */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
      return true;
    }
  }

  /* not found */
  return false;
}

/* helper function - select mask-frame based on SELECT_* mode */
static void mask_layer_shape_select(MaskLayerShape *mask_layer_shape, short select_mode)
{
  if (mask_layer_shape == NULL) {
    return;
  }

  switch (select_mode) {
    case SELECT_ADD:
      mask_layer_shape->flag |= MASK_SHAPE_SELECT;
      break;
    case SELECT_SUBTRACT:
      mask_layer_shape->flag &= ~MASK_SHAPE_SELECT;
      break;
    case SELECT_INVERT:
      mask_layer_shape->flag ^= MASK_SHAPE_SELECT;
      break;
  }
}

/* set all/none/invert select (like above, but with SELECT_* modes) */
void ED_mask_select_frames(MaskLayer *mask_layer, short select_mode)
{
  MaskLayerShape *mask_layer_shape;

  /* error checking */
  if (mask_layer == NULL) {
    return;
  }

  /* handle according to mode */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    mask_layer_shape_select(mask_layer_shape, select_mode);
  }
}

/* set all/none/invert select */
void ED_masklayer_frame_select_set(MaskLayer *mask_layer, short mode)
{
  /* error checking */
  if (mask_layer == NULL) {
    return;
  }

  /* now call the standard function */
  ED_mask_select_frames(mask_layer, mode);
}

/* select the frame in this layer that occurs on this frame (there should only be one at most) */
void ED_mask_select_frame(MaskLayer *mask_layer, int selx, short select_mode)
{
  MaskLayerShape *mask_layer_shape;

  if (mask_layer == NULL) {
    return;
  }

  mask_layer_shape = BKE_mask_layer_shape_find_frame(mask_layer, selx);

  if (mask_layer_shape) {
    mask_layer_shape_select(mask_layer_shape, select_mode);
  }
}

/* select the frames in this layer that occur within the bounds specified */
void ED_masklayer_frames_select_box(MaskLayer *mask_layer, float min, float max, short select_mode)
{
  MaskLayerShape *mask_layer_shape;

  if (mask_layer == NULL) {
    return;
  }

  /* only select those frames which are in bounds */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    if (IN_RANGE(mask_layer_shape->frame, min, max)) {
      mask_layer_shape_select(mask_layer_shape, select_mode);
    }
  }
}

/* select the frames in this layer that occur within the lasso/circle region specified */
void ED_masklayer_frames_select_region(KeyframeEditData *ked,
                                       MaskLayer *mask_layer,
                                       short tool,
                                       short select_mode)
{
  MaskLayerShape *mask_layer_shape;

  if (mask_layer == NULL) {
    return;
  }

  /* only select frames which are within the region */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape->next) {
    /* construct a dummy point coordinate to do this testing with */
    float pt[2] = {0};

    pt[0] = mask_layer_shape->frame;
    pt[1] = ked->channel_y;

    /* check the necessary regions */
    if (tool == BEZT_OK_CHANNEL_LASSO) {
      /* Lasso */
      if (keyframe_region_lasso_test(ked->data, pt)) {
        mask_layer_shape_select(mask_layer_shape, select_mode);
      }
    }
    else if (tool == BEZT_OK_CHANNEL_CIRCLE) {
      /* Circle */
      if (keyframe_region_circle_test(ked->data, pt)) {
        mask_layer_shape_select(mask_layer_shape, select_mode);
      }
    }
  }
}

/* ***************************************** */
/* Frame Editing Tools */

/* Delete selected frames */
bool ED_masklayer_frames_delete(MaskLayer *mask_layer)
{
  MaskLayerShape *mask_layer_shape, *mask_layer_shape_next;
  bool changed = false;

  /* error checking */
  if (mask_layer == NULL) {
    return false;
  }

  /* check for frames to delete */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = mask_layer_shape_next) {
    mask_layer_shape_next = mask_layer_shape->next;

    if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
      BKE_mask_layer_shape_unlink(mask_layer, mask_layer_shape);
      changed = true;
    }
  }

  return changed;
}

/* Duplicate selected frames from given mask-layer */
void ED_masklayer_frames_duplicate(MaskLayer *mask_layer)
{
  MaskLayerShape *mask_layer_shape, *gpfn;

  /* Error checking. */
  if (mask_layer == NULL) {
    return;
  }

  /* Duplicate selected frames. */
  for (mask_layer_shape = mask_layer->splines_shapes.first; mask_layer_shape;
       mask_layer_shape = gpfn) {
    gpfn = mask_layer_shape->next;

    /* Duplicate this frame. */
    if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
      MaskLayerShape *mask_shape_dupe;

      /* Duplicate frame, and deselect self. */
      mask_shape_dupe = BKE_mask_layer_shape_duplicate(mask_layer_shape);
      mask_layer_shape->flag &= ~MASK_SHAPE_SELECT;

      /* XXX: how to handle duplicate frames? */
      BLI_insertlinkafter(&mask_layer->splines_shapes, mask_layer_shape, mask_shape_dupe);
    }
  }
}

/* -------------------------------------- */
/* Snap Tools */

static bool snap_mask_layer_nearest(MaskLayerShape *mask_layer_shape, Scene *UNUSED(scene))
{
  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
    mask_layer_shape->frame = (int)(floor(mask_layer_shape->frame + 0.5));
  }
  return false;
}

static bool snap_mask_layer_nearestsec(MaskLayerShape *mask_layer_shape, Scene *scene)
{
  float secf = (float)FPS;
  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
    mask_layer_shape->frame = (int)(floorf(mask_layer_shape->frame / secf + 0.5f) * secf);
  }
  return false;
}

static bool snap_mask_layer_cframe(MaskLayerShape *mask_layer_shape, Scene *scene)
{
  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
    mask_layer_shape->frame = (int)CFRA;
  }
  return false;
}

static bool snap_mask_layer_nearmarker(MaskLayerShape *mask_layer_shape, Scene *scene)
{
  if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
    mask_layer_shape->frame = (int)ED_markers_find_nearest_marker_time(
        &scene->markers, (float)mask_layer_shape->frame);
  }
  return false;
}

/* snap selected frames to ... */
void ED_masklayer_snap_frames(MaskLayer *mask_layer, Scene *scene, short mode)
{
  switch (mode) {
    case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */
      ED_masklayer_frames_looper(mask_layer, scene, snap_mask_layer_nearest);
      break;
    case SNAP_KEYS_CURFRAME: /* snap to current frame */
      ED_masklayer_frames_looper(mask_layer, scene, snap_mask_layer_cframe);
      break;
    case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */
      ED_masklayer_frames_looper(mask_layer, scene, snap_mask_layer_nearmarker);
      break;
    case SNAP_KEYS_NEARSEC: /* snap to nearest second */
      ED_masklayer_frames_looper(mask_layer, scene, snap_mask_layer_nearestsec);
      break;
    default: /* just in case */
      break;
  }
}