InputHandler.h 3.0 KB

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