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.

122 lines
3.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2018-2021 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 <functional>
  24. using namespace std::placeholders;
  25. #include <math/util.h> // for KiROUND
  26. #include <math/vector2d.h>
  27. #include <tool/tool_manager.h>
  28. #include <view/view.h>
  29. #include <tool/grid_helper.h>
  30. GRID_HELPER::GRID_HELPER( TOOL_MANAGER* aToolMgr ) :
  31. m_toolMgr( aToolMgr )
  32. {
  33. m_maskTypes = ALL;
  34. m_enableSnap = true;
  35. m_enableSnapLine = true;
  36. m_enableGrid = true;
  37. m_snapItem = nullptr;
  38. }
  39. GRID_HELPER::~GRID_HELPER()
  40. {
  41. }
  42. VECTOR2I GRID_HELPER::GetGrid() const
  43. {
  44. VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetGridSize();
  45. return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
  46. }
  47. VECTOR2I GRID_HELPER::GetVisibleGrid() const
  48. {
  49. VECTOR2D size = m_toolMgr->GetView()->GetGAL()->GetVisibleGridSize();
  50. return VECTOR2I( KiROUND( size.x ), KiROUND( size.y ) );
  51. }
  52. VECTOR2I GRID_HELPER::GetOrigin() const
  53. {
  54. VECTOR2D origin = m_toolMgr->GetView()->GetGAL()->GetGridOrigin();
  55. return VECTOR2I( origin );
  56. }
  57. void GRID_HELPER::SetAuxAxes( bool aEnable, const VECTOR2I& aOrigin )
  58. {
  59. if( aEnable )
  60. {
  61. m_auxAxis = aOrigin;
  62. m_viewAxis.SetPosition( aOrigin );
  63. m_toolMgr->GetView()->SetVisible( &m_viewAxis, true );
  64. }
  65. else
  66. {
  67. m_auxAxis = std::optional<VECTOR2I>();
  68. m_toolMgr->GetView()->SetVisible( &m_viewAxis, false );
  69. }
  70. }
  71. VECTOR2I GRID_HELPER::AlignGrid( const VECTOR2I& aPoint ) const
  72. {
  73. const VECTOR2D gridOffset( GetOrigin() );
  74. const VECTOR2D grid( GetGrid() );
  75. VECTOR2I nearest( KiROUND( ( aPoint.x - gridOffset.x ) / grid.x ) * grid.x + gridOffset.x,
  76. KiROUND( ( aPoint.y - gridOffset.y ) / grid.y ) * grid.y + gridOffset.y );
  77. return nearest;
  78. }
  79. VECTOR2I GRID_HELPER::Align( const VECTOR2I& aPoint ) const
  80. {
  81. if( !canUseGrid() )
  82. return aPoint;
  83. VECTOR2I nearest = AlignGrid( aPoint );
  84. if( !m_auxAxis )
  85. return nearest;
  86. if( std::abs( m_auxAxis->x - aPoint.x ) < std::abs( nearest.x - aPoint.x ) )
  87. nearest.x = m_auxAxis->x;
  88. if( std::abs( m_auxAxis->y - aPoint.y ) < std::abs( nearest.y - aPoint.y ) )
  89. nearest.y = m_auxAxis->y;
  90. return nearest;
  91. }