InputHandler.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * InputHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/Rect.h"
  12. enum class EUserEvent;
  13. enum class MouseButton;
  14. union SDL_Event;
  15. struct SDL_UserEvent;
  16. class InputSourceMouse;
  17. class InputSourceKeyboard;
  18. class InputSourceTouch;
  19. class InputSourceText;
  20. class InputSourceGameController;
  21. class InputHandler
  22. {
  23. std::vector<SDL_Event> eventsQueue;
  24. boost::mutex eventsMutex;
  25. Point cursorPosition;
  26. const bool enableMouse;
  27. const bool enableTouch;
  28. const bool enableController;
  29. std::vector<SDL_Event> acquireEvents();
  30. void preprocessEvent(const SDL_Event & event);
  31. void handleCurrentEvent(const SDL_Event & current);
  32. void handleUserEvent(const SDL_UserEvent & current);
  33. std::unique_ptr<InputSourceMouse> mouseHandler;
  34. std::unique_ptr<InputSourceKeyboard> keyboardHandler;
  35. std::unique_ptr<InputSourceTouch> fingerHandler;
  36. std::unique_ptr<InputSourceText> textHandler;
  37. std::unique_ptr<InputSourceGameController> gameControllerHandler;
  38. public:
  39. InputHandler();
  40. ~InputHandler();
  41. /// Fetches events from SDL input system and prepares them for processing
  42. void fetchEvents();
  43. /// Performs actual processing and dispatching of previously fetched events
  44. void processEvents();
  45. /// drops all incoming events without processing them
  46. /// returns true if input event has been found
  47. bool ignoreEventsUntilInput();
  48. /// Moves cursor by specified distance
  49. void moveCursorPosition(const Point & distance);
  50. /// Moves cursor to a specified position
  51. void setCursorPosition(const Point & position);
  52. /// Initiates text input in selected area, potentially creating IME popup (mobile systems only at the moment)
  53. void startTextInput(const Rect & where);
  54. /// Ends any existing text input state
  55. void stopTextInput();
  56. /// do a haptic feedback
  57. void hapticFeedback();
  58. /// Get the number of milliseconds since SDL library initialization
  59. uint32_t getTicks();
  60. /// returns true if system has active touchscreen
  61. bool hasTouchInputDevice() const;
  62. /// Calls provided functor in main thread on next execution frame
  63. void dispatchMainThread(const std::function<void()> & functor);
  64. /// Returns current position of cursor, in VCMI logical screen coordinates
  65. const Point & getCursorPosition() const;
  66. /// returns true if chosen keyboard key is currently pressed down
  67. bool isKeyboardAltDown() const;
  68. bool isKeyboardCtrlDown() const;
  69. bool isKeyboardShiftDown() const;
  70. };