Refactorization: moved drawing 45 degree multiple line to a function.

This commit is contained in:
Maciej Suminski 2014-02-14 14:24:12 +01:00
parent 3ecae4fa52
commit b1fb59ad13
2 changed files with 29 additions and 32 deletions

View File

@ -693,23 +693,9 @@ int DRAWING_TOOL::drawSegment( int aShape, bool aContinous )
{ {
// 45 degree lines // 45 degree lines
if( linesAngle45 && aShape == S_SEGMENT ) if( linesAngle45 && aShape == S_SEGMENT )
{ make45DegLine( &graphic );
VECTOR2D lineVector( wxPoint( cursorPos.x, cursorPos.y ) - graphic.GetStart() );
double angle = lineVector.Angle();
double newAngle = round( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
VECTOR2D newLineVector = lineVector.Rotate( newAngle - angle );
// Snap the new line to the grid // TODO fix it, does not work good..
VECTOR2D newLineEnd = VECTOR2D( graphic.GetStart() ) + newLineVector;
VECTOR2D snapped = m_view->GetGAL()->GetGridPoint( newLineEnd );
graphic.SetEnd( wxPoint( snapped.x, snapped.y ) );
}
else else
{
graphic.SetEnd( wxPoint( cursorPos.x, cursorPos.y ) ); graphic.SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
}
// Show a preview of the item // Show a preview of the item
preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
@ -847,27 +833,11 @@ int DRAWING_TOOL::drawZone( bool aKeepout )
else if( evt->IsMotion() ) else if( evt->IsMotion() )
{ {
helperLine->SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
// 45 degree lines // 45 degree lines
if( linesAngle45 ) if( linesAngle45 )
{ make45DegLine( helperLine );
VECTOR2D lineVector( wxPoint( cursorPos.x, cursorPos.y ) - helperLine->GetStart() );
double angle = lineVector.Angle();
double newAngle = round( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
VECTOR2D newLineVector = lineVector.Rotate( newAngle - angle );
// Snap the new line to the grid // TODO fix it, does not work good..
VECTOR2D newLineEnd = VECTOR2D( helperLine->GetStart() ) + newLineVector;
VECTOR2D snapped = m_view->GetGAL()->GetGridPoint( newLineEnd );
helperLine->SetEnd( wxPoint( snapped.x, snapped.y ) );
}
else else
{
helperLine->SetEnd( wxPoint( cursorPos.x, cursorPos.y ) ); helperLine->SetEnd( wxPoint( cursorPos.x, cursorPos.y ) );
}
// Show a preview of the item // Show a preview of the item
preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY ); preview.ViewUpdate( KIGFX::VIEW_ITEM::GEOMETRY );
@ -888,6 +858,27 @@ int DRAWING_TOOL::drawZone( bool aKeepout )
} }
void DRAWING_TOOL::make45DegLine( DRAWSEGMENT* aSegment ) const
{
VECTOR2I cursorPos = m_controls->GetCursorPosition();
// Current line vector
VECTOR2D lineVector( wxPoint( cursorPos.x, cursorPos.y ) - aSegment->GetStart() );
double angle = lineVector.Angle();
// Find the closest angle, which is a multiple of 45 degrees
double newAngle = round( angle / ( M_PI / 4.0 ) ) * M_PI / 4.0;
VECTOR2D newLineVector = lineVector.Rotate( newAngle - angle );
VECTOR2D newLineEnd = VECTOR2D( aSegment->GetStart() ) + newLineVector;
// Snap the new line to the grid
newLineEnd = m_view->GetGAL()->GetGridPoint( newLineEnd );
aSegment->SetEnd( wxPoint( newLineEnd.x, newLineEnd.y ) );
}
void DRAWING_TOOL::setTransitions() void DRAWING_TOOL::setTransitions()
{ {
Go( &DRAWING_TOOL::DrawLine, COMMON_ACTIONS::drawLine.MakeEvent() ); Go( &DRAWING_TOOL::DrawLine, COMMON_ACTIONS::drawLine.MakeEvent() );

View File

@ -34,6 +34,7 @@ namespace KIGFX
} }
class BOARD; class BOARD;
class PCB_EDIT_FRAME; class PCB_EDIT_FRAME;
class DRAWSEGMENT;
/** /**
* Class DRAWING_TOOL * Class DRAWING_TOOL
@ -84,6 +85,11 @@ private:
///> @param aKeepout decides if the drawn polygon is a zone or a keepout area. ///> @param aKeepout decides if the drawn polygon is a zone or a keepout area.
int drawZone( bool aKeepout ); int drawZone( bool aKeepout );
///> Forces a DRAWSEGMENT to be drawn at multiple of 45 degrees. The origin
///> stays the same, the end of the aSegment is modified according to the
///> current cursor position.
void make45DegLine( DRAWSEGMENT* aSegment ) const;
///> Sets up handlers for various events. ///> Sets up handlers for various events.
void setTransitions(); void setTransitions();