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.

133 lines
4.8 KiB

  1. /*
  2. * This program source code file is part of KICAD, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016-2022 Kicad Developers, see change_log.txt for contributors.
  5. *
  6. * Base class for HiDPI aware wxGLCanvas implementations.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <gal/hidpi_gl_3D_canvas.h>
  26. const float HIDPI_GL_3D_CANVAS::m_delta_move_step_factor = 0.7f;
  27. HIDPI_GL_3D_CANVAS::HIDPI_GL_3D_CANVAS( const KIGFX::VC_SETTINGS& aVcSettings, CAMERA& aCamera,
  28. wxWindow* aParent, const wxGLAttributes& aGLAttribs,
  29. wxWindowID aId, const wxPoint& aPos,
  30. const wxSize& aSize, long aStyle, const wxString& aName,
  31. const wxPalette& aPalette ) :
  32. HIDPI_GL_CANVAS( aVcSettings, aParent, aGLAttribs, aId, aPos, aSize, aStyle, aName, aPalette ),
  33. m_mouse_is_moving( false ),
  34. m_mouse_was_moved( false ),
  35. m_camera_is_moving( false ),
  36. m_camera( aCamera )
  37. {
  38. }
  39. void HIDPI_GL_3D_CANVAS::OnMouseMoveCamera( wxMouseEvent& event )
  40. {
  41. if( m_camera_is_moving )
  42. return;
  43. const wxSize& nativeWinSize = GetNativePixelSize();
  44. const wxPoint& nativePosition = GetNativePosition( event.GetPosition() );
  45. m_camera.SetCurWindowSize( nativeWinSize );
  46. if( event.Dragging() )
  47. {
  48. if( event.LeftIsDown() ) // Drag
  49. m_camera.Drag( nativePosition );
  50. else if( event.MiddleIsDown() ) // Pan
  51. m_camera.Pan( nativePosition );
  52. m_mouse_is_moving = true;
  53. m_mouse_was_moved = true;
  54. }
  55. m_camera.SetCurMousePosition( nativePosition );
  56. }
  57. void HIDPI_GL_3D_CANVAS::OnMouseWheelCamera( wxMouseEvent& event, bool aPan )
  58. {
  59. bool mouseActivity = false;
  60. if( m_camera_is_moving )
  61. return;
  62. // Pick the modifier, if any. Shift beats control beats alt, we don't support more than one.
  63. int modifiers = event.ShiftDown() ? WXK_SHIFT
  64. : ( event.ControlDown() ? WXK_CONTROL
  65. : ( event.AltDown() ? WXK_ALT : 0 ) );
  66. float delta_move = m_delta_move_step_factor * m_camera.GetZoom();
  67. float horizontalSign = m_settings.m_scrollReversePanH ? -1 : 1;
  68. float zoomSign = m_settings.m_scrollReverseZoom ? -1 : 1;
  69. if( aPan )
  70. delta_move *= 0.01f * event.GetWheelRotation();
  71. else if( event.GetWheelRotation() < 0 )
  72. delta_move = -delta_move;
  73. // mousewheel_panning enabled:
  74. // wheel -> pan;
  75. // wheel + shift -> horizontal scrolling;
  76. // wheel + ctrl -> zooming;
  77. // mousewheel_panning disabled:
  78. // wheel + shift -> vertical scrolling;
  79. // wheel + ctrl -> horizontal scrolling;
  80. // wheel -> zooming.
  81. if( aPan && modifiers != m_settings.m_scrollModifierZoom )
  82. {
  83. if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL
  84. || modifiers == m_settings.m_scrollModifierPanH )
  85. m_camera.Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
  86. else
  87. m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
  88. mouseActivity = true;
  89. }
  90. else if( modifiers == m_settings.m_scrollModifierPanV && !aPan )
  91. {
  92. m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
  93. mouseActivity = true;
  94. }
  95. else if( modifiers == m_settings.m_scrollModifierPanH && !aPan )
  96. {
  97. m_camera.Pan( SFVEC3F( delta_move * horizontalSign, 0.0f, 0.0f ) );
  98. mouseActivity = true;
  99. }
  100. else
  101. {
  102. mouseActivity =
  103. m_camera.Zoom( ( event.GetWheelRotation() * zoomSign ) > 0 ? 1.1f : 1 / 1.1f );
  104. }
  105. // If it results on a camera movement
  106. if( mouseActivity )
  107. {
  108. m_mouse_is_moving = true;
  109. m_mouse_was_moved = true;
  110. }
  111. // Update the cursor current mouse position on the camera
  112. m_camera.SetCurMousePosition( GetNativePosition( event.GetPosition() ) );
  113. }