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.

98 lines
3.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 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 <preview_items/draw_context.h>
  24. #include <preview_items/preview_utils.h>
  25. #include <view/view.h>
  26. using namespace KIGFX::PREVIEW;
  27. static constexpr double ANGLE_EPSILON = 1e-9;
  28. static bool angleIsSpecial( double aRadians )
  29. {
  30. return std::fabs( std::remainder( aRadians, M_PI_4 ) ) < ANGLE_EPSILON;
  31. }
  32. static COLOR4D deemphasise( const COLOR4D& aColor, bool aDeEmphasised )
  33. {
  34. return aColor.WithAlpha( PreviewOverlayDeemphAlpha( aDeEmphasised ) );
  35. }
  36. DRAW_CONTEXT::DRAW_CONTEXT( KIGFX::VIEW& aView )
  37. : m_gal( *aView.GetGAL() ),
  38. m_render_settings( *aView.GetPainter()->GetSettings() ),
  39. m_currLayer( LAYER_AUX_ITEMS ),
  40. m_lineWidth( 1.0f )
  41. {
  42. }
  43. void DRAW_CONTEXT::DrawCircle( const VECTOR2I& aOrigin, double aRad, bool aDeEmphasised )
  44. {
  45. const COLOR4D& color = m_render_settings.GetLayerColor( m_currLayer );
  46. m_gal.SetLineWidth( m_lineWidth );
  47. m_gal.SetStrokeColor( deemphasise( color, aDeEmphasised ) );
  48. m_gal.SetIsStroke( true );
  49. m_gal.SetIsFill( false );
  50. m_gal.DrawCircle( aOrigin, aRad );
  51. }
  52. void DRAW_CONTEXT::DrawLine( const VECTOR2I& aStart, const VECTOR2I& aEnd, bool aDeEmphasised )
  53. {
  54. COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
  55. m_gal.SetLineWidth( m_lineWidth );
  56. m_gal.SetIsStroke( true );
  57. m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
  58. m_gal.DrawLine( aStart, aEnd );
  59. }
  60. void DRAW_CONTEXT::DrawLineWithAngleHighlight( const VECTOR2I& aStart, const VECTOR2I& aEnd,
  61. bool aDeEmphasised )
  62. {
  63. const VECTOR2I vec = aEnd - aStart;
  64. COLOR4D strokeColor = m_render_settings.GetLayerColor( m_currLayer );
  65. if( angleIsSpecial( vec.Angle() ) )
  66. strokeColor = getSpecialAngleColour();
  67. m_gal.SetLineWidth( m_lineWidth );
  68. m_gal.SetIsStroke( true );
  69. m_gal.SetStrokeColor( deemphasise( strokeColor, aDeEmphasised ) );
  70. m_gal.DrawLine( aStart, aEnd );
  71. }
  72. COLOR4D DRAW_CONTEXT::getSpecialAngleColour() const
  73. {
  74. return m_render_settings.IsBackgroundDark() ? COLOR4D( 0.5, 1.0, 0.5, 1.0 ) :
  75. COLOR4D( 0.0, 0.7, 0.0, 1.0 );
  76. }