From 976670e9875e5cf8f47d603c1a1c87198b395f0f Mon Sep 17 00:00:00 2001 From: Fabien Corona Date: Tue, 21 Jun 2022 23:38:22 +0000 Subject: [PATCH] Move 3D controls to a new class --- 3d-viewer/3d_canvas/eda_3d_canvas.cpp | 88 ++----------------- 3d-viewer/3d_canvas/eda_3d_canvas.h | 16 +--- common/gal/CMakeLists.txt | 1 + common/gal/hidpi_gl_3D_canvas.cpp | 122 ++++++++++++++++++++++++++ include/gal/hidpi_gl_3D_canvas.h | 73 +++++++++++++++ 5 files changed, 205 insertions(+), 95 deletions(-) create mode 100644 common/gal/hidpi_gl_3D_canvas.cpp create mode 100644 include/gal/hidpi_gl_3D_canvas.h diff --git a/3d-viewer/3d_canvas/eda_3d_canvas.cpp b/3d-viewer/3d_canvas/eda_3d_canvas.cpp index 2524168872..34607c1d9e 100644 --- a/3d-viewer/3d_canvas/eda_3d_canvas.cpp +++ b/3d-viewer/3d_canvas/eda_3d_canvas.cpp @@ -55,8 +55,6 @@ */ const wxChar* EDA_3D_CANVAS::m_logTrace = wxT( "KI_TRACE_EDA_3D_CANVAS" ); -const float EDA_3D_CANVAS::m_delta_move_step_factor = 0.7f; - // A custom event, used to call DoRePaint during an idle time wxDEFINE_EVENT( wxEVT_REFRESH_CUSTOM_COMMAND, wxEvent); @@ -90,24 +88,20 @@ END_EVENT_TABLE() EDA_3D_CANVAS::EDA_3D_CANVAS( wxWindow* aParent, const int* aAttribList, BOARD_ADAPTER& aBoardAdapter, CAMERA& aCamera, S3D_CACHE* a3DCachePointer ) - : HIDPI_GL_CANVAS( aParent, wxID_ANY, aAttribList, wxDefaultPosition, wxDefaultSize, - wxFULL_REPAINT_ON_RESIZE ), + : HIDPI_GL_3D_CANVAS( aCamera, aParent, wxID_ANY, aAttribList, wxDefaultPosition, wxDefaultSize, + wxFULL_REPAINT_ON_RESIZE ), m_eventDispatcher( nullptr ), m_parentStatusBar( nullptr ), m_parentInfoBar( nullptr ), m_glRC( nullptr ), m_is_opengl_initialized( false ), m_is_opengl_version_supported( true ), - m_mouse_is_moving( false ), - m_mouse_was_moved( false ), - m_camera_is_moving( false ), m_render_pivot( false ), m_camera_moving_speed( 1.0f ), m_strtime_camera_movement( 0 ), m_animation_enabled( true ), m_moving_speed_multiplier( 3 ), m_boardAdapter( aBoardAdapter ), - m_camera( aCamera ), m_3d_render( nullptr ), m_opengl_supports_raytracing( true ), m_render_raytracing_was_requested( false ), @@ -587,67 +581,16 @@ void EDA_3D_CANVAS::OnEraseBackground( wxEraseEvent& event ) void EDA_3D_CANVAS::OnMouseWheel( wxMouseEvent& event ) { - bool mouseActivity = false; - wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnMouseWheel" ) ); - if( m_camera_is_moving ) - return; + OnMouseWheelCamera( event, m_boardAdapter.m_MousewheelPanning ); - float delta_move = m_delta_move_step_factor * m_camera.GetZoom(); - - if( m_boardAdapter.m_MousewheelPanning ) - delta_move *= 0.01f * event.GetWheelRotation(); - else if( event.GetWheelRotation() < 0 ) - delta_move = -delta_move; - - // mousewheel_panning enabled: - // wheel -> pan; - // wheel + shift -> horizontal scrolling; - // wheel + ctrl -> zooming; - // mousewheel_panning disabled: - // wheel + shift -> vertical scrolling; - // wheel + ctrl -> horizontal scrolling; - // wheel -> zooming. - - if( m_boardAdapter.m_MousewheelPanning && !event.ControlDown() ) - { - if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL || event.ShiftDown() ) - m_camera.Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) ); - else - m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) ); - - mouseActivity = true; - } - else if( event.ShiftDown() && !m_boardAdapter.m_MousewheelPanning ) - { - m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) ); - mouseActivity = true; - } - else if( event.ControlDown() && !m_boardAdapter.m_MousewheelPanning ) - { - m_camera.Pan( SFVEC3F( delta_move, 0.0f, 0.0f ) ); - mouseActivity = true; - } - else - { - mouseActivity = m_camera.Zoom( event.GetWheelRotation() > 0 ? 1.1f : 1/1.1f ); - } - - // If it results on a camera movement - if( mouseActivity ) + if( m_mouse_was_moved ) { DisplayStatus(); Request_refresh(); - - m_mouse_is_moving = true; - m_mouse_was_moved = true; - restart_editingTimeOut_Timer(); } - - // Update the cursor current mouse position on the camera - m_camera.SetCurMousePosition( GetNativePosition( event.GetPosition() ) ); } @@ -675,32 +618,15 @@ void EDA_3D_CANVAS::OnMagnify( wxMouseEvent& event ) void EDA_3D_CANVAS::OnMouseMove( wxMouseEvent& event ) { //wxLogTrace( m_logTrace, wxT( "EDA_3D_CANVAS::OnMouseMove" ) ); + OnMouseMoveCamera( event ); - if( m_camera_is_moving ) - return; - - const wxSize& nativeWinSize = GetNativePixelSize(); - const wxPoint& nativePosition = GetNativePosition( event.GetPosition() ); - - m_camera.SetCurWindowSize( nativeWinSize ); - - if( event.Dragging() ) + if( m_mouse_was_moved ) { - if( event.LeftIsDown() ) // Drag - m_camera.Drag( nativePosition ); - else if( event.MiddleIsDown() ) // Pan - m_camera.Pan( nativePosition ); - - m_mouse_is_moving = true; - m_mouse_was_moved = true; - - // orientation has changed, redraw mesh DisplayStatus(); Request_refresh(); + restart_editingTimeOut_Timer(); } - m_camera.SetCurMousePosition( nativePosition ); - if( !event.Dragging() && m_boardAdapter.m_Cfg->m_Render.engine == RENDER_ENGINE::OPENGL ) { STATUSBAR_REPORTER reporter( m_parentStatusBar, diff --git a/3d-viewer/3d_canvas/eda_3d_canvas.h b/3d-viewer/3d_canvas/eda_3d_canvas.h index 4d74dec442..1269a8b50b 100644 --- a/3d-viewer/3d_canvas/eda_3d_canvas.h +++ b/3d-viewer/3d_canvas/eda_3d_canvas.h @@ -30,7 +30,7 @@ #include "3d_rendering/raytracing/accelerators/accelerator_3d.h" #include "3d_rendering/render_3d_base.h" #include "3d_cache/3d_cache.h" -#include +#include #include #include @@ -45,7 +45,7 @@ class RENDER_3D_OPENGL; /** * Implement a canvas based on a wxGLCanvas */ -class EDA_3D_CANVAS : public HIDPI_GL_CANVAS +class EDA_3D_CANVAS : public HIDPI_GL_3D_CANVAS { public: /** @@ -161,11 +161,6 @@ public: */ void OnEvent( wxEvent& aEvent ); - /** - * Get the canvas camera. - */ - CAMERA* GetCamera() { return &m_camera; } - /** * Get information used to display 3D board. */ @@ -276,9 +271,6 @@ private: wxTimer m_redraw_trigger_timer; // Used to schedule a redraw event std::atomic_flag m_is_currently_painting; // Avoid drawing twice at the same time - bool m_mouse_is_moving; // Mouse activity is in progress - bool m_mouse_was_moved; - bool m_camera_is_moving; // Camera animation is ongoing bool m_render_pivot; // Render the pivot while camera moving float m_camera_moving_speed; // 1.0f will be 1:1 unsigned m_strtime_camera_movement; // Ticktime of camera movement start @@ -286,14 +278,10 @@ private: int m_moving_speed_multiplier; // Camera animation speed multiplier option BOARD_ADAPTER& m_boardAdapter; // Pre-computed 3D info and settings - CAMERA& m_camera; RENDER_3D_BASE* m_3d_render; RENDER_3D_RAYTRACE* m_3d_render_raytracing; RENDER_3D_OPENGL* m_3d_render_opengl; - static const float m_delta_move_step_factor; // Step factor to used with cursor on - // relation to the current zoom - bool m_opengl_supports_raytracing; bool m_render_raytracing_was_requested; diff --git a/common/gal/CMakeLists.txt b/common/gal/CMakeLists.txt index 8c6a330124..7c91c57292 100644 --- a/common/gal/CMakeLists.txt +++ b/common/gal/CMakeLists.txt @@ -12,6 +12,7 @@ set( GAL_SRCS gal_display_options.cpp graphics_abstraction_layer.cpp hidpi_gl_canvas.cpp + hidpi_gl_3D_canvas.cpp ../view/view_controls.cpp ../view/view_overlay.cpp diff --git a/common/gal/hidpi_gl_3D_canvas.cpp b/common/gal/hidpi_gl_3D_canvas.cpp new file mode 100644 index 0000000000..06cc6c5ce6 --- /dev/null +++ b/common/gal/hidpi_gl_3D_canvas.cpp @@ -0,0 +1,122 @@ +/* + * This program source code file is part of KICAD, a free EDA CAD application. + * + * Copyright (C) 2016-2022 Kicad Developers, see change_log.txt for contributors. + * + * Base class for HiDPI aware wxGLCanvas implementations. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include + +const float HIDPI_GL_3D_CANVAS::m_delta_move_step_factor = 0.7f; + +HIDPI_GL_3D_CANVAS::HIDPI_GL_3D_CANVAS( CAMERA& aCamera, wxWindow* aParent, wxWindowID, + const int* aAttribList, const wxPoint& aPos, + const wxSize& aSize, long aStyle, const wxString& aName, + const wxPalette& aPalette ) : + HIDPI_GL_CANVAS( aParent, wxID_ANY, aAttribList, aPos, aSize, aStyle, aName, aPalette ), + m_mouse_is_moving( false ), + m_mouse_was_moved( false ), + m_camera_is_moving( false ), + m_camera( aCamera ) +{ +} + + +void HIDPI_GL_3D_CANVAS::OnMouseMoveCamera( wxMouseEvent& event ) +{ + if( m_camera_is_moving ) + return; + + const wxSize& nativeWinSize = GetNativePixelSize(); + const wxPoint& nativePosition = GetNativePosition( event.GetPosition() ); + + m_camera.SetCurWindowSize( nativeWinSize ); + + if( event.Dragging() ) + { + if( event.LeftIsDown() ) // Drag + m_camera.Drag( nativePosition ); + else if( event.MiddleIsDown() ) // Pan + m_camera.Pan( nativePosition ); + + m_mouse_is_moving = true; + m_mouse_was_moved = true; + } + m_camera.SetCurMousePosition( nativePosition ); +} + +void HIDPI_GL_3D_CANVAS::OnMouseWheelCamera( wxMouseEvent& event, bool aPan ) +{ + bool mouseActivity = false; + + if( m_camera_is_moving ) + return; + + float delta_move = m_delta_move_step_factor * m_camera.GetZoom(); + + if( aPan ) + delta_move *= 0.01f * event.GetWheelRotation(); + else if( event.GetWheelRotation() < 0 ) + delta_move = -delta_move; + + // mousewheel_panning enabled: + // wheel -> pan; + // wheel + shift -> horizontal scrolling; + // wheel + ctrl -> zooming; + // mousewheel_panning disabled: + // wheel + shift -> vertical scrolling; + // wheel + ctrl -> horizontal scrolling; + // wheel -> zooming. + + if( aPan && !event.ControlDown() ) + { + if( event.GetWheelAxis() == wxMOUSE_WHEEL_HORIZONTAL || event.ShiftDown() ) + m_camera.Pan( SFVEC3F( -delta_move, 0.0f, 0.0f ) ); + else + m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) ); + + mouseActivity = true; + } + else if( event.ShiftDown() && !aPan ) + { + m_camera.Pan( SFVEC3F( 0.0f, -delta_move, 0.0f ) ); + mouseActivity = true; + } + else if( event.ControlDown() && !aPan ) + { + m_camera.Pan( SFVEC3F( delta_move, 0.0f, 0.0f ) ); + mouseActivity = true; + } + else + { + mouseActivity = m_camera.Zoom( event.GetWheelRotation() > 0 ? 1.1f : 1 / 1.1f ); + } + + // If it results on a camera movement + if( mouseActivity ) + { + m_mouse_is_moving = true; + m_mouse_was_moved = true; + } + + // Update the cursor current mouse position on the camera + m_camera.SetCurMousePosition( GetNativePosition( event.GetPosition() ) ); +} diff --git a/include/gal/hidpi_gl_3D_canvas.h b/include/gal/hidpi_gl_3D_canvas.h new file mode 100644 index 0000000000..169723c15c --- /dev/null +++ b/include/gal/hidpi_gl_3D_canvas.h @@ -0,0 +1,73 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2015-2016 Mario Luzeiro + * Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef HIDPI_GL_3D_CANVAS_H +#define HIDPI_GL_3D_CANVAS_H + +#include +#include "3d_rendering/raytracing/accelerators/accelerator_3d.h" +#include "3d_cache/3d_cache.h" +#include +#include +#include + + +class RENDER_3D_RAYTRACE; +class RENDER_3D_OPENGL; + + +/** + * Provides basic 3D controls ( zoom, rotate, translate, ... ) + * + */ +class HIDPI_GL_3D_CANVAS : public HIDPI_GL_CANVAS +{ +public: + // wxGLCanvas constructor + + HIDPI_GL_3D_CANVAS( CAMERA& aCamera, wxWindow* parent, wxWindowID id = wxID_ANY, + const int* attribList = nullptr, const wxPoint& pos = wxDefaultPosition, + const wxSize& size = wxDefaultSize, long style = 0, + const wxString& name = wxGLCanvasName, + const wxPalette& palette = wxNullPalette ); + + bool m_mouse_is_moving; // Mouse activity is in progress + bool m_mouse_was_moved; + bool m_camera_is_moving; // Camera animation is ongoing + + CAMERA& m_camera; + static const float m_delta_move_step_factor; // Step factor to used with cursor on + // relation to the current zoom + + /** + * Get the canvas camera. + */ + CAMERA* GetCamera() { return &m_camera; } + + void OnMouseMoveCamera( wxMouseEvent& event ); + void OnMouseWheelCamera( wxMouseEvent& event, bool aPan ); +}; + + +#endif // HIDPI_GL_3D_CANVAS_H