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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 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
24#include "spnav_2d_plugin.h"
25
28#include <view/view.h>
30
31#include <wx/log.h>
32#include <pgm_base.h>
34
35
37 : m_timer( this ), m_canvas( aCanvas ), m_focused( true )
38{
39 m_driver = std::make_unique<LIBSPNAV_DRIVER>();
40 m_view = aCanvas->GetView();
41 m_scale = 1.0;
42
43 if( m_driver->Connect() )
44 {
45 m_driver->SetHandler( this );
46 Bind( wxEVT_TIMER, &SPNAV_2D_PLUGIN::onPollTimer, this );
47 m_timer.Start( 10 );
48 }
49}
50
52{
53 m_timer.Stop();
54
55 if( m_driver )
56 m_driver->Disconnect();
57}
58
59void SPNAV_2D_PLUGIN::SetFocus( bool aFocus )
60{
61 m_focused = aFocus;
62}
63
65{
66 m_canvas = aCanvas;
67 m_view = aCanvas->GetView();
68}
69
70void SPNAV_2D_PLUGIN::onPollTimer( wxTimerEvent& )
71{
72 if( m_driver && m_focused )
73 m_driver->Poll();
74}
75
76void SPNAV_2D_PLUGIN::OnPan( double x, double y, double z )
77{
78 const COMMON_SETTINGS* cfg = Pgm().GetCommonSettings();
79 double panFactor = cfg->m_SpaceMouse.pan_speed / 5.0;
80
81 wxLogTrace( "spacenav", "OnPan: x=%f, y=%f, z=%f", x, y, z );
82
83 if( !m_view )
84 return;
85
86 if( std::fabs( x ) > std::numeric_limits<double>::epsilon() ||
87 std::fabs( y ) > std::numeric_limits<double>::epsilon() ||
88 std::fabs( z ) > std::numeric_limits<double>::epsilon() )
89 {
90 double tx = x * panFactor;
91 double tz = z * panFactor;
92
94 tx = -tx;
95
97 tz = -tz;
98
99 VECTOR2D viewPos = m_view->GetCenter();
100 viewPos += m_view->ToWorld( VECTOR2D( tx, -tz ), false ) / 40.0;
101 m_view->SetCenter( viewPos );
102
103 double zoom = y * panFactor;
104
105 if( cfg->m_SpaceMouse.reverse_zoom )
106 zoom = -zoom;
107
108 double current_pixels = m_canvas->GetClientSize().GetWidth();
109 double current_scale = m_view->GetScale();
110 double desired_pixels = current_pixels + zoom / 10.0;
111
112 if( desired_pixels < 1 )
113 desired_pixels = 1;
114
115 double new_scale = current_scale * ( current_pixels / desired_pixels);
116
117 m_view->SetScale( new_scale, viewPos );
118
119 wxMouseEvent moveEvent( KIGFX::WX_VIEW_CONTROLS::EVT_REFRESH_MOUSE );
120 VECTOR2D msp = m_canvas->GetViewControls()->GetMousePosition( false );
121 moveEvent.SetX( msp.x );
122 moveEvent.SetY( msp.y );
123
124 m_canvas->RequestRefresh();
125 wxPostEvent( m_canvas, moveEvent );
126 }
127
128}
129
130void SPNAV_2D_PLUGIN::OnRotate( double rx, double ry, double rz )
131{
132 //Ignore the rotation
133}
134
135void SPNAV_2D_PLUGIN::OnButton( int button, bool pressed )
136{
137 // Buttons are ignored for now
138 (void) button;
139 (void) pressed;
140}
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:576
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.
Definition pgm_base.cpp:913
see class PGM_BASE
VECTOR2< double > VECTOR2D
Definition vector2d.h:694
WX_VIEW_CONTROLS class definition.