Sequencer - Moved point insertion/deletion to syncedit

This commit is contained in:
Emmanuel BENOîT 2017-11-26 18:20:22 +01:00
parent aef7546a57
commit 05447e7425
3 changed files with 90 additions and 32 deletions

View file

@ -130,10 +130,12 @@ T_SyncCurve SESDScaleCurve_(
const float duration{ nSeg.durations[ i ] * uSizeBefore }; const float duration{ nSeg.durations[ i ] * uSizeBefore };
nSeg.durations[ i ] = std::max( 1u , uint32_t( std::roundf( nSeg.durations[ i ] = std::max( 1u , uint32_t( std::roundf(
duration / uSizeAfter ) ) ); duration / uSizeAfter ) ) );
#if 0
printf( "initial duration %f (%d units) ; new duration %f (%d units)\n" , printf( "initial duration %f (%d units) ; new duration %f (%d units)\n" ,
duration , segment.durations[ i ] , duration , segment.durations[ i ] ,
uSizeAfter * nSeg.durations[ i ] , uSizeAfter * nSeg.durations[ i ] ,
nSeg.durations[ i ] ); nSeg.durations[ i ] );
#endif
} }
} }
return nCurve; return nCurve;
@ -183,7 +185,7 @@ void SyncEditor::ReplaceCurve(
// Create undo entry // Create undo entry
auto& undo{ dynamic_cast< T_UndoSyncChanges& >( auto& undo{ dynamic_cast< T_UndoSyncChanges& >(
Common::Undo( ).add< T_UndoSyncChanges >( ) ) }; Common::Undo( ).add< T_UndoSyncChanges >( ) ) };
undo.curveReplacement( std::move( *curve ) , replacement ); undo.curveReplacement( *curve , replacement );
Common::Sync( ).setCurve( std::move( replacement ) ); Common::Sync( ).setCurve( std::move( replacement ) );
} }
@ -300,3 +302,67 @@ void SyncEditor::SetSegmentType(
// Replace curve // Replace curve
Common::Sync( ).setCurve( std::move( copy ) ); 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 ) );
}

View file

@ -118,4 +118,20 @@ struct SyncEditor final
T_SyncCurve const& initial , T_SyncCurve const& initial ,
uint32_t segmentIndex , uint32_t segmentIndex ,
T_SyncSegment::E_SegmentType newType ) noexcept; 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;
}; };

View file

@ -1565,43 +1565,28 @@ void T_SyncViewImpl_::displayPointWindow( ) noexcept
PopItemWidth( ); PopItemWidth( );
const bool canUseButtons{ !changed && selUpdate == E_ChangeType::NONE }; 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( ) ) { if ( pid != 0 && pid != segment.durations.size( ) ) {
Separator( ); Separator( );
Text( " " ); Text( " " );
SameLine( 110 ); SameLine( 110 );
if ( Button( "Delete point" , ImVec2{ -1 , 0 } ) && canUseButtons ) { if ( Button( "Delete point" , ImVec2{ -1 , 0 } ) && canUseButtons ) {
// FIXME move to syncedit SyncEditor::DeletePoint( selId , sid , pid );
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 ) );
selPoint.clear( ); selPoint.clear( );
sub = SW_SEGMENT; 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 ) { if ( canInsertAfter || canInsertBefore ) {
Separator( ); Separator( );
if ( canInsertBefore ) { if ( canInsertBefore ) {
Text( " " ); Text( " " );
SameLine( 110 ); SameLine( 110 );
if ( Button( "Insert before" , ImVec2{ -1 , 0 } ) && canUseButtons ) { if ( Button( "Insert before" , ImVec2{ -1 , 0 } ) && canUseButtons ) {
// FIXME move to syncedit SyncEditor::InsertPoint( selId , sid , pid );
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 ) );
(*selPoint) ++; (*selPoint) ++;
} }
} }
@ -1609,16 +1594,7 @@ void T_SyncViewImpl_::displayPointWindow( ) noexcept
Text( " " ); Text( " " );
SameLine( 110 ); SameLine( 110 );
if ( Button( "Insert after" , ImVec2{ -1 , 0 } ) && canUseButtons ) { if ( Button( "Insert after" , ImVec2{ -1 , 0 } ) && canUseButtons ) {
// FIXME move to syncedit SyncEditor::InsertPoint( selId , sid , pid + 1 );
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 ) );
} }
} }
} }