Blender V5.3
mask_editaction.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10#include <cstddef>
11#include <cstdlib>
12#include <cstring>
13
14#include "MEM_guardedalloc.h"
15
16#include "BLI_listbase.hh"
17#include "BLI_utildefines.hh"
18
19#include "DNA_mask_types.h"
20#include "DNA_scene_types.h"
21
22#include "BKE_mask.hh"
23
25
26#include "ED_keyframes_edit.hh"
27#include "ED_markers.hh"
28#include "ED_mask.hh" /* own include */
29
30namespace blender {
31
32/* ***************************************** */
33/* NOTE ABOUT THIS FILE:
34 * This file contains code for editing Mask data in the Action Editor
35 * as a 'keyframes', so that a user can adjust the timing of Mask shape-keys.
36 * Therefore, this file mostly contains functions for selecting Mask frames (shape-keys).
37 */
38/* ***************************************** */
39/* Generics - Loopers */
40
42 Scene *scene,
43 bool (*mask_layer_shape_cb)(MaskLayerShape *, Scene *))
44{
45 /* error checker */
46 if (mask_layer == nullptr) {
47 return false;
48 }
49
50 /* do loop */
51 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
52 /* execute callback */
53 if (mask_layer_shape_cb(&mask_layer_shape, scene)) {
54 return true;
55 }
56 }
57
58 /* nothing to return */
59 return false;
60}
61
62/* ****************************************** */
63/* Data Conversion Tools */
64
65void ED_masklayer_make_cfra_list(MaskLayer *mask_layer, ListBaseT<CfraElem> *elems, bool onlysel)
66{
67
68 /* error checking */
69 if (ELEM(nullptr, mask_layer, elems)) {
70 return;
71 }
72
73 /* loop through mask-frames, adding */
74 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
75 if ((onlysel == false) || (mask_layer_shape.flag & MASK_SHAPE_SELECT)) {
76 CfraElem *ce = MEM_new_zeroed<CfraElem>("CfraElem");
77
78 ce->cfra = float(mask_layer_shape.frame);
79 ce->sel = (mask_layer_shape.flag & MASK_SHAPE_SELECT) ? 1 : 0;
80
81 BLI_addtail(elems, ce);
82 }
83 }
84}
85
86/* ***************************************** */
87/* Selection Tools */
88
90{
91 /* error checking */
92 if (mask_layer == nullptr) {
93 return false;
94 }
95
96 /* stop at the first one found */
97 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
98 if (mask_layer_shape.flag & MASK_SHAPE_SELECT) {
99 return true;
100 }
101 }
102
103 /* not found */
104 return false;
105}
106
107/* helper function - select mask-frame based on SELECT_* mode */
108static void mask_layer_shape_select(MaskLayerShape *mask_layer_shape, short select_mode)
109{
110 if (mask_layer_shape == nullptr) {
111 return;
112 }
113
114 switch (select_mode) {
115 case SELECT_ADD:
116 mask_layer_shape->flag |= MASK_SHAPE_SELECT;
117 break;
118 case SELECT_SUBTRACT:
119 mask_layer_shape->flag &= ~MASK_SHAPE_SELECT;
120 break;
121 case SELECT_INVERT:
122 mask_layer_shape->flag ^= MASK_SHAPE_SELECT;
123 break;
124 }
125}
126
127void ED_mask_select_frames(MaskLayer *mask_layer, short select_mode)
128{
129 /* error checking */
130 if (mask_layer == nullptr) {
131 return;
132 }
133
134 /* handle according to mode */
135 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
136 mask_layer_shape_select(&mask_layer_shape, select_mode);
137 }
138}
139
140void ED_masklayer_frame_select_set(MaskLayer *mask_layer, short mode)
141{
142 /* error checking */
143 if (mask_layer == nullptr) {
144 return;
145 }
146
147 /* now call the standard function */
148 ED_mask_select_frames(mask_layer, mode);
149}
150
151void ED_mask_select_frame(MaskLayer *mask_layer, int selx, short select_mode)
152{
153 MaskLayerShape *mask_layer_shape;
154
155 if (mask_layer == nullptr) {
156 return;
157 }
158
159 mask_layer_shape = BKE_mask_layer_shape_find_frame(mask_layer, selx);
160
161 if (mask_layer_shape) {
162 mask_layer_shape_select(mask_layer_shape, select_mode);
163 }
164}
165
166void ED_masklayer_frames_select_box(MaskLayer *mask_layer, float min, float max, short select_mode)
167{
168 if (mask_layer == nullptr) {
169 return;
170 }
171
172 /* only select those frames which are in bounds */
173 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
174 if (IN_RANGE(mask_layer_shape.frame, min, max)) {
175 mask_layer_shape_select(&mask_layer_shape, select_mode);
176 }
177 }
178}
179
181 MaskLayer *mask_layer,
182 short tool,
183 short select_mode)
184{
185 if (mask_layer == nullptr) {
186 return;
187 }
188
189 /* only select frames which are within the region */
190 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes) {
191 /* construct a dummy point coordinate to do this testing with */
192 float pt[2] = {0};
193
194 pt[0] = mask_layer_shape.frame;
195 pt[1] = ked->channel_y;
196
197 /* check the necessary regions */
198 if (tool == BEZT_OK_CHANNEL_LASSO) {
199 /* Lasso */
200 if (keyframe_region_lasso_test(static_cast<KeyframeEdit_LassoData *>(ked->data), pt)) {
201 mask_layer_shape_select(&mask_layer_shape, select_mode);
202 }
203 }
204 else if (tool == BEZT_OK_CHANNEL_CIRCLE) {
205 /* Circle */
206 if (keyframe_region_circle_test(static_cast<KeyframeEdit_CircleData *>(ked->data), pt)) {
207 mask_layer_shape_select(&mask_layer_shape, select_mode);
208 }
209 }
210 }
211}
212
213/* ***************************************** */
214/* Frame Editing Tools */
215
217{
218 bool changed = false;
219
220 /* error checking */
221 if (mask_layer == nullptr) {
222 return false;
223 }
224
225 const bool mask_was_animated = BKE_mask_layer_is_animated(*mask_layer);
226
227 /* check for frames to delete */
228 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes.items_mutable()) {
229 if (mask_layer_shape.flag & MASK_SHAPE_SELECT) {
230 BKE_mask_layer_shape_unlink(mask_layer, &mask_layer_shape);
231 changed = true;
232 }
233 }
234
235 const bool mask_now_animated = BKE_mask_layer_is_animated(*mask_layer);
236
237 /* The depsgraph relations were built assuming the mask was animated, so need to update
238 * relations. */
239 if (mask_was_animated != mask_now_animated) {
241 }
242
243 return changed;
244}
245
247{
248 bool changed = false;
249
250 /* Error checking. */
251 if (mask_layer == nullptr) {
252 return changed;
253 }
254
255 const bool mask_was_animated = BKE_mask_layer_is_animated(*mask_layer);
256
257 /* Duplicate selected frames. */
258 for (MaskLayerShape &mask_layer_shape : mask_layer->splines_shapes.items_mutable()) {
259
260 /* Duplicate this frame. */
261 if (mask_layer_shape.flag & MASK_SHAPE_SELECT) {
262 MaskLayerShape *mask_shape_dupe;
263
264 /* Duplicate frame, and deselect self. */
265 mask_shape_dupe = BKE_mask_layer_shape_duplicate(&mask_layer_shape);
266 mask_layer_shape.flag &= ~MASK_SHAPE_SELECT;
267
268 /* XXX: how to handle duplicate frames? */
269 BLI_insertlinkafter(&mask_layer->splines_shapes, &mask_layer_shape, mask_shape_dupe);
270 changed = true;
271 }
272 }
273
274 const bool mask_now_animated = BKE_mask_layer_is_animated(*mask_layer);
275
276 /* The depsgraph relations were built assuming the mask was not animated, so need to update
277 * relations. */
278 if (mask_was_animated != mask_now_animated) {
280 }
281
282 return changed;
283}
284
285/* -------------------------------------- */
286/* Snap Tools */
287
288static bool snap_mask_layer_nearest(MaskLayerShape *mask_layer_shape, Scene * /*scene*/)
289{
290 if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
291 mask_layer_shape->frame = int(floor(mask_layer_shape->frame + 0.5));
292 }
293 return false;
294}
295
296static bool snap_mask_layer_nearestsec(MaskLayerShape *mask_layer_shape, Scene *scene)
297{
298 float secf = float(scene->frames_per_second());
299 if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
300 mask_layer_shape->frame = int(floorf(mask_layer_shape->frame / secf + 0.5f) * secf);
301 }
302 return false;
303}
304
305static bool snap_mask_layer_cframe(MaskLayerShape *mask_layer_shape, Scene *scene)
306{
307 if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
308 mask_layer_shape->frame = scene->r.cfra;
309 }
310 return false;
311}
312
313static bool snap_mask_layer_nearmarker(MaskLayerShape *mask_layer_shape, Scene *scene)
314{
315 if (mask_layer_shape->flag & MASK_SHAPE_SELECT) {
316 mask_layer_shape->frame = ED_markers_find_nearest_marker_time(&scene->markers,
317 float(mask_layer_shape->frame));
318 }
319 return false;
320}
321
322void ED_masklayer_snap_frames(MaskLayer *mask_layer, Scene *scene, short mode)
323{
324 switch (mode) {
325 case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */
327 break;
328 case SNAP_KEYS_CURFRAME: /* snap to current frame */
330 break;
331 case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */
333 break;
334 case SNAP_KEYS_NEARSEC: /* snap to nearest second */
336 break;
337 default: /* just in case */
338 break;
339 }
340}
341
342} // namespace blender
Read Blender memory allocation and freeing.
#define IN_RANGE(a, b, c)
#define ELEM(...)
btScalar max
#define main()
#define floor
BMesh Iterators.
static bool snap_mask_layer_nearmarker(MaskLayerShape *mask_layer_shape, Scene *scene)
MaskLayerShape * BKE_mask_layer_shape_duplicate(MaskLayerShape *masklay_shape)
static bool snap_mask_layer_nearestsec(MaskLayerShape *mask_layer_shape, Scene *scene)
bool ED_masklayer_frames_duplicate(Main *main, MaskLayer *mask_layer)
void ED_masklayer_frames_select_region(KeyframeEditData *ked, MaskLayer *mask_layer, short tool, short select_mode)
void ED_masklayer_snap_frames(MaskLayer *mask_layer, Scene *scene, short mode)
void ED_mask_select_frames(MaskLayer *mask_layer, short select_mode)
bool keyframe_region_lasso_test(const KeyframeEdit_LassoData *data_lasso, const float xy[2])
static void mask_layer_shape_select(MaskLayerShape *mask_layer_shape, short select_mode)
bool BKE_mask_layer_is_animated(MaskLayer &mask_layer)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BKE_mask_layer_shape_unlink(MaskLayer *masklay, MaskLayerShape *masklay_shape)
void BLI_insertlinkafter(ListBase *listbase, void *vprevlink, void *vnewlink) ATTR_NONNULL(1)
Definition listbase.cc:339
bool ED_masklayer_frames_looper(MaskLayer *mask_layer, Scene *scene, bool(*mask_layer_shape_cb)(MaskLayerShape *, Scene *))
bool ED_masklayer_frames_delete(Main *main, MaskLayer *mask_layer)
void ED_mask_select_frame(MaskLayer *mask_layer, int selx, short select_mode)
static bool snap_mask_layer_nearest(MaskLayerShape *mask_layer_shape, Scene *)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
bool keyframe_region_circle_test(const KeyframeEdit_CircleData *data_circle, const float xy[2])
int ED_markers_find_nearest_marker_time(ListBaseT< TimeMarker > *markers, float x)
void ED_masklayer_frames_select_box(MaskLayer *mask_layer, float min, float max, short select_mode)
MaskLayerShape * BKE_mask_layer_shape_find_frame(MaskLayer *masklay, int frame)
static bool snap_mask_layer_cframe(MaskLayerShape *mask_layer_shape, Scene *scene)
void DEG_relations_tag_update(Main *bmain)
void ED_masklayer_frame_select_set(MaskLayer *mask_layer, short mode)
bool ED_masklayer_frame_select_check(const MaskLayer *mask_layer)
void ED_masklayer_make_cfra_list(MaskLayer *mask_layer, ListBaseT< CfraElem > *elems, bool onlysel)
#define floorf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
MaskLayerShapeFlag flag
ListBaseT< MaskLayerShape > splines_shapes
ListBaseT< TimeMarker > markers
struct RenderData r