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 "../../lib/Point.h"
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/EventDispatcher.h"
|
|
|
|
#include "../gui/MouseButton.h"
|
|
|
|
|
|
|
|
#include <SDL_events.h>
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseMotion(const SDL_MouseMotionEvent & motion)
|
|
|
|
{
|
2023-05-26 14:55:31 +02:00
|
|
|
Point newPosition(motion.x, motion.y);
|
2023-05-26 20:46:09 +02:00
|
|
|
Point distance(-motion.xrel, -motion.yrel);
|
2023-05-26 14:55:31 +02:00
|
|
|
|
|
|
|
GH.input().setCursorPosition(newPosition);
|
|
|
|
|
2023-05-26 20:46:09 +02:00
|
|
|
if (mouseButtonsMask & SDL_BUTTON(SDL_BUTTON_MIDDLE))
|
|
|
|
GH.events().dispatchGesturePanning(distance);
|
|
|
|
|
2023-05-26 14:55:31 +02:00
|
|
|
mouseButtonsMask = motion.state;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseButtonDown(const SDL_MouseButtonEvent & button)
|
|
|
|
{
|
|
|
|
Point position(button.x, button.y);
|
|
|
|
|
|
|
|
switch(button.button)
|
|
|
|
{
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
if(button.clicks > 1)
|
|
|
|
GH.events().dispatchMouseDoubleClick(position);
|
|
|
|
else
|
|
|
|
GH.events().dispatchMouseButtonPressed(MouseButton::LEFT, position);
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
GH.events().dispatchMouseButtonPressed(MouseButton::RIGHT, position);
|
|
|
|
break;
|
2023-05-29 12:08:08 +02:00
|
|
|
case SDL_BUTTON_MIDDLE:
|
|
|
|
GH.events().dispatchGesturePanningStarted(position);
|
|
|
|
break;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseWheel(const SDL_MouseWheelEvent & wheel)
|
|
|
|
{
|
2023-05-27 16:27:49 +02:00
|
|
|
GH.events().dispatchMouseScrolled(Point(wheel.x, wheel.y), GH.getCursorPosition());
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputSourceMouse::handleEventMouseButtonUp(const SDL_MouseButtonEvent & button)
|
|
|
|
{
|
|
|
|
Point position(button.x, button.y);
|
|
|
|
|
|
|
|
switch(button.button)
|
|
|
|
{
|
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
GH.events().dispatchMouseButtonReleased(MouseButton::LEFT, position);
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
GH.events().dispatchMouseButtonReleased(MouseButton::RIGHT, position);
|
|
|
|
break;
|
2023-05-29 12:08:08 +02:00
|
|
|
case SDL_BUTTON_MIDDLE:
|
|
|
|
GH.events().dispatchGesturePanningEnded();
|
|
|
|
break;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-26 14:55:31 +02:00
|
|
|
|
|
|
|
bool InputSourceMouse::isMouseButtonPressed(MouseButton button) const
|
|
|
|
{
|
|
|
|
static_assert(static_cast<uint32_t>(MouseButton::LEFT) == SDL_BUTTON_LEFT, "mismatch between VCMI and SDL enum!");
|
|
|
|
static_assert(static_cast<uint32_t>(MouseButton::MIDDLE) == SDL_BUTTON_MIDDLE, "mismatch between VCMI and SDL enum!");
|
|
|
|
static_assert(static_cast<uint32_t>(MouseButton::RIGHT) == SDL_BUTTON_RIGHT, "mismatch between VCMI and SDL enum!");
|
|
|
|
static_assert(static_cast<uint32_t>(MouseButton::EXTRA1) == SDL_BUTTON_X1, "mismatch between VCMI and SDL enum!");
|
|
|
|
static_assert(static_cast<uint32_t>(MouseButton::EXTRA2) == SDL_BUTTON_X2, "mismatch between VCMI and SDL enum!");
|
|
|
|
|
|
|
|
uint32_t index = static_cast<uint32_t>(button);
|
|
|
|
return mouseButtonsMask & SDL_BUTTON(index);
|
|
|
|
}
|