Cleanup: move animation snap utilities to a separate compilation unit

The snap functions of animation editors were scattered in
`transform_mode` and `transform_snap`.
This commit is contained in:
Germano Cavalcante 2021-08-19 09:52:09 -03:00
parent e648e38887
commit 46aafbbf66
9 changed files with 175 additions and 144 deletions

View File

@ -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

View File

@ -41,6 +41,8 @@
#include "transform.h"
#include "transform_convert.h"
#include "transform_snap.h"
#include "transform_mode.h"
typedef struct TransDataGraph {

View File

@ -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). */

View File

@ -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
* \{ */

View File

@ -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);

View File

@ -40,6 +40,8 @@
#include "transform.h"
#include "transform_convert.h"
#include "transform_snap.h"
#include "transform_mode.h"
/* -------------------------------------------------------------------- */

View File

@ -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])
{

View File

@ -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);

View File

@ -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;
}
}
}
/** \} */