KiCad PCB EDA Suite
Loading...
Searching...
No Matches
draw_context.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) 2019-2023 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
27#include <view/view.h>
28
29using namespace KIGFX::PREVIEW;
30
31using KIGFX::COLOR4D;
32
33
34static constexpr double ANGLE_EPSILON = 1e-9;
35
36static bool angleIsSpecial( EDA_ANGLE aAngle )
37{
38 return std::fabs( std::remainder( aAngle.AsRadians(), M_PI_4 ) ) < ANGLE_EPSILON;
39}
40
41
42static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
43{
44 return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
45}
46
47
49 : m_gal( *aView.GetGAL() ),
50 m_render_settings( *aView.GetPainter()->GetSettings() ),
51 m_currLayer( LAYER_AUX_ITEMS ),
52 m_lineWidth( 1.0f )
53{
54}
55
56
57void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
58{
60
62 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
63 m_gal.SetIsStroke( true );
64 m_gal.SetIsFill( false );
65 m_gal.DrawCircle( aOrigin, aRad );
66}
67
68
69void DRAW_CONTEXT::DrawCircleDashed( const VECTOR2I& aOrigin, double aRad, double aStepAngle,
70 double aFillAngle, bool aDeEmphasised )
71{
73
75 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
76 m_gal.SetIsStroke( true );
77 m_gal.SetIsFill( false );
78
79 for( int i = 0; i < 360; i += aStepAngle )
80 {
81 m_gal.DrawArc( aOrigin, aRad, EDA_ANGLE( i, DEGREES_T ),
82 EDA_ANGLE( i + aFillAngle, DEGREES_T ) );
83 }
84}
85
86
87void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
88{
90
92 m_gal.SetIsStroke( true );
93 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
94 m_gal.DrawLine( aStart, aEnd );
95}
96
97
98void DRAW_CONTEXT::DrawLineDashed( const VECTOR2I& aStart, const VECTOR2I& aEnd, int aDashStep,
99 int aDashFill, bool aDeEmphasised )
100{
102
104 m_gal.SetIsStroke( true );
105 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
106
107 VECTOR2I delta = aEnd - aStart;
108 int vecLen = delta.EuclideanNorm();
109
110 for( int i = 0; i < vecLen; i += aDashStep )
111 {
112 VECTOR2I a = aStart + delta.Resize( i );
113 VECTOR2I b = aStart + delta.Resize( std::min( i + aDashFill, vecLen ) );
114
115 m_gal.DrawLine( a, b );
116 }
117}
118
119
121 bool aDeEmphasised )
122{
123 const VECTOR2I vec = aEnd - aStart;
125
126 if( angleIsSpecial( EDA_ANGLE( vec ) ) )
127 strokeColor = getSpecialAngleColour();
128
130 m_gal.SetIsStroke( true );
131 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
132 m_gal.DrawLine( aStart, aEnd );
133}
134
135
137{
138 return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
139 COLOR4D( 0.0, 0.7, 0.0, 1.0 );
140}
int color
Definition: DXF_plotter.cpp:58
double AsRadians() const
Definition: eda_angle.h:117
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
COLOR4D WithAlpha(double aAlpha) const
Return a color with the same color, but the given alpha.
Definition: color4d.h:311
virtual void SetIsFill(bool aIsFillEnabled)
Enable/disable fill.
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.
virtual void DrawArc(const VECTOR2D &aCenterPoint, double aRadius, const EDA_ANGLE &aStartAngle, const EDA_ANGLE &aAngle)
Draw an arc.
float m_lineWidth
The line width to use for items.
Definition: draw_context.h:129
const KIGFX::RENDER_SETTINGS & m_render_settings
The current layer to draw onto.
Definition: draw_context.h:123
void DrawCircleDashed(const VECTOR2I &aOrigin, double aRad, double aStepAngle, double aFillAngle, bool aDeEmphasised)
Draw a dashed preview circle on the current layer.
DRAW_CONTEXT(KIGFX::VIEW &aView)
void DrawLine(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a simple line on the current layer.
void DrawLineWithAngleHighlight(const VECTOR2I &aStart, const VECTOR2I &aEnd, bool aDeEmphasised)
Draw a straight line on the current layer, with a special highlight when the line angle is a multiple...
void DrawLineDashed(const VECTOR2I &aStart, const VECTOR2I &aEn, int aDashStep, int aDashFill, bool aDeEmphasised)
Draw a dashed line on the current layer.
COLOR4D getSpecialAngleColour() const
The GAL to draw into.
void DrawCircle(const VECTOR2I &aOrigin, double aRad, bool aDeEmphasised)
Draw a preview circle on the current layer.
virtual bool IsBackgroundDark() const
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
static COLOR4D deemphasise(const COLOR4D &aColor, bool aDeEmphasised)
static constexpr double ANGLE_EPSILON
static bool angleIsSpecial(EDA_ANGLE aAngle)
@ DEGREES_T
Definition: eda_angle.h:31
@ LAYER_AUX_ITEMS
Auxiliary items (guides, rule, etc)
Definition: layer_ids.h:223
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
constexpr int delta