1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-11-14 17:14:07 +01:00

Some refactoring of SubViewAdjustHandle

This commit is contained in:
Paul Licameli
2020-01-15 12:46:28 -05:00
parent 468d48b8dd
commit 1fa41808c2

View File

@@ -111,6 +111,9 @@ struct SubViewAdjuster
mFirstSubView = first - begin; mFirstSubView = first - begin;
} }
size_t NVisible() const
{ return mPermutation.size() - mFirstSubView; }
bool ModifyPermutation( bool top ) bool ModifyPermutation( bool top )
{ {
bool rotated = false; bool rotated = false;
@@ -149,15 +152,20 @@ struct SubViewAdjuster
return rotated; return rotated;
} }
std::pair< size_t, bool > size_t FindIndex( WaveTrackSubView &subView ) const
HitTest( WaveTrackSubView &subView,
wxCoord yy, wxCoord top, wxCoord height )
{ {
const auto begin = mPermutation.begin(), end = mPermutation.end(); const auto begin = mPermutation.begin(), end = mPermutation.end();
auto iter = std::find_if( begin, end, [&](size_t ii){ auto iter = std::find_if( begin, end, [&](size_t ii){
return mSubViews[ ii ].get() == &subView; return mSubViews[ ii ].get() == &subView;
} ); } );
auto index = iter - begin; return iter - begin;
}
std::pair< size_t, bool >
HitTest( WaveTrackSubView &subView,
wxCoord yy, wxCoord top, wxCoord height )
{
const auto index = FindIndex( subView );
auto size = mPermutation.size(); auto size = mPermutation.size();
if ( index < (int)size ) { if ( index < (int)size ) {
yy -= top; yy -= top;
@@ -172,6 +180,30 @@ struct SubViewAdjuster
return { size, false }; // not hit return { size, false }; // not hit
} }
std::vector<wxCoord> ComputeHeights( wxCoord totalHeight )
{
// Compute integer-valued heights
float total = 0;
for (const auto index : mPermutation ) {
const auto &placement = mOrigPlacements[ index ];
total += std::max( 0.f, placement.fraction );
}
float partial = 0;
wxCoord lastCoord = 0;
std::vector<wxCoord> result;
for (const auto index : mPermutation ) {
const auto &placement = mOrigPlacements[ index ];
auto fraction = std::max( 0.f, placement.fraction );
wxCoord coord = ( (partial + fraction ) / total ) * totalHeight;
auto height = coord - lastCoord;
result.emplace_back( height );
mNewPlacements[ index ].fraction = height;
lastCoord = coord;
partial += fraction;
}
return result;
}
void UpdateViews( bool rollback ) void UpdateViews( bool rollback )
{ {
auto pView = mwView.lock(); auto pView = mwView.lock();
@@ -256,26 +288,7 @@ public:
const auto height = rect.GetHeight(); const auto height = rect.GetHeight();
mOrigHeight = height; mOrigHeight = height;
// Compute integer-valued heights mOrigHeights = mAdjuster.ComputeHeights( mViewHeight );
{
float total = 0;
for (const auto index : mAdjuster.mPermutation ) {
const auto &placement = mAdjuster.mOrigPlacements[ index ];
total += std::max( 0.f, placement.fraction );
}
float partial = 0;
wxCoord lastCoord = 0;
for (const auto index : mAdjuster.mPermutation ) {
const auto &placement = mAdjuster.mOrigPlacements[ index ];
auto fraction = std::max( 0.f, placement.fraction );
wxCoord coord = ( (partial + fraction ) / total ) * mViewHeight;
auto height = coord - lastCoord;
mOrigHeights.emplace_back( height );
mAdjuster.mNewPlacements[ index ].fraction = height;
lastCoord = coord;
partial += fraction;
}
}
// Find the total height of the sub-views that may resize // Find the total height of the sub-views that may resize
mTotalHeight = 0; mTotalHeight = 0;
@@ -435,9 +448,8 @@ std::pair<
auto pWaveTrackView = mwWaveTrackView.lock(); auto pWaveTrackView = mwWaveTrackView.lock();
if ( pWaveTrackView && !state.state.HasModifiers() ) { if ( pWaveTrackView && !state.state.HasModifiers() ) {
auto pHandle = SubViewAdjustHandle::HitTest( if ( auto pHandle = SubViewAdjustHandle::HitTest(
*pWaveTrackView, *this, state ); *pWaveTrackView, *this, state ) )
if (pHandle)
results.second.push_back( pHandle ); results.second.push_back( pHandle );
} }
if (auto result = CutlineHandle::HitTest( if (auto result = CutlineHandle::HitTest(