Skip to content

Commit 5bcf05c

Browse files
author
hoshiryu
committed
Manage keyframes using indices ;
Fix move ;
1 parent 673ecf7 commit 5bcf05c

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

src/GuiBase/KeyFrameEditor/KeyFrameEditor.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ class RA_GUIBASE_API KeyFrameEditor : public QDialog
3939
void cursorChanged( Scalar time );
4040

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

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

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

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

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

5656
public slots:
5757
/// Update the editor frame to match the given time.

src/GuiBase/KeyFrameEditor/KeyFrameEditorFrame.cpp

+45-27
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ void KeyFrameEditorFrame::setKeyFramedValue( KeyFrame* frame ) {
142142
update();
143143
}
144144

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

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

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

171171
registerKeyFrames();
172172
update();
173173
}
174174

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

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

185187
void KeyFrameEditorFrame::onDeleteKeyFrame() {
186-
auto times = m_value->getTimeSchedule();
187-
auto it = times.find( m_cursor );
188+
auto times = m_value->getTimes();
189+
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
190+
return Ra::Core::Math::areApproxEqual( t, m_cursor );
191+
} );
188192
if ( it == times.end() ) { return; }
189193

190-
emit keyFrameDeleted( m_cursor );
194+
emit keyFrameDeleted( std::distance( times.begin(), it ) );
191195

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

199-
emit keyFrameMoved( time0, time1 );
203+
auto times = m_value->getTimes();
204+
auto it = std::find_if( times.begin(), times.end(), [time0]( const auto& t ) {
205+
return Ra::Core::Math::areApproxEqual( t, time0 );
206+
} );
207+
208+
emit keyFrameMoved( std::distance( times.begin(), it ), time1 );
200209

201210
onUpdateKeyFrames( m_cursor );
202211
}
203212

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

207-
auto times = m_value->getTimeSchedule();
208-
auto it = times.find( time );
216+
auto times = m_value->getTimes();
217+
auto it = std::find_if( times.begin(), times.end(), [time]( const auto& t ) {
218+
return Ra::Core::Math::areApproxEqual( t, time );
219+
} );
209220
if ( it == times.end() ) { return; }
210221

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

213-
emit keyFramesMoved( time, offset );
224+
emit keyFramesMoved( std::distance( times.begin(), it ), offset );
214225

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

228239
Scalar dist = right - left;
229240

230-
auto times = m_value->getTimeSchedule();
241+
auto times = m_value->getTimes();
231242
auto it = times.begin();
232243
bool first = true;
233244
while ( it != times.end() )
@@ -240,7 +251,7 @@ void KeyFrameEditorFrame::deleteZone( Scalar time, Scalar time2 ) {
240251
{
241252
if ( first )
242253
{
243-
emit keyFramesMoved( keyFrame, -dist );
254+
emit keyFramesMoved( std::distance( times.begin(), it ), -dist );
244255
first = false;
245256
}
246257
}
@@ -256,7 +267,7 @@ void KeyFrameEditorFrame::deleteZone( Scalar time, Scalar time2 ) {
256267
}
257268

258269
void KeyFrameEditorFrame::onSetCursorToPreviousKeyFrame() {
259-
auto times = m_value->getTimeSchedule();
270+
auto times = m_value->getTimes();
260271
auto it = times.rbegin();
261272
while ( it != times.rend() && *it >= m_cursor )
262273
it++;
@@ -265,7 +276,7 @@ void KeyFrameEditorFrame::onSetCursorToPreviousKeyFrame() {
265276
}
266277

267278
void KeyFrameEditorFrame::onSetCursorToNextKeyFrame() {
268-
auto times = m_value->getTimeSchedule();
279+
auto times = m_value->getTimes();
269280
auto it = times.begin();
270281
while ( it != times.end() && *it <= m_cursor )
271282
it++;
@@ -454,7 +465,7 @@ void KeyFrameEditorFrame::paintEvent( QPaintEvent* ) {
454465
const int sliderH = m_ui->scrollArea->horizontalScrollBar()->value();
455466
const int areaWidth = m_ui->scrollArea->width();
456467

457-
const auto times = m_value->getTimeSchedule();
468+
const auto times = m_value->getTimes();
458469
if ( auto kf = dynamic_cast<KeyFramedValue<bool>*>( m_value ) )
459470
{
460471
if ( m_displayCurve[0] ) { drawFlat( m_curveControlPoints[0], py, path[0] ); }
@@ -628,8 +639,10 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
628639
// ------------------ RIGHT CLICK -------------------------------------
629640
else if ( event->button() == Qt::RightButton )
630641
{
631-
auto times = m_value->getTimeSchedule();
632-
auto it = times.find( m_cursor );
642+
auto times = m_value->getTimes();
643+
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
644+
return Ra::Core::Math::areApproxEqual( t, m_cursor );
645+
} );
633646
// if already on KeyFrame, move current KeyFrame
634647
// ------------------- CURSOR ON KEYFRAME -----------------------
635648
if ( it != times.end() )
@@ -639,8 +652,10 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
639652
if ( shiftDown )
640653
{
641654
// if no KeyFrame under mouse, move KeyFrame to newFrame
642-
if ( times.find( nearest ) == times.end() &&
643-
( std::abs( m_cursor - nearest ) > 1e-5_ra ) )
655+
auto it2 = std::find_if( times.begin(), times.end(), [nearest]( const auto& t ) {
656+
return Ra::Core::Math::areApproxEqual( t, nearest );
657+
} );
658+
if ( it2 == times.end() && ( std::abs( m_cursor - nearest ) > 1e-5_ra ) )
644659
{ onMoveKeyFrame( m_cursor, nearest ); }
645660
}
646661
// ---------- MULTIPLE MOVE -----------------------------------
@@ -660,15 +675,15 @@ void KeyFrameEditorFrame::mousePressEvent( QMouseEvent* event ) {
660675
// --------------- MOVE RIGHT KEYFRAME TO THE LEFT -----------------
661676
if ( shiftDown )
662677
{
663-
auto itRight = times.lower_bound( newFrame );
678+
auto itRight = std::lower_bound( times.begin(), times.end(), newFrame );
664679
// if KeyFrames on the right, remove or insert time
665680
if ( itRight != times.end() ) { onMoveKeyFrames( *itRight, newFrame - *itRight ); }
666681
}
667682
// if not shiftdown, slide first left KeyFrame to the right
668683
// ---------------- MOVE LEFT KEYFRAME TO THE RIGHT -----------
669684
else
670685
{
671-
auto itLeft = --times.lower_bound( newFrame );
686+
auto itLeft = --std::lower_bound( times.begin(), times.end(), newFrame );
672687
// if KeyFrames on the left, remove or insert time
673688
if ( itLeft != times.end() ) { onMoveKeyFrames( *itLeft, newFrame - *itLeft ); }
674689
}
@@ -803,7 +818,7 @@ Scalar KeyFrameEditorFrame::nearestStep( Scalar time ) const {
803818

804819
Scalar newCursor = time;
805820

806-
auto times = m_value->getTimeSchedule();
821+
auto times = m_value->getTimes();
807822
for ( auto keyFrame : times )
808823
{
809824
dist = qAbs( keyFrame - time );
@@ -888,7 +903,7 @@ void KeyFrameEditorFrame::registerKeyFrames( bool newValue ) {
888903
curve.clear();
889904
}
890905

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

10661081
void KeyFrameEditorFrame::updateCursorSpin() {
10671082
if ( m_ui == nullptr ) { return; }
1068-
auto times = m_value->getTimeSchedule();
1069-
if ( times.find( m_cursor ) != times.end() )
1083+
auto times = m_value->getTimes();
1084+
auto it = std::find_if( times.begin(), times.end(), [this]( const auto& t ) {
1085+
return Ra::Core::Math::areApproxEqual( t, m_cursor );
1086+
} );
1087+
if ( it != times.end() )
10701088
{
10711089
m_ui->m_currentTime_dsb->setStyleSheet( "background-color: yellow" );
10721090
m_ui->m_deleteKeyframe->setEnabled( true );

src/GuiBase/KeyFrameEditor/KeyFrameEditorFrame.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class KeyFrameEditorFrame : public QFrame
5353
* Return the set of points in time where a KeyFrame is defined for the
5454
* currently edited KeyFramedValue.
5555
*/
56-
std::set<Scalar> getKeyFrames() const;
56+
std::vector<Scalar> getKeyFrames() const;
5757

5858
public slots:
5959
/**
@@ -83,7 +83,7 @@ class KeyFrameEditorFrame : public QFrame
8383
void cursorChanged( Scalar time );
8484

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

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

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

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

109109
/// Emitted when the editor has been updated.
110110
void updated();

0 commit comments

Comments
 (0)