KiCad PCB EDA Suite
Loading...
Searching...
No Matches
edit_points.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) 2014-2019 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
23#include <gal/color4d.h>
24#include <gal/painter.h>
25#include <math/util.h> // for KiROUND
26#include <geometry/seg.h>
27#include <wx/string.h>
28#include <vector>
29#include <algorithm>
30#include "tool/edit_points.h"
31
32
33bool EDIT_POINT::WithinPoint( const VECTOR2I& aPoint, unsigned int aSize ) const
34{
35 // Corners of the EDIT_POINT square
36 VECTOR2I topLeft = GetPosition() - aSize;
37 VECTOR2I bottomRight = GetPosition() + aSize;
38
39 return ( aPoint.x > topLeft.x && aPoint.y > topLeft.y &&
40 aPoint.x < bottomRight.x && aPoint.y < bottomRight.y );
41}
42
43
46 m_parent( aParent ),
47 m_swapX( false ),
48 m_swapY( false ),
49 m_allowPoints( true ),
50 m_lineStateVisible( false )
51{
52}
53
54
55EDIT_POINT* EDIT_POINTS::FindPoint( const VECTOR2I& aLocation, KIGFX::VIEW *aView ) // fixme: ugly
56{
57 unsigned size = std::abs( KiROUND( aView->ToWorld( EDIT_POINT::POINT_SIZE ) ) );
58
59 if( m_allowPoints )
60 {
61 for( EDIT_POINT& point : m_points )
62 {
63 if( point.WithinPoint( aLocation, size ) )
64 return &point;
65 }
66 }
67
68 for( EDIT_LINE& line : m_lines )
69 {
70 if( line.WithinPoint( aLocation, size ) )
71 return &line;
72 }
73
74 return nullptr;
75}
76
77
78int EDIT_POINTS::GetContourStartIdx( int aPointIdx ) const
79{
80 int lastIdx = 0;
81
82 for( int idx : m_contours )
83 {
84 if( idx >= aPointIdx )
85 return lastIdx;
86
87 lastIdx = idx + 1;
88 }
89
90 return lastIdx;
91}
92
93
94int EDIT_POINTS::GetContourEndIdx( int aPointIdx ) const
95{
96 for( int idx : m_contours )
97 {
98 if( idx >= aPointIdx )
99 return idx;
100 }
101
102 return m_points.size() - 1;
103}
104
105
106bool EDIT_POINTS::IsContourStart( int aPointIdx ) const
107{
108 for( int idx : m_contours )
109 {
110 if( idx + 1 == aPointIdx )
111 return true;
112
113 // the list is sorted, so we cannot expect it any further
114 if( idx > aPointIdx )
115 break;
116 }
117
118 return ( aPointIdx == 0 );
119}
120
121
122bool EDIT_POINTS::IsContourEnd( int aPointIdx ) const
123{
124 for( int idx : m_contours )
125 {
126 if( idx == aPointIdx )
127 return true;
128
129 // the list is sorted, so we cannot expect it any further
130 if( idx > aPointIdx )
131 break;
132 }
133
134 // the end of the list surely is the end of a contour
135 return ( aPointIdx == (int) m_points.size() - 1 );
136}
137
138
139EDIT_POINT* EDIT_POINTS::Previous( const EDIT_POINT& aPoint, bool aTraverseContours )
140{
141 for( unsigned int i = 0; i < m_points.size(); ++i )
142 {
143 if( m_points[i] == aPoint )
144 {
145 if( !aTraverseContours && IsContourStart( i ) )
146 return &m_points[GetContourEndIdx( i )];
147
148 if( i == 0 )
149 return &m_points[m_points.size() - 1];
150 else
151 return &m_points[i - 1];
152 }
153 }
154
155 return nullptr;
156}
157
158
160{
161 for( unsigned int i = 0; i < m_lines.size(); ++i )
162 {
163 if( m_lines[i] == aLine )
164 {
165 if( i == 0 )
166 return &m_lines[m_lines.size() - 1];
167 else
168 return &m_lines[i - 1];
169 }
170 }
171
172 return nullptr;
173}
174
175
176EDIT_POINT* EDIT_POINTS::Next( const EDIT_POINT& aPoint, bool aTraverseContours )
177{
178 for( unsigned int i = 0; i < m_points.size(); ++i )
179 {
180 if( m_points[i] == aPoint )
181 {
182 if( !aTraverseContours && IsContourEnd( i ) )
183 return &m_points[GetContourStartIdx( i )];
184
185 if( i == m_points.size() - 1 )
186 return &m_points[0];
187 else
188 return &m_points[i + 1];
189 }
190 }
191
192 return nullptr;
193}
194
195
197{
198 for( unsigned int i = 0; i < m_lines.size(); ++i )
199 {
200 if( m_lines[i] == aLine )
201 {
202 if( i == m_lines.size() - 1 )
203 return &m_lines[0];
204 else
205 return &m_lines[i + 1];
206 }
207 }
208
209 return nullptr;
210}
211
212
214{
215 BOX2I box;
216 bool empty = true;
217
218 for( const auto& point : m_points )
219 {
220 if( empty )
221 {
222 box.SetOrigin( point.GetPosition() );
223 empty = false;
224 }
225 else
226 {
227 box.Merge( point.GetPosition() );
228 }
229 }
230
231 for( const auto& line : m_lines )
232 {
233 if( empty )
234 {
235 box.SetOrigin( line.GetOrigin().GetPosition() );
236 box.SetEnd( line.GetEnd().GetPosition() );
237 empty = false;
238 }
239 else
240 {
241 box.Merge( line.GetOrigin().GetPosition() );
242 box.Merge( line.GetEnd().GetPosition() );
243 }
244 }
245
246 return box;
247}
248
249
250void EDIT_POINTS::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
251{
252 KIGFX::GAL* gal = aView->GetGAL();
253 KIGFX::RENDER_SETTINGS* settings = aView->GetPainter()->GetSettings();
254 KIGFX::COLOR4D drawColor = settings->GetLayerColor( LAYER_AUX_ITEMS );
255
256 // Don't assume LAYER_AUX_ITEMS is always a good choice. Compare with background.
257 if( aView->GetGAL()->GetClearColor().Distance( drawColor ) < 0.5 )
258 drawColor.Invert();
259
260 // Linear darkening doesn't fit well with human color perception, and there's no guarantee
261 // that there's enough room for contrast either.
262 KIGFX::COLOR4D borderColor;
263 KIGFX::COLOR4D highlightColor;
264 double brightness = drawColor.GetBrightness();
265
266 if( brightness > 0.5 )
267 {
268 borderColor = drawColor.Darkened( 0.7 ).WithAlpha( 0.8 );
269 highlightColor = drawColor.Darkened( 0.5 ).WithAlpha( 0.8 );
270 }
271 else if( brightness > 0.2 )
272 {
273 borderColor = drawColor.Brightened( 0.4 ).WithAlpha( 0.8 );
274 highlightColor = drawColor.Brightened( 0.3 ).WithAlpha( 0.8 );
275 }
276 else
277 {
278 borderColor = drawColor.Brightened( 0.7 ).WithAlpha( 0.8 );
279 highlightColor = drawColor.Brightened( 0.5 ).WithAlpha( 0.8 );
280 }
281
283 gal->SetFillColor( drawColor );
284 gal->SetStrokeColor( borderColor );
285 gal->SetIsFill( true );
286 gal->SetIsStroke( true );
287 gal->SetLayerDepth( gal->GetMinDepth() );
288
289 double size = aView->ToWorld( EDIT_POINT::POINT_SIZE ) / 2.0;
290 double borderSize = aView->ToWorld( EDIT_POINT::BORDER_SIZE );
291 double hoverSize = aView->ToWorld( EDIT_POINT::HOVER_SIZE );
292
293 auto drawPoint = [&]( const EDIT_POINT& aPoint, bool aDrawCircle = false, bool aShowState = true )
294 {
295 bool fill = true;
296
297 if( aShowState && aPoint.IsSelected() )
298 {
299 gal->SetStrokeColor( highlightColor );
300 gal->SetLineWidth( hoverSize );
301 gal->SetFillColor( highlightColor );
302 }
303 else if( aShowState && ( aPoint.IsHover() || aPoint.IsActive() ) )
304 {
305 gal->SetStrokeColor( highlightColor );
306 gal->SetLineWidth( hoverSize );
307 gal->SetFillColor( drawColor );
308 }
309 else if( aShowState && aPoint.IsDependent() )
310 {
311 gal->SetStrokeColor( highlightColor );
312 gal->SetLineWidth( borderSize );
313 fill = false;
314 }
315 else
316 {
317 gal->SetStrokeColor( borderColor );
318 gal->SetLineWidth( borderSize );
319 gal->SetFillColor( drawColor );
320 }
321
322 gal->SetIsFill( fill );
323
324 if( aDrawCircle )
325 gal->DrawCircle( aPoint.GetPosition(), size );
326 else
327 gal->DrawRectangle( aPoint.GetPosition() - size, aPoint.GetPosition() + size );
328
329 gal->SetIsFill( true );
330 };
331
332 for( const EDIT_POINT& point : m_points )
333 drawPoint( point, point.DrawCircle() );
334
335 for( const EDIT_LINE& line : m_lines )
336 {
337 if( line.HasCenterPoint() )
338 {
339 // Only an editor that lets an edge be picked on its own shows the handle's state
340 drawPoint( line, true, m_lineStateVisible );
341 }
342
343 if( line.DrawLine() )
344 {
345 gal->SetLineWidth( borderSize / 4 );
346 gal->SetStrokeColor( borderColor );
347 gal->DrawLine( line.GetOrigin().GetPosition(), line.GetEnd().GetPosition() );
348 }
349 }
350
351}
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:240
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:597
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:300
EDA_ITEM(EDA_ITEM *parent, KICAD_T idType, bool isSCH_ITEM=false, bool isBOARD_ITEM=false)
Definition eda_item.cpp:84
Represent a line connecting two EDIT_POINTs.
bool IsContourStart(int aPointIdx) const
Check if a point with given index is a contour origin.
bool m_allowPoints
If false, only allow editing of EDIT_LINES.
int GetContourEndIdx(int aPointIdx) const
Return index of the contour finish for a point with given index.
virtual const BOX2I ViewBBox() const override
Return the bounding box of the item covering all its layers.
EDIT_POINT * Previous(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is after the given point in the list.
EDIT_POINT * Next(const EDIT_POINT &aPoint, bool aTraverseContours=true)
Return the point that is before the given point in the list.
std::deque< EDIT_LINE > m_lines
EDIT_LINEs for modifying m_parent.
std::list< int > m_contours
Indices of end contour points.
EDIT_POINTS(EDA_ITEM *aParent)
bool m_swapY
Parent's Y coords are inverted.
bool IsContourEnd(int aPointIdx) const
Check is a point with given index is a contour finish.
EDA_ITEM * m_parent
Parent of the EDIT_POINTs.
std::deque< EDIT_POINT > m_points
EDIT_POINTs for modifying m_parent.
bool m_swapX
Parent's X coords are inverted.
EDIT_POINT * FindPoint(const VECTOR2I &aLocation, KIGFX::VIEW *aView)
Return a point that is at given coordinates or NULL if there is no such point.
int GetContourStartIdx(int aPointIdx) const
Return index of the contour origin for a point with given index.
bool m_lineStateVisible
If false, an edge handle draws plain whatever state it carries.
virtual void ViewDraw(int aLayer, KIGFX::VIEW *aView) const override
Draw the parts of the object belonging to layer aLayer.
Represent a single point that can be used for modifying items.
Definition edit_points.h:44
static const int HOVER_SIZE
Border size when hovering.
bool IsActive() const
static const int POINT_SIZE
Single point size in pixels.
bool IsDependent() const
virtual VECTOR2I GetPosition() const
Return coordinates of an EDIT_POINT.
Definition edit_points.h:72
bool WithinPoint(const VECTOR2I &aPoint, unsigned int aSize) const
Check if given point is within a square centered in the EDIT_POINT position.
bool IsHover() const
static const int BORDER_SIZE
Border size when not hovering.
bool IsSelected() const
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition color4d.h:309
COLOR4D & Invert()
Makes the color inverted, alpha remains the same.
Definition color4d.h:239
COLOR4D Darkened(double aFactor) const
Return a color that is darker by a given factor, without modifying object.
Definition color4d.h:279
double GetBrightness() const
Returns the brightness value of the color ranged from 0.0 to 1.0.
Definition color4d.h:331
COLOR4D Brightened(double aFactor) const
Return a color that is brighter by a given factor, without modifying object.
Definition color4d.h:265
double Distance(const COLOR4D &other) const
Returns the distance (in RGB space) between two colors.
Definition color4d.cpp:551
Attribute save/restore for GAL attributes.
Abstract interface for drawing on a 2D-surface.
virtual void SetLayerDepth(double aLayerDepth)
Set the depth of the layer (position on the z-axis)
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
virtual void DrawRectangle(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a rectangle.
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 SetLineWidth(float aLineWidth)
Set the line width.
virtual void SetStrokeColor(const COLOR4D &aColor)
Set the stroke color.
virtual void SetIsStroke(bool aIsStrokeEnabled)
Enable/disable stroked outlines.
virtual void DrawLine(const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint)
Draw a line.
const COLOR4D & GetClearColor() const
double GetMinDepth() const
virtual RENDER_SETTINGS * GetSettings()=0
Return a pointer to current settings that are going to be used when drawing items.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
const COLOR4D & GetLayerColor(int aLayer) const
Return the color used to draw a layer.
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:224
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:790
PAINTER * GetPainter() const
Return the painter object used by the view for drawing VIEW_ITEM objects.
Definition view.h:242
static bool empty(const wxTextEntryBase *aCtrl)
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc).
Definition layer_ids.h:279
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
@ NOT_USED
the 3d code uses this value
Definition typeinfo.h:71
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708