KiCad PCB EDA Suite
Loading...
Searching...
No Matches
snap_indicator.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
27
28using namespace KIGFX;
29
30SNAP_INDICATOR::SNAP_INDICATOR( const COLOR4D& aColor, int aSize, const VECTOR2D& aPosition ) :
31 ORIGIN_VIEWITEM( aColor, CIRCLE_CROSS, aSize, aPosition )
32{
33}
34
35
37{
39}
40
41
43{
45}
46
47
48static void DrawSnapNode( GAL& aGal, const VECTOR2I& aPosition, int aNodeRadius )
49{
50 aGal.SetIsFill( true );
51 aGal.SetIsStroke( false );
52
53 aGal.DrawCircle( aPosition, aNodeRadius );
54
55 aGal.SetIsFill( false );
56 aGal.SetIsStroke( true );
57}
58
59
60static void DrawCornerIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
61{
62 const int nodeRad = aSize / 8;
63 const VECTOR2I corner =
64 aPosition - VECTOR2I( aSize / 2, aSize / 2 ) + VECTOR2I( nodeRad, nodeRad );
65 aGal.DrawLine( corner, corner + VECTOR2I( aSize - nodeRad, 0 ) );
66 aGal.DrawLine( corner, corner + VECTOR2I( 0, aSize - nodeRad ) );
67
68 DrawSnapNode( aGal, corner, nodeRad );
69}
70
71
72static void DrawLineEndpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
73{
74 const int nodeRadius = aSize / 8;
75 const VECTOR2I lineStart = aPosition - VECTOR2I( aSize / 2 - nodeRadius, 0 );
76
77 DrawSnapNode( aGal, lineStart, nodeRadius );
78 aGal.DrawLine( lineStart, lineStart + VECTOR2I( aSize - nodeRadius, 0 ) );
79}
80
81
82static void DrawMidpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
83{
84 const int nodeRadius = aSize / 8;
85
86 DrawSnapNode( aGal, aPosition, nodeRadius );
87 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
88}
89
90
91static void DrawCentrePointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
92{
93 const int ringRadius = aSize / 4;
94
95 aGal.DrawCircle( aPosition, ringRadius );
96
97 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
98 aGal.DrawLine( aPosition - VECTOR2I( 0, aSize / 2 ), aPosition + VECTOR2I( 0, aSize / 2 ) );
99}
100
101
102static void DrawQuadrantPointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
103{
104 const int nodeRadius = aSize / 8;
105
106 const VECTOR2I quadPoint = aPosition - VECTOR2I( 0, aSize / 2 - nodeRadius );
107 const int arcRadius = aSize - nodeRadius * 2;
108
109 DrawSnapNode( aGal, quadPoint, nodeRadius );
110
111 // Most of the top half of a circle, passing through the node centre
112 const VECTOR2I arcCenter = quadPoint + VECTOR2I( 0, arcRadius );
113 aGal.DrawArc( arcCenter, arcRadius, EDA_ANGLE( -160, EDA_ANGLE_T::DEGREES_T ),
114 EDA_ANGLE( 140, EDA_ANGLE_T::DEGREES_T ) );
115}
116
117
118static void DrawIntersectionIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
119{
120 const int nodeRadius = aSize / 8;
121
122 DrawSnapNode( aGal, aPosition, nodeRadius );
123
124 // Slightly squashed X shape
125 VECTOR2I xLeg = VECTOR2I( aSize / 2, aSize / 3 );
126
127 aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
128 xLeg.y = -xLeg.y;
129 aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
130}
131
132
133static void DrawOnElementIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
134{
135 const int nodeRadius = aSize / 8;
136
137 // A bit like midpoint by off to one side
138 DrawSnapNode( aGal, aPosition + VECTOR2I( aSize / 4, 0 ), nodeRadius );
139
140 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
141}
142
143
144void SNAP_INDICATOR::ViewDraw( int, VIEW* aView ) const
145{
146 GAL& gal = *aView->GetGAL();
147
148 // Draw the origin marker
149 ORIGIN_VIEWITEM::ViewDraw( 0, aView );
150
151 gal.SetFillColor( m_color );
152
153 // Put the icon near the x-line, so it doesn't overlap with the ruler helpers
154 const VECTOR2I typeIconPos = m_position + aView->ToWorld( { 24, 10 }, false );
155 const int size = aView->ToWorld( 16 );
156
157 // For now, choose the first type that is set
158 if( m_snapTypes & POINT_TYPE::PT_CORNER )
159 {
160 DrawCornerIcon( gal, typeIconPos, size );
161 }
162 else if( m_snapTypes & POINT_TYPE::PT_END )
163 {
164 DrawLineEndpointIcon( gal, typeIconPos, size );
165 }
166 else if( m_snapTypes & POINT_TYPE::PT_MID )
167 {
168 DrawMidpointIcon( gal, typeIconPos, size );
169 }
170 else if( m_snapTypes & POINT_TYPE::PT_CENTER )
171 {
172 DrawCentrePointIcon( gal, typeIconPos, size );
173 }
174 else if( m_snapTypes & POINT_TYPE::PT_QUADRANT )
175 {
176 DrawQuadrantPointIcon( gal, typeIconPos, size );
177 }
178 else if( m_snapTypes & POINT_TYPE::PT_INTERSECTION )
179 {
180 DrawIntersectionIcon( gal, typeIconPos, size );
181 }
182 else if( m_snapTypes & POINT_TYPE::PT_ON_ELEMENT )
183 {
184 DrawOnElementIcon( gal, typeIconPos, size );
185 }
186}
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
Abstract interface for drawing on a 2D-surface.
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void SetFillColor(const COLOR4D &aColor)
Set the fill color.
virtual void DrawCircle(const VECTOR2D &aCenterPoint, double aRadius)
Draw a circle using world coordinates.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle)
Draw an arc.
COLOR4D m_color
Marker color.
VECTOR2D m_position
Marker coordinates.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
int m_size
Marker size (in pixels).
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
View item to draw an origin marker with an optional snap type indicator.
void ViewDraw(int aLayer, VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
SNAP_INDICATOR(const COLOR4D &aColor=COLOR4D::WHITE, int aSize=16, const VECTOR2D &aPosition=VECTOR2D(0, 0))
SNAP_INDICATOR * Clone() const override
Create a duplicate of this item with linked list members set to NULL.
const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition: view.h:67
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:198
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition: view.cpp:457
The Cairo implementation of the graphics abstraction layer.
Definition: color4d.cpp:247
static void DrawCentrePointIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawIntersectionIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawLineEndpointIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawQuadrantPointIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawOnElementIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawSnapNode(GAL &aGal, const VECTOR2I &aPosition, int aNodeRadius)
static void DrawCornerIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
static void DrawMidpointIcon(GAL &aGal, const VECTOR2I &aPosition, int aSize)
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:695