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"
|
2024-04-03 14:34:22 +02:00
|
|
|
#include "InputSourceGameController.h"
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
#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>())
|
2024-04-30 10:38:13 +02:00
|
|
|
, gameControllerHandler(std::make_unique<InputSourceGameController>())
|
2024-04-30 19:11:09 +02:00
|
|
|
, enableMouse(settings["input"]["enableMouse"].Bool())
|
|
|
|
, enableTouch(settings["input"]["enableTouch"].Bool())
|
|
|
|
, enableController(settings["input"]["enableController"].Bool())
|
2023-05-18 19:32:29 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InputHandler::~InputHandler() = default;
|
|
|
|
|
|
|
|
void InputHandler::handleCurrentEvent(const SDL_Event & current)
|
|
|
|
{
|
|
|
|
switch (current.type)
|
|
|
|
{
|
|
|
|
case SDL_KEYDOWN:
|
2024-04-30 19:11:09 +02:00
|
|
|
keyboardHandler->handleEventKeyDown(current.key);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_KEYUP:
|
2024-04-30 19:11:09 +02:00
|
|
|
keyboardHandler->handleEventKeyUp(current.key);
|
|
|
|
return;
|
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:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableMouse)
|
|
|
|
mouseHandler->handleEventMouseMotion(current.motion);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableMouse)
|
|
|
|
mouseHandler->handleEventMouseButtonDown(current.button);
|
|
|
|
return;
|
2023-05-26 21:17:36 +02:00
|
|
|
case SDL_MOUSEBUTTONUP:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableMouse)
|
|
|
|
mouseHandler->handleEventMouseButtonUp(current.button);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_MOUSEWHEEL:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableMouse)
|
|
|
|
mouseHandler->handleEventMouseWheel(current.wheel);
|
|
|
|
return;
|
2023-06-16 12:07:02 +02:00
|
|
|
#endif
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_TEXTINPUT:
|
2024-04-30 19:11:09 +02:00
|
|
|
textHandler->handleEventTextInput(current.text);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_TEXTEDITING:
|
2024-04-30 19:11:09 +02:00
|
|
|
textHandler->handleEventTextEditing(current.edit);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_FINGERMOTION:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableTouch)
|
|
|
|
fingerHandler->handleEventFingerMotion(current.tfinger);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_FINGERDOWN:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableTouch)
|
|
|
|
fingerHandler->handleEventFingerDown(current.tfinger);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
case SDL_FINGERUP:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableTouch)
|
|
|
|
fingerHandler->handleEventFingerUp(current.tfinger);
|
|
|
|
return;
|
2024-04-30 10:38:13 +02:00
|
|
|
case SDL_CONTROLLERAXISMOTION:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableController)
|
|
|
|
gameControllerHandler->handleEventAxisMotion(current.caxis);
|
|
|
|
return;
|
2024-04-30 10:38:13 +02:00
|
|
|
case SDL_CONTROLLERBUTTONDOWN:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableController)
|
|
|
|
gameControllerHandler->handleEventButtonDown(current.cbutton);
|
|
|
|
return;
|
2024-04-30 10:38:13 +02:00
|
|
|
case SDL_CONTROLLERBUTTONUP:
|
2024-04-30 19:11:09 +02:00
|
|
|
if (enableController)
|
|
|
|
gameControllerHandler->handleEventButtonUp(current.cbutton);
|
|
|
|
return;
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2024-04-30 10:38:13 +02:00
|
|
|
gameControllerHandler->handleUpdate();
|
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:
|
2024-04-30 10:38:13 +02:00
|
|
|
case SDL_CONTROLLERBUTTONDOWN:
|
2023-05-18 22:31:05 +02:00
|
|
|
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();
|
|
|
|
|
2024-02-03 16:14:59 +02:00
|
|
|
GH.onScreenResize(false);
|
2023-09-27 17:33:52 +02:00
|
|
|
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) {
|
2024-04-30 10:38:13 +02:00
|
|
|
case SDL_WINDOWEVENT_RESTORED:
|
2023-05-18 19:32:29 +02:00
|
|
|
#ifndef VCMI_IOS
|
|
|
|
{
|
2023-09-27 17:33:52 +02:00
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2024-02-03 16:14:59 +02:00
|
|
|
GH.onScreenResize(false);
|
2023-05-18 19:32:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
2024-04-30 10:38:13 +02:00
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_GAINED:
|
2023-09-27 17:33:52 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-12-10 15:49:27 +02:00
|
|
|
if(settings["general"]["audioMuteFocus"].Bool()) {
|
2023-09-27 17:33:52 +02:00
|
|
|
CCS->musich->setVolume(settings["general"]["music"].Integer());
|
|
|
|
CCS->soundh->setVolume(settings["general"]["sound"].Integer());
|
|
|
|
}
|
2023-09-18 21:28:23 +02:00
|
|
|
}
|
2024-04-30 10:38:13 +02:00
|
|
|
break;
|
|
|
|
case SDL_WINDOWEVENT_FOCUS_LOST:
|
2023-09-27 17:33:52 +02:00
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock interfaceLock(GH.interfaceMutex);
|
2023-12-10 15:49:27 +02:00
|
|
|
if(settings["general"]["audioMuteFocus"].Bool()) {
|
2023-09-27 17:33:52 +02:00
|
|
|
CCS->musich->setVolume(0);
|
|
|
|
CCS->soundh->setVolume(0);
|
|
|
|
}
|
2023-09-18 21:28:23 +02:00
|
|
|
}
|
2024-04-30 10:38:13 +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);
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 10:38:13 +02:00
|
|
|
else if(ev.type == SDL_CONTROLLERDEVICEADDED)
|
|
|
|
{
|
|
|
|
gameControllerHandler->handleEventDeviceAdded(ev.cdevice);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ev.type == SDL_CONTROLLERDEVICEREMOVED)
|
|
|
|
{
|
|
|
|
gameControllerHandler->handleEventDeviceRemoved(ev.cdevice);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(ev.type == SDL_CONTROLLERDEVICEREMAPPED)
|
|
|
|
{
|
|
|
|
gameControllerHandler->handleEventDeviceRemapped(ev.cdevice);
|
|
|
|
return;
|
|
|
|
}
|
2023-05-18 19:32:29 +02:00
|
|
|
|
|
|
|
//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;
|
2024-02-12 12:33:09 +02:00
|
|
|
event.user.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-11-13 12:09:55 +02:00
|
|
|
|
|
|
|
delete heapFunctor;
|
2023-06-26 20:51:10 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 19:32:29 +02:00
|
|
|
const Point & InputHandler::getCursorPosition() const
|
|
|
|
{
|
|
|
|
return cursorPosition;
|
|
|
|
}
|