Enable angle snap for pcbnew GAL ruler tool

This uses the two-point geometry manager to split the logic of the ruler
geometry and the preview item display. This allows the ruler to use that
manager's angle snap feature.
This commit is contained in:
John Beard 2017-03-16 22:37:39 +08:00 committed by Maciej Suminski
parent ddf4f3b2bc
commit d99fbddc22
3 changed files with 28 additions and 33 deletions

View File

@ -203,8 +203,9 @@ void drawBacksideTicks( KIGFX::GAL& aGal, const VECTOR2D& aOrigin,
}
RULER_ITEM::RULER_ITEM():
EDA_ITEM( NOT_USED ) // Never added to anything - just a preview
RULER_ITEM::RULER_ITEM( const TWO_POINT_GEOMETRY_MANAGER& aGeomMgr ):
EDA_ITEM( NOT_USED ), // Never added to anything - just a preview
m_geomMgr( aGeomMgr )
{}
@ -212,8 +213,8 @@ const BOX2I RULER_ITEM::ViewBBox() const
{
BOX2I tmp;
tmp.SetOrigin( m_origin );
tmp.SetEnd( m_end );
tmp.SetOrigin( m_geomMgr.GetOrigin() );
tmp.SetEnd( m_geomMgr.GetEnd() );
tmp.Normalize();
return tmp;
}
@ -230,20 +231,23 @@ void RULER_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
{
auto& gal = *aView->GetGAL();
const auto origin = m_geomMgr.GetOrigin();
const auto end = m_geomMgr.GetEnd();
gal.SetLineWidth( 1.0 );
gal.SetIsStroke( true );
gal.SetIsFill( false );
gal.SetStrokeColor( PreviewOverlayDefaultColor() );
// draw the main line from the origin to cursor
gal.DrawLine( m_origin, m_end );
gal.DrawLine( origin, end );
VECTOR2D rulerVec( m_end - m_origin );
VECTOR2D rulerVec( end - origin );
// constant text size on screen
SetConstantGlyphHeight( gal, 12.0 );
drawCursorStrings( gal, m_end, rulerVec );
drawCursorStrings( gal, end, rulerVec );
// tick label size
SetConstantGlyphHeight( gal, 10.0 );
@ -251,11 +255,11 @@ void RULER_ITEM::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
// basic tick size
const double minorTickLen = 5.0 / gal.GetWorldScale();
drawTicksAlongLine( gal, m_origin, rulerVec, minorTickLen );
drawTicksAlongLine( gal, origin, rulerVec, minorTickLen );
gal.SetStrokeColor( PreviewOverlayDefaultColor().WithAlpha( PreviewOverlayDeemphAlpha( true ) ) );
drawBacksideTicks( gal, m_origin, rulerVec, minorTickLen * majorTickLengthFactor, 2 );
drawBacksideTicks( gal, origin, rulerVec, minorTickLen * majorTickLengthFactor, 2 );
// draw the back of the origin "crosshair"
gal.DrawLine( m_origin, m_origin + rulerVec.Resize( -minorTickLen * midTickLengthFactor ) );
gal.DrawLine( origin, origin + rulerVec.Resize( -minorTickLen * midTickLengthFactor ) );
}

View File

@ -25,6 +25,7 @@
#define PREVIEW_ITEMS_RULER_ITEM_H
#include <base_struct.h>
#include <preview_items/two_point_geom_manager.h>
namespace KIGFX
{
@ -32,6 +33,7 @@ class GAL;
namespace PREVIEW
{
class TWO_POINT_GEOMETRY_MANAGER;
/**
* Class RULER_ITEM
@ -42,7 +44,7 @@ class RULER_ITEM : public EDA_ITEM
{
public:
RULER_ITEM();
RULER_ITEM( const TWO_POINT_GEOMETRY_MANAGER& m_geomMgr );
///> @copydoc EDA_ITEM::ViewBBox()
const BOX2I ViewBBox() const override;
@ -69,24 +71,9 @@ public:
return wxT( "RULER_ITEM" );
}
///> Set the origin of the ruler (the fixed end)
void SetOrigin( VECTOR2I aOrigin )
{
m_origin = aOrigin;
}
/**
* Set the current end of the rectangle (the end that moves
* with the cursor.
*/
void SetEnd( VECTOR2I aEnd )
{
m_end = aEnd;
}
private:
VECTOR2I m_origin, m_end;
const TWO_POINT_GEOMETRY_MANAGER& m_geomMgr;
};
} // PREVIEW

View File

@ -950,7 +950,10 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
: ID_PCB_MEASUREMENT_TOOL,
wxCURSOR_PENCIL, _( "Measure distance between two points" ) );
KIGFX::PREVIEW::RULER_ITEM ruler;
KIGFX::PREVIEW::TWO_POINT_GEOMETRY_MANAGER twoPtMgr;
KIGFX::PREVIEW::RULER_ITEM ruler( twoPtMgr );
view.Add( &ruler );
view.SetVisible( &ruler, false );
@ -974,8 +977,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
{
if( !evt->IsDrag( BUT_LEFT ) )
{
ruler.SetOrigin( cursorPos );
ruler.SetEnd( cursorPos );
twoPtMgr.SetOrigin( cursorPos );
twoPtMgr.SetEnd( cursorPos );
}
controls.CaptureCursor( true );
@ -988,8 +991,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
{
// make sure the origin is set before a drag starts
// otherwise you can miss a step
ruler.SetOrigin( cursorPos );
ruler.SetEnd( cursorPos );
twoPtMgr.SetOrigin( cursorPos );
twoPtMgr.SetEnd( cursorPos );
}
// second click or mouse up after drag ends
@ -1008,7 +1011,8 @@ int EDIT_TOOL::MeasureTool( const TOOL_EVENT& aEvent )
else if( originSet &&
( evt->IsMotion() || evt->IsDrag( BUT_LEFT ) ) )
{
ruler.SetEnd( cursorPos );
twoPtMgr.SetAngleSnap( evt->Modifier( MD_CTRL ) );
twoPtMgr.SetEnd( cursorPos );
view.SetVisible( &ruler, true );
view.Update( &ruler, KIGFX::GEOMETRY );