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

158 lines
4.1 KiB

  1. /*
  2. * Copyright (C) 2021 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 "mapper.hpp"
  20. #include "mouse.hpp"
  21. #include "event/event-dispatcher.hpp"
  22. namespace input {
  23. mapper::mapper():
  24. event_dispatcher(nullptr),
  25. control(nullptr),
  26. callback(nullptr),
  27. enabled(false)
  28. {}
  29. mapper::~mapper()
  30. {
  31. set_event_dispatcher(nullptr);
  32. }
  33. void mapper::set_event_dispatcher(::event_dispatcher* event_dispatcher)
  34. {
  35. if (this->event_dispatcher)
  36. {
  37. this->event_dispatcher->unsubscribe<key_pressed_event>(this);
  38. this->event_dispatcher->unsubscribe<mouse_moved_event>(this);
  39. this->event_dispatcher->unsubscribe<mouse_wheel_scrolled_event>(this);
  40. this->event_dispatcher->unsubscribe<mouse_button_pressed_event>(this);
  41. this->event_dispatcher->unsubscribe<game_controller_axis_moved_event>(this);
  42. this->event_dispatcher->unsubscribe<game_controller_button_pressed_event>(this);
  43. }
  44. this->event_dispatcher = event_dispatcher;
  45. if (event_dispatcher)
  46. {
  47. event_dispatcher->subscribe<key_pressed_event>(this);
  48. event_dispatcher->subscribe<mouse_moved_event>(this);
  49. event_dispatcher->subscribe<mouse_wheel_scrolled_event>(this);
  50. event_dispatcher->subscribe<mouse_button_pressed_event>(this);
  51. event_dispatcher->subscribe<game_controller_axis_moved_event>(this);
  52. event_dispatcher->subscribe<game_controller_button_pressed_event>(this);
  53. }
  54. }
  55. void mapper::set_control(input::control* control)
  56. {
  57. this->control = control;
  58. }
  59. void mapper::set_callback(std::function<void(const mapping&)> callback)
  60. {
  61. this->callback = callback;
  62. }
  63. void mapper::set_enabled(bool enabled)
  64. {
  65. this->enabled = enabled;
  66. }
  67. void mapper::handle_event(const key_pressed_event& event)
  68. {
  69. if (!is_enabled() || !callback)
  70. {
  71. return;
  72. }
  73. callback(key_mapping(control, event.keyboard, event.scancode));
  74. }
  75. void mapper::handle_event(const mouse_moved_event& event)
  76. {
  77. if (!is_enabled() || !callback)
  78. {
  79. return;
  80. }
  81. if (event.dx != 0)
  82. {
  83. mouse_motion_axis axis = (event.dx < 0) ? mouse_motion_axis::negative_x : mouse_motion_axis::positive_x;
  84. callback(mouse_motion_mapping(control, event.mouse, axis));
  85. }
  86. if (event.dy != 0)
  87. {
  88. mouse_motion_axis axis = (event.dy < 0) ? mouse_motion_axis::negative_y : mouse_motion_axis::positive_y;
  89. callback(mouse_motion_mapping(control, event.mouse, axis));
  90. }
  91. }
  92. void mapper::handle_event(const mouse_button_pressed_event& event)
  93. {
  94. if (!is_enabled() || !callback)
  95. {
  96. return;
  97. }
  98. callback(mouse_button_mapping(control, event.mouse, event.button));
  99. }
  100. void mapper::handle_event(const mouse_wheel_scrolled_event& event)
  101. {
  102. if (!is_enabled() || !callback)
  103. {
  104. return;
  105. }
  106. if (event.x != 0)
  107. {
  108. mouse_wheel_axis axis = (event.x < 0) ? mouse_wheel_axis::negative_x : mouse_wheel_axis::positive_x;
  109. callback(mouse_wheel_mapping(control, event.mouse, axis));
  110. }
  111. if (event.y != 0)
  112. {
  113. mouse_wheel_axis axis = (event.y < 0) ? mouse_wheel_axis::negative_y : mouse_wheel_axis::positive_y;
  114. callback(mouse_wheel_mapping(control, event.mouse, axis));
  115. }
  116. }
  117. void mapper::handle_event(const game_controller_button_pressed_event& event)
  118. {
  119. if (!is_enabled() || !callback)
  120. {
  121. return;
  122. }
  123. callback(game_controller_button_mapping(control, event.controller, event.button));
  124. }
  125. void mapper::handle_event(const game_controller_axis_moved_event& event)
  126. {
  127. if (!is_enabled() || !callback)
  128. {
  129. return;
  130. }
  131. callback(game_controller_axis_mapping(control, event.controller, event.axis, (event.value < 0.0f)));
  132. }
  133. } // namespace input