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.

186 lines
5.7 KiB

  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, 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/snap_indicator.h"
  24. #include <gal/graphics_abstraction_layer.h>
  25. using namespace KIGFX;
  26. SNAP_INDICATOR::SNAP_INDICATOR( const COLOR4D& aColor, int aSize, const VECTOR2D& aPosition ) :
  27. ORIGIN_VIEWITEM( aColor, CIRCLE_CROSS, aSize, aPosition )
  28. {
  29. }
  30. SNAP_INDICATOR* SNAP_INDICATOR::Clone() const
  31. {
  32. return new SNAP_INDICATOR( m_color, m_size, m_position );
  33. }
  34. const BOX2I SNAP_INDICATOR::ViewBBox() const
  35. {
  36. return ORIGIN_VIEWITEM::ViewBBox();
  37. }
  38. static void DrawSnapNode( GAL& aGal, const VECTOR2I& aPosition, int aNodeRadius )
  39. {
  40. aGal.SetIsFill( true );
  41. aGal.SetIsStroke( false );
  42. aGal.DrawCircle( aPosition, aNodeRadius );
  43. aGal.SetIsFill( false );
  44. aGal.SetIsStroke( true );
  45. }
  46. static void DrawCornerIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  47. {
  48. const int nodeRad = aSize / 8;
  49. const VECTOR2I corner =
  50. aPosition - VECTOR2I( aSize / 2, aSize / 2 ) + VECTOR2I( nodeRad, nodeRad );
  51. aGal.DrawLine( corner, corner + VECTOR2I( aSize - nodeRad, 0 ) );
  52. aGal.DrawLine( corner, corner + VECTOR2I( 0, aSize - nodeRad ) );
  53. DrawSnapNode( aGal, corner, nodeRad );
  54. }
  55. static void DrawLineEndpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  56. {
  57. const int nodeRadius = aSize / 8;
  58. const VECTOR2I lineStart = aPosition - VECTOR2I( aSize / 2 - nodeRadius, 0 );
  59. DrawSnapNode( aGal, lineStart, nodeRadius );
  60. aGal.DrawLine( lineStart, lineStart + VECTOR2I( aSize - nodeRadius, 0 ) );
  61. }
  62. static void DrawMidpointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  63. {
  64. const int nodeRadius = aSize / 8;
  65. DrawSnapNode( aGal, aPosition, nodeRadius );
  66. aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
  67. }
  68. static void DrawCentrePointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  69. {
  70. const int ringRadius = aSize / 4;
  71. aGal.DrawCircle( aPosition, ringRadius );
  72. aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
  73. aGal.DrawLine( aPosition - VECTOR2I( 0, aSize / 2 ), aPosition + VECTOR2I( 0, aSize / 2 ) );
  74. }
  75. static void DrawQuadrantPointIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  76. {
  77. const int nodeRadius = aSize / 8;
  78. const VECTOR2I quadPoint = aPosition - VECTOR2I( 0, aSize / 2 - nodeRadius );
  79. const int arcRadius = aSize - nodeRadius * 2;
  80. DrawSnapNode( aGal, quadPoint, nodeRadius );
  81. // Most of the top half of a circle, passing through the node centre
  82. const VECTOR2I arcCenter = quadPoint + VECTOR2I( 0, arcRadius );
  83. aGal.DrawArc( arcCenter, arcRadius, EDA_ANGLE( -160, EDA_ANGLE_T::DEGREES_T ),
  84. EDA_ANGLE( 140, EDA_ANGLE_T::DEGREES_T ) );
  85. }
  86. static void DrawIntersectionIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  87. {
  88. const int nodeRadius = aSize / 8;
  89. DrawSnapNode( aGal, aPosition, nodeRadius );
  90. // Slightly squashed X shape
  91. VECTOR2I xLeg = VECTOR2I( aSize / 2, aSize / 3 );
  92. aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
  93. xLeg.y = -xLeg.y;
  94. aGal.DrawLine( aPosition - xLeg, aPosition + xLeg );
  95. }
  96. static void DrawOnElementIcon( GAL& aGal, const VECTOR2I& aPosition, int aSize )
  97. {
  98. const int nodeRadius = aSize / 8;
  99. // A bit like midpoint by off to one side
  100. DrawSnapNode( aGal, aPosition + VECTOR2I( aSize / 4, 0 ), nodeRadius );
  101. aGal.DrawLine( aPosition - VECTOR2I( aSize / 2, 0 ), aPosition + VECTOR2I( aSize / 2, 0 ) );
  102. }
  103. void SNAP_INDICATOR::ViewDraw( int, VIEW* aView ) const
  104. {
  105. GAL& gal = *aView->GetGAL();
  106. // Draw the origin marker
  107. ORIGIN_VIEWITEM::ViewDraw( 0, aView );
  108. gal.SetFillColor( m_color );
  109. // Put the icon near the x-line, so it doesn't overlap with the ruler helpers
  110. const VECTOR2I typeIconPos = m_position + aView->ToWorld( { 24, 10 }, false );
  111. const int size = aView->ToWorld( 16 );
  112. // For now, choose the first type that is set
  113. if( m_snapTypes & POINT_TYPE::PT_CORNER )
  114. {
  115. DrawCornerIcon( gal, typeIconPos, size );
  116. }
  117. else if( m_snapTypes & POINT_TYPE::PT_END )
  118. {
  119. DrawLineEndpointIcon( gal, typeIconPos, size );
  120. }
  121. else if( m_snapTypes & POINT_TYPE::PT_MID )
  122. {
  123. DrawMidpointIcon( gal, typeIconPos, size );
  124. }
  125. else if( m_snapTypes & POINT_TYPE::PT_CENTER )
  126. {
  127. DrawCentrePointIcon( gal, typeIconPos, size );
  128. }
  129. else if( m_snapTypes & POINT_TYPE::PT_QUADRANT )
  130. {
  131. DrawQuadrantPointIcon( gal, typeIconPos, size );
  132. }
  133. else if( m_snapTypes & POINT_TYPE::PT_INTERSECTION )
  134. {
  135. DrawIntersectionIcon( gal, typeIconPos, size );
  136. }
  137. else if( m_snapTypes & POINT_TYPE::PT_ON_ELEMENT )
  138. {
  139. DrawOnElementIcon( gal, typeIconPos, size );
  140. }
  141. }