Clamp cursor to limits of coordinates representation

Also improves large distance handling.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/8846
This commit is contained in:
dsa-t 2022-02-11 16:42:52 +00:00 committed by Seth Hillbrand
parent 940e03e567
commit 68655540eb
4 changed files with 83 additions and 18 deletions

View File

@ -36,6 +36,7 @@
#include <trace_helpers.h> #include <trace_helpers.h>
#include <settings/common_settings.h> #include <settings/common_settings.h>
#include <math/util.h> // for KiROUND #include <math/util.h> // for KiROUND
#include <geometry/geometry_utils.h>
#include <widgets/ui_common.h> #include <widgets/ui_common.h>
#include <class_draw_panel_gal.h> #include <class_draw_panel_gal.h>
#include <kiplatform/ui.h> #include <kiplatform/ui.h>
@ -302,7 +303,7 @@ void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
} }
if( m_updateCursor ) // do not update the cursor position if it was explicitly set if( m_updateCursor ) // do not update the cursor position if it was explicitly set
m_cursorPos = m_view->ToWorld( mousePos ); m_cursorPos = GetClampedCoords( m_view->ToWorld( mousePos ) );
else else
m_updateCursor = true; m_updateCursor = true;
@ -648,7 +649,7 @@ VECTOR2D WX_VIEW_CONTROLS::GetMousePosition( bool aWorldCoordinates ) const
wxPoint msp = getMouseScreenPosition(); wxPoint msp = getMouseScreenPosition();
VECTOR2D screenPos( msp.x, msp.y ); VECTOR2D screenPos( msp.x, msp.y );
return aWorldCoordinates ? m_view->ToWorld( screenPos ) : screenPos; return aWorldCoordinates ? GetClampedCoords( m_view->ToWorld( screenPos ) ) : screenPos;
} }
@ -675,7 +676,7 @@ VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition( bool aEnableSnapping ) const
} }
else else
{ {
return GetRawCursorPosition( aEnableSnapping ); return GetClampedCoords( GetRawCursorPosition( aEnableSnapping ) );
} }
} }
@ -685,10 +686,12 @@ void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpV
{ {
m_updateCursor = false; m_updateCursor = false;
VECTOR2D clampedPosition = GetClampedCoords( aPosition );
if( aTriggeredByArrows ) if( aTriggeredByArrows )
{ {
m_settings.m_lastKeyboardCursorPositionValid = true; m_settings.m_lastKeyboardCursorPositionValid = true;
m_settings.m_lastKeyboardCursorPosition = aPosition; m_settings.m_lastKeyboardCursorPosition = clampedPosition;
m_settings.m_lastKeyboardCursorCommand = aArrowCommand; m_settings.m_lastKeyboardCursorCommand = aArrowCommand;
m_cursorWarped = false; m_cursorWarped = false;
} }
@ -700,8 +703,8 @@ void WX_VIEW_CONTROLS::SetCursorPosition( const VECTOR2D& aPosition, bool aWarpV
m_cursorWarped = true; m_cursorWarped = true;
} }
WarpCursor( aPosition, true, aWarpView ); WarpCursor( clampedPosition, true, aWarpView );
m_cursorPos = aPosition; m_cursorPos = clampedPosition;
} }
@ -710,14 +713,16 @@ void WX_VIEW_CONTROLS::SetCrossHairCursorPosition( const VECTOR2D& aPosition,
{ {
m_updateCursor = false; m_updateCursor = false;
VECTOR2D clampedPosition = GetClampedCoords( aPosition );
const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize(); const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
BOX2I screen( VECTOR2I( 0, 0 ), screenSize ); BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
VECTOR2D screenPos = m_view->ToScreen( aPosition ); VECTOR2D screenPos = m_view->ToScreen( clampedPosition );
if( aWarpView && !screen.Contains( screenPos ) ) if( aWarpView && !screen.Contains( screenPos ) )
m_view->SetCenter( aPosition ); m_view->SetCenter( clampedPosition );
m_cursorPos = aPosition; m_cursorPos = clampedPosition;
} }
@ -727,14 +732,15 @@ void WX_VIEW_CONTROLS::WarpCursor( const VECTOR2D& aPosition, bool aWorldCoordin
if( aWorldCoordinates ) if( aWorldCoordinates )
{ {
const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize(); const VECTOR2I& screenSize = m_view->GetGAL()->GetScreenPixelSize();
BOX2I screen( VECTOR2I( 0, 0 ), screenSize ); BOX2I screen( VECTOR2I( 0, 0 ), screenSize );
VECTOR2D screenPos = m_view->ToScreen( aPosition ); VECTOR2D clampedPosition = GetClampedCoords( aPosition );
VECTOR2D screenPos = m_view->ToScreen( clampedPosition );
if( !screen.Contains( screenPos ) ) if( !screen.Contains( screenPos ) )
{ {
if( aWarpView ) if( aWarpView )
{ {
m_view->SetCenter( aPosition ); m_view->SetCenter( clampedPosition );
m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 ); m_parentPanel->WarpPointer( screenSize.x / 2, screenSize.y / 2 );
} }
} }
@ -889,7 +895,7 @@ void WX_VIEW_CONTROLS::refreshMouse()
moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) ); moveEvent.SetShiftDown( wxGetKeyState( WXK_SHIFT ) );
moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) ); moveEvent.SetAltDown( wxGetKeyState( WXK_ALT ) );
m_cursorPos = m_view->ToWorld( VECTOR2D( msp.x, msp.y ) ); m_cursorPos = GetClampedCoords( m_view->ToWorld( VECTOR2D( msp.x, msp.y ) ) );
wxPostEvent( m_parentPanel, moveEvent ); wxPostEvent( m_parentPanel, moveEvent );
} }
@ -943,6 +949,8 @@ void WX_VIEW_CONTROLS::UpdateScrollbars()
void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition ) void WX_VIEW_CONTROLS::ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition )
{ {
VECTOR2D clampedPosition = GetClampedCoords( aPosition );
m_settings.m_forceCursorPosition = aEnabled; m_settings.m_forceCursorPosition = aEnabled;
m_settings.m_forcedPosition = aPosition; m_settings.m_forcedPosition = clampedPosition;
} }

View File

@ -204,6 +204,8 @@ public:
* @note The position may be different from the cursor position if snapping is * @note The position may be different from the cursor position if snapping is
* enabled (@see GetCursorPosition()). * enabled (@see GetCursorPosition()).
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aWorldCoordinates if true, the result is given in world coordinates, otherwise * @param aWorldCoordinates if true, the result is given in world coordinates, otherwise
* it is given in screen coordinates. * it is given in screen coordinates.
* @return The current mouse pointer position in either world or screen coordinates. * @return The current mouse pointer position in either world or screen coordinates.
@ -216,6 +218,8 @@ public:
* @note The position may be different from the mouse pointer position if snapping is * @note The position may be different from the mouse pointer position if snapping is
* enabled or cursor position is forced to a specific point. * enabled or cursor position is forced to a specific point.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @return The current cursor position in world coordinates. * @return The current cursor position in world coordinates.
*/ */
VECTOR2D GetCursorPosition() const VECTOR2D GetCursorPosition() const
@ -237,6 +241,8 @@ public:
* @note The position may be different from the mouse pointer position if snapping is * @note The position may be different from the mouse pointer position if snapping is
* enabled or cursor position is forced to a specific point. * enabled or cursor position is forced to a specific point.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aEnableSnapping selects whether cursor position should be snapped to the grid. * @param aEnableSnapping selects whether cursor position should be snapped to the grid.
* @return The current cursor position in world coordinates. * @return The current cursor position in world coordinates.
*/ */
@ -245,6 +251,8 @@ public:
/** /**
* Place the cursor immediately at a given point. Mouse movement is ignored. * Place the cursor immediately at a given point. Mouse movement is ignored.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aEnabled enable forced cursor position * @param aEnabled enable forced cursor position
* @param aPosition the position (world coordinates). * @param aPosition the position (world coordinates).
*/ */
@ -260,6 +268,8 @@ public:
* The position is not forced and will be overridden with the next mouse motion event. * The position is not forced and will be overridden with the next mouse motion event.
* Mouse cursor follows the world cursor. * Mouse cursor follows the world cursor.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aPosition is the requested cursor position in the world coordinates. * @param aPosition is the requested cursor position in the world coordinates.
* @param aWarpView enables/disables view warp if the cursor is outside the current viewport. * @param aWarpView enables/disables view warp if the cursor is outside the current viewport.
*/ */
@ -270,6 +280,8 @@ public:
/** /**
* Move the graphic crosshair cursor to the requested position expressed in world coordinates. * Move the graphic crosshair cursor to the requested position expressed in world coordinates.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aPosition is the requested cursor position in the world coordinates. * @param aPosition is the requested cursor position in the world coordinates.
* @param aWarpView enables/disables view warp if the cursor is outside the current viewport. * @param aWarpView enables/disables view warp if the cursor is outside the current viewport.
*/ */
@ -303,6 +315,8 @@ public:
* If enabled (@see SetEnableCursorWarping(), warps the cursor to the specified position, * If enabled (@see SetEnableCursorWarping(), warps the cursor to the specified position,
* expressed either in the screen coordinates or the world coordinates. * expressed either in the screen coordinates or the world coordinates.
* *
* @note The position is clamped if outside of coordinates representation limits.
*
* @param aPosition is the position where the cursor should be warped. * @param aPosition is the position where the cursor should be warped.
* @param aWorldCoordinates if true treats aPosition as the world coordinates, otherwise it * @param aWorldCoordinates if true treats aPosition as the world coordinates, otherwise it
* uses it as the screen coordinates. * uses it as the screen coordinates.

View File

@ -133,6 +133,47 @@ VECTOR2<T> GetVectorSnapped45( const VECTOR2<T>& aVec, bool only45 = false )
} }
/**
* Clamps a vector to values that can be negated, respecting numeric limits
* of coordinates data type with specified padding.
*
* Numeric limits are (-2^31 + 1) to (2^31 - 1).
*
* Takes care of rounding in case of floating point to integer conversion.
*
* @param aCoord - vector to clamp.
* @param aPadding - padding from the limits. Must not be negative.
* @return clamped vector.
*/
template <typename in_type, typename ret_type = in_type, typename pad_type = unsigned int,
typename = typename std::enable_if<std::is_unsigned<pad_type>::value>::type>
VECTOR2<ret_type> GetClampedCoords( const VECTOR2<in_type>& aCoords, pad_type aPadding = 0u )
{
typedef std::numeric_limits<int> coord_limits;
long max = coord_limits::max() - aPadding;
long min = -max;
in_type x = aCoords.x;
in_type y = aCoords.y;
if( x < min )
x = min;
else if( x > max )
x = max;
if( y < min )
y = min;
else if( y > max )
y = max;
if( !std::is_integral<in_type>() && std::is_integral<ret_type>() )
return VECTOR2<ret_type>( KiROUND( x ), KiROUND( y ) );
return VECTOR2<ret_type>( x, y );
}
/** /**
* Test if any part of a line falls within the bounds of a rectangle. * Test if any part of a line falls within the bounds of a rectangle.
* *

View File

@ -260,10 +260,12 @@ VECTOR2I PCB_GRID_HELPER::BestSnapAnchor( const VECTOR2I& aOrigin, const LSET& a
// see https://gitlab.com/kicad/code/kicad/-/issues/7125 // see https://gitlab.com/kicad/code/kicad/-/issues/7125
double snapScale = snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale(); double snapScale = snapSize / m_toolMgr->GetView()->GetGAL()->GetWorldScale();
int snapRange = std::min( KiROUND( snapScale ), GetGrid().x ); int snapRange = std::min( KiROUND( snapScale ), GetGrid().x );
int snapDist = snapRange; int snapDist = snapRange;
BOX2I bb( VECTOR2I( aOrigin.x - snapRange / 2, aOrigin.y - snapRange / 2 ), //Respect limits of coordinates representation
VECTOR2I( snapRange, snapRange ) ); BOX2I bb;
bb.SetOrigin( GetClampedCoords<double, int>( VECTOR2D( aOrigin ) - snapRange / 2 ) );
bb.SetEnd( GetClampedCoords<double, int>( VECTOR2D( aOrigin ) + snapRange / 2 ) );
clearAnchors(); clearAnchors();