Fixed alignment to grid when there is a grid offset (GAL).

This commit is contained in:
Maciej Suminski 2015-06-30 14:08:03 +02:00
parent 5ca8e0b9d6
commit b5ef511063
5 changed files with 51 additions and 33 deletions

View File

@ -39,6 +39,7 @@
#include <gal/graphics_abstraction_layer.h>
#include <class_draw_panel_gal.h>
#include <tool/tool_manager.h>
#include <tools/common_actions.h>
// Max values for grid size
#define MAX_GRID_SIZE ( 50.0 * IU_PER_MM )
@ -282,9 +283,15 @@ bool PCB_BASE_FRAME::InvokeDialogGrid()
TOOL_MANAGER* mgr = GetToolManager();
if( mgr && IsGalCanvasActive() )
{
mgr->RunAction( "common.Control.gridPreset", true,
screen->GetGridCmdId() - ID_POPUP_GRID_LEVEL_1000 );
TOOL_EVENT gridOriginUpdate = COMMON_ACTIONS::gridSetOrigin.MakeEvent();
gridOriginUpdate.SetParameter( new VECTOR2D( grid_origin ) );
mgr->ProcessEvent( gridOriginUpdate );
}
m_canvas->Refresh();
return true;

View File

@ -228,7 +228,7 @@ int EDIT_TOOL::Main( const TOOL_EVENT& aEvent )
if( m_dragging )
{
m_cursor = grid.BestSnapAnchor( evt->Position(), selection.Item<BOARD_ITEM>( 0 ) );
getViewControls()->ForceCursorPosition ( true, m_cursor );
getViewControls()->ForceCursorPosition( true, m_cursor );
wxPoint movement = wxPoint( m_cursor.x, m_cursor.y ) -
selection.Item<BOARD_ITEM>( 0 )->GetPosition();

View File

@ -60,26 +60,27 @@ void GRID_HELPER::SetGrid( int aSize )
void GRID_HELPER::SetOrigin( const VECTOR2I& aOrigin )
{
assert( false );
}
VECTOR2I GRID_HELPER::GetGrid()
VECTOR2I GRID_HELPER::GetGrid() const
{
PCB_SCREEN* screen = m_frame->GetScreen();
const wxRealPoint& size = screen->GetGridSize();
return VECTOR2I ( KiROUND( size.x ), KiROUND( size.y ) );
return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
}
VECTOR2I GRID_HELPER::GetOrigin()
VECTOR2I GRID_HELPER::GetOrigin() const
{
return VECTOR2I( 0, 0 );
return VECTOR2I( m_frame->GetGridOrigin() );
}
void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I aOrigin, bool aEnableDiagonal )
void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin, bool aEnableDiagonal )
{
if( aEnable )
m_auxAxis = aOrigin;
@ -90,13 +91,13 @@ void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I aOrigin, bool aEnable
}
VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint )
VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const
{
const VECTOR2D gridOffset( GetOrigin () );
const VECTOR2D gridOffset( GetOrigin() );
const VECTOR2D gridSize( GetGrid() );
VECTOR2I nearest( round( ( aPoint.x - gridOffset.x ) / gridSize.x ) * gridSize.x + gridOffset.x,
round( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
round( ( aPoint.y - gridOffset.y ) / gridSize.y ) * gridSize.y + gridOffset.y );
if( !m_auxAxis )
return nearest;
@ -152,7 +153,7 @@ VECTOR2I GRID_HELPER::BestDragOrigin( const VECTOR2I &aMousePos, BOARD_ITEM* aIt
}
std::set<BOARD_ITEM*> GRID_HELPER::queryVisible( const BOX2I& aArea )
std::set<BOARD_ITEM*> GRID_HELPER::queryVisible( const BOX2I& aArea ) const
{
std::set<BOARD_ITEM*> items;
@ -172,12 +173,12 @@ std::set<BOARD_ITEM*> GRID_HELPER::queryVisible( const BOX2I& aArea )
}
VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I &aOrigin, BOARD_ITEM* aDraggedItem )
VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem )
{
double worldScale = m_frame->GetGalCanvas()->GetGAL()->GetWorldScale();
int snapRange = (int) ( 100.0 / worldScale );
BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange/2 ), VECTOR2I( snapRange, snapRange ) );
BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), VECTOR2I( snapRange, snapRange ) );
clearAnchors();
@ -197,9 +198,9 @@ VECTOR2I GRID_HELPER::BestSnapAnchor( const VECTOR2I &aOrigin, BOARD_ITEM* aDrag
if( nearest && snapDist < gridDist )
return nearest->pos;
}
}
return nearestGrid;
return nearestGrid;
}
@ -323,7 +324,7 @@ void GRID_HELPER::computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos )
}
GRID_HELPER::ANCHOR* GRID_HELPER::nearestAnchor( VECTOR2I aPos, int aFlags, LSET aMatchLayers )
GRID_HELPER::ANCHOR* GRID_HELPER::nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers )
{
double minDist = std::numeric_limits<double>::max();
ANCHOR* best = NULL;

View File

@ -43,15 +43,15 @@ public:
void SetGrid( int aSize );
void SetOrigin( const VECTOR2I& aOrigin );
VECTOR2I GetGrid();
VECTOR2I GetOrigin();
VECTOR2I GetGrid() const;
VECTOR2I GetOrigin() const;
void SetAuxAxes( bool aEnable, const VECTOR2I aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false );
void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ), bool aEnableDiagonal = false );
VECTOR2I Align( const VECTOR2I& aPoint );
VECTOR2I Align( const VECTOR2I& aPoint ) const;
VECTOR2I BestDragOrigin ( const VECTOR2I &aMousePos, BOARD_ITEM* aItem );
VECTOR2I BestSnapAnchor ( const VECTOR2I &aOrigin, BOARD_ITEM* aDraggedItem );
VECTOR2I BestDragOrigin( const VECTOR2I& aMousePos, BOARD_ITEM* aItem );
VECTOR2I BestSnapAnchor( const VECTOR2I& aOrigin, BOARD_ITEM* aDraggedItem );
private:
enum ANCHOR_FLAGS {
@ -70,28 +70,28 @@ private:
int flags;
BOARD_ITEM* item;
double Distance( const VECTOR2I& aP )
double Distance( const VECTOR2I& aP ) const
{
return ( aP - pos ).EuclideanNorm();
}
bool CanSnapItem( const BOARD_ITEM* aItem );
//bool CanSnapItem( const BOARD_ITEM* aItem ) const;
};
std::vector<ANCHOR> m_anchors;
std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea );
std::set<BOARD_ITEM*> queryVisible( const BOX2I& aArea ) const;
void addAnchor( VECTOR2I aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL )
void addAnchor( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, BOARD_ITEM* aItem = NULL )
{
m_anchors.push_back( ANCHOR( aPos, aFlags, aItem ) );
}
ANCHOR* nearestAnchor( VECTOR2I aPos, int aFlags, LSET aMatchLayers );
ANCHOR* nearestAnchor( const VECTOR2I& aPos, int aFlags, LSET aMatchLayers );
void computeAnchors( BOARD_ITEM* aItem, const VECTOR2I& aRefPos );
void clearAnchors ()
void clearAnchors()
{
m_anchors.clear();
}

View File

@ -472,13 +472,23 @@ static bool setOrigin( KIGFX::VIEW* aView, PCB_BASE_FRAME* aFrame,
int PCBNEW_CONTROL::GridSetOrigin( const TOOL_EVENT& aEvent )
{
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
VECTOR2D* origin = aEvent.Parameter<VECTOR2D*>();
// TODO it will not check the toolbar button in module editor, as it uses a different ID..
m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) );
picker->SetClickHandler( boost::bind( setOrigin, getView(), m_frame, m_gridOrigin, _1 ) );
picker->Activate();
if( origin )
{
setOrigin( getView(), m_frame, m_gridOrigin, *origin );
delete origin;
}
else
{
PICKER_TOOL* picker = m_toolMgr->GetTool<PICKER_TOOL>();
assert( picker );
// TODO it will not check the toolbar button in module editor, as it uses a different ID..
m_frame->SetToolID( ID_PCB_PLACE_GRID_COORD_BUTT, wxCURSOR_PENCIL, _( "Adjust grid origin" ) );
picker->SetClickHandler( boost::bind( setOrigin, getView(), m_frame, m_gridOrigin, _1 ) );
picker->Activate();
}
return 0;
}