From 05447e7425e78610c2f309eeae47690d0e96a722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 26 Nov 2017 18:20:22 +0100 Subject: [PATCH] Sequencer - Moved point insertion/deletion to syncedit --- c-syncedit.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++++- c-syncedit.hh | 16 ++++++++++++ ui-sequencer.cc | 38 +++++---------------------- 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/c-syncedit.cc b/c-syncedit.cc index 31031e4..d8f9b1b 100644 --- a/c-syncedit.cc +++ b/c-syncedit.cc @@ -130,10 +130,12 @@ T_SyncCurve SESDScaleCurve_( const float duration{ nSeg.durations[ i ] * uSizeBefore }; nSeg.durations[ i ] = std::max( 1u , uint32_t( std::roundf( duration / uSizeAfter ) ) ); +#if 0 printf( "initial duration %f (%d units) ; new duration %f (%d units)\n" , duration , segment.durations[ i ] , uSizeAfter * nSeg.durations[ i ] , nSeg.durations[ i ] ); +#endif } } return nCurve; @@ -183,7 +185,7 @@ void SyncEditor::ReplaceCurve( // Create undo entry auto& undo{ dynamic_cast< T_UndoSyncChanges& >( Common::Undo( ).add< T_UndoSyncChanges >( ) ) }; - undo.curveReplacement( std::move( *curve ) , replacement ); + undo.curveReplacement( *curve , replacement ); Common::Sync( ).setCurve( std::move( replacement ) ); } @@ -300,3 +302,67 @@ void SyncEditor::SetSegmentType( // Replace curve Common::Sync( ).setCurve( std::move( copy ) ); } + +/*----------------------------------------------------------------------------*/ + +void SyncEditor::InsertPoint( + T_String const& id , + const uint32_t segmentIndex , + const uint32_t pointIndex ) noexcept +{ + auto& sync{ Common::Sync( ) }; + auto const* const curve{ sync.getCurve( id ) }; + if ( !curve || segmentIndex >= curve->segments.size( ) ) { + return; + } + auto const& segment{ curve->segments[ segmentIndex ] }; + if ( pointIndex == 0 || pointIndex > segment.values.size( ) + || segment.durations[ pointIndex - 1 ] == 1 ) { + return; + } + + auto c{ *curve }; + auto& ns{ c.segments[ segmentIndex ] }; + + const auto hd{ ns.durations[ pointIndex - 1 ] / 2 }; + // FIXME: this should use the actual value + const float hv{ ( ns.values[ pointIndex ] + ns.values[ pointIndex - 1 ] ) * .5f }; + + ns.durations[ pointIndex - 1 ] -= hd; + ns.durations.insert( pointIndex - 1 , hd ); + ns.values.insert( pointIndex , hv ); + + auto& undo{ dynamic_cast< T_UndoSyncChanges& >( + Common::Undo( ).add< T_UndoSyncChanges >( ) ) }; + undo.curveReplacement( *curve , c ); + sync.setCurve( std::move( c ) ); +} + +/*----------------------------------------------------------------------------*/ + +void SyncEditor::DeletePoint( + T_String const& id , + const uint32_t segmentIndex , + const uint32_t pointIndex ) noexcept +{ + auto& sync{ Common::Sync( ) }; + auto const* const curve{ sync.getCurve( id ) }; + if ( !curve || segmentIndex >= curve->segments.size( ) ) { + return; + } + auto const& segment{ curve->segments[ segmentIndex ] }; + if ( pointIndex == 0 || pointIndex >= segment.values.size( ) ) { + return; + } + + auto c{ *curve }; + auto& ns{ c.segments[ segmentIndex ] }; + ns.durations[ pointIndex ] += ns.durations[ pointIndex - 1 ]; + ns.durations.remove( pointIndex - 1 ); + ns.values.remove( pointIndex ); + + auto& undo{ dynamic_cast< T_UndoSyncChanges& >( + Common::Undo( ).add< T_UndoSyncChanges >( ) ) }; + undo.curveReplacement( *curve , c ); + sync.setCurve( std::move( c ) ); +} diff --git a/c-syncedit.hh b/c-syncedit.hh index ac0b999..7595258 100644 --- a/c-syncedit.hh +++ b/c-syncedit.hh @@ -118,4 +118,20 @@ struct SyncEditor final T_SyncCurve const& initial , uint32_t segmentIndex , T_SyncSegment::E_SegmentType newType ) noexcept; + + //---------------------------------------------------------------------- + + // Insert a point in a segment. The pointIndex parameter indicates the + // index of the new point; it must not be 0 or nbPoints( segment ) + static void InsertPoint( + T_String const& id , + uint32_t segmentIndex , + uint32_t pointIndex ) noexcept; + + // Delete a point from a segment. The point must not be the first or last + // point in the segment. + static void DeletePoint( + T_String const& id , + uint32_t segmentIndex , + uint32_t pointIndex ) noexcept; }; diff --git a/ui-sequencer.cc b/ui-sequencer.cc index d7c4a0e..79cbffc 100644 --- a/ui-sequencer.cc +++ b/ui-sequencer.cc @@ -1565,43 +1565,28 @@ void T_SyncViewImpl_::displayPointWindow( ) noexcept PopItemWidth( ); const bool canUseButtons{ !changed && selUpdate == E_ChangeType::NONE }; + const auto canInsertBefore{ pid != 0 + && segment.durations[ pid - 1 ] > 1 }; + const auto canInsertAfter{ pid != segment.durations.size( ) + && segment.durations[ pid ] > 1 }; if ( pid != 0 && pid != segment.durations.size( ) ) { Separator( ); Text( " " ); SameLine( 110 ); if ( Button( "Delete point" , ImVec2{ -1 , 0 } ) && canUseButtons ) { - // FIXME move to syncedit - auto c{ *curve }; - auto& ns{ c.segments[ sid ] }; - ns.durations[ pid ] += ns.durations[ pid - 1 ]; - ns.durations.remove( pid - 1 ); - ns.values.remove( pid ); - SyncEditor::ReplaceCurve( std::move( c ) ); + SyncEditor::DeletePoint( selId , sid , pid ); selPoint.clear( ); sub = SW_SEGMENT; } } - const auto canInsertBefore{ pid != 0 - && segment.durations[ pid - 1 ] > 1 }; - const auto canInsertAfter{ pid != segment.durations.size( ) - && segment.durations[ pid ] > 1 }; if ( canInsertAfter || canInsertBefore ) { Separator( ); if ( canInsertBefore ) { Text( " " ); SameLine( 110 ); if ( Button( "Insert before" , ImVec2{ -1 , 0 } ) && canUseButtons ) { - // FIXME move to syncedit - auto c{ *curve }; - auto& ns{ c.segments[ sid ] }; - const auto hd{ ns.durations[ pid - 1 ] / 2 }; - // FIXME: this should use the actual value - const float hv{ ( ns.values[ pid ] + ns.values[ pid - 1 ] ) * .5f }; - ns.durations[ pid - 1 ] -= hd; - ns.durations.insert( pid - 1 , hd ); - ns.values.insert( pid , hv ); - SyncEditor::ReplaceCurve( std::move( c ) ); + SyncEditor::InsertPoint( selId , sid , pid ); (*selPoint) ++; } } @@ -1609,16 +1594,7 @@ void T_SyncViewImpl_::displayPointWindow( ) noexcept Text( " " ); SameLine( 110 ); if ( Button( "Insert after" , ImVec2{ -1 , 0 } ) && canUseButtons ) { - // FIXME move to syncedit - auto c{ *curve }; - auto& ns{ c.segments[ sid ] }; - const auto hd{ ns.durations[ pid ] / 2 }; - // FIXME: this should use the actual value - const float hv{ ( ns.values[ pid ] + ns.values[ pid + 1 ] ) * .5f }; - ns.durations[ pid ] -= hd; - ns.durations.insert( pid , hd ); - ns.values.insert( pid + 1 , hv ); - SyncEditor::ReplaceCurve( std::move( c ) ); + SyncEditor::InsertPoint( selId , sid , pid + 1 ); } } }