From 9ad3180f6cdf897fe49826319291669d409dd337 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 2 Apr 2014 15:57:21 +0200 Subject: [PATCH] Added EC_SNAPLINE to make possible snapping EDIT_LINES by their ends rather than by its center. --- pcbnew/tools/edit_constraints.cpp | 18 ++++++++++++++++-- pcbnew/tools/edit_constraints.h | 28 ++++++++++++++++++++++++++++ pcbnew/tools/edit_points.h | 16 +++++++++++++--- pcbnew/tools/point_editor.cpp | 18 +++++++++++------- 4 files changed, 68 insertions(+), 12 deletions(-) diff --git a/pcbnew/tools/edit_constraints.cpp b/pcbnew/tools/edit_constraints.cpp index 84d417c2ab..f0edf9bc8b 100644 --- a/pcbnew/tools/edit_constraints.cpp +++ b/pcbnew/tools/edit_constraints.cpp @@ -131,8 +131,8 @@ EC_CONVERGING::~EC_CONVERGING() void EC_CONVERGING::Apply( EDIT_LINE& aHandle ) { // The dragged segment endpoints - EDIT_POINT& origin = m_constrained.GetOrigin(); - EDIT_POINT& end = m_constrained.GetEnd(); + EDIT_POINT& origin = aHandle.GetOrigin(); + EDIT_POINT& end = aHandle.GetEnd(); if( m_colinearConstraint ) { @@ -173,3 +173,17 @@ void EC_CONVERGING::Apply( EDIT_LINE& aHandle ) end.SetPosition( *originEndIntersect ); } } + + +EC_SNAPLINE::EC_SNAPLINE( EDIT_LINE& aLine, V2D_TRANSFORM_FUN aSnapFun ) : + EDIT_CONSTRAINT( aLine ), m_snapFun( aSnapFun ) +{} + + +void EC_SNAPLINE::Apply( EDIT_LINE& aHandle ) +{ + VECTOR2D delta = aHandle.GetEnd().GetPosition() - aHandle.GetOrigin().GetPosition(); + + aHandle.GetOrigin().SetPosition( m_snapFun( aHandle.GetOrigin().GetPosition() ) ); + aHandle.GetEnd().SetPosition( aHandle.GetOrigin().GetPosition() + delta ); +} diff --git a/pcbnew/tools/edit_constraints.h b/pcbnew/tools/edit_constraints.h index c86afb5a75..6df17e438d 100644 --- a/pcbnew/tools/edit_constraints.h +++ b/pcbnew/tools/edit_constraints.h @@ -26,6 +26,7 @@ #define EDIT_CONSTRAINTS_H_ #include +#include class EDIT_POINT; class EDIT_LINE; @@ -236,4 +237,31 @@ private: VECTOR2I m_draggedVector; }; + +/** + * Class EC_SNAPLINE + * + * EDIT_CONSTRAINT for a EDIT_LINE, one of the ends is snapped to a spot determined by a + * transform function passed as parameter (e.g. it can be snapped to a grid), instead of having + * the line center snapped to a point. + */ +class EC_SNAPLINE : public EDIT_CONSTRAINT +{ +public: + ///> Typedef for a function that determines snapping point. + typedef boost::function V2D_TRANSFORM_FUN; + + EC_SNAPLINE( EDIT_LINE& aLine, V2D_TRANSFORM_FUN aSnapFun ); + + virtual ~EC_SNAPLINE() + {} + + ///> @copydoc EDIT_CONSTRAINT::Apply() + virtual void Apply( EDIT_LINE& aHandle ); + +private: + ///> Function that determines snapping point. + V2D_TRANSFORM_FUN m_snapFun; +}; + #endif /* EDIT_CONSTRAINTS_H_ */ diff --git a/pcbnew/tools/edit_points.h b/pcbnew/tools/edit_points.h index 52f54960ff..b06ec30975 100644 --- a/pcbnew/tools/edit_points.h +++ b/pcbnew/tools/edit_points.h @@ -418,15 +418,25 @@ public: } /** - * Function Size() + * Function PointsSize() * - * Returns number of stored points. + * Returns number of stored EDIT_POINTs. */ - unsigned int Size() const + unsigned int PointsSize() const { return m_points.size(); } + /** + * Function LinesSize() + * + * Returns number of stored EDIT_LINEs. + */ + unsigned int LinesSize() const + { + return m_lines.size(); + } + ///> @copydoc VIEW_ITEM::ViewBBox() virtual const BOX2I ViewBBox() const { diff --git a/pcbnew/tools/point_editor.cpp b/pcbnew/tools/point_editor.cpp index 5fd82edf53..f4aa4baa17 100644 --- a/pcbnew/tools/point_editor.cpp +++ b/pcbnew/tools/point_editor.cpp @@ -23,6 +23,7 @@ */ #include +#include #include #include @@ -64,16 +65,11 @@ enum DIMENSION_POINTS DIM_FEATUREDO, }; -/** - * Class POINT_EDITOR - * - * Tool that displays edit points allowing to modify items by dragging the points. - */ class EDIT_POINTS_FACTORY { public: - static boost::shared_ptr Make( EDA_ITEM* aItem ) + static boost::shared_ptr Make( EDA_ITEM* aItem, KIGFX::GAL* aGal ) { boost::shared_ptr points = boost::make_shared( aItem ); @@ -126,10 +122,18 @@ public: // Lines have to be added after creating edit points, // as they use EDIT_POINT references for( int i = 0; i < cornersCount - 1; ++i ) + { points->AddLine( points->Point( i ), points->Point( i + 1 ) ); + points->Line( i ).SetConstraint( + new EC_SNAPLINE( points->Line( i ), + boost::bind( &KIGFX::GAL::GetGridPoint, aGal, _1 ) ) ); + } // The last missing line, connecting the last and the first polygon point points->AddLine( points->Point( cornersCount - 1 ), points->Point( 0 ) ); + points->Line( points->LinesSize() - 1 ).SetConstraint( + new EC_SNAPLINE( points->Line( points->LinesSize() - 1 ), + boost::bind( &KIGFX::GAL::GetGridPoint, aGal, _1 ) ) ); break; } @@ -208,7 +212,7 @@ int POINT_EDITOR::OnSelectionChange( TOOL_EVENT& aEvent ) PCB_EDIT_FRAME* editFrame = getEditFrame(); EDA_ITEM* item = selection.items.GetPickedItem( 0 ); - m_editPoints = EDIT_POINTS_FACTORY::Make( item ); + m_editPoints = EDIT_POINTS_FACTORY::Make( item, m_toolMgr->GetView()->GetGAL() ); if( !m_editPoints ) { setTransitions();