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.

123 lines
4.1 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( CAMERA& aCamera, wxWindow* aParent, wxWindowID,
  28. const int* aAttribList, const wxPoint& aPos,
  29. const wxSize& aSize, long aStyle, const wxString& aName,
  30. const wxPalette& aPalette ) :
  31. HIDPI_GL_CANVAS( aParent, wxID_ANY, aAttribList, aPos, aSize, aStyle, aName, aPalette ),
  32. m_mouse_is_moving( false ),
  33. m_mouse_was_moved( false ),
  34. m_camera_is_moving( false ),
  35. m_camera( aCamera )
  36. {
  37. }
  38. void HIDPI_GL_3D_CANVAS::OnMouseMoveCamera( wxMouseEvent& event )
  39. {
  40. if( m_camera_is_moving )
  41. return;
  42. const wxSize& nativeWinSize = GetNativePixelSize();
  43. const wxPoint& nativePosition = GetNativePosition( event.GetPosition() );
  44. m_camera.SetCurWindowSize( nativeWinSize );
  45. if( event.Dragging() )
  46. {
  47. if( event.LeftIsDown() ) // Drag
  48. m_camera.Drag( nativePosition );
  49. else if( event.MiddleIsDown() ) // Pan
  50. m_camera.Pan( nativePosition );
  51. m_mouse_is_moving = true;
  52. m_mouse_was_moved = true;
  53. }
  54. m_camera.SetCurMousePosition( nativePosition );
  55. }
  56. void HIDPI_GL_3D_CANVAS::OnMouseWheelCamera( wxMouseEvent& event, bool aPan )
  57. {
  58. bool mouseActivity = false;
  59. if( m_camera_is_moving )
  60. return;
  61. float delta_move = m_delta_move_step_factor * m_camera.GetZoom();
  62. if( aPan )
  63. delta_move *= 0.01f * event.GetWheelRotation();
  64. else if( event.GetWheelRotation() < 0 )
  65. delta_move = -delta_move;
  66. // mousewheel_panning enabled:
  67. // wheel -> pan;
  68. // wheel + shift -> horizontal scrolling;
  69. // wheel + ctrl -> zooming;
  70. // mousewheel_panning disabled:
  71. // wheel + shift -> vertical scrolling;
  72. // wheel + ctrl -> horizontal scrolling;
  73. // wheel -> zooming.
  74. if( aPan && !event.ControlDown() )
  75. {
  76. if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL || event.ShiftDown() )
  77. m_camera.Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) );
  78. else
  79. m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
  80. mouseActivity = true;
  81. }
  82. else if( event.ShiftDown() && !aPan )
  83. {
  84. m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) );
  85. mouseActivity = true;
  86. }
  87. else if( event.ControlDown() && !aPan )
  88. {
  89. m_camera.Pan( SFVEC3F( delta_move, 0.0f, 0.0f ) );
  90. mouseActivity = true;
  91. }
  92. else
  93. {
  94. mouseActivity = m_camera.Zoom( event.GetWheelRotation() > 0 ? 1.1f : 1 / 1.1f );
  95. }
  96. // If it results on a camera movement
  97. if( mouseActivity )
  98. {
  99. m_mouse_is_moving = true;
  100. m_mouse_was_moved = true;
  101. }
  102. // Update the cursor current mouse position on the camera
  103. m_camera.SetCurMousePosition( GetNativePosition( event.GetPosition() ) );
  104. }