Introduced VIEW_CONTROLS::SETTINGS to keep settings

The new class facilitates saving and restoring VIEW_CONTROLS
settings such as cursor visibility, snapping, etc.
This commit is contained in:
Maciej Suminski 2017-02-11 20:54:44 +01:00
parent d3edc4f843
commit 57050cdeb3
5 changed files with 151 additions and 75 deletions

View File

@ -32,16 +32,51 @@ using namespace KIGFX;
void VIEW_CONTROLS::ShowCursor( bool aEnabled )
{
m_settings.m_showCursor = aEnabled;
m_view->GetGAL()->SetCursorEnabled( aEnabled );
}
bool VIEW_CONTROLS::IsCursorShown() const
{
assert( m_settings.m_showCursor == m_view->GetGAL()->IsCursorEnabled() );
return m_settings.m_showCursor;
}
void VIEW_CONTROLS::Reset()
{
SetSnapping( false );
SetAutoPan( false );
ForceCursorPosition( false );
ShowCursor( false );
CaptureCursor( false );
SetGrabMouse( false );
// Get the default settings from the default constructor
SETTINGS dummy;
ApplySettings( dummy );
}
void VIEW_CONTROLS::SETTINGS::Reset()
{
m_showCursor = false;
m_forceCursorPosition = false;
m_cursorCaptured = false;
m_snappingEnabled = false;
m_grabMouse = false;
m_autoPanEnabled = false;
m_autoPanMargin = 0.1;
m_autoPanSpeed = 0.15;
m_warpCursor = false;
m_enableMousewheelPan = false;
}
void VIEW_CONTROLS::ApplySettings( const SETTINGS& aSettings )
{
ShowCursor( aSettings.m_showCursor );
ForceCursorPosition( aSettings.m_forceCursorPosition, aSettings.m_forcedPosition );
CaptureCursor( aSettings.m_cursorCaptured );
SetSnapping( aSettings.m_snappingEnabled );
SetGrabMouse( aSettings.m_grabMouse );
SetAutoPan( aSettings.m_autoPanEnabled );
SetAutoPanMargin( aSettings.m_autoPanMargin );
SetAutoPanSpeed( aSettings.m_autoPanSpeed );
EnableCursorWarping( aSettings.m_warpCursor );
EnableMousewheelPan( aSettings.m_enableMousewheelPan );
}

View File

@ -73,15 +73,15 @@ void WX_VIEW_CONTROLS::onMotion( wxMouseEvent& aEvent )
{
bool isAutoPanning = false;
if( m_autoPanEnabled )
if( m_settings.m_autoPanEnabled )
isAutoPanning = handleAutoPanning( aEvent );
if( !isAutoPanning && aEvent.Dragging() )
{
if( m_state == DRAG_PANNING )
{
VECTOR2D d = m_dragStartPoint - VECTOR2D( aEvent.GetX(), aEvent.GetY() );
VECTOR2D delta = m_view->ToWorld( d, false );
VECTOR2D d = m_dragStartPoint - VECTOR2D( aEvent.GetX(), aEvent.GetY() );
VECTOR2D delta = m_view->ToWorld( d, false );
m_view->SetCenter( m_lookStartPoint + delta );
aEvent.StopPropagation();
@ -105,8 +105,8 @@ void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
// wheel + ctrl -> zooming;
// wheel + shift -> horizontal scrolling.
if( ( !m_enableMousewheelPan && ( aEvent.ControlDown() || aEvent.ShiftDown() ) ) ||
( m_enableMousewheelPan && !aEvent.ControlDown() ) )
if( ( !m_settings.m_enableMousewheelPan && ( aEvent.ControlDown() || aEvent.ShiftDown() ) ) ||
( m_settings.m_enableMousewheelPan && !aEvent.ControlDown() ) )
{
// Scrolling
VECTOR2D scrollVec = m_view->ToWorld( m_view->GetScreenPixelSize(), false ) *
@ -115,7 +115,7 @@ void WX_VIEW_CONTROLS::onWheel( wxMouseEvent& aEvent )
double scrollX = 0.0;
double scrollY = 0.0;
if( m_enableMousewheelPan )
if( m_settings.m_enableMousewheelPan )
{
if ( axis == wxMOUSE_WHEEL_HORIZONTAL || aEvent.ShiftDown() )
scrollX = scrollVec.x;
@ -237,7 +237,7 @@ void WX_VIEW_CONTROLS::onEnter( wxMouseEvent& aEvent )
void WX_VIEW_CONTROLS::onLeave( wxMouseEvent& aEvent )
{
if( m_cursorCaptured )
if( m_settings.m_cursorCaptured )
{
bool warp = false;
int x = aEvent.GetX();
@ -283,8 +283,8 @@ void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
break;
#endif
double borderSize = std::min( m_autoPanMargin * m_view->GetScreenPixelSize().x,
m_autoPanMargin * m_view->GetScreenPixelSize().y );
double borderSize = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().y );
VECTOR2D dir( m_panDirection );
@ -292,7 +292,7 @@ void WX_VIEW_CONTROLS::onTimer( wxTimerEvent& aEvent )
dir = dir.Resize( borderSize );
dir = m_view->ToWorld( dir, false );
m_view->SetCenter( m_view->GetCenter() + dir * m_autoPanSpeed );
m_view->SetCenter( m_view->GetCenter() + dir * m_settings.m_autoPanSpeed );
// Notify tools that the cursor position has changed in the world coordinates
wxMouseEvent moveEvent( EVT_REFRESH_MOUSE );
@ -337,9 +337,9 @@ void WX_VIEW_CONTROLS::onScroll( wxScrollWinEvent& aEvent )
void WX_VIEW_CONTROLS::SetGrabMouse( bool aEnabled )
{
if( aEnabled && !m_grabMouse )
if( aEnabled && !m_settings.m_grabMouse )
m_parentPanel->CaptureMouse();
else if( !aEnabled && m_grabMouse )
else if( !aEnabled && m_settings.m_grabMouse )
m_parentPanel->ReleaseMouse();
VIEW_CONTROLS::SetGrabMouse( aEnabled );
@ -357,15 +357,15 @@ VECTOR2I WX_VIEW_CONTROLS::GetMousePosition() const
VECTOR2D WX_VIEW_CONTROLS::GetCursorPosition() const
{
if( m_forceCursorPosition )
if( m_settings.m_forceCursorPosition )
{
return m_forcedPosition;
return m_settings.m_forcedPosition;
}
else
{
VECTOR2D mousePosition = GetMousePosition();
if( m_snappingEnabled )
if( m_settings.m_snappingEnabled )
return m_view->GetGAL()->GetGridPoint( m_view->ToWorld( mousePosition ) );
else
return m_view->ToWorld( mousePosition );
@ -420,8 +420,8 @@ bool WX_VIEW_CONTROLS::handleAutoPanning( const wxMouseEvent& aEvent )
VECTOR2D p( aEvent.GetX(), aEvent.GetY() );
// Compute areas where autopanning is active
double borderStart = std::min( m_autoPanMargin * m_view->GetScreenPixelSize().x,
m_autoPanMargin * m_view->GetScreenPixelSize().y );
double borderStart = std::min( m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().x,
m_settings.m_autoPanMargin * m_view->GetScreenPixelSize().y );
double borderEndX = m_view->GetScreenPixelSize().x - borderStart;
double borderEndY = m_view->GetScreenPixelSize().y - borderStart;

View File

@ -863,6 +863,15 @@ public:
isCursorEnabled = aCursorEnabled;
}
/**
* @brief Returns information about cursor visibility.
* @return True if cursor is visible.
*/
bool IsCursorEnabled() const
{
return isCursorEnabled;
}
/**
* @brief Set the cursor color.
*

View File

@ -48,15 +48,13 @@ class VIEW;
class VIEW_CONTROLS
{
public:
VIEW_CONTROLS( VIEW* aView ) : m_view( aView ),
m_forceCursorPosition( false ), m_cursorCaptured( false ), m_snappingEnabled( false ),
m_grabMouse( false ), m_autoPanEnabled( false ), m_autoPanMargin( 0.1 ),
m_autoPanSpeed( 0.15 ), m_warpCursor( false ), m_enableMousewheelPan( false )
VIEW_CONTROLS( VIEW* aView ) : m_view( aView )
{
}
virtual ~VIEW_CONTROLS()
{}
{
}
/**
* Function SetSnapping()
@ -66,7 +64,7 @@ public:
*/
virtual void SetSnapping( bool aEnabled )
{
m_snappingEnabled = aEnabled;
m_settings.m_snappingEnabled = aEnabled;
}
/**
@ -76,7 +74,7 @@ public:
*/
virtual void SetGrabMouse( bool aEnabled )
{
m_grabMouse = aEnabled;
m_settings.m_grabMouse = aEnabled;
}
/**
@ -87,7 +85,7 @@ public:
*/
virtual void SetAutoPan( bool aEnabled )
{
m_autoPanEnabled = aEnabled;
m_settings.m_autoPanEnabled = aEnabled;
}
/**
@ -97,7 +95,7 @@ public:
*/
virtual void SetAutoPanSpeed( float aSpeed )
{
m_autoPanSpeed = aSpeed;
m_settings.m_autoPanSpeed = aSpeed;
}
/**
@ -107,7 +105,7 @@ public:
*/
virtual void SetAutoPanMargin( float aMargin )
{
m_autoPanMargin = aMargin;
m_settings.m_autoPanMargin = aMargin;
}
/**
@ -137,8 +135,8 @@ public:
*/
virtual void ForceCursorPosition( bool aEnabled, const VECTOR2D& aPosition = VECTOR2D( 0, 0 ) )
{
m_forcedPosition = aPosition;
m_forceCursorPosition = aEnabled;
m_settings.m_forceCursorPosition = aEnabled;
m_settings.m_forcedPosition = aPosition;
}
/**
@ -148,6 +146,13 @@ public:
*/
virtual void ShowCursor( bool aEnabled );
/**
* Function IsCursorShown()
* Returns true when cursor is visible.
* @return True if cursor is visible.
*/
bool IsCursorShown() const;
/**
* Function CaptureCursor()
* Forces the cursor to stay within the drawing panel area.
@ -155,12 +160,12 @@ public:
*/
virtual void CaptureCursor( bool aEnabled )
{
m_cursorCaptured = aEnabled;
m_settings.m_cursorCaptured = aEnabled;
}
inline bool IsCursorPositionForced() const
{
return m_forceCursorPosition;
return m_settings.m_forceCursorPosition;
}
/**
@ -183,7 +188,7 @@ public:
*/
void EnableCursorWarping( bool aEnable )
{
m_warpCursor = aEnable;
m_settings.m_warpCursor = aEnable;
}
/**
@ -192,7 +197,7 @@ public:
*/
bool IsCursorWarpingEnabled() const
{
return m_warpCursor;
return m_settings.m_warpCursor;
}
/**
@ -202,7 +207,7 @@ public:
*/
virtual void EnableMousewheelPan( bool aEnable )
{
m_enableMousewheelPan = aEnable;
m_settings.m_enableMousewheelPan = aEnable;
}
/**
@ -211,7 +216,7 @@ public:
*/
virtual bool IsMousewheelPanEnabled() const
{
return m_enableMousewheelPan;
return m_settings.m_enableMousewheelPan;
}
/**
@ -227,42 +232,69 @@ public:
*/
virtual void Reset();
///> Structure to keep VIEW_CONTROLS settings for easy store/restore operations
struct SETTINGS
{
SETTINGS()
{
Reset();
}
///> Restores the default settings
void Reset();
///> Flag determining the cursor visibility
bool m_showCursor;
///> Forced cursor position (world coordinates)
VECTOR2D m_forcedPosition;
///> Is the forced cursor position enabled
bool m_forceCursorPosition;
///> Should the cursor be locked within the parent window area
bool m_cursorCaptured;
///> Should the cursor snap to grid or move freely
bool m_snappingEnabled;
///> Flag for grabbing the mouse cursor
bool m_grabMouse;
///> Flag for turning on autopanning
bool m_autoPanEnabled;
///> Distance from cursor to VIEW edge when panning is active
float m_autoPanMargin;
///> How fast is panning when in auto mode
float m_autoPanSpeed;
///> If the cursor is allowed to be warped
bool m_warpCursor;
///> Mousewheel (2-finger touchpad) panning
bool m_enableMousewheelPan;
};
///> Returns the current VIEW_CONTROLS settings
const SETTINGS& GetSettings() const
{
return m_settings;
}
///> Applies VIEW_CONTROLS settings from an object
void ApplySettings( const SETTINGS& aSettings );
protected:
/// Pointer to controlled VIEW.
VIEW* m_view;
///> Pointer to controlled VIEW.
VIEW* m_view;
/// Current cursor position
VECTOR2D m_cursorPosition;
///> Current cursor position (world coordinates)
VECTOR2D m_cursorPosition;
/// Forced cursor position
VECTOR2D m_forcedPosition;
/// Is the forced cursor position enabled
bool m_forceCursorPosition;
/// Should the cursor be locked within the parent window area
bool m_cursorCaptured;
/// Should the cursor snap to grid or move freely
bool m_snappingEnabled;
/// Flag for grabbing the mouse cursor
bool m_grabMouse;
/// Flag for turning on autopanning
bool m_autoPanEnabled;
/// Distance from cursor to VIEW edge when panning is active
float m_autoPanMargin;
/// How fast is panning when in auto mode
float m_autoPanSpeed;
/// If the cursor is allowed to be warped
bool m_warpCursor;
/// Mousewheel (2-finger touchpad) panning
bool m_enableMousewheelPan;
///> Current VIEW_CONTROLS settings
SETTINGS m_settings;
};
} // namespace KIGFX

View File

@ -78,7 +78,7 @@ public:
*/
void SetAutoPan( bool aEnabled ) override
{
m_autoPanEnabled = aEnabled;
m_settings.m_autoPanEnabled = aEnabled;
if( m_state == AUTO_PANNING )
m_state = IDLE;