From 62235348d168de7c2be045bb5e774578864f4107 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 8 Mar 2017 10:35:51 +0100 Subject: [PATCH] Use GAL axes instead of a ORIGIN_VIEWITEM in module editor --- common/gal/graphics_abstraction_layer.cpp | 296 +++++++++++----------- common/gal/opengl/opengl_gal.cpp | 58 +++-- include/gal/graphics_abstraction_layer.h | 8 + pcbnew/moduleframe.cpp | 2 + pcbnew/tools/module_editor_tools.cpp | 14 - pcbnew/tools/module_editor_tools.h | 4 - 6 files changed, 198 insertions(+), 184 deletions(-) diff --git a/common/gal/graphics_abstraction_layer.cpp b/common/gal/graphics_abstraction_layer.cpp index 3d83d05597..13d79fd09c 100644 --- a/common/gal/graphics_abstraction_layer.cpp +++ b/common/gal/graphics_abstraction_layer.cpp @@ -79,6 +79,7 @@ GAL::~GAL() { } + void GAL::OnGalDisplayOptionsChanged( const GAL_DISPLAY_OPTIONS& aOptions ) { // defer to the child class first @@ -179,9 +180,6 @@ double GAL::computeMinGridSpacing() const void GAL::DrawGrid() { - if( !gridVisibility ) - return; - SetTarget( TARGET_NONCACHED ); // Draw the grid @@ -202,146 +200,6 @@ void GAL::DrawGrid() double marker = std::max( 1.0, gridLineWidth ) / worldScale; double doubleMarker = 2.0 * marker; - // Check if the grid would not be too dense - if( std::max( gridScreenSizeDense, gridScreenSizeCoarse ) > gridThreshold ) - { - // Compute grid variables - int gridStartX = KiROUND( worldStartPoint.x / gridSize.x ); - int gridEndX = KiROUND( worldEndPoint.x / gridSize.x ); - int gridStartY = KiROUND( worldStartPoint.y / gridSize.y ); - int gridEndY = KiROUND( worldEndPoint.y / gridSize.y ); - - // Correct the index, else some lines are not correctly painted - - gridStartY -= std::abs( gridOrigin.y / gridSize.y ) + 1; - gridEndY += std::abs( gridOrigin.y / gridSize.y ) + 1; - - if ( gridStartX <= gridEndX ) - { - gridStartX -= std::abs( gridOrigin.x / gridSize.x ) + 1; - gridEndX += std::abs( gridOrigin.x / gridSize.x ) + 1; - } - else - { - gridStartX += std::abs( gridOrigin.x / gridSize.x ) + 1; - gridEndX -= std::abs( gridOrigin.x / gridSize.x ) + 1; - } - - int dirX = gridEndX >= gridStartX ? 1 : -1; - int dirY = gridEndY >= gridStartY ? 1 : -1; - - // Draw the grid behind all other layers - SetLayerDepth( depthRange.y * 0.75 ); - - if( gridStyle == GRID_STYLE::LINES ) - { - SetIsFill( false ); - SetIsStroke( true ); - SetStrokeColor( gridColor ); - - // Now draw the grid, every coarse grid line gets the double width - - // Vertical lines - for( int j = gridStartY; j != gridEndY; j += dirY ) - { - if( j % gridTick == 0 && gridScreenSizeDense > gridThreshold ) - SetLineWidth( doubleMarker ); - else - SetLineWidth( marker ); - - if( ( j % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) - || gridScreenSizeDense > gridThreshold ) - { - drawGridLine( VECTOR2D( gridStartX * gridSize.x, j * gridSize.y + gridOrigin.y ), - VECTOR2D( gridEndX * gridSize.x, j * gridSize.y + gridOrigin.y ) ); - } - } - - // Horizontal lines - for( int i = gridStartX; i != gridEndX; i += dirX ) - { - if( i % gridTick == 0 && gridScreenSizeDense > gridThreshold ) - SetLineWidth( doubleMarker ); - else - SetLineWidth( marker ); - - if( ( i % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) - || gridScreenSizeDense > gridThreshold ) - { - drawGridLine( VECTOR2D( i * gridSize.x + gridOrigin.x, gridStartY * gridSize.y ), - VECTOR2D( i * gridSize.x + gridOrigin.x, gridEndY * gridSize.y ) ); - } - } - } - else if( gridStyle == GRID_STYLE::SMALL_CROSS ) - { - SetIsFill( false ); - SetIsStroke( true ); - SetStrokeColor( gridColor ); - - SetLineWidth( marker ); - double lineLen = GetLineWidth() * 2; - - // Vertical positions: - for( int j = gridStartY; j != gridEndY; j += dirY ) - { - if( ( j % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) - || gridScreenSizeDense > gridThreshold ) - { - int posY = j * gridSize.y + gridOrigin.y; - - // Horizontal positions: - for( int i = gridStartX; i != gridEndX; i += dirX ) - { - if( ( i % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) - || gridScreenSizeDense > gridThreshold ) - { - int posX = i * gridSize.x + gridOrigin.x; - - drawGridLine( VECTOR2D( posX - lineLen, posY ), - VECTOR2D( posX + lineLen, posY ) ); - - drawGridLine( VECTOR2D( posX, posY - lineLen ), - VECTOR2D( posX, posY + lineLen ) ); - } - } - } - } - } - else // Dotted grid - { - bool tickX, tickY; - SetIsFill( true ); - SetIsStroke( false ); - SetFillColor( gridColor ); - - for( int j = gridStartY; j != gridEndY; j += dirY ) - { - if( j % gridTick == 0 && gridScreenSizeDense > gridThreshold ) - tickY = true; - else - tickY = false; - - for( int i = gridStartX; i != gridEndX; i += dirX ) - { - if( i % gridTick == 0 && gridScreenSizeDense > gridThreshold ) - tickX = true; - else - tickX = false; - - if( tickX || tickY || gridScreenSizeDense > gridThreshold ) - { - double radius = ( ( tickX && tickY ) ? doubleMarker : marker ) / 2.0; - DrawRectangle( VECTOR2D( i * gridSize.x - radius + gridOrigin.x, - j * gridSize.y - radius + gridOrigin.y ), - VECTOR2D( i * gridSize.x + radius + gridOrigin.x, - j * gridSize.y + radius + gridOrigin.y ) ); - } - } - } - } - } - // Draw axes if desired if( axesEnabled ) { @@ -356,6 +214,158 @@ void GAL::DrawGrid() drawGridLine( VECTOR2D( 0, worldStartPoint.y ), VECTOR2D( 0, worldEndPoint.y ) ); } + + if( !gridVisibility ) + return; + + // Check if the grid would not be too dense + if( std::max( gridScreenSizeDense, gridScreenSizeCoarse ) <= gridThreshold ) + return; + + // Compute grid variables + int gridStartX = KiROUND( worldStartPoint.x / gridSize.x ); + int gridEndX = KiROUND( worldEndPoint.x / gridSize.x ); + int gridStartY = KiROUND( worldStartPoint.y / gridSize.y ); + int gridEndY = KiROUND( worldEndPoint.y / gridSize.y ); + + // Correct the index, else some lines are not correctly painted + gridStartY -= std::abs( gridOrigin.y / gridSize.y ) + 1; + gridEndY += std::abs( gridOrigin.y / gridSize.y ) + 1; + + if( gridStartX <= gridEndX ) + { + gridStartX -= std::abs( gridOrigin.x / gridSize.x ) + 1; + gridEndX += std::abs( gridOrigin.x / gridSize.x ) + 1; + } + else + { + gridStartX += std::abs( gridOrigin.x / gridSize.x ) + 1; + gridEndX -= std::abs( gridOrigin.x / gridSize.x ) + 1; + } + + int dirX = gridEndX >= gridStartX ? 1 : -1; + int dirY = gridEndY >= gridStartY ? 1 : -1; + + // Draw the grid behind all other layers + SetLayerDepth( depthRange.y * 0.75 ); + + if( gridStyle == GRID_STYLE::LINES ) + { + SetIsFill( false ); + SetIsStroke( true ); + SetStrokeColor( gridColor ); + + // Now draw the grid, every coarse grid line gets the double width + + // Vertical lines + for( int j = gridStartY; j != gridEndY; j += dirY ) + { + const double y = j * gridSize.y + gridOrigin.y; + + if( axesEnabled && y == 0 ) + continue; + + if( j % gridTick == 0 && gridScreenSizeDense > gridThreshold ) + SetLineWidth( doubleMarker ); + else + SetLineWidth( marker ); + + if( ( j % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) + || gridScreenSizeDense > gridThreshold ) + { + drawGridLine( VECTOR2D( gridStartX * gridSize.x, y ), + VECTOR2D( gridEndX * gridSize.x, y ) ); + } + } + + // Horizontal lines + for( int i = gridStartX; i != gridEndX; i += dirX ) + { + const double x = i * gridSize.x + gridOrigin.x; + + if( axesEnabled && x == 0 ) + continue; + + if( i % gridTick == 0 && gridScreenSizeDense > gridThreshold ) + SetLineWidth( doubleMarker ); + else + SetLineWidth( marker ); + + if( ( i % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) + || gridScreenSizeDense > gridThreshold ) + { + drawGridLine( VECTOR2D( x, gridStartY * gridSize.y ), + VECTOR2D( x, gridEndY * gridSize.y ) ); + } + } + } + else if( gridStyle == GRID_STYLE::SMALL_CROSS ) + { + SetIsFill( false ); + SetIsStroke( true ); + SetStrokeColor( gridColor ); + + SetLineWidth( marker ); + double lineLen = GetLineWidth() * 2; + + // Vertical positions: + for( int j = gridStartY; j != gridEndY; j += dirY ) + { + if( ( j % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) + || gridScreenSizeDense > gridThreshold ) + { + int posY = j * gridSize.y + gridOrigin.y; + + // Horizontal positions: + for( int i = gridStartX; i != gridEndX; i += dirX ) + { + if( ( i % gridTick == 0 && gridScreenSizeCoarse > gridThreshold ) + || gridScreenSizeDense > gridThreshold ) + { + int posX = i * gridSize.x + gridOrigin.x; + + drawGridLine( VECTOR2D( posX - lineLen, posY ), + VECTOR2D( posX + lineLen, posY ) ); + + drawGridLine( VECTOR2D( posX, posY - lineLen ), + VECTOR2D( posX, posY + lineLen ) ); + } + } + } + } + } + else // Dotted grid + { + bool tickX, tickY; + SetIsFill( true ); + SetIsStroke( false ); + SetFillColor( gridColor ); + + for( int j = gridStartY; j != gridEndY; j += dirY ) + { + if( j % gridTick == 0 && gridScreenSizeDense > gridThreshold ) + tickY = true; + else + tickY = false; + + for( int i = gridStartX; i != gridEndX; i += dirX ) + { + if( i % gridTick == 0 && gridScreenSizeDense > gridThreshold ) + tickX = true; + else + tickX = false; + + if( tickX || tickY || gridScreenSizeDense > gridThreshold ) + { + double radius = ( ( tickX && tickY ) ? doubleMarker : marker ) / 2.0; + DrawRectangle( VECTOR2D( i * gridSize.x - radius + gridOrigin.x, + j * gridSize.y - radius + gridOrigin.y ), + VECTOR2D( i * gridSize.x + radius + gridOrigin.x, + j * gridSize.y + radius + gridOrigin.y ) ); + } + } + } + } } diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index 3e56a8cf57..390dd0f01d 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -848,9 +848,6 @@ void OPENGL_GAL::BitmapText( const wxString& aText, const VECTOR2D& aPosition, void OPENGL_GAL::DrawGrid() { - if( !gridVisibility ) - return; - int gridScreenSizeDense = KiROUND( gridSize.x * worldScale ); int gridScreenSizeCoarse = KiROUND( gridSize.x * static_cast( gridTick ) * worldScale ); @@ -873,6 +870,26 @@ void OPENGL_GAL::DrawGrid() VECTOR2D worldStartPoint = screenWorldMatrix * VECTOR2D( 0.0, 0.0 ); VECTOR2D worldEndPoint = screenWorldMatrix * VECTOR2D( screenSize ); + // Draw axes if desired + if( axesEnabled ) + { + glLineWidth( minorLineWidth ); + glColor4d( axesColor.r, axesColor.g, axesColor.b, 1.0 ); + + glBegin( GL_LINES ); + glVertex2d( worldStartPoint.x, 0 ); + glVertex2d( worldEndPoint.x, 0 ); + glEnd(); + + glBegin( GL_LINES ); + glVertex2d( 0, worldStartPoint.y ); + glVertex2d( 0, worldEndPoint.y ); + glEnd(); + } + + if( !gridVisibility ) + return; + // Compute grid variables int gridStartX = KiROUND( worldStartPoint.x / gridSize.x ); int gridEndX = KiROUND( worldEndPoint.x / gridSize.x ); @@ -883,7 +900,7 @@ void OPENGL_GAL::DrawGrid() gridStartY -= std::abs( gridOrigin.y / gridSize.y ) + 1; gridEndY += std::abs( gridOrigin.y / gridSize.y ) + 1; - if ( gridStartX <= gridEndX ) + if( gridStartX <= gridEndX ) { gridStartX -= std::abs( gridOrigin.x / gridSize.x ) + 1; gridEndX += std::abs( gridOrigin.x / gridSize.x ) + 1; @@ -952,6 +969,12 @@ void OPENGL_GAL::DrawGrid() // Vertical lines for( int j = gridStartY; j != gridEndY; j += dirY ) { + const double y = j * gridSize.y + gridOrigin.y; + + // If axes are drawn, skip the lines that would cover them + if( axesEnabled && y == 0 ) + continue; + if( j % gridTick == 0 && gridScreenSizeDense > gridThreshold ) glLineWidth( majorLineWidth ); else @@ -961,8 +984,8 @@ void OPENGL_GAL::DrawGrid() || gridScreenSizeDense > gridThreshold ) { glBegin( GL_LINES ); - glVertex2d( gridStartX * gridSize.x, j * gridSize.y + gridOrigin.y ); - glVertex2d( gridEndX * gridSize.x, j * gridSize.y + gridOrigin.y ); + glVertex2d( gridStartX * gridSize.x, y ); + glVertex2d( gridEndX * gridSize.x, y ); glEnd(); } } @@ -976,6 +999,12 @@ void OPENGL_GAL::DrawGrid() // Horizontal lines for( int i = gridStartX; i != gridEndX; i += dirX ) { + const double x = i * gridSize.x + gridOrigin.x; + + // If axes are drawn, skip the lines that would cover them + if( axesEnabled && x == 0 ) + continue; + if( i % gridTick == 0 && gridScreenSizeDense > gridThreshold ) glLineWidth( majorLineWidth ); else @@ -995,23 +1024,6 @@ void OPENGL_GAL::DrawGrid() glDisable( GL_STENCIL_TEST ); } - // Draw axes if desired - if( axesEnabled ) - { - glLineWidth( minorLineWidth ); - glColor4d( axesColor.r, axesColor.g, axesColor.b, 1.0 ); - - glBegin( GL_LINES ); - glVertex2d( worldStartPoint.x, 0 ); - glVertex2d( worldEndPoint.x, 0 ); - glEnd(); - - glBegin( GL_LINES ); - glVertex2d( 0, worldStartPoint.y ); - glVertex2d( 0, worldEndPoint.y ); - glEnd(); - } - glEnable( GL_DEPTH_TEST ); glEnable( GL_TEXTURE_2D ); } diff --git a/include/gal/graphics_abstraction_layer.h b/include/gal/graphics_abstraction_layer.h index a9e9fed7e9..f12888a665 100644 --- a/include/gal/graphics_abstraction_layer.h +++ b/include/gal/graphics_abstraction_layer.h @@ -809,6 +809,14 @@ public: axesColor = aAxesColor; } + /** + * @brief Enables drawing the axes. + */ + inline void SetAxesEnabled( bool aAxesEnabled ) + { + axesEnabled = aAxesEnabled; + } + /** * @brief Draw every tick line wider. * diff --git a/pcbnew/moduleframe.cpp b/pcbnew/moduleframe.cpp index 99c45e249a..337c3f0831 100644 --- a/pcbnew/moduleframe.cpp +++ b/pcbnew/moduleframe.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -330,6 +331,7 @@ FOOTPRINT_EDIT_FRAME::FOOTPRINT_EDIT_FRAME( KIWAY* aKiway, wxWindow* aParent ) : // Create the manager and dispatcher & route draw panel events to the dispatcher setupTools(); + GetGalCanvas()->GetGAL()->SetAxesEnabled( true ); UseGalCanvas( parentFrame->IsGalCanvasActive() ); if( m_auimgr.GetPane( "m_LayersManagerToolBar" ).IsShown() ) diff --git a/pcbnew/tools/module_editor_tools.cpp b/pcbnew/tools/module_editor_tools.cpp index ffb39cfbe5..e82a89c4c8 100644 --- a/pcbnew/tools/module_editor_tools.cpp +++ b/pcbnew/tools/module_editor_tools.cpp @@ -83,18 +83,11 @@ MODULE_EDITOR_TOOLS::MODULE_EDITOR_TOOLS() : TOOL_INTERACTIVE( "pcbnew.ModuleEditor" ), m_view( NULL ), m_controls( NULL ), m_board( NULL ), m_frame( NULL ) { - // Generate an origin marker at 0,0 which is used as an axis origin marker (0,0) - m_axisOrigin = new KIGFX::ORIGIN_VIEWITEM( KIGFX::COLOR4D(0.0, 0.0, 0.8, 1.0), - KIGFX::ORIGIN_VIEWITEM::CROSS, - 20000, - VECTOR2D(0,0) ); - m_axisOrigin->SetDrawAtZero( true ); } MODULE_EDITOR_TOOLS::~MODULE_EDITOR_TOOLS() { - delete m_axisOrigin; } @@ -105,13 +98,6 @@ void MODULE_EDITOR_TOOLS::Reset( RESET_REASON aReason ) m_controls = getViewControls(); m_board = getModel(); m_frame = getEditFrame(); - - if( aReason == MODEL_RELOAD || aReason == GAL_SWITCH ) - { - // Draw the axis origin if we're editing modules (essentially in the footprint editor) - m_view->Remove( m_axisOrigin ); - m_view->Add( m_axisOrigin ); - } } diff --git a/pcbnew/tools/module_editor_tools.h b/pcbnew/tools/module_editor_tools.h index 68391c2e48..e3d7a02a1a 100644 --- a/pcbnew/tools/module_editor_tools.h +++ b/pcbnew/tools/module_editor_tools.h @@ -26,7 +26,6 @@ #define MODULE_EDITOR_TOOLS_H #include -#include namespace KIGFX { @@ -106,9 +105,6 @@ private: BOARD* m_board; PCB_EDIT_FRAME* m_frame; - ///> Axis 0 marker - KIGFX::ORIGIN_VIEWITEM* m_axisOrigin; - }; #endif