InputHandler.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. // Cached power state updated asynchronously via TBB
  60. std::atomic<int> cachedPowerStateMode;
  61. std::atomic<int> cachedPowerStateSeconds;
  62. std::atomic<int> cachedPowerStatePercent;
  63. uint32_t powerStateFrameCounter;
  64. void updatePowerState();
  65. public:
  66. InputHandler();
  67. ~InputHandler();
  68. /// Fetches events from SDL input system and prepares them for processing
  69. void fetchEvents();
  70. /// Performs actual processing and dispatching of previously fetched events
  71. void processEvents();
  72. /// drops all incoming events without processing them
  73. /// returns true if input event has been found
  74. bool ignoreEventsUntilInput();
  75. /// Moves cursor by specified distance
  76. void moveCursorPosition(const Point & distance);
  77. /// Moves cursor to a specified position
  78. void setCursorPosition(const Point & position);
  79. /// Initiates text input in selected area, potentially creating IME popup (mobile systems only at the moment)
  80. void startTextInput(const Rect & where);
  81. /// Ends any existing text input state
  82. void stopTextInput();
  83. /// do a haptic feedback
  84. void hapticFeedback();
  85. /// Get the number of milliseconds since SDL library initialization
  86. uint32_t getTicks();
  87. /// returns true if system has active touchscreen
  88. bool hasTouchInputDevice() const;
  89. /// returns number of fingers on touchscreen
  90. int getNumTouchFingers() const;
  91. /// Calls provided functor in main thread on next execution frame
  92. void dispatchMainThread(const std::function<void()> & functor);
  93. /// Returns current position of cursor, in VCMI logical screen coordinates
  94. const Point & getCursorPosition() const;
  95. /// returns true if chosen keyboard key is currently pressed down
  96. bool isKeyboardAltDown() const;
  97. bool isKeyboardCmdDown() const;
  98. bool isKeyboardCtrlDown() const;
  99. bool isKeyboardShiftDown() const;
  100. InputMode getCurrentInputMode();
  101. void copyToClipBoard(const std::string & text);
  102. PowerState getPowerState();
  103. };