2023-05-18 19:32:29 +02:00
/*
* InputSourceMouse . cpp , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
# include "StdInc.h"
# include "InputSourceMouse.h"
2023-05-26 14:55:31 +02:00
# include "InputHandler.h"
2023-05-18 19:32:29 +02:00
# include "../gui/CGuiHandler.h"
# include "../gui/EventDispatcher.h"
# include "../gui/MouseButton.h"
2024-07-22 13:11:11 +02:00
# include "../render/IScreenHandler.h"
2024-04-30 19:11:09 +02:00
# include "../../lib/Point.h"
# include "../../lib/CConfigHandler.h"
2023-05-18 19:32:29 +02:00
# include <SDL_events.h>
2023-07-04 19:11:16 +02:00
# include <SDL_hints.h>
2024-08-29 14:36:42 +02:00
# include <SDL_version.h>
2023-07-04 19:11:16 +02:00
InputSourceMouse : : InputSourceMouse ( )
2024-04-30 19:11:09 +02:00
: mouseToleranceDistance ( settings [ " input " ] [ " mouseToleranceDistance " ] . Integer ( ) )
2023-07-04 19:11:16 +02:00
{
SDL_SetHint ( SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH , " 1 " ) ;
}
2023-05-18 19:32:29 +02:00
void InputSourceMouse : : handleEventMouseMotion ( const SDL_MouseMotionEvent & motion )
{
2024-07-22 13:11:11 +02:00
Point newPosition = Point ( motion . x , motion . y ) / GH . screenHandler ( ) . getScalingFactor ( ) ;
2024-08-28 22:17:05 +02:00
Point distance = Point ( - motion . xrel , - motion . yrel ) / GH . screenHandler ( ) . getScalingFactor ( ) ;
2023-05-26 14:55:31 +02:00
2023-07-16 16:56:47 +02:00
mouseButtonsMask = motion . state ;
2023-05-26 20:46:09 +02:00
if ( mouseButtonsMask & SDL_BUTTON ( SDL_BUTTON_MIDDLE ) )
2023-05-30 23:33:10 +02:00
GH . events ( ) . dispatchGesturePanning ( middleClickPosition , newPosition , distance ) ;
2023-06-22 21:11:48 +02:00
else if ( mouseButtonsMask & SDL_BUTTON ( SDL_BUTTON_LEFT ) )
GH . events ( ) . dispatchMouseDragged ( newPosition , distance ) ;
2024-08-28 22:17:05 +02:00
else if ( mouseButtonsMask & SDL_BUTTON ( SDL_BUTTON_RIGHT ) )
GH . events ( ) . dispatchMouseDraggedPopup ( newPosition , distance ) ;
2023-05-31 12:08:12 +02:00
else
GH . input ( ) . setCursorPosition ( newPosition ) ;
2023-05-18 19:32:29 +02:00
}
void InputSourceMouse : : handleEventMouseButtonDown ( const SDL_MouseButtonEvent & button )
{
2024-07-22 13:11:11 +02:00
Point position = Point ( button . x , button . y ) / GH . screenHandler ( ) . getScalingFactor ( ) ;
2023-05-18 19:32:29 +02:00
switch ( button . button )
{
case SDL_BUTTON_LEFT :
if ( button . clicks > 1 )
2024-04-30 19:11:09 +02:00
GH . events ( ) . dispatchMouseDoubleClick ( position , mouseToleranceDistance ) ;
2023-05-18 19:32:29 +02:00
else
2024-04-30 19:11:09 +02:00
GH . events ( ) . dispatchMouseLeftButtonPressed ( position , mouseToleranceDistance ) ;
2023-05-18 19:32:29 +02:00
break ;
case SDL_BUTTON_RIGHT :
2024-04-30 19:11:09 +02:00
GH . events ( ) . dispatchShowPopup ( position , mouseToleranceDistance ) ;
2023-05-18 19:32:29 +02:00
break ;
2023-05-29 12:08:08 +02:00
case SDL_BUTTON_MIDDLE :
2023-05-30 23:33:10 +02:00
middleClickPosition = position ;
2023-05-29 12:08:08 +02:00
GH . events ( ) . dispatchGesturePanningStarted ( position ) ;
break ;
2023-05-18 19:32:29 +02:00
}
}
void InputSourceMouse : : handleEventMouseWheel ( const SDL_MouseWheelEvent & wheel )
{
2024-09-23 16:02:55 +02:00
//NOTE: while mouseX / mouseY properties are available since 2.26.0, they are not converted into logical coordinates so don't account for resolution scaling
// This SDL bug was fixed in 2.30.1: https://github.com/libsdl-org/SDL/issues/9097
# if SDL_VERSION_ATLEAST(2,30,1)
2024-08-29 14:36:42 +02:00
GH . events ( ) . dispatchMouseScrolled ( Point ( wheel . x , wheel . y ) , Point ( wheel . mouseX , wheel . mouseY ) / GH . screenHandler ( ) . getScalingFactor ( ) ) ;
# else
GH . events ( ) . dispatchMouseScrolled ( Point ( wheel . x , wheel . y ) , GH . getCursorPosition ( ) ) ;
# endif
2023-05-18 19:32:29 +02:00
}
void InputSourceMouse : : handleEventMouseButtonUp ( const SDL_MouseButtonEvent & button )
{
2024-07-22 13:11:11 +02:00
Point position = Point ( button . x , button . y ) / GH . screenHandler ( ) . getScalingFactor ( ) ;
2023-05-18 19:32:29 +02:00
switch ( button . button )
{
case SDL_BUTTON_LEFT :
2024-04-30 19:11:09 +02:00
GH . events ( ) . dispatchMouseLeftButtonReleased ( position , mouseToleranceDistance ) ;
2023-05-18 19:32:29 +02:00
break ;
case SDL_BUTTON_RIGHT :
2023-06-11 19:38:42 +02:00
GH . events ( ) . dispatchClosePopup ( position ) ;
2023-05-18 19:32:29 +02:00
break ;
2023-05-29 12:08:08 +02:00
case SDL_BUTTON_MIDDLE :
2023-05-30 23:33:10 +02:00
GH . events ( ) . dispatchGesturePanningEnded ( middleClickPosition , position ) ;
2023-05-29 12:08:08 +02:00
break ;
2023-05-18 19:32:29 +02:00
}
}