From 1cc9792cdb820e93f42385dcd8a2a8af1bbb44b3 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Wed, 21 Dec 2022 17:45:31 -0800 Subject: [PATCH] Fix snapping dist when disabling grid The disable grid hotkey should allow the snap scale to be completely determined by the snapSize and world scale. This prevents snapRange from overriding in the case where grid is disabled. Additionally, we disallow grid snapping when the grid scale is not visible. This means that when zoomed out sufficiently to not show the minor ticks, these minor ticks will not override a snap Fixes https://gitlab.com/kicad/code/kicad/issues/12303 --- common/gal/opengl/opengl_gal.cpp | 16 ++-------------- common/tool/grid_helper.cpp | 8 ++++++++ include/gal/graphics_abstraction_layer.h | 24 ++++++++++++++++++++++++ include/tool/grid_helper.h | 1 + pcbnew/tools/pcb_grid_helper.cpp | 7 ++++--- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index 67b9b14dde..3ef8ea4aec 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -1605,23 +1605,11 @@ void OPENGL_GAL::DrawGrid() if( !m_gridVisibility || m_gridSize.x == 0 || m_gridSize.y == 0 ) return; - VECTOR2D gridScreenSize( m_gridSize ); - - double gridThreshold = computeMinGridSpacing() / m_worldScale; - - if( m_gridStyle == GRID_STYLE::SMALL_CROSS ) - gridThreshold *= 2.0; - - // If we cannot display the grid density, scale down by a tick size and - // try again. Eventually, we get some representation of the grid - while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold ) - { - gridScreenSize = gridScreenSize * static_cast( m_gridTick ); - } + VECTOR2D gridScreenSize = GetVisibleGridSize(); // Compute grid starting and ending indexes to draw grid points on the // visible screen area - // Note: later any point coordinate will be offsetted by m_gridOrigin + // Note: later any point coordinate will be offset by m_gridOrigin int gridStartX = KiROUND( ( worldStartPoint.x - m_gridOrigin.x ) / gridScreenSize.x ); int gridEndX = KiROUND( ( worldEndPoint.x - m_gridOrigin.x ) / gridScreenSize.x ); int gridStartY = KiROUND( ( worldStartPoint.y - m_gridOrigin.y ) / gridScreenSize.y ); diff --git a/common/tool/grid_helper.cpp b/common/tool/grid_helper.cpp index 198ae97a76..39844a6db3 100644 --- a/common/tool/grid_helper.cpp +++ b/common/tool/grid_helper.cpp @@ -56,6 +56,14 @@ VECTOR2I GRID_HELPER::GetGrid() const } +VECTOR2I GRID_HELPER::GetVisibleGrid() const +{ + VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetVisibleGridSize(); + + return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) ); +} + + VECTOR2I GRID_HELPER::GetOrigin() const { VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin(); diff --git a/include/gal/graphics_abstraction_layer.h b/include/gal/graphics_abstraction_layer.h index 67a497b641..3d5eddddfa 100644 --- a/include/gal/graphics_abstraction_layer.h +++ b/include/gal/graphics_abstraction_layer.h @@ -775,6 +775,30 @@ public: return m_gridSize; } + /** + * Return the visible grid size in x and y directions + * + * @return A vector containing the spacing of visible grid marks + */ + inline VECTOR2D GetVisibleGridSize() const + { + VECTOR2D gridScreenSize( m_gridSize ); + + double gridThreshold = computeMinGridSpacing() / m_worldScale; + + if( m_gridStyle == GRID_STYLE::SMALL_CROSS ) + gridThreshold *= 2.0; + + // If we cannot display the grid density, scale down by a tick size and + // try again. Eventually, we get some representation of the grid + while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold ) + { + gridScreenSize = gridScreenSize * static_cast( m_gridTick ); + } + + return gridScreenSize; + } + /** * Set the grid color. * diff --git a/include/tool/grid_helper.h b/include/tool/grid_helper.h index 67be107f40..f967847f92 100644 --- a/include/tool/grid_helper.h +++ b/include/tool/grid_helper.h @@ -40,6 +40,7 @@ public: virtual ~GRID_HELPER(); VECTOR2I GetGrid() const; + VECTOR2I GetVisibleGrid() const; VECTOR2I GetOrigin() const; void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) ); diff --git a/pcbnew/tools/pcb_grid_helper.cpp b/pcbnew/tools/pcb_grid_helper.cpp index 76be6dc7e0..0a441febaf 100644 --- a/pcbnew/tools/pcb_grid_helper.cpp +++ b/pcbnew/tools/pcb_grid_helper.cpp @@ -285,9 +285,10 @@ VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& a // points that are visible can always be snapped to. // see https://gitlab.com/kicad/code/kicad/-/issues/5638 // see https://gitlab.com/kicad/code/kicad/-/issues/7125 - double snapScale = snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale(); - int snapRange = std::min( KiROUND( snapScale ), GetGrid().x ); - int snapDist = snapRange; + // see https://gitlab.com/kicad/code/kicad/-/issues/12303 + int snapScale = KiROUND( snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale() ); + int snapRange = m_enableGrid ? std::min( snapScale, GetVisibleGrid().x ) : snapScale; + int snapDist = snapRange; //Respect limits of coordinates representation BOX2I bb;