2023-05-18 20:32:29 +03: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 15:55:31 +03:00
|
|
|
#include "InputHandler.h"
|
2023-05-18 20:32:29 +03:00
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/EventDispatcher.h"
|
|
|
|
#include "../gui/MouseButton.h"
|
|
|
|
|
2024-07-22 11:11:11 +00:00
|
|
|
#include "../render/IScreenHandler.h"
|
|
|
|
|
2024-04-30 20:11:09 +03:00
|
|
|
#include "../../lib/Point.h"
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
|
2023-05-18 20:32:29 +03:00
|
|
|
#include <SDL_events.h>
|
2023-07-04 20:11:16 +03:00
|
|
|
#include <SDL_hints.h>
|
2024-08-29 12:36:42 +00:00
|
|
|
#include <SDL_version.h>
|
2023-07-04 20:11:16 +03:00
|
|
|
|
|
|
|
InputSourceMouse::InputSourceMouse()
|
2024-04-30 20:11:09 +03:00
|
|
|
:mouseToleranceDistance(settings["input"]["mouseToleranceDistance"].Integer())
|
2023-07-04 20:11:16 +03:00
|
|
|
{
|
|
|
|
SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1");
|
|
|
|
}
|
2023-05-18 20:32:29 +03:00
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
|
|
|
|
{
|
2024-07-22 11:11:11 +00:00
|
|
|
Point newPosition = Point(motion.x, motion.y) / GH.screenHandler().getScalingFactor();
|
|
|
|
Point distance= Point(-motion.xrel, -motion.yrel) / GH.screenHandler().getScalingFactor();
|
2023-05-26 15:55:31 +03:00
|
|
|
|
2023-07-16 17:56:47 +03:00
|
|
|
mouseButtonsMask = motion.state;
|
|
|
|
|
2023-05-26 21:46:09 +03:00
|
|
|
if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
|
2023-05-31 00:33:10 +03:00
|
|
|
GH.events().dispatchGesturePanning(middleClickPosition, newPosition, distance);
|
2023-06-22 22:11:48 +03:00
|
|
|
else if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_LEFT))
|
|
|
|
GH.events().dispatchMouseDragged(newPosition, distance);
|
2023-05-31 13:08:12 +03:00
|
|
|
else
|
|
|
|
GH.input().setCursorPosition(newPosition);
|
2023-05-18 20:32:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
|
|
|
|
{
|
2024-07-22 11:11:11 +00:00
|
|
|
Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
|
2023-05-18 20:32:29 +03:00
|
|
|
|
|
|
|
switch(button.button)
|
|
|
|
{
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
if(button.clicks > 1)
|
2024-04-30 20:11:09 +03:00
|
|
|
GH.events().dispatchMouseDoubleClick(position, mouseToleranceDistance);
|
2023-05-18 20:32:29 +03:00
|
|
|
else
|
2024-04-30 20:11:09 +03:00
|
|
|
GH.events().dispatchMouseLeftButtonPressed(position, mouseToleranceDistance);
|
2023-05-18 20:32:29 +03:00
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
2024-04-30 20:11:09 +03:00
|
|
|
GH.events().dispatchShowPopup(position, mouseToleranceDistance);
|
2023-05-18 20:32:29 +03:00
|
|
|
break;
|
2023-05-29 13:08:08 +03:00
|
|
|
case SDL_BUTTON_MIDDLE:
|
2023-05-31 00:33:10 +03:00
|
|
|
middleClickPosition = position;
|
2023-05-29 13:08:08 +03:00
|
|
|
GH.events().dispatchGesturePanningStarted(position);
|
|
|
|
break;
|
2023-05-18 20:32:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
|
|
|
|
{
|
2024-08-29 12:36:42 +00:00
|
|
|
#if SDL_VERSION_ATLEAST(2,26,0)
|
|
|
|
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 20:32:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
|
|
|
|
{
|
2024-07-22 11:11:11 +00:00
|
|
|
Point position = Point(button.x, button.y) / GH.screenHandler().getScalingFactor();
|
2023-05-18 20:32:29 +03:00
|
|
|
|
|
|
|
switch(button.button)
|
|
|
|
{
|
|
|
|
case SDL_BUTTON_LEFT:
|
2024-04-30 20:11:09 +03:00
|
|
|
GH.events().dispatchMouseLeftButtonReleased(position, mouseToleranceDistance);
|
2023-05-18 20:32:29 +03:00
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
2023-06-11 20:38:42 +03:00
|
|
|
GH.events().dispatchClosePopup(position);
|
2023-05-18 20:32:29 +03:00
|
|
|
break;
|
2023-05-29 13:08:08 +03:00
|
|
|
case SDL_BUTTON_MIDDLE:
|
2023-05-31 00:33:10 +03:00
|
|
|
GH.events().dispatchGesturePanningEnded(middleClickPosition, position);
|
2023-05-29 13:08:08 +03:00
|
|
|
break;
|
2023-05-18 20:32:29 +03:00
|
|
|
}
|
|
|
|
}
|