From 30b679ae5e40227067fb2f210573cc76e80e224f Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 30 Jun 2015 14:08:24 +0200 Subject: [PATCH] Cursor control with keyboard (GAL). --- pcbnew/tools/common_actions.cpp | 27 ++++++++ pcbnew/tools/common_actions.h | 18 +++++ pcbnew/tools/pcbnew_control.cpp | 117 +++++++++++++++++++++++++++++++- pcbnew/tools/pcbnew_control.h | 2 + 4 files changed, 163 insertions(+), 1 deletion(-) diff --git a/pcbnew/tools/common_actions.cpp b/pcbnew/tools/common_actions.cpp index 41228181f7..9f18db1008 100644 --- a/pcbnew/tools/common_actions.cpp +++ b/pcbnew/tools/common_actions.cpp @@ -430,6 +430,33 @@ TOOL_ACTION COMMON_ACTIONS::moduleTextOutlines( "pcbnew.ModuleEditor.textOutline "", "" ); +// Cursor control +TOOL_ACTION COMMON_ACTIONS::cursorUp( "pcbnew.Control.cursorUp", + AS_GLOBAL, WXK_UP, "", "", NULL, AF_NONE, (void*) CURSOR_UP ); +TOOL_ACTION COMMON_ACTIONS::cursorDown( "pcbnew.Control.cursorDown", + AS_GLOBAL, WXK_DOWN, "", "" , NULL, AF_NONE, (void*) CURSOR_DOWN ); +TOOL_ACTION COMMON_ACTIONS::cursorLeft( "pcbnew.Control.cursorLeft", + AS_GLOBAL, WXK_LEFT, "", "" , NULL, AF_NONE, (void*) CURSOR_LEFT ); +TOOL_ACTION COMMON_ACTIONS::cursorRight( "pcbnew.Control.cursorRight", + AS_GLOBAL, WXK_RIGHT, "", "" , NULL, AF_NONE, (void*) CURSOR_RIGHT ); + +TOOL_ACTION COMMON_ACTIONS::cursorUpFast( "pcbnew.Control.cursorUpFast", + AS_GLOBAL, MD_CTRL + WXK_UP, "", "", NULL, AF_NONE, (void*) ( CURSOR_UP | CURSOR_FAST_MOVE ) ); +TOOL_ACTION COMMON_ACTIONS::cursorDownFast( "pcbnew.Control.cursorDownFast", + AS_GLOBAL, MD_CTRL + WXK_DOWN, "", "" , NULL, AF_NONE, (void*) ( CURSOR_DOWN | CURSOR_FAST_MOVE ) ); +TOOL_ACTION COMMON_ACTIONS::cursorLeftFast( "pcbnew.Control.cursorLeftFast", + AS_GLOBAL, MD_CTRL + WXK_LEFT, "", "" , NULL, AF_NONE, (void*) ( CURSOR_LEFT | CURSOR_FAST_MOVE ) ); +TOOL_ACTION COMMON_ACTIONS::cursorRightFast( "pcbnew.Control.cursorRightFast", + AS_GLOBAL, MD_CTRL + WXK_RIGHT, "", "" , NULL, AF_NONE, (void*) ( CURSOR_RIGHT | CURSOR_FAST_MOVE ) ); + +TOOL_ACTION COMMON_ACTIONS::cursorClick( "pcbnew.Control.cursorClick", + AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_LEFT_CLICK ), + "", "", NULL, AF_NONE, (void*) CURSOR_CLICK ); +TOOL_ACTION COMMON_ACTIONS::cursorDblClick( "pcbnew.Control.cursorDblClick", + AS_GLOBAL, TOOL_ACTION::LegacyHotKey( HK_LEFT_DCLICK ), + "", "", NULL, AF_NONE, (void*) CURSOR_DBL_CLICK ); + + // Miscellaneous TOOL_ACTION COMMON_ACTIONS::selectionTool( "pcbnew.Control.selectionTool", AS_GLOBAL, 0, diff --git a/pcbnew/tools/common_actions.h b/pcbnew/tools/common_actions.h index c53ae75ef3..3dbe7ef383 100644 --- a/pcbnew/tools/common_actions.h +++ b/pcbnew/tools/common_actions.h @@ -269,6 +269,20 @@ public: /// Display module texts as outlines static TOOL_ACTION moduleTextOutlines; + /// Cursor control with keyboard + static TOOL_ACTION cursorUp; + static TOOL_ACTION cursorDown; + static TOOL_ACTION cursorLeft; + static TOOL_ACTION cursorRight; + + static TOOL_ACTION cursorUpFast; + static TOOL_ACTION cursorDownFast; + static TOOL_ACTION cursorLeftFast; + static TOOL_ACTION cursorRightFast; + + static TOOL_ACTION cursorClick; + static TOOL_ACTION cursorDblClick; + // Miscellaneous static TOOL_ACTION selectionTool; static TOOL_ACTION pickerTool; @@ -303,6 +317,10 @@ public: * no corresponding TOOL_ACTION. */ static boost::optional TranslateLegacyId( int aId ); + + ///> Cursor control event types + enum CURSOR_EVENT_TYPE { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, + CURSOR_CLICK, CURSOR_DBL_CLICK, CURSOR_FAST_MOVE = 0x8000 }; }; void registerAllTools( TOOL_MANAGER* aToolManager ); diff --git a/pcbnew/tools/pcbnew_control.cpp b/pcbnew/tools/pcbnew_control.cpp index d1249c1cca..458255a713 100644 --- a/pcbnew/tools/pcbnew_control.cpp +++ b/pcbnew/tools/pcbnew_control.cpp @@ -26,6 +26,7 @@ #include "common_actions.h" #include "selection_tool.h" #include "picker_tool.h" +#include "grid_helper.h" #include #include @@ -121,7 +122,7 @@ int PCBNEW_CONTROL::ZoomFitScreen( const TOOL_EVENT& aEvent ) BOARD* board = getModel(); board->ComputeBoundingBox(); BOX2I boardBBox = board->ViewBBox(); - VECTOR2I screenSize = gal->GetScreenPixelSize(); + const VECTOR2I& screenSize = gal->GetScreenPixelSize(); if( boardBBox.GetSize().x == 0 || boardBBox.GetSize().y == 0 ) { @@ -421,6 +422,108 @@ int PCBNEW_CONTROL::LayerAlphaDec( const TOOL_EVENT& aEvent ) } +// Cursor control +int PCBNEW_CONTROL::CursorControl( const TOOL_EVENT& aEvent ) +{ + long type = aEvent.Parameter(); + bool fastMove = type & COMMON_ACTIONS::CURSOR_FAST_MOVE; + type &= ~COMMON_ACTIONS::CURSOR_FAST_MOVE; + + GRID_HELPER gridHelper( m_frame ); + VECTOR2D cursor = getViewControls()->GetCursorPosition(); + VECTOR2I gridSize = gridHelper.GetGrid(); + VECTOR2D newCursor = gridHelper.Align( cursor ); + bool warp = true; + + if( fastMove ) + gridSize = gridSize * 10; + + switch( type ) + { + case COMMON_ACTIONS::CURSOR_UP: + newCursor -= VECTOR2D( 0, gridSize.y ); + break; + + case COMMON_ACTIONS::CURSOR_DOWN: + newCursor += VECTOR2D( 0, gridSize.y ); + break; + + case COMMON_ACTIONS::CURSOR_LEFT: + newCursor -= VECTOR2D( gridSize.x, 0 ); + break; + + case COMMON_ACTIONS::CURSOR_RIGHT: + newCursor += VECTOR2D( gridSize.x, 0 ); + break; + + case COMMON_ACTIONS::CURSOR_CLICK: + { + TOOL_EVENT evtClick( TC_MOUSE, TA_MOUSE_CLICK, BUT_LEFT ); + evtClick.SetMousePosition( getViewControls()->GetCursorPosition() ); + m_toolMgr->ProcessEvent( evtClick ); + warp = false; + } + break; + + case COMMON_ACTIONS::CURSOR_DBL_CLICK: + { + TOOL_EVENT evtDblClick( TC_MOUSE, TA_MOUSE_DBLCLICK, BUT_LEFT ); + evtDblClick.SetMousePosition( getViewControls()->GetCursorPosition() ); + m_toolMgr->ProcessEvent( evtDblClick ); + warp = false; + } + break; + } + + if( warp ) + { + KIGFX::VIEW* view = getView(); + newCursor = view->ToScreen( newCursor ); + + // Pan the screen if required + const VECTOR2I& screenSize = view->GetGAL()->GetScreenPixelSize(); + BOX2I screenBox( VECTOR2I( 0, 0 ), screenSize ); + + if( !screenBox.Contains( newCursor ) ) + { + VECTOR2D delta( 0, 0 ); + + if( newCursor.x < screenBox.GetLeft() ) + { + delta.x = newCursor.x - screenBox.GetLeft(); + newCursor.x = screenBox.GetLeft(); + } + else if( newCursor.x > screenBox.GetRight() ) + { + delta.x = newCursor.x - screenBox.GetRight(); + // -1 is to keep the cursor within the drawing area, + // so the cursor coordinates are updated + newCursor.x = screenBox.GetRight() - 1; + } + + if( newCursor.y < screenBox.GetTop() ) + { + delta.y = newCursor.y - screenBox.GetTop(); + newCursor.y = screenBox.GetTop(); + } + else if( newCursor.y > screenBox.GetBottom() ) + { + delta.y = newCursor.y - screenBox.GetBottom(); + // -1 is to keep the cursor within the drawing area, + // so the cursor coordinates are updated + newCursor.y = screenBox.GetBottom() - 1; + } + + view->SetCenter( view->GetCenter() + view->ToWorld( delta, false ) ); + } + + m_frame->GetGalCanvas()->WarpPointer( newCursor.x, newCursor.y ); + } + + return 0; +} + + // Grid control int PCBNEW_CONTROL::GridFast1( const TOOL_EVENT& aEvent ) { @@ -635,6 +738,18 @@ void PCBNEW_CONTROL::SetTransitions() Go( &PCBNEW_CONTROL::LayerAlphaInc, COMMON_ACTIONS::layerAlphaInc.MakeEvent() ); Go( &PCBNEW_CONTROL::LayerAlphaDec, COMMON_ACTIONS::layerAlphaDec.MakeEvent() ); + // Cursor control + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorUp.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDown.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorLeft.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorRight.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorUpFast.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDownFast.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorLeftFast.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorRightFast.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorClick.MakeEvent() ); + Go( &PCBNEW_CONTROL::CursorControl, COMMON_ACTIONS::cursorDblClick.MakeEvent() ); + // Grid control Go( &PCBNEW_CONTROL::GridFast1, COMMON_ACTIONS::gridFast1.MakeEvent() ); Go( &PCBNEW_CONTROL::GridFast2, COMMON_ACTIONS::gridFast2.MakeEvent() ); diff --git a/pcbnew/tools/pcbnew_control.h b/pcbnew/tools/pcbnew_control.h index ee95b67f7a..69b525d510 100644 --- a/pcbnew/tools/pcbnew_control.h +++ b/pcbnew/tools/pcbnew_control.h @@ -71,6 +71,8 @@ public: int LayerAlphaInc( const TOOL_EVENT& aEvent ); int LayerAlphaDec( const TOOL_EVENT& aEvent ); + int CursorControl( const TOOL_EVENT& aEvent ); + // Grid control int GridFast1( const TOOL_EVENT& aEvent ); int GridFast2( const TOOL_EVENT& aEvent );