demotool/syncedit.cc

171 lines
4.1 KiB
C++

#include "externals.hh"
#include "syncedit.hh"
#include "globals.hh"
/*= T_UndoSyncChanges ========================================================*/
void T_UndoSyncChanges::undo( ) const noexcept
{
auto& sync{ Globals::Sync( ) };
const auto n{ changes_.size( ) };
for ( auto i = 0u ; i < n ; i ++ ) {
auto const& c( changes_[ i ] );
if ( c.before ) {
sync.setCurve( *c.before );
} else {
sync.removeCurve( c.inputId );
}
}
}
void T_UndoSyncChanges::redo( ) const noexcept
{
auto& sync{ Globals::Sync( ) };
const auto n{ changes_.size( ) };
for ( auto i = 0u ; i < n ; i ++ ) {
auto const& c( changes_[ i ] );
if ( c.after ) {
sync.setCurve( *c.after );
} else {
sync.removeCurve( c.inputId );
}
}
}
/*----------------------------------------------------------------------------*/
T_UndoSyncChanges& T_UndoSyncChanges::curveCreation(
T_SyncCurve curve ) noexcept
{
#ifndef NDEBUG
{
const auto n{ changes_.size( ) };
for ( auto i = 0u ; i < n ; i ++ ) {
assert( changes_[ i ].inputId != curve.name );
}
}
#endif
changes_.addNew( true , std::move( curve ) );
return *this;
}
T_UndoSyncChanges& T_UndoSyncChanges::curveDeletion(
T_SyncCurve curve ) noexcept
{
#ifndef NDEBUG
{
const auto n{ changes_.size( ) };
for ( auto i = 0u ; i < n ; i ++ ) {
assert( changes_[ i ].inputId != curve.name );
}
}
#endif
changes_.addNew( std::move( curve ) );
return *this;
}
T_UndoSyncChanges& T_UndoSyncChanges::curveReplacement(
T_SyncCurve before ,
T_SyncCurve after ) noexcept
{
assert( before.name == after.name );
#ifndef NDEBUG
{
const auto n{ changes_.size( ) };
for ( auto i = 0u ; i < n ; i ++ ) {
assert( changes_[ i ].inputId != before.name );
}
}
#endif
changes_.addNew( std::move( before ) , std::move( after ) );
return *this;
}
/*= T_UndoDurationChanges ====================================================*/
T_UndoDurationChanges::T_UndoDurationChanges(
const uint32_t units ,
const uint32_t oldUnits ,
const float unitSize ,
const float oldUnitSize ) noexcept
: T_UndoSyncChanges( ) ,
unitsBefore_{ oldUnits } ,
unitsAfter_{ units } ,
uSizeBefore_{ oldUnitSize } ,
uSizeAfter_{ unitSize }
{ }
void T_UndoDurationChanges::undo( ) const noexcept
{
Globals::Sync( ).setDuration( uSizeBefore_ , unitsBefore_ );
T_UndoSyncChanges::undo( );
}
void T_UndoDurationChanges::redo( ) const noexcept
{
Globals::Sync( ).setDuration( uSizeAfter_ , unitsAfter_ );
T_UndoSyncChanges::redo( );
}
/*= SyncEditor ===============================================================*/
namespace {
T_SyncCurve SESDScaleCurve_(
const float uSizeBefore ,
const float uSizeAfter ,
T_SyncCurve const& curve ) noexcept
{
T_SyncCurve nCurve;
nCurve.name = curve.name;
for ( auto const& segment : curve.segments ) {
const auto nsid{ nCurve.segments.add( segment ) };
T_SyncSegment& nSeg{ nCurve.segments[ nsid ] };
const auto nd{ nSeg.durations.size( ) };
for ( auto i = 0u ; i < nd ; i ++ ) {
const float duration{ nSeg.durations[ i ] * uSizeBefore };
nSeg.durations[ i ] = std::max( 1u , uint32_t( std::roundf(
duration / uSizeAfter ) ) );
printf( "initial duration %f (%d units) ; new duration %f (%d units)\n" ,
duration , segment.durations[ i ] ,
uSizeAfter * nSeg.durations[ i ] ,
nSeg.durations[ i ] );
}
}
return nCurve;
}
} // namespace <anon>
void SyncEditor::SetDuration(
const uint32_t units ,
const float uSize ,
const bool scaleCurves ) noexcept
{
auto& sync{ Globals::Sync( ) };
const float oldUnitSize{ sync.durationUnitSize( ) };
const uint32_t oldUnits{ sync.durationUnits( ) };
if ( oldUnits == units && oldUnitSize == uSize ) {
return;
}
auto& undo{ dynamic_cast< T_UndoDurationChanges& >(
Globals::Undo( ).add< T_UndoDurationChanges >(
units , oldUnits ,
uSize , oldUnitSize ) ) };
sync.setDuration( uSize , units );
if ( !scaleCurves || uSize == oldUnitSize ) {
return;
}
auto const& curves( sync.curves( ) );
for ( auto const& curve : curves ) {
auto nc{ SESDScaleCurve_( oldUnitSize , uSize , curve ) };
undo.curveReplacement( curve , nc );
sync.setCurve( std::move( nc ) );
}
}