💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.2 KiB

  1. /*
  2. * Copyright (C) 2023 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_APP_INPUT_MANAGER_HPP
  20. #define ANTKEEPER_APP_INPUT_MANAGER_HPP
  21. #include "input/device.hpp"
  22. #include "input/gamepad.hpp"
  23. #include "input/keyboard.hpp"
  24. #include "input/mouse.hpp"
  25. #include "event/queue.hpp"
  26. #include <map>
  27. #include <memory>
  28. #include <unordered_set>
  29. namespace app {
  30. /**
  31. * Manages virtual input devices.
  32. */
  33. class input_manager
  34. {
  35. public:
  36. /**
  37. * Allocates and returns an input manager.
  38. */
  39. static input_manager* instance();
  40. /// Destructs an input manager.
  41. virtual ~input_manager() = default;
  42. /**
  43. * Processes input events.
  44. */
  45. virtual void update() = 0;
  46. /**
  47. * Makes the cursor visible.
  48. */
  49. virtual void show_cursor() = 0;
  50. /**
  51. * Makes the cursor invisible.
  52. */
  53. virtual void hide_cursor() = 0;
  54. /**
  55. * Returns the event queue associated with registered input devices.
  56. */
  57. [[nodiscard]] inline const ::event::queue& get_event_queue() const noexcept
  58. {
  59. return event_queue;
  60. }
  61. /**
  62. * Returns the event queue associated with registered input devices.
  63. */
  64. [[nodiscard]] inline ::event::queue& get_event_queue() noexcept
  65. {
  66. return event_queue;
  67. }
  68. /// Returns the set of registered gamepads.
  69. [[nodiscard]] inline const std::unordered_set<input::gamepad*>& get_gamepads() noexcept
  70. {
  71. return gamepads;
  72. }
  73. /// Returns the set of registered keyboards.
  74. [[nodiscard]] inline const std::unordered_set<input::keyboard*>& get_keyboards() noexcept
  75. {
  76. return keyboards;
  77. }
  78. /// Returns the set of registered mice.
  79. [[nodiscard]] inline const std::unordered_set<input::mouse*>& get_mice() noexcept
  80. {
  81. return mice;
  82. }
  83. protected:
  84. /**
  85. * Registers an input device.
  86. *
  87. * @param device Input device to register.
  88. */
  89. /// @{
  90. void register_device(input::device& device);
  91. void register_gamepad(input::gamepad& device);
  92. void register_keyboard(input::keyboard& device);
  93. void register_mouse(input::mouse& device);
  94. /// @}
  95. /**
  96. * Unregisters an input device.
  97. *
  98. * @param device Input device to unregister.
  99. */
  100. /// @{
  101. void unregister_device(input::device& device);
  102. void unregister_gamepad(input::gamepad& device);
  103. void unregister_keyboard(input::keyboard& device);
  104. void unregister_mouse(input::mouse& device);
  105. /// @}
  106. ::event::queue event_queue;
  107. private:
  108. std::multimap<input::device*, std::shared_ptr<::event::subscription>> subscriptions;
  109. std::unordered_set<input::gamepad*> gamepads;
  110. std::unordered_set<input::keyboard*> keyboards;
  111. std::unordered_set<input::mouse*> mice;
  112. };
  113. } // namespace app
  114. #endif // ANTKEEPER_APP_INPUT_MANAGER_HPP