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 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, see <https://www.gnu.org/licenses/>.
18 */
19
23#include <view/view.h>
24
25using namespace KIGFX::PREVIEW;
26
27using KIGFX::COLOR4D;
28
29
30static constexpr double ANGLE_EPSILON = 1e-9;
31
32static bool angleIsSpecial( EDA_ANGLE aAngle )
33{
34 return std::fabs( std::remainder( aAngle.AsRadians(), M_PI_4 ) ) < ANGLE_EPSILON;
35}
36
37
38static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
39{
40 return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
41}
42
43
45 : m_gal( *aView.GetGAL() ),
46 m_render_settings( *aView.GetPainter()->GetSettings() ),
48 m_lineWidth( 1.0f )
49{
50}
51
52
53void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
54{
55 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
56
57 m_gal.SetLineWidth( m_lineWidth );
58 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
59 m_gal.SetIsStroke( true );
60 m_gal.SetIsFill( false );
61 m_gal.DrawCircle( aOrigin, aRad );
62}
63
64
65void DRAW_CONTEXT::DrawCircleDashed( const VECTOR2I& aOrigin, double aRad, double aStepAngle,
66 double aFillAngle, bool aDeEmphasised )
67{
68 const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
69
70 m_gal.SetLineWidth( m_lineWidth );
71 m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
72 m_gal.SetIsStroke( true );
73 m_gal.SetIsFill( false );
74
75 for( int i = 0; i < 360; i += aStepAngle )
76 {
77 m_gal.DrawArc( aOrigin, aRad, EDA_ANGLE( i, DEGREES_T ),
78 EDA_ANGLE( i + aFillAngle, DEGREES_T ) );
79 }
80}
81
82
83void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
84{
85 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
86
87 m_gal.SetLineWidth( m_lineWidth );
88 m_gal.SetIsStroke( true );
89 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
90 m_gal.DrawLine( aStart, aEnd );
91}
92
93
94void DRAW_CONTEXT::DrawLineDashed( const VECTOR2I& aStart, const VECTOR2I& aEnd, int aDashStep,
95 int aDashFill, bool aDeEmphasised )
96{
97 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
98
99 m_gal.SetLineWidth( m_lineWidth );
100 m_gal.SetIsStroke( true );
101 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
102
103 VECTOR2I delta = aEnd - aStart;
104 int vecLen = delta.EuclideanNorm();
105
106 for( int i = 0; i < vecLen; i += aDashStep )
107 {
108 VECTOR2I a = aStart + delta.Resize( i );
109 VECTOR2I b = aStart + delta.Resize( std::min( i + aDashFill, vecLen ) );
110
111 m_gal.DrawLine( a, b );
112 }
113}
114
115
117 bool aDeEmphasised )
118{
119 const VECTOR2I vec = aEnd - aStart;
120 COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
121
122 if( angleIsSpecial( EDA_ANGLE( vec ) ) )
123 strokeColor = getSpecialAngleColour();
124
125 m_gal.SetLineWidth( m_lineWidth );
126 m_gal.SetIsStroke( true );
127 m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
128 m_gal.DrawLine( aStart, aEnd );
129}
130
131
133{
134 return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
135 COLOR4D( 0.0, 0.7, 0.0, 1.0 );
136}
double AsRadians() const
Definition eda_angle.h:120
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:308
float m_lineWidth
The line width to use for items.
const KIGFX::RENDER_SETTINGS & m_render_settings
The current layer to draw onto.
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.
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:63
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:279
double PreviewOverlayDeemphAlpha(bool aDeemph=true)
Default alpha of "de-emphasised" features (like previously locked-in lines.
int delta
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683