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 (C) 2024 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
71static void DrawLineEndpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
72{
73 const int nodeRadius = aSize / 8;
74 const VECTOR2I lineStart = aPosition - VECTOR2I( aSize / 2 - nodeRadius, 0 );
75
76 DrawSnapNode( aGal, lineStart, nodeRadius );
77 aGal.DrawLine( lineStart, lineStart + VECTOR2I( aSize - nodeRadius, 0 ) );
78}
79
80static void DrawMidpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
81{
82 const int nodeRadius = aSize / 8;
83
84 DrawSnapNode( aGal, aPosition, nodeRadius );
85 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
86}
87
88static void DrawCentrePointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
89{
90 const int ringRadius = aSize / 4;
91
92 aGal.DrawCircle( aPosition, ringRadius );
93
94 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
95 aGal.DrawLine( aPosition - VECTOR2I( 0, aSize / 2 ), aPosition + VECTOR2I( 0, aSize / 2 ) );
96}
97
98static void DrawQuadrantPointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
99{
100 const int nodeRadius = aSize / 8;
101
102 const VECTOR2I quadPoint = aPosition - VECTOR2I( 0, aSize / 2 - nodeRadius );
103 const int arcRadius = aSize - nodeRadius * 2;
104
105 DrawSnapNode( aGal, quadPoint, nodeRadius );
106
107 // Most of the top half of a circle, passing through the node centre
108 const VECTOR2I arcCenter = quadPoint + VECTOR2I( 0, arcRadius );
109 aGal.DrawArc( arcCenter, arcRadius, EDA_ANGLE( -160, EDA_ANGLE_T::DEGREES_T ),
110 EDA_ANGLE( 140, EDA_ANGLE_T::DEGREES_T ) );
111}
112
113static void DrawIntersectionIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
114{
115 const int nodeRadius = aSize / 8;
116
117 DrawSnapNode( aGal, aPosition, nodeRadius );
118
119 // Slightly squashed X shape
120 VECTOR2I xLeg = VECTOR2I( aSize / 2, aSize / 3 );
121
122 aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
123 xLeg.y = -xLeg.y;
124 aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
125}
126
127static void DrawOnElementIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
128{
129 const int nodeRadius = aSize / 8;
130
131 // A bit like midpoint by off to one side
132 DrawSnapNode( aGal, aPosition + VECTOR2I( aSize / 4, 0 ), nodeRadius );
133
134 aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
135}
136
137
138void SNAP_INDICATOR::ViewDraw( int, VIEW* aView ) const
139{
140 GAL& gal = *aView->GetGAL();
141
142 // Draw the origin marker
143 ORIGIN_VIEWITEM::ViewDraw( 0, aView );
144
145 gal.SetFillColor( m_color );
146
147 // Put the icon near the x-line, so it doesn't overlap with the ruler helpers
148 const VECTOR2I typeIconPos = m_position + aView->ToWorld( { 24, 10 }, false );
149 const int size = aView->ToWorld( 16 );
150
151 // For now, choose the first type that is set
152 if( m_snapTypes & POINT_TYPE::PT_CORNER )
153 {
154 DrawCornerIcon( gal, typeIconPos, size );
155 }
156 else if( m_snapTypes & POINT_TYPE::PT_END )
157 {
158 DrawLineEndpointIcon( gal, typeIconPos, size );
159 }
160 else if( m_snapTypes & POINT_TYPE::PT_MID )
161 {
162 DrawMidpointIcon( gal, typeIconPos, size );
163 }
164 else if( m_snapTypes & POINT_TYPE::PT_CENTER )
165 {
166 DrawCentrePointIcon( gal, typeIconPos, size );
167 }
168 else if( m_snapTypes & POINT_TYPE::PT_QUADRANT )
169 {
170 DrawQuadrantPointIcon( gal, typeIconPos, size );
171 }
172 else if( m_snapTypes & POINT_TYPE::PT_INTERSECTION )
173 {
174 DrawIntersectionIcon( gal, typeIconPos, size );
175 }
176 else if( m_snapTypes & POINT_TYPE::PT_ON_ELEMENT )
177 {
178 DrawOnElementIcon( gal, typeIconPos, size );
179 }
180}
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 symbol.
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 color.
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:68
GAL * GetGAL() const
Return the #GAL this view is using to draw graphical primitives.
Definition: view.h:203
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:484
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:691