KiCad PCB EDA Suite
Loading...
Searching...
No Matches
spnav_2d_plugin.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see CHANGELOG.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 3
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, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "spnav_2d_plugin.h"
21
24#include <view/view.h>
26
27#include <wx/log.h>
28#include <pgm_base.h>
30
31
33 : m_timer( this ), m_canvas( aCanvas ), m_focused( true )
34{
35 m_driver = std::make_unique<LIBSPNAV_DRIVER>();
36 m_view = aCanvas->GetView();
37 m_scale = 1.0;
38
39 if( m_driver->Connect() )
40 {
41 m_driver->SetHandler( this );
42 Bind( wxEVT_TIMER, &SPNAV_2D_PLUGIN::onPollTimer, this );
43 m_timer.Start( 10 );
44 }
45}
46
48{
49 m_timer.Stop();
50
51 if( m_driver )
52 m_driver->Disconnect();
53}
54
55void SPNAV_2D_PLUGIN::SetFocus( bool aFocus )
56{
57 m_focused = aFocus;
58}
59
61{
62 m_canvas = aCanvas;
63 m_view = aCanvas->GetView();
64}
65
66void SPNAV_2D_PLUGIN::onPollTimer( wxTimerEvent& )
67{
68 if( m_driver && m_focused )
69 m_driver->Poll();
70}
71
72void SPNAV_2D_PLUGIN::OnPan( double x, double y, double z )
73{
74 const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
75 double panFactor = cfg->m_SpaceMouse.pan_speed / 5.0;
76
77 wxLogTrace( "spacenav", "OnPan: x=%f, y=%f, z=%f", x, y, z );
78
79 if( !m_view )
80 return;
81
82 if( std::fabs( x ) > std::numeric_limits<double>::epsilon() ||
83 std::fabs( y ) > std::numeric_limits<double>::epsilon() ||
84 std::fabs( z ) > std::numeric_limits<double>::epsilon() )
85 {
86 double tx = x * panFactor;
87 double tz = z * panFactor;
88
90 tx = -tx;
91
93 tz = -tz;
94
95 VECTOR2D viewPos = m_view->GetCenter();
96 viewPos += m_view->ToWorld( VECTOR2D( tx, -tz ), false ) / 40.0;
97 m_view->SetCenter( viewPos );
98
99 double zoom = y * panFactor;
100
101 if( cfg->m_SpaceMouse.reverse_zoom )
102 zoom = -zoom;
103
104 double current_pixels = m_canvas->GetClientSize().GetWidth();
105 double current_scale = m_view->GetScale();
106 double desired_pixels = current_pixels + zoom / 10.0;
107
108 if( desired_pixels < 1 )
109 desired_pixels = 1;
110
111 double new_scale = current_scale * ( current_pixels / desired_pixels);
112
113 m_view->SetScale( new_scale, viewPos );
114
115 wxMouseEvent moveEvent( KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE );
116 VECTOR2D msp = m_canvas->GetViewControls()->GetMousePosition( false );
117 moveEvent.SetX( msp.x );
118 moveEvent.SetY( msp.y );
119
120 m_canvas->RequestRefresh();
121 wxPostEvent( m_canvas, moveEvent );
122 }
123
124}
125
126void SPNAV_2D_PLUGIN::OnRotate( double rx, double ry, double rz )
127{
128 //Ignore the rotation
129}
130
131void SPNAV_2D_PLUGIN::OnButton( int button, bool pressed )
132{
133 // Buttons are ignored for now
134 (void) button;
135 (void) pressed;
136}
SPACEMOUSE m_SpaceMouse
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
static const wxEventType EVT_REFRESH_MOUSE
Event that forces mouse move event in the dispatcher (eg.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:528
KIGFX::VIEW * m_view
void SetFocus(bool aFocus=true)
void onPollTimer(wxTimerEvent &evt)
void SetCanvas(EDA_DRAW_PANEL_GAL *aCanvas)
std::unique_ptr< SPACENAV_DRIVER > m_driver
void OnButton(int button, bool pressed) override
Handle button press/release events.
EDA_DRAW_PANEL_GAL * m_canvas
SPNAV_2D_PLUGIN(EDA_DRAW_PANEL_GAL *aCanvas)
void OnPan(double x, double y, double z) override
Handle translation (pan) events.
void OnRotate(double rx, double ry, double rz) override
Handle rotational events.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
WX_VIEW_CONTROLS class definition.