💿🐜 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.

130 lines
4.0 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. #include <engine/app/input-manager.hpp>
  20. #include <engine/app/sdl/sdl-input-manager.hpp>
  21. namespace app {
  22. input_manager* input_manager::instance()
  23. {
  24. return new sdl_input_manager();
  25. }
  26. void input_manager::register_device(input::device& device)
  27. {
  28. switch (device.get_device_type())
  29. {
  30. case input::device_type::gamepad:
  31. register_gamepad(static_cast<input::gamepad&>(device));
  32. break;
  33. case input::device_type::keyboard:
  34. register_keyboard(static_cast<input::keyboard&>(device));
  35. break;
  36. case input::device_type::mouse:
  37. register_mouse(static_cast<input::mouse&>(device));
  38. break;
  39. default:
  40. //std::unreachable();
  41. break;
  42. }
  43. }
  44. void input_manager::register_gamepad(input::gamepad& device)
  45. {
  46. // Connect gamepad event signals to the event queue
  47. subscriptions.emplace(&device, device.get_connected_channel().subscribe(event_queue));
  48. subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(event_queue));
  49. subscriptions.emplace(&device, device.get_axis_moved_channel().subscribe(event_queue));
  50. subscriptions.emplace(&device, device.get_button_pressed_channel().subscribe(event_queue));
  51. subscriptions.emplace(&device, device.get_button_released_channel().subscribe(event_queue));
  52. // Add gamepad to list of gamepads
  53. gamepads.emplace(&device);
  54. }
  55. void input_manager::register_keyboard(input::keyboard& device)
  56. {
  57. // Connect keyboard event signals to the event queue
  58. subscriptions.emplace(&device, device.get_connected_channel().subscribe(event_queue));
  59. subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(event_queue));
  60. subscriptions.emplace(&device, device.get_key_pressed_channel().subscribe(event_queue));
  61. subscriptions.emplace(&device, device.get_key_released_channel().subscribe(event_queue));
  62. // Add keyboard to list of keyboards
  63. keyboards.emplace(&device);
  64. }
  65. void input_manager::register_mouse(input::mouse& device)
  66. {
  67. // Connect mouse event signals to the event queue
  68. subscriptions.emplace(&device, device.get_connected_channel().subscribe(event_queue));
  69. subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(event_queue));
  70. subscriptions.emplace(&device, device.get_button_pressed_channel().subscribe(event_queue));
  71. subscriptions.emplace(&device, device.get_button_released_channel().subscribe(event_queue));
  72. subscriptions.emplace(&device, device.get_moved_channel().subscribe(event_queue));
  73. subscriptions.emplace(&device, device.get_scrolled_channel().subscribe(event_queue));
  74. // Add mouse to list of mice
  75. mice.emplace(&device);
  76. }
  77. void input_manager::unregister_device(input::device& device)
  78. {
  79. subscriptions.erase(&device);
  80. switch (device.get_device_type())
  81. {
  82. case input::device_type::gamepad:
  83. unregister_gamepad(static_cast<input::gamepad&>(device));
  84. break;
  85. case input::device_type::keyboard:
  86. unregister_keyboard(static_cast<input::keyboard&>(device));
  87. break;
  88. case input::device_type::mouse:
  89. unregister_mouse(static_cast<input::mouse&>(device));
  90. break;
  91. default:
  92. //std::unreachable();
  93. break;
  94. }
  95. }
  96. void input_manager::unregister_gamepad(input::gamepad& gamepad)
  97. {
  98. gamepads.erase(&gamepad);
  99. }
  100. void input_manager::unregister_keyboard(input::keyboard& keyboard)
  101. {
  102. keyboards.erase(&keyboard);
  103. }
  104. void input_manager::unregister_mouse(input::mouse& mouse)
  105. {
  106. mice.erase(&mouse);
  107. }
  108. } // namespace app