Skip to content

Commit

Permalink
Manage keyframes using indices ;
Browse files Browse the repository at this point in the history
Fix move ;
  • Loading branch information
hoshiryu committed Jun 5, 2020
1 parent 673ecf7 commit 5bcf05c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/GuiBase/KeyFrameEditor/KeyFrameEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ class RA_GUIBASE_API KeyFrameEditor : public QDialog
void cursorChanged( Scalar time );

/// Emitted when the user changes a keyframe's value in the editor frame.
void keyFrameChanged( Scalar time );
void keyFrameChanged( size_t i );

/// Emitted when the user adds a keyframe to the KeyFramedValue in the editor frame.
void keyFrameAdded( Scalar time );

/// Emitted when the user removes a keyframe from the KeyFramedValue in the editor frame.
void keyFrameDeleted( Scalar time );
void keyFrameDeleted( size_t i );

/// Emitted when the user changes a keyframe's time in the editor frame.
void keyFrameMoved( Scalar time0, Scalar time1 );
void keyFrameMoved( size_t i, Scalar time1 );

/// Emitted when the user offsets keyframes time in the editor frame.
void keyFramesMoved( Scalar time, Scalar offset );
void keyFramesMoved( size_t first, Scalar offset );

public slots:
/// Update the editor frame to match the given time.
Expand Down
72 changes: 45 additions & 27 deletions src/GuiBase/KeyFrameEditor/KeyFrameEditorFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ void KeyFrameEditorFrame::setKeyFramedValue( KeyFrame* frame ) {
update();
}

std::set<Scalar> KeyFrameEditorFrame::getKeyFrames() const {
return m_value->getTimeSchedule();
std::vector<Scalar> KeyFrameEditorFrame::getKeyFrames() const {
return m_value->getTimes();
}

void KeyFrameEditorFrame::onChangeCursor( Scalar time, bool internal ) {
Expand All @@ -166,15 +166,17 @@ void KeyFrameEditorFrame::onUpdateKeyFrames( Scalar currentTime ) {
m_cursor = currentTime;

updateCursorSpin();
m_ui->m_nbKeyFrame->setText( QString::number( m_value->getTimeSchedule().size() ) );
m_ui->m_nbKeyFrame->setText( QString::number( m_value->getTimes().size() ) );

registerKeyFrames();
update();
}

void KeyFrameEditorFrame::onAddKeyFrame() {
auto times = m_value->getTimeSchedule();
auto it = times.find( m_cursor );
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, m_cursor );
} );
if ( it != times.end() ) { return; }

emit keyFrameAdded( m_cursor );
Expand All @@ -183,11 +185,13 @@ void KeyFrameEditorFrame::onAddKeyFrame() {
}

void KeyFrameEditorFrame::onDeleteKeyFrame() {
auto times = m_value->getTimeSchedule();
auto it = times.find( m_cursor );
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, m_cursor );
} );
if ( it == times.end() ) { return; }

emit keyFrameDeleted( m_cursor );
emit keyFrameDeleted( std::distance( times.begin(), it ) );

onUpdateKeyFrames( m_cursor );
}
Expand All @@ -196,21 +200,28 @@ void KeyFrameEditorFrame::onMoveKeyFrame( Scalar time0, Scalar time1 ) {
if ( Ra::Core::Math::areApproxEqual( time0, time1 ) ) { return; }
m_cursor = time1;

emit keyFrameMoved( time0, time1 );
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [time0]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, time0 );
} );

emit keyFrameMoved( std::distance( times.begin(), it ), time1 );

onUpdateKeyFrames( m_cursor );
}

void KeyFrameEditorFrame::onMoveKeyFrames( Scalar time, Scalar offset ) {
if ( Ra::Core::Math::areApproxEqual( offset, 0_ra ) ) { return; }

auto times = m_value->getTimeSchedule();
auto it = times.find( time );
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [time]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, time );
} );
if ( it == times.end() ) { return; }

Scalar left = ( offset > 0 ) ? ( time ) : ( time + offset );

emit keyFramesMoved( time, offset );
emit keyFramesMoved( std::distance( times.begin(), it ), offset );

if ( m_cursor >= left )
{
Expand All @@ -227,7 +238,7 @@ void KeyFrameEditorFrame::deleteZone( Scalar time, Scalar time2 ) {

Scalar dist = right - left;

auto times = m_value->getTimeSchedule();
auto times = m_value->getTimes();
auto it = times.begin();
bool first = true;
while ( it != times.end() )
Expand All @@ -240,7 +251,7 @@ void KeyFrameEditorFrame::deleteZone( Scalar time, Scalar time2 ) {
{
if ( first )
{
emit keyFramesMoved( keyFrame, -dist );
emit keyFramesMoved( std::distance( times.begin(), it ), -dist );
first = false;
}
}
Expand All @@ -256,7 +267,7 @@ void KeyFrameEditorFrame::deleteZone( Scalar time, Scalar time2 ) {
}

void KeyFrameEditorFrame::onSetCursorToPreviousKeyFrame() {
auto times = m_value->getTimeSchedule();
auto times = m_value->getTimes();
auto it = times.rbegin();
while ( it != times.rend() && *it >= m_cursor )
it++;
Expand All @@ -265,7 +276,7 @@ void KeyFrameEditorFrame::onSetCursorToPreviousKeyFrame() {
}

void KeyFrameEditorFrame::onSetCursorToNextKeyFrame() {
auto times = m_value->getTimeSchedule();
auto times = m_value->getTimes();
auto it = times.begin();
while ( it != times.end() && *it <= m_cursor )
it++;
Expand Down Expand Up @@ -454,7 +465,7 @@ void KeyFrameEditorFrame::paintEvent( QPaintEvent* ) {
const int sliderH = m_ui->scrollArea->horizontalScrollBar()->value();
const int areaWidth = m_ui->scrollArea->width();

const auto times = m_value->getTimeSchedule();
const auto times = m_value->getTimes();
if ( auto kf = dynamic_cast<KeyFramedValue<bool>*>( m_value ) )
{
if ( m_displayCurve[0] ) { drawFlat( m_curveControlPoints[0], py, path[0] ); }
Expand Down Expand Up @@ -628,8 +639,10 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
// ------------------ RIGHT CLICK -------------------------------------
else if ( event->button() == Qt::RightButton )
{
auto times = m_value->getTimeSchedule();
auto it = times.find( m_cursor );
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, m_cursor );
} );
// if already on KeyFrame, move current KeyFrame
// ------------------- CURSOR ON KEYFRAME -----------------------
if ( it != times.end() )
Expand All @@ -639,8 +652,10 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
if ( shiftDown )
{
// if no KeyFrame under mouse, move KeyFrame to newFrame
if ( times.find( nearest ) == times.end() &&
( std::abs( m_cursor - nearest ) > 1e-5_ra ) )
auto it2 = std::find_if( times.begin(), times.end(), [nearest]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, nearest );
} );
if ( it2 == times.end() && ( std::abs( m_cursor - nearest ) > 1e-5_ra ) )
{ onMoveKeyFrame( m_cursor, nearest ); }
}
// ---------- MULTIPLE MOVE -----------------------------------
Expand All @@ -660,15 +675,15 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
// --------------- MOVE RIGHT KEYFRAME TO THE LEFT -----------------
if ( shiftDown )
{
auto itRight = times.lower_bound( newFrame );
auto itRight = std::lower_bound( times.begin(), times.end(), newFrame );
// if KeyFrames on the right, remove or insert time
if ( itRight != times.end() ) { onMoveKeyFrames( *itRight, newFrame - *itRight ); }
}
// if not shiftdown, slide first left KeyFrame to the right
// ---------------- MOVE LEFT KEYFRAME TO THE RIGHT -----------
else
{
auto itLeft = --times.lower_bound( newFrame );
auto itLeft = --std::lower_bound( times.begin(), times.end(), newFrame );
// if KeyFrames on the left, remove or insert time
if ( itLeft != times.end() ) { onMoveKeyFrames( *itLeft, newFrame - *itLeft ); }
}
Expand Down Expand Up @@ -803,7 +818,7 @@ Scalar KeyFrameEditorFrame::nearestStep( Scalar time ) const {

Scalar newCursor = time;

auto times = m_value->getTimeSchedule();
auto times = m_value->getTimes();
for ( auto keyFrame : times )
{
dist = qAbs( keyFrame - time );
Expand Down Expand Up @@ -888,7 +903,7 @@ void KeyFrameEditorFrame::registerKeyFrames( bool newValue ) {
curve.clear();
}

auto times = m_value->getTimeSchedule();
auto times = m_value->getTimes();
m_ui->m_nbKeyFrame->setText( QString::number( times.size() ) );
if ( auto kf = dynamic_cast<KeyFramedValue<bool>*>( m_value ) )
{
Expand Down Expand Up @@ -1065,8 +1080,11 @@ void KeyFrameEditorFrame::registerKeyFrames( bool newValue ) {

void KeyFrameEditorFrame::updateCursorSpin() {
if ( m_ui == nullptr ) { return; }
auto times = m_value->getTimeSchedule();
if ( times.find( m_cursor ) != times.end() )
auto times = m_value->getTimes();
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
return Ra::Core::Math::areApproxEqual( t, m_cursor );
} );
if ( it != times.end() )
{
m_ui->m_currentTime_dsb->setStyleSheet( "background-color: yellow" );
m_ui->m_deleteKeyframe->setEnabled( true );
Expand Down
10 changes: 5 additions & 5 deletions src/GuiBase/KeyFrameEditor/KeyFrameEditorFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class KeyFrameEditorFrame : public QFrame
* Return the set of points in time where a KeyFrame is defined for the
* currently edited KeyFramedValue.
*/
std::set<Scalar> getKeyFrames() const;
std::vector<Scalar> getKeyFrames() const;

public slots:
/**
Expand Down Expand Up @@ -83,7 +83,7 @@ class KeyFrameEditorFrame : public QFrame
void cursorChanged( Scalar time );

/// Emitted when the user changes a KeyFrame's value.
void keyFrameChanged( Scalar time );
void keyFrameChanged( size_t i );

/// Emitted when the user adds a KeyFrame to the KeyFramedValue.
void keyFrameAdded( Scalar time );
Expand All @@ -94,17 +94,17 @@ class KeyFrameEditorFrame : public QFrame
* - the user deleting an entire timezone containing a KeyFrame.
* \note Emitted for each suppressed KeyFrame.
*/
void keyFrameDeleted( Scalar time );
void keyFrameDeleted( size_t i );

/// Emitted when the user changes a KeyFrame's time.
void keyFrameMoved( Scalar time0, Scalar time1 );
void keyFrameMoved( size_t i, Scalar time1 );

/**
* Emitted when a KeyFrame has been added through
* - the user offsetting a set of keyframes.
* - the user deleting an entire timezone preceding KeyFrames.
*/
void keyFramesMoved( Scalar time, Scalar offset );
void keyFramesMoved( size_t first, Scalar offset );

/// Emitted when the editor has been updated.
void updated();
Expand Down

0 comments on commit 5bcf05c

Please sign in to comment.