From 46aafbbf6683d20c57f3668d04ce1cf6cd14e0e8 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Thu, 19 Aug 2021 09:52:09 -0300 Subject: Cleanup: move animation snap utilities to a separate compilation unit The snap functions of animation editors were scattered in `transform_mode` and `transform_snap`. --- source/blender/editors/transform/CMakeLists.txt | 1 + .../editors/transform/transform_convert_graph.c | 2 + .../editors/transform/transform_convert_nla.c | 2 + source/blender/editors/transform/transform_mode.c | 96 ------------- source/blender/editors/transform/transform_mode.h | 3 - .../editors/transform/transform_mode_timescale.c | 2 + source/blender/editors/transform/transform_snap.c | 40 +----- source/blender/editors/transform/transform_snap.h | 17 ++- .../editors/transform/transform_snap_animation.c | 156 +++++++++++++++++++++ 9 files changed, 175 insertions(+), 144 deletions(-) create mode 100644 source/blender/editors/transform/transform_snap_animation.c (limited to 'source/blender/editors/transform') diff --git a/source/blender/editors/transform/CMakeLists.txt b/source/blender/editors/transform/CMakeLists.txt index ad0a330f0f4..e9efed3cd61 100644 --- a/source/blender/editors/transform/CMakeLists.txt +++ b/source/blender/editors/transform/CMakeLists.txt @@ -101,6 +101,7 @@ set(SRC transform_ops.c transform_orientations.c transform_snap.c + transform_snap_animation.c transform_snap_object.c transform_snap_sequencer.c diff --git a/source/blender/editors/transform/transform_convert_graph.c b/source/blender/editors/transform/transform_convert_graph.c index a6cbbb299ac..c0acdd89b02 100644 --- a/source/blender/editors/transform/transform_convert_graph.c +++ b/source/blender/editors/transform/transform_convert_graph.c @@ -41,6 +41,8 @@ #include "transform.h" #include "transform_convert.h" +#include "transform_snap.h" + #include "transform_mode.h" typedef struct TransDataGraph { diff --git a/source/blender/editors/transform/transform_convert_nla.c b/source/blender/editors/transform/transform_convert_nla.c index f96f2e93bbc..4d98b7a489f 100644 --- a/source/blender/editors/transform/transform_convert_nla.c +++ b/source/blender/editors/transform/transform_convert_nla.c @@ -42,6 +42,8 @@ #include "transform.h" #include "transform_convert.h" +#include "transform_snap.h" + #include "transform_mode.h" /** Used for NLA transform (stored in #TransData.extra pointer). */ diff --git a/source/blender/editors/transform/transform_mode.c b/source/blender/editors/transform/transform_mode.c index 362ee179e7d..8df95222fa1 100644 --- a/source/blender/editors/transform/transform_mode.c +++ b/source/blender/editors/transform/transform_mode.c @@ -1067,102 +1067,6 @@ void ElementResize(const TransInfo *t, /** \} */ -/* -------------------------------------------------------------------- */ -/** \name Transform (Frame Utils) - * \{ */ - -/** - * This function returns the snapping 'mode' for Animation Editors only. - * We cannot use the standard snapping due to NLA-strip scaling complexities. - * - * TODO: these modifier checks should be key-mappable. - */ -short getAnimEdit_SnapMode(TransInfo *t) -{ - short autosnap = SACTSNAP_OFF; - - if (t->spacetype == SPACE_ACTION) { - SpaceAction *saction = (SpaceAction *)t->area->spacedata.first; - - if (saction) { - autosnap = saction->autosnap; - } - } - else if (t->spacetype == SPACE_GRAPH) { - SpaceGraph *sipo = (SpaceGraph *)t->area->spacedata.first; - - if (sipo) { - autosnap = sipo->autosnap; - } - } - else if (t->spacetype == SPACE_NLA) { - SpaceNla *snla = (SpaceNla *)t->area->spacedata.first; - - if (snla) { - autosnap = snla->autosnap; - } - } - else { - autosnap = SACTSNAP_OFF; - } - - /* toggle autosnap on/off - * - when toggling on, prefer nearest frame over 1.0 frame increments - */ - if (t->modifiers & MOD_SNAP_INVERT) { - if (autosnap) { - autosnap = SACTSNAP_OFF; - } - else { - autosnap = SACTSNAP_FRAME; - } - } - - return autosnap; -} - -/* This function is used by Animation Editor specific transform functions to do - * the Snap Keyframe to Nearest Frame/Marker - */ -void doAnimEdit_SnapFrame( - TransInfo *t, TransData *td, TransData2D *td2d, AnimData *adt, short autosnap) -{ - if (autosnap != SACTSNAP_OFF) { - float val; - - /* convert frame to nla-action time (if needed) */ - if (adt && (t->spacetype != SPACE_SEQ)) { - val = BKE_nla_tweakedit_remap(adt, *(td->val), NLATIME_CONVERT_MAP); - } - else { - val = *(td->val); - } - - snapFrameTransform(t, autosnap, true, val, &val); - - /* convert frame out of nla-action time */ - if (adt && (t->spacetype != SPACE_SEQ)) { - *(td->val) = BKE_nla_tweakedit_remap(adt, val, NLATIME_CONVERT_UNMAP); - } - else { - *(td->val) = val; - } - } - - /* If the handles are to be moved too - * (as side-effect of keyframes moving, to keep the general effect) - * offset them by the same amount so that the general angles are maintained - * (i.e. won't change while handles are free-to-roam and keyframes are snap-locked). - */ - if ((td->flag & TD_MOVEHANDLE1) && td2d->h1) { - td2d->h1[0] = td2d->ih1[0] + *td->val - td->ival; - } - if ((td->flag & TD_MOVEHANDLE2) && td2d->h2) { - td2d->h2[0] = td2d->ih2[0] + *td->val - td->ival; - } -} -/** \} */ - /* -------------------------------------------------------------------- */ /** \name Transform Mode Initialization * \{ */ diff --git a/source/blender/editors/transform/transform_mode.h b/source/blender/editors/transform/transform_mode.h index 027fb6b6982..d8601000ddb 100644 --- a/source/blender/editors/transform/transform_mode.h +++ b/source/blender/editors/transform/transform_mode.h @@ -63,9 +63,6 @@ void ElementResize(const TransInfo *t, const TransDataContainer *tc, TransData *td, const float mat[3][3]); -short getAnimEdit_SnapMode(TransInfo *t); -void doAnimEdit_SnapFrame( - TransInfo *t, TransData *td, TransData2D *td2d, struct AnimData *adt, short autosnap); void transform_mode_init(TransInfo *t, struct wmOperator *op, const int mode); void transform_mode_default_modal_orientation_set(TransInfo *t, int type); diff --git a/source/blender/editors/transform/transform_mode_timescale.c b/source/blender/editors/transform/transform_mode_timescale.c index 98ffc0abbd4..4123663e849 100644 --- a/source/blender/editors/transform/transform_mode_timescale.c +++ b/source/blender/editors/transform/transform_mode_timescale.c @@ -40,6 +40,8 @@ #include "transform.h" #include "transform_convert.h" +#include "transform_snap.h" + #include "transform_mode.h" /* -------------------------------------------------------------------- */ diff --git a/source/blender/editors/transform/transform_snap.c b/source/blender/editors/transform/transform_snap.c index 656a1e5d9c7..ea08c8912e9 100644 --- a/source/blender/editors/transform/transform_snap.c +++ b/source/blender/editors/transform/transform_snap.c @@ -1461,47 +1461,9 @@ bool snapNodesTransform( /** \} */ /* -------------------------------------------------------------------- */ -/** \name snap Frames +/** \name snap Grid * \{ */ -/* This function is used by Animation Editor specific transform functions to do - * the Snap Keyframe to Nearest Frame/Marker - */ -void snapFrameTransform(TransInfo *t, - const eAnimEdit_AutoSnap autosnap, - const bool is_frame_value, - const float delta, - float *r_val) -{ - double val = delta; - switch (autosnap) { - case SACTSNAP_STEP: - case SACTSNAP_FRAME: - val = floor(val + 0.5); - break; - case SACTSNAP_MARKER: - /* snap to nearest marker */ - /* TODO: need some more careful checks for where data comes from. */ - val = ED_markers_find_nearest_marker_time(&t->scene->markers, (float)val); - break; - case SACTSNAP_SECOND: - case SACTSNAP_TSTEP: { - /* second step */ - const Scene *scene = t->scene; - const double secf = FPS; - val = floor((val / secf) + 0.5); - if (is_frame_value) { - val *= secf; - } - break; - } - case SACTSNAP_OFF: { - break; - } - } - *r_val = (float)val; -} - static void snap_grid_apply( TransInfo *t, const int max_index, const float grid_dist, const float loc[3], float r_out[3]) { diff --git a/source/blender/editors/transform/transform_snap.h b/source/blender/editors/transform/transform_snap.h index 6dfaeab93e6..20cc590d7cc 100644 --- a/source/blender/editors/transform/transform_snap.h +++ b/source/blender/editors/transform/transform_snap.h @@ -45,12 +45,6 @@ bool snapNodesTransform(struct TransInfo *t, float r_loc[2], float *r_dist_px, char *r_node_border); -void snapFrameTransform(struct TransInfo *t, - const eAnimEdit_AutoSnap autosnap, - const bool is_frame_value, - const float delta, - /* return args */ - float *r_val); bool transformModeUseSnap(const TransInfo *t); @@ -86,3 +80,14 @@ struct TransSeqSnapData *transform_snap_sequencer_data_alloc(const TransInfo *t) void transform_snap_sequencer_data_free(struct TransSeqSnapData *data); bool transform_snap_sequencer_calc(struct TransInfo *t); void transform_snap_sequencer_apply_translate(TransInfo *t, float *vec); + +/* transform_snap_animation.c */ +short getAnimEdit_SnapMode(TransInfo *t); +void snapFrameTransform(struct TransInfo *t, + const eAnimEdit_AutoSnap autosnap, + const bool is_frame_value, + const float delta, + /* return args */ + float *r_val); +void doAnimEdit_SnapFrame( + TransInfo *t, TransData *td, TransData2D *td2d, struct AnimData *adt, short autosnap); diff --git a/source/blender/editors/transform/transform_snap_animation.c b/source/blender/editors/transform/transform_snap_animation.c new file mode 100644 index 00000000000..ac7c47e408f --- /dev/null +++ b/source/blender/editors/transform/transform_snap_animation.c @@ -0,0 +1,156 @@ +/* + * 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) 2001-2002 by NaN Holding BV. + * All rights reserved. + */ + +/** \file + * \ingroup edtransform + */ + +#include "DNA_anim_types.h" + +#include "BLI_math.h" + +#include "BKE_context.h" +#include "BKE_nla.h" + +#include "ED_markers.h" +#include "ED_screen.h" + +#include "transform.h" +#include "transform_snap.h" + +/* -------------------------------------------------------------------- */ +/** \name Snappint in Anim Editors + * \{ */ + +/** + * This function returns the snapping 'mode' for Animation Editors only. + * We cannot use the standard snapping due to NLA-strip scaling complexities. + * + * TODO: these modifier checks should be key-mappable. + */ +short getAnimEdit_SnapMode(TransInfo *t) +{ + short autosnap = SACTSNAP_OFF; + + if (t->spacetype == SPACE_ACTION) { + SpaceAction *saction = (SpaceAction *)t->area->spacedata.first; + + if (saction) { + autosnap = saction->autosnap; + } + } + else if (t->spacetype == SPACE_GRAPH) { + SpaceGraph *sipo = (SpaceGraph *)t->area->spacedata.first; + + if (sipo) { + autosnap = sipo->autosnap; + } + } + else if (t->spacetype == SPACE_NLA) { + SpaceNla *snla = (SpaceNla *)t->area->spacedata.first; + + if (snla) { + autosnap = snla->autosnap; + } + } + else { + autosnap = SACTSNAP_OFF; + } + + /* toggle autosnap on/off + * - when toggling on, prefer nearest frame over 1.0 frame increments + */ + if (t->modifiers & MOD_SNAP_INVERT) { + if (autosnap) { + autosnap = SACTSNAP_OFF; + } + else { + autosnap = SACTSNAP_FRAME; + } + } + + return autosnap; +} + +void snapFrameTransform(TransInfo *t, + const eAnimEdit_AutoSnap autosnap, + const bool is_frame_value, + const float delta, + /* return args */ + float *r_val) +{ + double val = delta; + switch (autosnap) { + case SACTSNAP_STEP: + case SACTSNAP_FRAME: + val = floor(val + 0.5); + break; + case SACTSNAP_MARKER: + /* snap to nearest marker */ + /* TODO: need some more careful checks for where data comes from. */ + val = ED_markers_find_nearest_marker_time(&t->scene->markers, (float)val); + break; + case SACTSNAP_SECOND: + case SACTSNAP_TSTEP: { + /* second step */ + const Scene *scene = t->scene; + const double secf = FPS; + val = floor((val / secf) + 0.5); + if (is_frame_value) { + val *= secf; + } + break; + } + case SACTSNAP_OFF: { + break; + } + } + *r_val = (float)val; +} + +/* This function is used by Animation Editor specific transform functions to do + * the Snap Keyframe to Nearest Frame/Marker + */ +void doAnimEdit_SnapFrame( + TransInfo *t, TransData *td, TransData2D *td2d, AnimData *adt, short autosnap) +{ + if (autosnap != SACTSNAP_OFF) { + float val; + + /* convert frame to nla-action time (if needed) */ + if (adt && (t->spacetype != SPACE_SEQ)) { + val = BKE_nla_tweakedit_remap(adt, *(td->val), NLATIME_CONVERT_MAP); + } + else { + val = *(td->val); + } + + snapFrameTransform(t, autosnap, true, val, &val); + + /* convert frame out of nla-action time */ + if (adt && (t->spacetype != SPACE_SEQ)) { + *(td->val) = BKE_nla_tweakedit_remap(adt, val, NLATIME_CONVERT_UNMAP); + } + else { + *(td->val) = val; + } + } +} + +/** \} */ -- cgit v1.2.3