InputHandler.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. enum class InputMode
  22. {
  23. KEYBOARD_AND_MOUSE,
  24. TOUCH,
  25. CONTROLLER
  26. };
  27. class InputHandler
  28. {
  29. std::vector<SDL_Event> eventsQueue;
  30. boost::mutex eventsMutex;
  31. Point cursorPosition;
  32. const bool enableMouse;
  33. const bool enableTouch;
  34. const bool enableController;
  35. InputMode currentInputMode;
  36. void setCurrentInputMode(InputMode modi);
  37. std::vector<SDL_Event> acquireEvents();
  38. void preprocessEvent(const SDL_Event & event);
  39. void handleCurrentEvent(const SDL_Event & current);
  40. void handleUserEvent(const SDL_UserEvent & current);
  41. std::unique_ptr<InputSourceMouse> mouseHandler;
  42. std::unique_ptr<InputSourceKeyboard> keyboardHandler;
  43. std::unique_ptr<InputSourceTouch> fingerHandler;
  44. std::unique_ptr<InputSourceText> textHandler;
  45. std::unique_ptr<InputSourceGameController> gameControllerHandler;
  46. public:
  47. InputHandler();
  48. ~InputHandler();
  49. /// Fetches events from SDL input system and prepares them for processing
  50. void fetchEvents();
  51. /// Performs actual processing and dispatching of previously fetched events
  52. void processEvents();
  53. /// drops all incoming events without processing them
  54. /// returns true if input event has been found
  55. bool ignoreEventsUntilInput();
  56. /// Moves cursor by specified distance
  57. void moveCursorPosition(const Point & distance);
  58. /// Moves cursor to a specified position
  59. void setCursorPosition(const Point & position);
  60. /// Initiates text input in selected area, potentially creating IME popup (mobile systems only at the moment)
  61. void startTextInput(const Rect & where);
  62. /// Ends any existing text input state
  63. void stopTextInput();
  64. /// do a haptic feedback
  65. void hapticFeedback();
  66. /// Get the number of milliseconds since SDL library initialization
  67. uint32_t getTicks();
  68. /// returns true if system has active touchscreen
  69. bool hasTouchInputDevice() const;
  70. /// Calls provided functor in main thread on next execution frame
  71. void dispatchMainThread(const std::function<void()> & functor);
  72. /// Returns current position of cursor, in VCMI logical screen coordinates
  73. const Point & getCursorPosition() const;
  74. /// returns true if chosen keyboard key is currently pressed down
  75. bool isKeyboardAltDown() const;
  76. bool isKeyboardCmdDown() const;
  77. bool isKeyboardCtrlDown() const;
  78. bool isKeyboardShiftDown() const;
  79. InputMode getCurrentInputMode();
  80. void copyToClipBoard(const std::string & text);
  81. };