2021-01-16 23:17:32 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GRID_HELPER_H
|
|
|
|
#define GRID_HELPER_H
|
|
|
|
|
2023-08-23 17:05:08 +00:00
|
|
|
#include "tool/selection.h"
|
2021-02-10 00:01:36 +00:00
|
|
|
#include <tool/tool_manager.h>
|
2021-01-16 23:17:32 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <math/vector2d.h>
|
|
|
|
#include <origin_viewitem.h>
|
|
|
|
|
|
|
|
class TOOL_MANAGER;
|
|
|
|
class EDA_ITEM;
|
|
|
|
|
2023-08-23 17:05:08 +00:00
|
|
|
enum GRID_HELPER_GRIDS : int
|
|
|
|
{
|
|
|
|
// When the item doesn't match an override, use the current user grid
|
|
|
|
GRID_CURRENT,
|
|
|
|
|
|
|
|
GRID_CONNECTABLE,
|
|
|
|
GRID_WIRES,
|
2023-08-24 14:13:59 +00:00
|
|
|
GRID_VIAS,
|
2023-08-23 17:05:08 +00:00
|
|
|
GRID_TEXT,
|
|
|
|
GRID_GRAPHICS
|
|
|
|
};
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
class GRID_HELPER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GRID_HELPER( TOOL_MANAGER* aToolMgr );
|
2021-02-02 17:40:04 +00:00
|
|
|
virtual ~GRID_HELPER();
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
VECTOR2I GetGrid() const;
|
2023-08-11 15:58:33 +00:00
|
|
|
VECTOR2D GetVisibleGrid() const;
|
2021-01-16 23:17:32 +00:00
|
|
|
VECTOR2I GetOrigin() const;
|
|
|
|
|
|
|
|
void SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin = VECTOR2I( 0, 0 ) );
|
|
|
|
|
2023-08-23 17:05:08 +00:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
|
|
|
|
{
|
|
|
|
return Align( aPoint, GetGridSize( aGrid ), GetOrigin() );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual VECTOR2I AlignGrid( const VECTOR2I& aPoint, GRID_HELPER_GRIDS aGrid ) const
|
|
|
|
{
|
|
|
|
return AlignGrid( aPoint, GetGridSize( aGrid ), GetOrigin() );
|
|
|
|
}
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint ) const;
|
2023-07-23 17:51:42 +00:00
|
|
|
virtual VECTOR2I Align( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
|
|
|
|
const VECTOR2D& aOffset ) const;
|
2021-01-16 23:17:32 +00:00
|
|
|
|
|
|
|
VECTOR2I AlignGrid( const VECTOR2I& aPoint ) const;
|
2023-07-23 17:51:42 +00:00
|
|
|
VECTOR2I AlignGrid( const VECTOR2I& aPoint, const VECTOR2D& aGrid,
|
|
|
|
const VECTOR2D& aOffset ) const;
|
2021-01-16 23:17:32 +00:00
|
|
|
|
2023-08-23 17:05:08 +00:00
|
|
|
/**
|
|
|
|
* Gets the coarsest grid that applies to a selecion of items.
|
|
|
|
*/
|
|
|
|
virtual GRID_HELPER_GRIDS GetSelectionGrid( const SELECTION& aSelection ) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the coarsest grid that applies to an item.
|
|
|
|
*/
|
|
|
|
virtual GRID_HELPER_GRIDS GetItemGrid( const EDA_ITEM* aItem ) const { return GRID_CURRENT; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the size of the specified grid
|
|
|
|
*/
|
|
|
|
virtual VECTOR2D GetGridSize( GRID_HELPER_GRIDS aGrid ) const;
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetSkipPoint( const VECTOR2I& aPoint )
|
|
|
|
{
|
|
|
|
m_skipPoint = aPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We clear the skip point by setting it to an unreachable position, thereby preventing matching
|
|
|
|
*/
|
|
|
|
void ClearSkipPoint()
|
|
|
|
{
|
|
|
|
m_skipPoint = VECTOR2I( std::numeric_limits<int>::min(), std::numeric_limits<int>::min() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetSnap( bool aSnap ) { m_enableSnap = aSnap; }
|
2021-01-18 22:31:48 +00:00
|
|
|
bool GetSnap() const { return m_enableSnap; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetUseGrid( bool aSnapToGrid ) { m_enableGrid = aSnapToGrid; }
|
2021-01-18 22:31:48 +00:00
|
|
|
bool GetUseGrid() const { return m_enableGrid; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
void SetSnapLine( bool aSnap ) { m_enableSnapLine = aSnap; }
|
|
|
|
|
2021-02-02 17:40:04 +00:00
|
|
|
void SetMask( int aMask ) { m_maskTypes = aMask; }
|
|
|
|
void SetMaskFlag( int aFlag ) { m_maskTypes |= aFlag; }
|
|
|
|
void ClearMaskFlag( int aFlag ) { m_maskTypes = m_maskTypes & ~aFlag; }
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
enum ANCHOR_FLAGS {
|
|
|
|
CORNER = 1,
|
|
|
|
OUTLINE = 2,
|
|
|
|
SNAPPABLE = 4,
|
|
|
|
ORIGIN = 8,
|
|
|
|
VERTICAL = 16,
|
2021-02-02 17:40:04 +00:00
|
|
|
HORIZONTAL = 32,
|
|
|
|
ALL = CORNER | OUTLINE | SNAPPABLE | ORIGIN | VERTICAL | HORIZONTAL
|
2021-01-16 23:17:32 +00:00
|
|
|
};
|
|
|
|
|
2021-02-02 17:40:04 +00:00
|
|
|
protected:
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
struct ANCHOR
|
|
|
|
{
|
2021-07-26 23:47:26 +00:00
|
|
|
ANCHOR( const VECTOR2I& aPos, int aFlags = CORNER | SNAPPABLE, EDA_ITEM* aItem = nullptr ) :
|
2021-01-16 23:17:32 +00:00
|
|
|
pos( aPos ),
|
|
|
|
flags( aFlags ),
|
|
|
|
item( aItem )
|
|
|
|
{ };
|
|
|
|
|
|
|
|
VECTOR2I pos;
|
|
|
|
int flags;
|
|
|
|
EDA_ITEM* item;
|
|
|
|
|
|
|
|
double Distance( const VECTOR2I& aP ) const
|
|
|
|
{
|
2024-04-02 21:39:55 +00:00
|
|
|
return VECTOR2D( (double) aP.x - pos.x, (double) aP.y - pos.y ).EuclideanNorm();
|
2021-01-16 23:17:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void addAnchor( const VECTOR2I& aPos, int aFlags, EDA_ITEM* aItem )
|
|
|
|
{
|
2021-02-02 17:40:04 +00:00
|
|
|
if( ( aFlags & m_maskTypes ) == aFlags )
|
|
|
|
m_anchors.emplace_back( ANCHOR( aPos, aFlags, aItem ) );
|
2021-01-16 23:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clearAnchors()
|
|
|
|
{
|
|
|
|
m_anchors.clear();
|
2022-11-29 17:45:06 +00:00
|
|
|
m_snapItem = nullptr;
|
2021-01-16 23:17:32 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 00:01:36 +00:00
|
|
|
/**
|
|
|
|
* Check whether it is possible to use the grid -- this depends both on local grid helper
|
|
|
|
* settings and global (tool manager) KiCad settings.
|
|
|
|
*/
|
2023-09-18 23:52:27 +00:00
|
|
|
bool canUseGrid() const;
|
2021-02-10 00:01:36 +00:00
|
|
|
|
2023-07-23 17:51:42 +00:00
|
|
|
VECTOR2I computeNearest( const VECTOR2I& aPoint, const VECTOR2I& aGrid,
|
|
|
|
const VECTOR2I& aOffset ) const;
|
|
|
|
|
2021-01-16 23:17:32 +00:00
|
|
|
protected:
|
2023-08-26 12:29:24 +00:00
|
|
|
std::vector<ANCHOR> m_anchors;
|
|
|
|
|
|
|
|
TOOL_MANAGER* m_toolMgr;
|
|
|
|
std::optional<VECTOR2I> m_auxAxis;
|
|
|
|
|
|
|
|
int m_maskTypes; // Mask of allowed snap types
|
|
|
|
|
|
|
|
bool m_enableSnap; // Allow snapping to other items on the layers
|
|
|
|
bool m_enableGrid; // If true, allow snapping to grid
|
|
|
|
bool m_enableSnapLine; // Allow drawing lines from snap points
|
|
|
|
ANCHOR* m_snapItem; // Pointer to the currently snapped item in m_anchors
|
|
|
|
// (NULL if not snapped)
|
|
|
|
VECTOR2I m_skipPoint; // When drawing a line, we avoid snapping to the
|
|
|
|
// source point
|
|
|
|
KIGFX::ORIGIN_VIEWITEM m_viewSnapPoint;
|
|
|
|
KIGFX::ORIGIN_VIEWITEM m_viewSnapLine;
|
|
|
|
KIGFX::ORIGIN_VIEWITEM m_viewAxis;
|
2021-01-16 23:17:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|