gal: Decoupled X and Y grid step sizes

Made the X and Y step sizes for the grid separate in the GAL.

Cleaned up the types inside the DrawGrid functions.

Fixes: lp:1821416
* https://bugs.launchpad.net/kicad/+bug/1821416
This commit is contained in:
Ian McInerney 2019-05-27 02:29:01 +01:00 committed by Seth Hillbrand
parent f2d7bb7b0c
commit 45f10fcff1
2 changed files with 42 additions and 46 deletions

View File

@ -1525,14 +1525,6 @@ void CAIRO_GAL_BASE::DrawGrid()
VECTOR2D worldStartPoint = screenWorldMatrix * VECTOR2D( 0.0, 0.0 );
VECTOR2D worldEndPoint = screenWorldMatrix * VECTOR2D( screenSize );
double gridThreshold = KiROUND( computeMinGridSpacing() / worldScale );
if( gridStyle == GRID_STYLE::SMALL_CROSS )
gridThreshold *= 2.0;
int gridScreenSizeDense = gridSize.x;
int gridScreenSizeCoarse = KiROUND( gridSize.x * static_cast<double>( gridTick ) );
// Compute the line marker or point radius of the grid
// Note: generic grids can't handle sub-pixel lines without
// either losing fine/course distinction or having some dots
@ -1550,21 +1542,27 @@ void CAIRO_GAL_BASE::DrawGrid()
if( !gridVisibility )
return;
VECTOR2D gridScreenSize( gridSize );
double gridThreshold = KiROUND( computeMinGridSpacing() / worldScale );
if( 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( gridScreenSizeDense, gridScreenSizeCoarse ) <= gridThreshold )
while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold )
{
gridScreenSizeCoarse *= gridTick;
gridScreenSizeDense *= gridTick;
gridScreenSize = gridScreenSize * static_cast<double>( gridTick );
}
// Compute grid starting and ending indexes to draw grid points on the
// visible screen area
// Note: later any point coordinate will be offsetted by gridOrigin
int gridStartX = KiROUND( ( worldStartPoint.x - gridOrigin.x ) / gridScreenSizeDense );
int gridEndX = KiROUND( ( worldEndPoint.x - gridOrigin.x ) / gridScreenSizeDense );
int gridStartY = KiROUND( ( worldStartPoint.y - gridOrigin.y ) / gridScreenSizeDense );
int gridEndY = KiROUND( ( worldEndPoint.y - gridOrigin.y ) / gridScreenSizeDense );
int gridStartX = KiROUND( ( worldStartPoint.x - gridOrigin.x ) / gridScreenSize.x );
int gridEndX = KiROUND( ( worldEndPoint.x - gridOrigin.x ) / gridScreenSize.x );
int gridStartY = KiROUND( ( worldStartPoint.y - gridOrigin.y ) / gridScreenSize.y );
int gridEndY = KiROUND( ( worldEndPoint.y - gridOrigin.y ) / gridScreenSize.y );
// Ensure start coordinate > end coordinate
@ -1585,27 +1583,27 @@ void CAIRO_GAL_BASE::DrawGrid()
// Vertical lines
for( int j = gridStartY; j <= gridEndY; j++ )
{
const double y = j * gridScreenSizeDense + gridOrigin.y;
const double y = j * gridScreenSize.y + gridOrigin.y;
if( axesEnabled && y == 0 )
if( axesEnabled && y == 0.0 )
continue;
SetLineWidth( ( j % gridTick ) ? marker : doubleMarker );
drawGridLine( VECTOR2D( gridStartX * gridScreenSizeDense + gridOrigin.x, y ),
VECTOR2D( gridEndX * gridScreenSizeDense + gridOrigin.x, y ) );
drawGridLine( VECTOR2D( gridStartX * gridScreenSize.x + gridOrigin.x, y ),
VECTOR2D( gridEndX * gridScreenSize.x + gridOrigin.x, y ) );
}
// Horizontal lines
for( int i = gridStartX; i <= gridEndX; i++ )
{
const double x = i * gridScreenSizeDense + gridOrigin.x;
const double x = i * gridScreenSize.x + gridOrigin.x;
if( axesEnabled && x == 0 )
if( axesEnabled && x == 0.0 )
continue;
SetLineWidth( ( i % gridTick ) ? marker : doubleMarker );
drawGridLine( VECTOR2D( x, gridStartY * gridScreenSizeDense + gridOrigin.y ),
VECTOR2D( x, gridEndY * gridScreenSizeDense + gridOrigin.y ) );
drawGridLine( VECTOR2D( x, gridStartY * gridScreenSize.y + gridOrigin.y ),
VECTOR2D( x, gridEndY * gridScreenSize.y + gridOrigin.y ) );
}
}
@ -1619,8 +1617,8 @@ void CAIRO_GAL_BASE::DrawGrid()
{
bool tickX = ( i % gridTick == 0 );
SetLineWidth( ( ( tickX && tickY ) ? doubleMarker : marker ) );
auto pos = VECTOR2D( i * gridScreenSizeDense + gridOrigin.x,
j * gridScreenSizeDense + gridOrigin.y );
auto pos = VECTOR2D( i * gridScreenSize.x + gridOrigin.x,
j * gridScreenSize.y + gridOrigin.y );
if( gridStyle == GRID_STYLE::SMALL_CROSS )
drawGridCross( pos );

View File

@ -1256,29 +1256,27 @@ void OPENGL_GAL::DrawGrid()
if( !gridVisibility )
return;
int gridScreenSizeDense = gridSize.x;
int gridScreenSizeCoarse = KiROUND( gridSize.x * static_cast<double>( gridTick ) );
VECTOR2D gridScreenSize( gridSize );
double gridThreshold = KiROUND( computeMinGridSpacing() / worldScale );
double gridThreshold = computeMinGridSpacing() / worldScale;
if( 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( gridScreenSizeDense, gridScreenSizeCoarse ) <= gridThreshold )
while( std::min( gridScreenSize.x, gridScreenSize.y ) <= gridThreshold )
{
gridScreenSizeCoarse *= gridTick;
gridScreenSizeDense *= gridTick;
gridScreenSize = gridScreenSize * static_cast<double>( gridTick );
}
// Compute grid staring and ending indexes to draw grid points on the
// Compute grid starting and ending indexes to draw grid points on the
// visible screen area
// Note: later any point coordinate will be offsetted by gridOrigin
int gridStartX = KiROUND( ( worldStartPoint.x - gridOrigin.x ) / gridScreenSizeDense );
int gridEndX = KiROUND( ( worldEndPoint.x - gridOrigin.x ) / gridScreenSizeDense );
int gridStartY = KiROUND( ( worldStartPoint.y - gridOrigin.y ) / gridScreenSizeDense );
int gridEndY = KiROUND( ( worldEndPoint.y - gridOrigin.y ) / gridScreenSizeDense );
int gridStartX = KiROUND( ( worldStartPoint.x - gridOrigin.x ) / gridScreenSize.x );
int gridEndX = KiROUND( ( worldEndPoint.x - gridOrigin.x ) / gridScreenSize.x );
int gridStartY = KiROUND( ( worldStartPoint.y - gridOrigin.y ) / gridScreenSize.y );
int gridEndY = KiROUND( ( worldEndPoint.y - gridOrigin.y ) / gridScreenSize.y );
// Ensure start coordinate > end coordinate
SWAP( gridStartX, >, gridEndX );
@ -1312,7 +1310,7 @@ void OPENGL_GAL::DrawGrid()
for( int j = gridStartY; j <= gridEndY; j++ )
{
bool tickY = ( j % gridTick == 0 );
int posY = j * gridScreenSizeDense + gridOrigin.y;
const double posY = j * gridScreenSize.y + gridOrigin.y;
// Horizontal positions
for( int i = gridStartX; i <= gridEndX; i++ )
@ -1320,7 +1318,7 @@ void OPENGL_GAL::DrawGrid()
bool tickX = ( i % gridTick == 0 );
SetLineWidth( ( ( tickX && tickY ) ? majorLineWidth : minorLineWidth ) );
auto lineLen = 2.0 * GetLineWidth();
auto posX = i * gridScreenSizeDense + gridOrigin.x;
auto posX = i * gridScreenSize.x + gridOrigin.x;
DrawLine( VECTOR2D( posX - lineLen, posY ), VECTOR2D( posX + lineLen, posY ) );
DrawLine( VECTOR2D( posX, posY - lineLen ), VECTOR2D( posX, posY + lineLen ) );
@ -1334,15 +1332,15 @@ void OPENGL_GAL::DrawGrid()
// Vertical lines
for( int j = gridStartY; j <= gridEndY; j++ )
{
const double y = j * gridScreenSizeDense + gridOrigin.y;
const double y = j * gridScreenSize.y + gridOrigin.y;
// If axes are drawn, skip the lines that would cover them
if( axesEnabled && y == 0 )
if( axesEnabled && y == 0.0 )
continue;
SetLineWidth( ( j % gridTick == 0 ) ? majorLineWidth : minorLineWidth );
VECTOR2D a ( gridStartX * gridScreenSizeDense + gridOrigin.x, y );
VECTOR2D b ( gridEndX * gridScreenSizeDense + gridOrigin.x, y );
VECTOR2D a ( gridStartX * gridScreenSize.x + gridOrigin.x, y );
VECTOR2D b ( gridEndX * gridScreenSize.x + gridOrigin.x, y );
DrawLine( a, b );
}
@ -1359,15 +1357,15 @@ void OPENGL_GAL::DrawGrid()
// Horizontal lines
for( int i = gridStartX; i <= gridEndX; i++ )
{
const double x = i * gridScreenSizeDense + gridOrigin.x;
const double x = i * gridScreenSize.x + gridOrigin.x;
// If axes are drawn, skip the lines that would cover them
if( axesEnabled && x == 0 )
if( axesEnabled && x == 0.0 )
continue;
SetLineWidth( ( i % gridTick == 0 ) ? majorLineWidth : minorLineWidth );
VECTOR2D a ( x, gridStartY * gridScreenSizeDense + gridOrigin.y );
VECTOR2D b ( x, gridEndY * gridScreenSizeDense + gridOrigin.y );
VECTOR2D a ( x, gridStartY * gridScreenSize.y + gridOrigin.y );
VECTOR2D b ( x, gridEndY * gridScreenSize.y + gridOrigin.y );
DrawLine( a, b );
}