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.

140 lines
3.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2013 CERN
  5. * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <preview_items/selection_area.h>
  25. #include <gal/graphics_abstraction_layer.h>
  26. #include <view/view.h>
  27. #include <pcb_painter.h>
  28. using namespace KIGFX::PREVIEW;
  29. struct SELECTION_COLORS
  30. {
  31. COLOR4D normal;
  32. COLOR4D additive;
  33. COLOR4D subtract;
  34. COLOR4D outline_l2r;
  35. COLOR4D outline_r2l;
  36. };
  37. static const SELECTION_COLORS selectionColorScheme[2] = {
  38. { // dark background
  39. COLOR4D( 0.3, 0.3, 0.7, 0.3 ), // Slight blue
  40. COLOR4D( 0.3, 0.7, 0.3, 0.3 ), // Slight green
  41. COLOR4D( 0.7, 0.3, 0.3, 0.3 ), // Slight red
  42. COLOR4D( 1.0, 1.0, 0.4, 1.0 ), // yellow
  43. COLOR4D( 0.4, 0.4, 1.0, 1.0 ) // blue
  44. },
  45. { // bright background
  46. COLOR4D( 0.5, 0.3, 1.0, 0.5 ), // Slight blue
  47. COLOR4D( 0.5, 1.0, 0.5, 0.5 ), // Slight green
  48. COLOR4D( 1.0, 0.5, 0.5, 0.5 ), // Slight red
  49. COLOR4D( 0.7, 0.7, 0.0, 1.0 ), // yellow
  50. COLOR4D( 0.1, 0.1, 1.0, 1.0 ) // blue
  51. }
  52. };
  53. SELECTION_AREA::SELECTION_AREA() :
  54. m_additive( false ),
  55. m_subtractive( false )
  56. {
  57. }
  58. void SELECTION_AREA::SetAdditive( bool aAdditive )
  59. {
  60. m_additive = aAdditive;
  61. if( m_additive )
  62. m_subtractive = false;
  63. }
  64. void SELECTION_AREA::SetSubtractive( bool aSubtractive )
  65. {
  66. m_subtractive = aSubtractive;
  67. if( m_subtractive )
  68. m_additive = false;
  69. }
  70. const BOX2I SELECTION_AREA::ViewBBox() const
  71. {
  72. BOX2I tmp;
  73. tmp.SetOrigin( m_origin );
  74. tmp.SetEnd( m_end );
  75. tmp.Normalize();
  76. return tmp;
  77. }
  78. void SELECTION_AREA::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
  79. {
  80. auto& gal = *aView->GetGAL();
  81. auto rs = aView->GetPainter()->GetSettings();
  82. const auto& scheme = rs->IsBackgroundDark() ? selectionColorScheme[0] : selectionColorScheme[1];
  83. // Set the fill of the selection rectangle
  84. // based on the selection mode
  85. if( m_additive )
  86. {
  87. gal.SetFillColor( scheme.additive );
  88. }
  89. else if( m_subtractive )
  90. {
  91. gal.SetFillColor( scheme.subtract );
  92. }
  93. else
  94. {
  95. gal.SetFillColor( scheme.normal );
  96. }
  97. gal.SetIsStroke( true );
  98. gal.SetIsFill( true );
  99. // force 1-pixel-wide line
  100. gal.SetLineWidth( 0.0 );
  101. // Set the stroke color to indicate window or crossing selection
  102. bool windowSelection = ( m_origin.x <= m_end.x ) ? true : false;
  103. if( aView->IsMirroredX() )
  104. windowSelection = !windowSelection;
  105. gal.SetStrokeColor( windowSelection ? scheme.outline_l2r : scheme.outline_r2l );
  106. gal.SetIsFill( false );
  107. gal.DrawRectangle( m_origin, m_end );
  108. gal.SetIsFill( true );
  109. // draw the fill as the second object so that Z test will not clamp
  110. // the single-pixel-wide rectangle sides
  111. gal.DrawRectangle( m_origin, m_end );
  112. }