InputHandler.h 3.1 KB

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