You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.5 KiB

  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. #include <gal/graphics_abstraction_layer.h>
  24. #include <preview_items/draw_context.h>
  25. #include <preview_items/preview_utils.h>
  26. #include <view/view.h>
  27. using namespace KIGFX::PREVIEW;
  28. using KIGFX::COLOR4D;
  29. static constexpr double ANGLE_EPSILON = 1e-9;
  30. static bool angleIsSpecial( EDA_ANGLE aAngle )
  31. {
  32. return std::fabs( std::remainder( aAngle.AsRadians(), M_PI_4 ) ) < ANGLE_EPSILON;
  33. }
  34. static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
  35. {
  36. return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
  37. }
  38. DRAW_CONTEXT::DRAW_CONTEXT( KIGFX::VIEW& aView )
  39. : m_gal( *aView.GetGAL() ),
  40. m_render_settings( *aView.GetPainter()->GetSettings() ),
  41. m_currLayer( LAYER_AUX_ITEMS ),
  42. m_lineWidth( 1.0f )
  43. {
  44. }
  45. void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
  46. {
  47. const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
  48. m_gal.SetLineWidth( m_lineWidth );
  49. m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
  50. m_gal.SetIsStroke( true );
  51. m_gal.SetIsFill( false );
  52. m_gal.DrawCircle( aOrigin, aRad );
  53. }
  54. void DRAW_CONTEXT::DrawCircleDashed( const VECTOR2I& aOrigin, double aRad, double aStepAngle,
  55. double aFillAngle, bool aDeEmphasised )
  56. {
  57. const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
  58. m_gal.SetLineWidth( m_lineWidth );
  59. m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
  60. m_gal.SetIsStroke( true );
  61. m_gal.SetIsFill( false );
  62. for( int i = 0; i < 360; i += aStepAngle )
  63. {
  64. m_gal.DrawArc( aOrigin, aRad, EDA_ANGLE( i, DEGREES_T ),
  65. EDA_ANGLE( i + aFillAngle, DEGREES_T ) );
  66. }
  67. }
  68. void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
  69. {
  70. COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
  71. m_gal.SetLineWidth( m_lineWidth );
  72. m_gal.SetIsStroke( true );
  73. m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
  74. m_gal.DrawLine( aStart, aEnd );
  75. }
  76. void DRAW_CONTEXT::DrawLineDashed( const VECTOR2I& aStart, const VECTOR2I& aEnd, int aDashStep,
  77. int aDashFill, bool aDeEmphasised )
  78. {
  79. COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
  80. m_gal.SetLineWidth( m_lineWidth );
  81. m_gal.SetIsStroke( true );
  82. m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
  83. VECTOR2I delta = aEnd - aStart;
  84. int vecLen = delta.EuclideanNorm();
  85. for( int i = 0; i < vecLen; i += aDashStep )
  86. {
  87. VECTOR2I a = aStart + delta.Resize( i );
  88. VECTOR2I b = aStart + delta.Resize( std::min( i + aDashFill, vecLen ) );
  89. m_gal.DrawLine( a, b );
  90. }
  91. }
  92. void DRAW_CONTEXT::DrawLineWithAngleHighlight( const VECTOR2I& aStart, const VECTOR2I& aEnd,
  93. bool aDeEmphasised )
  94. {
  95. const VECTOR2I vec = aEnd - aStart;
  96. COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
  97. if( angleIsSpecial( EDA_ANGLE( vec ) ) )
  98. strokeColor = getSpecialAngleColour();
  99. m_gal.SetLineWidth( m_lineWidth );
  100. m_gal.SetIsStroke( true );
  101. m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
  102. m_gal.DrawLine( aStart, aEnd );
  103. }
  104. COLOR4D DRAW_CONTEXT::getSpecialAngleColour() const
  105. {
  106. return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
  107. COLOR4D( 0.0, 0.7, 0.0, 1.0 );
  108. }