pcbnew - refactor move exactly tool (2/2)

This patch does the following:
* extracts a method to determine the anchor point of the move
* improves the logic of the anchor point determination for selections of multiple objects which do not contain footprints
This commit is contained in:
Robbert Lagerweij 2017-06-22 21:30:23 +02:00 committed by Maciej Suminski
parent bf832dd941
commit b7f2525c5d
3 changed files with 76 additions and 49 deletions

View File

@ -7,6 +7,7 @@ include_directories(
../../3d-viewer
../../pcbnew
../../polygon
../dialogs
${INC_AFTER}
)

View File

@ -738,8 +738,46 @@ int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent )
VECTOR2I rp = selection.GetCenter();
wxPoint rotPoint( rp.x, rp.y );
// Begin at the center of the selection determined above
wxPoint anchorPoint = rotPoint;
wxPoint anchorPoint = getAnchorPoint( selection, params );
wxPoint finalMoveVector = params.translation - anchorPoint;
// Make sure the rotation is from the right reference point
rotPoint += finalMoveVector;
for( auto item : selection )
{
m_commit->Modify( item );
static_cast<BOARD_ITEM*>( item )->Move( finalMoveVector );
static_cast<BOARD_ITEM*>( item )->Rotate( rotPoint, params.rotation );
if( !m_dragging )
getView()->Update( item );
}
m_commit->Push( _( "Move exact" ) );
if( selection.IsHover() )
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
m_toolMgr->RunAction( PCB_ACTIONS::selectionModified, true );
}
return 0;
}
wxPoint EDIT_TOOL::getAnchorPoint( const SELECTION &selection, const MOVE_PARAMETERS &params ) const
{
wxPoint anchorPoint;
if( params.origin == RELATIVE_TO_CURRENT_POSITION )
{
return wxPoint( 0, 0 );
}
// set default anchor
VECTOR2I rp = selection.GetCenter();
anchorPoint = wxPoint( rp.x, rp.y );
// If the anchor is not ANCHOR_FROM_LIBRARY then the user applied an override.
// Also run through this block if only one item is slected because it may be a module,
@ -748,12 +786,9 @@ int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent )
{
BOARD_ITEM* topLeftItem = static_cast<BOARD_ITEM*>( selection.GetTopLeftModule() );
// no module found if the GetTopLeftModule() returns null, retry for
if( topLeftItem == nullptr )
// no module found if the GetTopLeftModule() returns null
if( topLeftItem != nullptr )
{
topLeftItem = static_cast<BOARD_ITEM*>( selection.GetTopLeftItem() );
anchorPoint = topLeftItem->GetPosition();
}
if( topLeftItem->Type() == PCB_MODULE_T )
{
@ -787,31 +822,19 @@ int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent )
}
}
}
wxPoint finalMoveVector = params.translation - anchorPoint;
// Make sure the rotation is from the right reference point
rotPoint += finalMoveVector;
for( auto item : selection )
else // no module found in the selection
{
m_commit->Modify( item );
static_cast<BOARD_ITEM*>( item )->Move( finalMoveVector );
static_cast<BOARD_ITEM*>( item )->Rotate( rotPoint, params.rotation );
if( !m_dragging )
getView()->Update( item );
// in a selection of non-modules
if( params.anchor == ANCHOR_TOP_LEFT_PAD )
{
// approach the top left pad override for non-modules by using the position of
// the topleft item as an anchor
topLeftItem = static_cast<BOARD_ITEM*>( selection.GetTopLeftItem() );
anchorPoint = topLeftItem->GetPosition();
}
m_commit->Push( _( "Move exact" ) );
if( selection.IsHover() )
m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
m_toolMgr->RunAction( PCB_ACTIONS::selectionModified, true );
}
return 0;
}
return anchorPoint;
}

View File

@ -28,6 +28,7 @@
#include <math/vector2d.h>
#include <tools/pcb_tool.h>
#include <dialogs/dialog_move_exact.h>
class BOARD_COMMIT;
class BOARD_ITEM;
@ -205,6 +206,8 @@ private:
}
std::unique_ptr<BOARD_COMMIT> m_commit;
wxPoint getAnchorPoint( const SELECTION &selection, const MOVE_PARAMETERS &params ) const;
};
#endif