diff --git a/common/drawframe.cpp b/common/drawframe.cpp index b7c9c9c139..a38908e970 100644 --- a/common/drawframe.cpp +++ b/common/drawframe.cpp @@ -987,9 +987,6 @@ void EDA_DRAW_FRAME::UseGalCanvas( bool aEnable ) // Set up grid settings gal->SetGridVisibility( IsGridVisible() ); - // Default grid color - dark cyan does not look good - //gal->SetGridColor( KiGfx::COLOR4D( GetGridColor() ) ); - gal->SetGridColor( KiGfx::COLOR4D( 0.1, 0.1, 0.1, 1.0 ) ); gal->SetGridSize( VECTOR2D( screen->GetGridSize().x, screen->GetGridSize().y ) ); gal->SetGridOrigin( VECTOR2D( screen->GetGridOrigin() ) ); gal->SetGridOriginMarkerSize( 15 ); diff --git a/common/drawpanel_gal.cpp b/common/drawpanel_gal.cpp index 83fa574802..5c1925b66f 100644 --- a/common/drawpanel_gal.cpp +++ b/common/drawpanel_gal.cpp @@ -134,7 +134,7 @@ void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) ) if( m_view->IsTargetDirty( KiGfx::TARGET_NONCACHED ) ) m_gal->DrawGrid(); m_view->Redraw(); - m_gal->DrawCursor( m_viewControls->GetMousePosition() ); + m_gal->DrawCursor( m_viewControls->GetCursorPosition() ); m_gal->EndDrawing(); diff --git a/common/gal/cairo/cairo_gal.cpp b/common/gal/cairo/cairo_gal.cpp index 86bd566bf7..058c34b338 100644 --- a/common/gal/cairo/cairo_gal.cpp +++ b/common/gal/cairo/cairo_gal.cpp @@ -69,6 +69,9 @@ CAIRO_GAL::CAIRO_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener, screenSize = VECTOR2D( aParent->GetSize() ); initCursor( 20 ); + // Grid color settings are different in Cairo and OpenGL + SetGridColor( COLOR4D( 0.1, 0.1, 0.1, 0.8 ) ); + // Allocate memory for pixel storage allocateBitmaps(); } diff --git a/common/gal/graphics_abstraction_layer.cpp b/common/gal/graphics_abstraction_layer.cpp index 593d59a9e9..50e3d5723d 100644 --- a/common/gal/graphics_abstraction_layer.cpp +++ b/common/gal/graphics_abstraction_layer.cpp @@ -48,7 +48,6 @@ GAL::GAL() : // Set grid defaults SetGridVisibility( true ); SetGridStyle( GRID_STYLE_LINES ); - SetGridColor( COLOR4D( 0.4, 0.4, 0.4, 1.0 ) ); SetCoarseGrid( 10 ); SetGridLineWidth( 0.5 ); @@ -230,3 +229,14 @@ void GAL::DrawGrid() } } } + + +VECTOR2D GAL::GetGridPoint( VECTOR2D aPoint ) const +{ + VECTOR2D pointWorld = ToWorld( aPoint ); + + pointWorld.x = round( pointWorld.x / gridSize.x ) * gridSize.x; + pointWorld.y = round( pointWorld.y / gridSize.y ) * gridSize.y; + + return ToScreen( pointWorld ); +} diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index dd446c3f58..efe801d137 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -84,6 +84,9 @@ OPENGL_GAL::OPENGL_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener, screenSize = VECTOR2D( aParent->GetSize() ); initCursor( 20 ); + // Grid color settings are different in Cairo and OpenGL + SetGridColor( COLOR4D( 0.8, 0.8, 0.8, 0.1 ) ); + // Tesselator initialization tesselator = gluNewTess(); InitTesselatorCallbacks( tesselator ); diff --git a/common/view/wx_view_controls.cpp b/common/view/wx_view_controls.cpp index 4066f8d9e6..58fef65260 100644 --- a/common/view/wx_view_controls.cpp +++ b/common/view/wx_view_controls.cpp @@ -27,6 +27,7 @@ #include #include +#include using namespace KiGfx; @@ -37,6 +38,7 @@ WX_VIEW_CONTROLS::WX_VIEW_CONTROLS( VIEW* aView, wxWindow* aParentPanel ) : m_autoPanEnabled( false ), m_autoPanMargin( 0.1 ), m_autoPanSpeed( 0.15 ), + m_snappingEnabled( true ), m_parentPanel( aParentPanel ) { m_parentPanel->Connect( wxEVT_MOTION, wxMouseEventHandler( @@ -203,7 +205,16 @@ void WX_VIEW_CONTROLS::SetGrabMouse( bool aEnabled ) } -void WX_VIEW_CONTROLS::handleAutoPanning( wxMouseEvent& aEvent ) +VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition() const +{ + if( m_snappingEnabled ) + return m_view->GetGAL()->GetGridPoint( m_mousePosition ); + + return m_mousePosition; +} + + +void WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent ) { VECTOR2D p( aEvent.GetX(), aEvent.GetY() ); diff --git a/include/gal/graphics_abstraction_layer.h b/include/gal/graphics_abstraction_layer.h index 0e334694a1..2b87606de0 100644 --- a/include/gal/graphics_abstraction_layer.h +++ b/include/gal/graphics_abstraction_layer.h @@ -652,13 +652,23 @@ public: /** * @brief Set the grid size. * - * @param aGridSize is a vector containing the grid size in x- and y direction. + * @param aGridSize is a vector containing the grid size in x and y direction. */ inline void SetGridSize( const VECTOR2D& aGridSize ) { gridSize = aGridSize; } + /** + * @brief Returns the grid size. + * + * @return A vector containing the grid size in x and y direction. + */ + inline const VECTOR2D& GetGridSize() const + { + return gridSize; + } + /** * @brief Set the grid color. * @@ -702,6 +712,17 @@ public: /// @brief Draw the grid void DrawGrid(); + + /** + * Function GetGridPoint() + * For a given point it returns the nearest point belonging to the grid. + * + * @param aPoint is the point for which the grid point is searched. + * @return The nearest grid point. + */ + VECTOR2D GetGridPoint( VECTOR2D aPoint ) const; + + /** * @brief Change the grid display style. * diff --git a/include/view/wx_view_controls.h b/include/view/wx_view_controls.h index 47da6b9126..7c9621fb58 100644 --- a/include/view/wx_view_controls.h +++ b/include/view/wx_view_controls.h @@ -61,10 +61,21 @@ public: * Function SetGrabMouse() * Enables/disables mouse cursor grabbing (limits the movement field only to the panel area). * - * @param aEnabled says whether the option should enabled or disabled. + * @param aEnabled says whether the option should be enabled or disabled. */ void SetGrabMouse( bool aEnabled ); + /** + * Function SetSnapping() + * Enables/disables snapping cursor to grid. + * + * @param aEnabled says whether the opion should be enabled or disabled. + */ + void SetSnapping( bool aEnabled ) + { + m_snappingEnabled = aEnabled; + } + /** * Function SetAutoPan() * Enables/disables autopanning (panning when mouse cursor reaches the panel border). @@ -73,20 +84,31 @@ public: */ void SetAutoPan( bool aEnabled ) { - m_autoPanEnabled = true; + m_autoPanEnabled = aEnabled; } /** * Function GetMousePosition() - * Returns the current mouse cursor position in the screen coordinates. + * Returns the current mouse pointer position in the screen coordinates. Note, that it may be + * different from the cursor position if snapping is enabled (@see GetCursorPosition()). * - * @return The current mouse cursor position. + * @return The current mouse pointer position. */ const VECTOR2D& GetMousePosition() const { return m_mousePosition; } + + /** + * Function GetCursorPosition() + * Returns the current cursor position in the screen coordinates. Note, that it may be + * different from the mouse pointer position if snapping is enabled (@see GetMousePosition()). + * + * @return The current cursor position. + */ + VECTOR2D GetCursorPosition() const; + private: enum State { IDLE = 1, @@ -95,7 +117,7 @@ private: }; /// Computes new viewport settings while in autopanning mode - void handleAutoPanning( wxMouseEvent& aEvent ); + void handleAutoPanning( const wxMouseEvent& aEvent ); /// Current state of VIEW_CONTROLS State m_state; @@ -105,11 +127,16 @@ private: /// Flag for grabbing the mouse cursor bool m_grabMouse; + /// Flag for turning on autopanning bool m_autoPanEnabled; /// Distance from cursor to VIEW edge when panning is active float m_autoPanMargin; + + /// Should the cursor snap to grid or move freely + bool m_snappingEnabled; + /// How fast is panning when in auto mode float m_autoPanSpeed;