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

127 lines
3.0 KiB

  1. /*
  2. * Copyright (C) 2020 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 "input-listener.hpp"
  20. #include "event/event-dispatcher.hpp"
  21. input_listener::input_listener():
  22. event_dispatcher(nullptr),
  23. callback(nullptr),
  24. enabled(false)
  25. {}
  26. input_listener::~input_listener()
  27. {
  28. set_event_dispatcher(nullptr);
  29. }
  30. void input_listener::set_event_dispatcher(::event_dispatcher* event_dispatcher)
  31. {
  32. if (this->event_dispatcher)
  33. {
  34. this->event_dispatcher->unsubscribe<key_pressed_event>(this);
  35. this->event_dispatcher->unsubscribe<mouse_moved_event>(this);
  36. this->event_dispatcher->unsubscribe<mouse_wheel_scrolled_event>(this);
  37. this->event_dispatcher->unsubscribe<mouse_button_pressed_event>(this);
  38. this->event_dispatcher->unsubscribe<game_controller_axis_moved_event>(this);
  39. this->event_dispatcher->unsubscribe<game_controller_button_pressed_event>(this);
  40. }
  41. this->event_dispatcher = event_dispatcher;
  42. if (event_dispatcher)
  43. {
  44. event_dispatcher->subscribe<key_pressed_event>(this);
  45. event_dispatcher->subscribe<mouse_moved_event>(this);
  46. event_dispatcher->subscribe<mouse_wheel_scrolled_event>(this);
  47. event_dispatcher->subscribe<mouse_button_pressed_event>(this);
  48. event_dispatcher->subscribe<game_controller_axis_moved_event>(this);
  49. event_dispatcher->subscribe<game_controller_button_pressed_event>(this);
  50. }
  51. }
  52. void input_listener::set_callback(std::function<void(const event_base&)> callback)
  53. {
  54. this->callback = callback;
  55. }
  56. void input_listener::set_enabled(bool enabled)
  57. {
  58. this->enabled = enabled;
  59. }
  60. void input_listener::handle_event(const key_pressed_event& event)
  61. {
  62. if (!is_enabled() || !callback)
  63. {
  64. return;
  65. }
  66. callback(event);
  67. }
  68. void input_listener::handle_event(const mouse_moved_event& event)
  69. {
  70. if (!is_enabled() || !callback)
  71. {
  72. return;
  73. }
  74. callback(event);
  75. }
  76. void input_listener::handle_event(const mouse_button_pressed_event& event)
  77. {
  78. if (!is_enabled() || !callback)
  79. {
  80. return;
  81. }
  82. callback(event);
  83. }
  84. void input_listener::handle_event(const mouse_wheel_scrolled_event& event)
  85. {
  86. if (!is_enabled() || !callback)
  87. {
  88. return;
  89. }
  90. callback(event);
  91. }
  92. void input_listener::handle_event(const game_controller_button_pressed_event& event)
  93. {
  94. if (!is_enabled() || !callback)
  95. {
  96. return;
  97. }
  98. callback(event);
  99. }
  100. void input_listener::handle_event(const game_controller_axis_moved_event& event)
  101. {
  102. if (!is_enabled() || !callback)
  103. {
  104. return;
  105. }
  106. callback(event);
  107. }