From d14c6ba71fa42de4a69e263c16d1880b2b189226 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 11 Sep 2020 10:33:12 -0700 Subject: [PATCH] Keep snap line alignment before new snaps Also fixes the alignment line that was supposed to be dashed and not solid. --- common/origin_viewitem.cpp | 1 - eeschema/tools/ee_grid_helper.cpp | 50 +++++++++++++++++-------------- pcbnew/tools/grid_helper.cpp | 50 ++++++++++++++++++------------- 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/common/origin_viewitem.cpp b/common/origin_viewitem.cpp index 1f3aa63177..78b86bbd0c 100644 --- a/common/origin_viewitem.cpp +++ b/common/origin_viewitem.cpp @@ -127,7 +127,6 @@ void ORIGIN_VIEWITEM::ViewDraw( int, VIEW* aView ) const start = next; } - gal->DrawLine( m_position, m_end ); gal->DrawCircle( m_end, scaledSize.x / 4 ); break; } diff --git a/eeschema/tools/ee_grid_helper.cpp b/eeschema/tools/ee_grid_helper.cpp index bc34bd2964..48bae67aa2 100644 --- a/eeschema/tools/ee_grid_helper.cpp +++ b/eeschema/tools/ee_grid_helper.cpp @@ -252,7 +252,8 @@ VECTOR2I EE_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aL const std::vector& aSkip ) { double worldScale = m_toolMgr->GetView()->GetGAL()->GetWorldScale(); - int snapRange = (int) ( m_snapSize / worldScale ); + double snapDist = m_snapSize / worldScale; + int snapRange = KiROUND( snapDist ); BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) ); @@ -265,37 +266,23 @@ VECTOR2I EE_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aL ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers ); VECTOR2I nearestGrid = Align( aOrigin ); - if( nearest && m_enableSnap ) - { - double snapDist = nearest->Distance( aOrigin ); - - if( snapDist <= snapRange ) - { - m_viewSnapPoint.SetPosition( wxPoint( nearest->pos ) ); - m_viewSnapLine.SetPosition( wxPoint( nearest->pos ) ); - m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false ); - - if( m_toolMgr->GetView()->IsVisible( &m_viewSnapPoint ) ) - m_toolMgr->GetView()->Update( &m_viewSnapPoint, KIGFX::GEOMETRY); - else - m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, true ); - - m_snapItem = nearest; - return nearest->pos; - } - } + if( nearest ) + snapDist = nearest->Distance( aOrigin ); if( m_snapItem && m_enableSnapLine && m_enableSnap ) { bool snapLine = false; + int x_dist = std::abs( m_viewSnapLine.GetPosition().x - aOrigin.x ); + int y_dist = std::abs( m_viewSnapLine.GetPosition().y - aOrigin.y ); - if( std::abs( m_viewSnapLine.GetPosition().x - aOrigin.x ) < snapRange ) + /// Allows de-snapping from the line if you are closer to another snap point + if( x_dist < snapRange && x_dist < snapDist ) { nearestGrid.x = m_viewSnapLine.GetPosition().x; snapLine = true; } - if( std::abs( m_viewSnapLine.GetPosition().y - aOrigin.y ) < snapRange ) + if( y_dist < snapRange && y_dist < snapDist ) { nearestGrid.y = m_viewSnapLine.GetPosition().y; snapLine = true; @@ -315,6 +302,25 @@ VECTOR2I EE_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aL } } + if( nearest && m_enableSnap ) + { + + if( snapDist <= snapRange ) + { + m_viewSnapPoint.SetPosition( wxPoint( nearest->pos ) ); + m_viewSnapLine.SetPosition( wxPoint( nearest->pos ) ); + m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false ); + + if( m_toolMgr->GetView()->IsVisible( &m_viewSnapPoint ) ) + m_toolMgr->GetView()->Update( &m_viewSnapPoint, KIGFX::GEOMETRY); + else + m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, true ); + + m_snapItem = nearest; + return nearest->pos; + } + } + m_snapItem = nullptr; m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, false ); m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false ); diff --git a/pcbnew/tools/grid_helper.cpp b/pcbnew/tools/grid_helper.cpp index f5a5503966..969ed48eca 100644 --- a/pcbnew/tools/grid_helper.cpp +++ b/pcbnew/tools/grid_helper.cpp @@ -306,7 +306,8 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLaye const std::vector& aSkip ) { double worldScale = m_toolMgr->GetView()->GetGAL()->GetWorldScale(); - int snapRange = (int) ( m_snapSize / worldScale ); + double snapDist = m_snapSize / worldScale; + int snapRange = KiROUND( snapDist ); BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) ); @@ -319,35 +320,24 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLaye ANCHOR* nearest = nearestAnchor( aOrigin, SNAPPABLE, aLayers ); VECTOR2I nearestGrid = Align( aOrigin ); - if( nearest && m_enableSnap ) - { - if( nearest->Distance( aOrigin ) <= snapRange ) - { - m_viewSnapPoint.SetPosition( wxPoint( nearest->pos ) ); - m_viewSnapLine.SetPosition( wxPoint( nearest->pos ) ); - m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false ); + if( nearest ) + snapDist = nearest->Distance( aOrigin ); - if( m_toolMgr->GetView()->IsVisible( &m_viewSnapPoint ) ) - m_toolMgr->GetView()->Update( &m_viewSnapPoint, KIGFX::GEOMETRY); - else - m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, true ); - - m_snapItem = nearest; - return nearest->pos; - } - } - - if( m_snapItem && m_enableSnapLine ) + // Existing snap lines need priority over new snaps + if( m_snapItem && m_enableSnapLine && m_enableSnap ) { bool snapLine = false; + int x_dist = std::abs( m_viewSnapLine.GetPosition().x - aOrigin.x ); + int y_dist = std::abs( m_viewSnapLine.GetPosition().y - aOrigin.y ); - if( std::abs( m_viewSnapLine.GetPosition().x - aOrigin.x ) < GetGrid().x ) + /// Allows de-snapping from the line if you are closer to another snap point + if( x_dist < snapRange && x_dist < snapDist ) { nearestGrid.x = m_viewSnapLine.GetPosition().x; snapLine = true; } - if( std::abs( m_viewSnapLine.GetPosition().y - aOrigin.y ) < GetGrid().y ) + if( y_dist < snapRange && y_dist < snapDist ) { nearestGrid.y = m_viewSnapLine.GetPosition().y; snapLine = true; @@ -367,6 +357,24 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& aLaye } } + if( nearest && m_enableSnap ) + { + if( nearest->Distance( aOrigin ) <= snapRange ) + { + m_viewSnapPoint.SetPosition( wxPoint( nearest->pos ) ); + m_viewSnapLine.SetPosition( wxPoint( nearest->pos ) ); + m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false ); + + if( m_toolMgr->GetView()->IsVisible( &m_viewSnapPoint ) ) + m_toolMgr->GetView()->Update( &m_viewSnapPoint, KIGFX::GEOMETRY); + else + m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, true ); + + m_snapItem = nearest; + return nearest->pos; + } + } + m_snapItem = nullptr; m_toolMgr->GetView()->SetVisible( &m_viewSnapPoint, false ); m_toolMgr->GetView()->SetVisible( &m_viewSnapLine, false );