2023-05-18 19:32:29 +02:00
|
|
|
/*
|
|
|
|
* InputHandler.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 "InputHandler.h"
|
|
|
|
|
|
|
|
#include "NotificationHandler.h"
|
|
|
|
#include "InputSourceMouse.h"
|
|
|
|
#include "InputSourceKeyboard.h"
|
|
|
|
#include "InputSourceTouch.h"
|
|
|
|
#include "InputSourceText.h"
|
|
|
|
|
|
|
|
#include "../gui/CGuiHandler.h"
|
|
|
|
#include "../gui/CursorHandler.h"
|
|
|
|
#include "../gui/EventDispatcher.h"
|
2023-05-18 22:31:05 +02:00
|
|
|
#include "../gui/MouseButton.h"
|
2023-05-18 19:32:29 +02:00
|
|
|
#include "../CMT.h"
|
|
|
|
#include "../CPlayerInterface.h"
|
|
|
|
#include "../CGameInfo.h"
|
2023-09-18 21:28:23 +02:00
|
|
|
#include "../CMusicHandler.h"
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
|
|
|
|
#include <SDL_events.h>
|
2023-08-26 19:53:36 +02:00
|
|
|
#include <SDL_timer.h>
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
InputHandler::InputHandler()
|
|
|
|
: mouseHandler(std::make_unique<InputSourceMouse>())
|
|
|
|
, keyboardHandler(std::make_unique<InputSourceKeyboard>())
|
|
|
|
, fingerHandler(std::make_unique<InputSourceTouch>())
|
|
|
|
, textHandler(std::make_unique<InputSourceText>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputHandler::~InputHandler() = default;
|
|
|
|
|
|
|
|
void InputHandler::handleCurrentEvent(const SDL_Event & current)
|
|
|
|
{
|
|
|
|
switch (current.type)
|
|
|
|
{
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
return keyboardHandler->handleEventKeyDown(current.key);
|
|
|
|
case SDL_KEYUP:
|
|
|
|
return keyboardHandler->handleEventKeyUp(current.key);
|
2023-06-16 12:07:02 +02:00
|
|
|
#ifndef VCMI_EMULATE_TOUCHSCREEN_WITH_MOUSE
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_MOUSEMOTION:
|
|
|
|
return mouseHandler->handleEventMouseMotion(current.motion);
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
return mouseHandler->handleEventMouseButtonDown(current.button);
|
2023-05-26 21:17:36 +02:00
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
return mouseHandler->handleEventMouseButtonUp(current.button);
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_MOUSEWHEEL:
|
|
|
|
return mouseHandler->handleEventMouseWheel(current.wheel);
|
2023-06-16 12:07:02 +02:00
|
|
|
#endif
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_TEXTINPUT:
|
|
|
|
return textHandler->handleEventTextInput(current.text);
|
|
|
|
case SDL_TEXTEDITING:
|
|
|
|
return textHandler->handleEventTextEditing(current.edit);
|
|
|
|
case SDL_FINGERMOTION:
|
|
|
|
return fingerHandler->handleEventFingerMotion(current.tfinger);
|
|
|
|
case SDL_FINGERDOWN:
|
|
|
|
return fingerHandler->handleEventFingerDown(current.tfinger);
|
|
|
|
case SDL_FINGERUP:
|
|
|
|
return fingerHandler->handleEventFingerUp(current.tfinger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 22:01:44 +02:00
|
|
|
std::vector<SDL_Event> InputHandler::acquireEvents()
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-19 17:15:56 +02:00
|
|
|
boost::unique_lock<boost::mutex> lock(eventsMutex);
|
2023-07-24 22:01:44 +02:00
|
|
|
|
|
|
|
std::vector<SDL_Event> result;
|
|
|
|
std::swap(result, eventsQueue);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputHandler::processEvents()
|
|
|
|
{
|
|
|
|
std::vector<SDL_Event> eventsToProcess = acquireEvents();
|
|
|
|
|
|
|
|
for(const auto & currentEvent : eventsToProcess)
|
2023-05-18 19:32:29 +02:00
|
|
|
handleCurrentEvent(currentEvent);
|
2023-05-26 14:55:31 +02:00
|
|
|
|
2023-05-26 17:55:26 +02:00
|
|
|
fingerHandler->handleUpdate();
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 22:31:05 +02:00
|
|
|
bool InputHandler::ignoreEventsUntilInput()
|
|
|
|
{
|
|
|
|
bool inputFound = false;
|
|
|
|
|
2023-05-19 17:15:56 +02:00
|
|
|
boost::unique_lock<boost::mutex> lock(eventsMutex);
|
2023-06-22 21:11:48 +02:00
|
|
|
for(const auto & event : eventsQueue)
|
2023-05-18 22:31:05 +02:00
|
|
|
{
|
2023-05-19 17:15:56 +02:00
|
|
|
switch(event.type)
|
2023-05-18 22:31:05 +02:00
|
|
|
{
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
case SDL_FINGERDOWN:
|
|
|
|
case SDL_KEYDOWN:
|
|
|
|
inputFound = true;
|
|
|
|
}
|
|
|
|
}
|
2023-05-19 17:15:56 +02:00
|
|
|
eventsQueue.clear();
|
2023-05-18 22:31:05 +02:00
|
|
|
|
|
|
|
return inputFound;
|
|
|
|
}
|
|
|
|
|
2023-05-18 19:32:29 +02:00
|
|
|
void InputHandler::preprocessEvent(const SDL_Event & ev)
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
if(ev.type == SDL_QUIT)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-11-12 17:35:43 +02:00
|
|
|
#ifdef VCMI_ANDROID
|
2023-05-18 19:32:29 +02:00
|
|
|
handleQuit(false);
|
2023-11-12 17:35:43 +02:00
|
|
|
#else
|
|
|
|
handleQuit(true);
|
|
|
|
#endif
|
2023-05-18 19:32:29 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-09-27 17:33:52 +02:00
|
|
|
else if(ev.type == SDL_KEYDOWN)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
if(ev.key.keysym.sym == SDLK_F4 && (ev.key.keysym.mod & KMOD_ALT))
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
handleQuit(true);
|
|
|
|
return;
|
|
|
|
}
|
2023-07-05 17:14:37 +02:00
|
|
|
|
2023-09-27 17:33:52 +02:00
|
|
|
if(ev.key.keysym.scancode == SDL_SCANCODE_AC_BACK)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
handleQuit(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_F4)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
Settings full = settings.write["video"]["fullscreen"];
|
|
|
|
full->Bool() = !full->Bool();
|
|
|
|
|
|
|
|
GH.onScreenResize();
|
|
|
|
return;
|
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
else if(ev.type == SDL_USEREVENT)
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-06-26 20:51:10 +02:00
|
|
|
handleUserEvent(ev.user);
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ev.type == SDL_WINDOWEVENT)
|
|
|
|
{
|
|
|
|
switch (ev.window.event) {
|
|
|
|
case SDL_WINDOWEVENT_RESTORED:
|
|
|
|
#ifndef VCMI_IOS
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-05-18 19:32:29 +02:00
|
|
|
GH.onScreenResize();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
2023-09-18 21:28:23 +02:00
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
2023-09-27 17:33:52 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
if(settings["general"]["enableUiEnhancements"].Bool()) {
|
|
|
|
CCS->musich->setVolume(settings["general"]["music"].Integer());
|
|
|
|
CCS->soundh->setVolume(settings["general"]["sound"].Integer());
|
|
|
|
}
|
2023-09-18 21:28:23 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
2023-09-27 17:33:52 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
|
|
|
if(settings["general"]["enableUiEnhancements"].Bool()) {
|
|
|
|
CCS->musich->setVolume(0);
|
|
|
|
CCS->soundh->setVolume(0);
|
|
|
|
}
|
2023-09-18 21:28:23 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ev.type == SDL_SYSWMEVENT)
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-05-18 19:32:29 +02:00
|
|
|
if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
|
|
|
|
{
|
|
|
|
NotificationHandler::handleSdlEvent(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//preprocessing
|
|
|
|
if(ev.type == SDL_MOUSEMOTION)
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-05-18 22:31:05 +02:00
|
|
|
if (CCS && CCS->curh)
|
|
|
|
CCS->curh->cursorMove(ev.motion.x, ev.motion.y);
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-05-19 17:15:56 +02:00
|
|
|
boost::unique_lock<boost::mutex> lock(eventsMutex);
|
|
|
|
|
2023-05-27 16:27:49 +02:00
|
|
|
// In a sequence of motion events, skip all but the last one.
|
|
|
|
// This prevents freezes when every motion event takes longer to handle than interval at which
|
|
|
|
// the events arrive (like dragging on the minimap in world view, with redraw at every event)
|
|
|
|
// so that the events would start piling up faster than they can be processed.
|
|
|
|
if (!eventsQueue.empty())
|
2023-05-19 17:15:56 +02:00
|
|
|
{
|
2023-05-27 16:27:49 +02:00
|
|
|
const SDL_Event & prev = eventsQueue.back();
|
|
|
|
|
|
|
|
if(ev.type == SDL_MOUSEMOTION && prev.type == SDL_MOUSEMOTION)
|
|
|
|
{
|
|
|
|
SDL_Event accumulated = ev;
|
|
|
|
accumulated.motion.xrel += prev.motion.xrel;
|
|
|
|
accumulated.motion.yrel += prev.motion.yrel;
|
|
|
|
eventsQueue.back() = accumulated;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ev.type == SDL_FINGERMOTION && prev.type == SDL_FINGERMOTION && ev.tfinger.fingerId == prev.tfinger.fingerId)
|
|
|
|
{
|
|
|
|
SDL_Event accumulated = ev;
|
|
|
|
accumulated.tfinger.dx += prev.tfinger.dx;
|
|
|
|
accumulated.tfinger.dy += prev.tfinger.dy;
|
|
|
|
eventsQueue.back() = accumulated;
|
|
|
|
return;
|
|
|
|
}
|
2023-05-19 17:15:56 +02:00
|
|
|
}
|
|
|
|
eventsQueue.push_back(ev);
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputHandler::fetchEvents()
|
|
|
|
{
|
|
|
|
SDL_Event ev;
|
|
|
|
|
|
|
|
while(1 == SDL_PollEvent(&ev))
|
|
|
|
{
|
|
|
|
preprocessEvent(ev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputHandler::isKeyboardCtrlDown() const
|
|
|
|
{
|
2023-05-26 14:57:53 +02:00
|
|
|
return keyboardHandler->isKeyboardCtrlDown();
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputHandler::isKeyboardAltDown() const
|
|
|
|
{
|
2023-05-26 14:57:53 +02:00
|
|
|
return keyboardHandler->isKeyboardAltDown();
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InputHandler::isKeyboardShiftDown() const
|
|
|
|
{
|
2023-05-26 14:57:53 +02:00
|
|
|
return keyboardHandler->isKeyboardShiftDown();
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 14:55:31 +02:00
|
|
|
void InputHandler::moveCursorPosition(const Point & distance)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-05-26 14:55:31 +02:00
|
|
|
setCursorPosition(getCursorPosition() + distance);
|
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
|
2023-05-26 14:55:31 +02:00
|
|
|
void InputHandler::setCursorPosition(const Point & position)
|
|
|
|
{
|
|
|
|
cursorPosition = position;
|
2023-06-22 21:11:48 +02:00
|
|
|
GH.events().dispatchMouseMoved(Point(0, 0), position);
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void InputHandler::startTextInput(const Rect & where)
|
|
|
|
{
|
|
|
|
textHandler->startTextInput(where);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputHandler::stopTextInput()
|
|
|
|
{
|
|
|
|
textHandler->stopTextInput();
|
|
|
|
}
|
|
|
|
|
2023-07-23 15:17:30 +02:00
|
|
|
void InputHandler::hapticFeedback()
|
|
|
|
{
|
|
|
|
fingerHandler->hapticFeedback();
|
|
|
|
}
|
|
|
|
|
2023-08-26 22:30:02 +02:00
|
|
|
uint32_t InputHandler::getTicks()
|
2023-08-26 19:53:36 +02:00
|
|
|
{
|
2023-08-26 22:30:02 +02:00
|
|
|
return SDL_GetTicks();
|
2023-08-26 19:53:36 +02:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:08:27 +02:00
|
|
|
bool InputHandler::hasTouchInputDevice() const
|
|
|
|
{
|
|
|
|
return fingerHandler->hasTouchInputDevice();
|
|
|
|
}
|
|
|
|
|
2023-06-26 20:51:10 +02:00
|
|
|
void InputHandler::dispatchMainThread(const std::function<void()> & functor)
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
2023-06-26 20:51:10 +02:00
|
|
|
auto heapFunctor = new std::function<void()>(functor);
|
|
|
|
|
2023-05-18 19:32:29 +02:00
|
|
|
SDL_Event event;
|
|
|
|
event.type = SDL_USEREVENT;
|
2023-06-26 20:51:10 +02:00
|
|
|
event.user.code = 0;
|
|
|
|
event.user.data1 = static_cast <void*>(heapFunctor);
|
|
|
|
event.user.data2 = nullptr;
|
2023-05-18 19:32:29 +02:00
|
|
|
SDL_PushEvent(&event);
|
|
|
|
}
|
|
|
|
|
2023-06-26 20:51:10 +02:00
|
|
|
void InputHandler::handleUserEvent(const SDL_UserEvent & current)
|
|
|
|
{
|
|
|
|
auto heapFunctor = static_cast<std::function<void()>*>(current.data1);
|
|
|
|
|
|
|
|
(*heapFunctor)();
|
|
|
|
}
|
|
|
|
|
2023-05-18 19:32:29 +02:00
|
|
|
const Point & InputHandler::getCursorPosition() const
|
|
|
|
{
|
|
|
|
return cursorPosition;
|
|
|
|
}
|